diff --git a/src/main.rs b/src/main.rs index 3a2786e..52ad0ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1067,8 +1067,7 @@ impl App { card(ui, p, "Sidetone", |ui| { ui.label(RichText::new("Hear your own voice in the headset while you talk.").color(p.sub)); if let Some(v) = live_slider(ui, "sidetone", s.sidetone, 0..=128, |v| if v == 0 { "off".into() } else { format!("{}%", v * 100 / 128) }) { - s.sidetone = v; - let _ = self.w.headset.send(HeadsetCmd::Sidetone(v)); + let _ = self.w.headset.send(HeadsetCmd::Sidetone(v, std::mem::replace(&mut s.sidetone, v))); } }); } @@ -1076,8 +1075,7 @@ impl App { card(ui, p, "Auto power-off", |ui| { ui.label(RichText::new("Turn the headset off after this long without sound.").color(p.sub)); if let Some(v) = live_slider(ui, "idle", s.idle_minutes, 0..=90, |v| if v == 0 { "never".into() } else { format!("{v} min") }) { - s.idle_minutes = v; - let _ = self.w.headset.send(HeadsetCmd::Idle(v)); + let _ = self.w.headset.send(HeadsetCmd::Idle(v, std::mem::replace(&mut s.idle_minutes, v))); } }); } diff --git a/src/workers.rs b/src/workers.rs index e851430..35dc686 100644 --- a/src/workers.rs +++ b/src/workers.rs @@ -169,9 +169,10 @@ pub enum MouseCmd { Activate(zbus::OwnedObjectPath, &'static str), } +/// (new, previous): the previous value goes back into settings if the headset rejects the new one. pub enum HeadsetCmd { - Sidetone(u32), - Idle(u32), + Sidetone(u32, u32), + Idle(u32, u32), } pub struct MicCmd { @@ -404,14 +405,28 @@ fn headset(st: Shared, ctx: Context, rx: Receiver) { update(&st, &ctx, |s| s.headset = dev); match rx.recv_timeout(Duration::from_secs(30)) { - Ok(HeadsetCmd::Sidetone(v)) => report(&st, &ctx, audio::headset_set("-s", v), "Sidetone set"), - Ok(HeadsetCmd::Idle(v)) => report(&st, &ctx, audio::headset_set("-i", v), "Auto-off set"), + Ok(HeadsetCmd::Sidetone(v, prev)) => set_or_revert(&st, &ctx, "-s", v, prev, |s| &mut s.sidetone, "Sidetone set"), + Ok(HeadsetCmd::Idle(v, prev)) => set_or_revert(&st, &ctx, "-i", v, prev, |s| &mut s.idle_minutes, "Auto-off set"), Err(RecvTimeoutError::Timeout) => {} Err(RecvTimeoutError::Disconnected) => return, } } } +/// The slider already saved `v`; put `prev` back if the headset didn't take it (unless it moved on since). +fn set_or_revert(st: &Shared, ctx: &Context, flag: &str, v: u32, prev: u32, field: fn(&mut Settings) -> &mut u32, ok: &str) { + let r = audio::headset_set(flag, v); + if r.is_err() { + update(st, ctx, |s| { + let f = field(&mut s.settings); + if *f == v { + *f = prev; + } + }); + } + report(st, ctx, r, ok); +} + // -------------------------------------------------------------------- mic fn mic(st: Shared, ctx: Context, rx: Receiver) {