feat(slint): HiFi wizard review-and-apply + done steps — config generation (Slice 10)

The review step generates the copy-paste config per selected DAC (read-only —
QBZ never writes these files, Decision 2): an honest info banner, a backup
CommandBlock first, then one collapsible accordion per DAC (Decision 5 multi-DAC)
holding the three snippets ported 1:1 from the Tauri wizard — pipewire
allowed-rates, per-app bit-perfect client rules, and the WirePlumber rule that
pins node.name + rates + disables resample/channelmix — and finally an
init-aware restart CommandBlock. Rates are re-probed at generation time
(query_dac_capabilities); the filename slug is the description (fixes the old
dacShortName regex that only kept two underscore tokens). The done step lists
the config paths.

Controller: checked_dacs -> gen_configs_blocking (off-thread re-probe + template
build via line-join, no format!-brace escaping) -> apply_configs; toggle_config
flips an accordion. gen-configs fires on the select-dacs -> review transition.
2 unit tests (slugify, wireplumber pins node+rates). DacConfigRow state +
gen-configs/toggle-config actions.

cc
This commit is contained in:
vicrodh committed 2026-06-16 10:47:54 -06:00
1 parent 97fd942aa0
commit ca09b35917
5 files changed
+404 -8

No files matched your search

+218 -1
View File
@@ -11,7 +11,7 @@ use std::sync::Mutex;
use qbz_audio::{AudioStackHealth, Distro, InitSystem};
use slint::{ComponentHandle, Model, ModelRc, VecModel};
use crate::{AppWindow, DacCandidateRow, DacWizardState, RemediationRow};
use crate::{AppWindow, DacCandidateRow, DacConfigRow, DacWizardState, RemediationRow};
// Last probe result, so a distro override recomputes commands without
// re-shelling on every dropdown change.
@@ -559,3 +559,220 @@ fn khz(hz: u32) -> String {
format!("{:.1} kHz", hz as f64 / 1000.0)
}
}
// ── Slice 10: review-and-apply (per-DAC config generation) ─────────────────
/// Plain, `Send` per-DAC generated config (built on a worker thread).
pub struct DacConfigData {
name: String,
node_name: String,
pipewire_conf: String,
pulse_conf: String,
wireplumber_conf: String,
}
/// (node_name, display_name) for every checked candidate, or a valid manual one.
pub fn checked_dacs(window: &AppWindow) -> Vec<(String, String)> {
let st = window.global::<DacWizardState>();
let model = st.get_candidates();
let mut out = Vec::new();
for i in 0..model.row_count() {
if let Some(r) = model.row_data(i) {
if r.checked {
out.push((r.id.to_string(), r.description.to_string()));
}
}
}
if out.is_empty() {
let manual = st.get_manual_node_name().to_string();
if !manual.trim().is_empty() && st.get_manual_valid() {
out.push((manual.clone(), manual));
}
}
out
}
/// Re-probe rates + build the three config snippets per DAC (blocking).
pub fn gen_configs_blocking(dacs: Vec<(String, String)>) -> Vec<DacConfigData> {
dacs.into_iter()
.map(|(node_name, name)| {
let rates = qbz_audio::query_dac_capabilities(&node_name).sample_rates;
let short = short_name(&name, &node_name);
DacConfigData {
pipewire_conf: pipewire_conf(&short, &rates),
pulse_conf: pulse_conf(&short),
wireplumber_conf: wireplumber_conf(&short, &node_name, &rates, &name),
name,
node_name,
}
})
.collect()
}
/// Push generated configs + backup/restart/paths to the state.
pub fn apply_configs(window: &AppWindow, data: Vec<DacConfigData>) {
let st = window.global::<DacWizardState>();
let single = data.len() == 1;
let rows: Vec<DacConfigRow> = data
.iter()
.map(|d| DacConfigRow {
name: d.name.clone().into(),
node_name: d.node_name.clone().into(),
pipewire_conf: d.pipewire_conf.clone().into(),
pulse_conf: d.pulse_conf.clone().into(),
wireplumber_conf: d.wireplumber_conf.clone().into(),
expanded: single, // one DAC → expanded; multiple → collapsed accordions
})
.collect();
let mut paths: Vec<slint::SharedString> = Vec::new();
for d in &data {
let short = short_name(&d.name, &d.node_name);
paths.push(format!("~/.config/pipewire/pipewire.conf.d/99-qbz-dac-{short}.conf").into());
paths.push(format!("~/.config/pipewire/client.conf.d/99-qbz-bitperfect-{short}.conf").into());
paths
.push(format!("~/.config/wireplumber/wireplumber.conf.d/99-qbz-dac-{short}.conf").into());
}
st.set_dac_configs(ModelRc::new(VecModel::from(rows)));
st.set_created_paths(ModelRc::new(VecModel::from(paths)));
st.set_backup_cmd(BACKUP_CMD.into());
let init = InitSystem::ALL
.get(st.get_init_index().max(0) as usize)
.copied()
.unwrap_or(InitSystem::Unknown);
st.set_restart_cmd(restart_cmd(init).into());
}
/// Collapse/expand one DAC's generated-config accordion.
pub fn toggle_config(window: &AppWindow, index: i32) {
let model = window.global::<DacWizardState>().get_dac_configs();
if let Some(vm) = model.as_any().downcast_ref::<VecModel<DacConfigRow>>() {
if let Some(mut row) = vm.row_data(index.max(0) as usize) {
row.expanded = !row.expanded;
vm.set_row_data(index.max(0) as usize, row);
}
}
}
const BACKUP_CMD: &str = "BACKUP=~/.config/qbz/backups/pipewire-$(date +%Y%m%d-%H%M%S)\nmkdir -p \"$BACKUP\"\ncp -a ~/.config/pipewire \"$BACKUP/\" 2>/dev/null || true\ncp -a ~/.config/wireplumber \"$BACKUP/\" 2>/dev/null || true\necho \"Backup created at: $BACKUP\"";
/// A short, filename-safe DAC name: slug of the description, else the node.name.
fn short_name(name: &str, node_name: &str) -> String {
let slug = slugify(name);
if !slug.is_empty() {
return slug;
}
let nslug = slugify(node_name);
if nslug.is_empty() {
"dac".to_string()
} else {
nslug
}
}
fn slugify(s: &str) -> String {
let mut out = String::new();
let mut prev_dash = false;
for ch in s.chars() {
if ch.is_ascii_alphanumeric() {
out.push(ch.to_ascii_lowercase());
prev_dash = false;
} else if !prev_dash && !out.is_empty() {
out.push('-');
prev_dash = true;
}
}
out.trim_matches('-').to_string()
}
fn rates_list(rates: &[u32]) -> String {
if rates.is_empty() {
"44100 48000 88200 96000 176400 192000".to_string()
} else {
rates
.iter()
.map(|r| r.to_string())
.collect::<Vec<_>>()
.join(" ")
}
}
fn pipewire_conf(short: &str, rates: &[u32]) -> String {
let rates = rates_list(rates);
[
"mkdir -p ~/.config/pipewire/pipewire.conf.d".to_string(),
format!("cat > ~/.config/pipewire/pipewire.conf.d/99-qbz-dac-{short}.conf << 'EOF'"),
"# QBZ DAC Setup - Sample Rate Switching".to_string(),
"context.properties = {".to_string(),
format!(" default.clock.allowed-rates = [ {rates} ]"),
"}".to_string(),
"EOF".to_string(),
]
.join("\n")
}
fn pulse_conf(short: &str) -> String {
[
"mkdir -p ~/.config/pipewire/client.conf.d".to_string(),
format!("cat > ~/.config/pipewire/client.conf.d/99-qbz-bitperfect-{short}.conf << 'EOF'"),
"# QBZ DAC Setup - Per-App Bit-Perfect".to_string(),
"stream.rules = [".to_string(),
" {".to_string(),
" matches = [".to_string(),
" { application.process.binary = \"qbz\" }".to_string(),
" { application.name = \"PipeWire ALSA [qbz]\" }".to_string(),
" ]".to_string(),
" actions = { update-props = { resample.disable = true, channelmix.disable = true } }"
.to_string(),
" }".to_string(),
"]".to_string(),
"EOF".to_string(),
]
.join("\n")
}
fn wireplumber_conf(short: &str, node_name: &str, rates: &[u32], description: &str) -> String {
let rates = rates_list(rates);
[
"mkdir -p ~/.config/wireplumber/wireplumber.conf.d".to_string(),
format!("cat > ~/.config/wireplumber/wireplumber.conf.d/99-qbz-dac-{short}.conf << 'EOF'"),
format!("# QBZ DAC Setup - {description}"),
"monitor.alsa.rules = [".to_string(),
" {".to_string(),
" matches = [".to_string(),
format!(" {{ node.name = \"{node_name}\", media.class = \"Audio/Sink\" }}"),
" ]".to_string(),
" actions = {".to_string(),
" update-props = {".to_string(),
format!(" audio.allowed-rates = [ {rates} ]"),
" resample.disable = true".to_string(),
" channelmix.disable = true".to_string(),
" }".to_string(),
" }".to_string(),
" }".to_string(),
"]".to_string(),
"EOF".to_string(),
]
.join("\n")
}
#[cfg(test)]
mod slice10_tests {
use super::*;
#[test]
fn slugifies_descriptions() {
assert_eq!(slugify("DacMagic Plus Analog Stereo"), "dacmagic-plus-analog-stereo");
assert_eq!(slugify("Built-in Audio Analog Stereo"), "built-in-audio-analog-stereo");
assert_eq!(slugify(" weird__name!! "), "weird-name");
assert_eq!(slugify(""), "");
}
#[test]
fn wireplumber_conf_pins_node_and_rates() {
let c = wireplumber_conf("dacmagic", "alsa_output.usb-x.analog-stereo", &[44100, 192000], "DacMagic");
assert!(c.contains("node.name = \"alsa_output.usb-x.analog-stereo\""));
assert!(c.contains("audio.allowed-rates = [ 44100 192000 ]"));
assert!(c.contains("99-qbz-dac-dacmagic.conf"));
assert!(c.contains("resample.disable = true"));
}
}
+27
View File
@@ -11498,6 +11498,33 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = runtime.core().resume();
});
}
{
// Generate the per-DAC copy-paste config (Slice 10): re-probe rates off
// the UI thread, then fill the review step.
let weak = window.as_weak();
let handle = tokio_rt.handle().clone();
window.global::<DacWizardActions>().on_gen_configs(move || {
let Some(w) = weak.upgrade() else {
return;
};
let dacs = dac_wizard::checked_dacs(&w);
let weak2 = w.as_weak();
handle.spawn_blocking(move || {
let data = dac_wizard::gen_configs_blocking(dacs);
let _ = weak2.upgrade_in_event_loop(move |w| {
dac_wizard::apply_configs(&w, data);
});
});
});
}
{
let weak = window.as_weak();
window.global::<DacWizardActions>().on_toggle_config(move |i| {
if let Some(w) = weak.upgrade() {
dac_wizard::toggle_config(&w, i);
}
});
}
{
window
.global::<DacWizardActions>()