diff --git a/src/app.rs b/src/app.rs index 441c62c..9f7a689 100644 --- a/src/app.rs +++ b/src/app.rs @@ -142,15 +142,21 @@ impl SoundcoreApp { self.supports_manual_ambient_ranges = supports_manual_ambient_ranges; self.set_current_icon(context, &name, icon.as_deref()); self.device_name = name.clone(); - self.tray - .update(TrayState::connected(&self.device_name, &snapshot)); + self.tray.update(TrayState::connected( + &self.device_name, + &snapshot, + self.has_ambient(&snapshot), + )); soundcore_control::notify::buds_connected(&name, &snapshot); self.snapshot = snapshot; self.message = None; } DeviceEvent::Snapshot(snapshot) => { - self.tray - .update(TrayState::connected(&self.device_name, &snapshot)); + self.tray.update(TrayState::connected( + &self.device_name, + &snapshot, + self.has_ambient(&snapshot), + )); self.snapshot = snapshot; self.connection = ConnectionView::Connected; self.message = None; @@ -240,7 +246,11 @@ impl SoundcoreApp { /// intensity range or at least one discrete listening mode. Devices with neither (e.g. /// the P20i, which has no ANC hardware) don't get an Ambient tab at all. pub(crate) fn has_ambient_options(&self) -> bool { - self.supports_manual_ambient_ranges || !self.snapshot.mode_options.is_empty() + self.has_ambient(&self.snapshot) + } + + fn has_ambient(&self, snapshot: &DeviceSnapshot) -> bool { + self.supports_manual_ambient_ranges || !snapshot.mode_options.is_empty() } /// Whether the connected device reports any daily or button control. Devices with diff --git a/src/device.rs b/src/device.rs index 042cb60..949fcfb 100644 --- a/src/device.rs +++ b/src/device.rs @@ -217,6 +217,12 @@ async fn event_loop( } continue; } + // `openscq30-lib` panics (not errors) when asked to write a setting the model + // doesn't register, e.g. a tray ambient-mode pick on earbuds without ambient modes. + if is_ambient_write(&command) && device.setting(&SettingId::AmbientSoundMode).is_none() { + tracing::warn!(?command, "dropping ambient write: device has no ambient modes"); + continue; + } for change in setting_changes(command) { if let Err(error) = device.set_setting_values(vec![change]).await { events.send(DeviceEvent::Error(format!("Could not update the earbuds: {error}"))).ok(); @@ -308,6 +314,13 @@ async fn disconnect_bluetooth(mac_address: MacAddr6) -> Result<()> { .context("BlueZ could not disconnect the earbuds") } +fn is_ambient_write(command: &DeviceCommand) -> bool { + matches!( + command, + DeviceCommand::SetListeningMode(_) | DeviceCommand::SetAmbientLevel(_) + ) +} + fn read_snapshot(device: &dyn OpenSCQ30Device) -> DeviceSnapshot { for setting_id in [ SettingId::BatteryLevelLeft, diff --git a/src/main.rs b/src/main.rs index 9e6ad9b..b00d57e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,7 @@ fn run_tray_only() { let worker = DeviceWorker::spawn(); let mut open_window = false; let mut device_name = String::new(); + let mut supports_manual_ambient_ranges = false; 'service: loop { while let Some(event) = worker.try_recv() { @@ -72,14 +73,19 @@ fn run_tray_only() { name, snapshot, icon: _, - supports_manual_ambient_ranges: _, + supports_manual_ambient_ranges: manual_ranges, } => { device_name = name; - tray.update(TrayState::connected(&device_name, &snapshot)); + supports_manual_ambient_ranges = manual_ranges; + let has_ambient = + supports_manual_ambient_ranges || !snapshot.mode_options.is_empty(); + tray.update(TrayState::connected(&device_name, &snapshot, has_ambient)); soundcore_control::notify::buds_connected(&device_name, &snapshot); } DeviceEvent::Snapshot(snapshot) => { - tray.update(TrayState::connected(&device_name, &snapshot)); + let has_ambient = + supports_manual_ambient_ranges || !snapshot.mode_options.is_empty(); + tray.update(TrayState::connected(&device_name, &snapshot, has_ambient)); } DeviceEvent::Error(error) => { tracing::error!(%error, "tray Bluetooth error"); diff --git a/src/tray.rs b/src/tray.rs index 923ad0c..e62ec49 100644 --- a/src/tray.rs +++ b/src/tray.rs @@ -27,6 +27,9 @@ pub struct TrayState { battery_case: Option, battery_single: Option, listening_mode: Option, + /// Whether the device has any ambient control; the Ambient mode submenu is disabled + /// otherwise (picking a mode on such a device would write an unregistered setting). + has_ambient: bool, } impl Default for TrayState { @@ -64,6 +67,7 @@ impl TrayState { battery_case: None, battery_single: None, listening_mode: None, + has_ambient: false, } } @@ -76,7 +80,7 @@ impl TrayState { } #[must_use] - pub fn connected(device_name: &str, snapshot: &DeviceSnapshot) -> Self { + pub fn connected(device_name: &str, snapshot: &DeviceSnapshot, has_ambient: bool) -> Self { Self { device_name: Some(device_name.to_owned()), status: "Connected".into(), @@ -86,6 +90,7 @@ impl TrayState { battery_case: snapshot.battery_case, battery_single: snapshot.battery_single, listening_mode: Some(snapshot.listening_mode), + has_ambient, } } @@ -271,7 +276,7 @@ impl ksni::Tray for SoundcoreTray { "Ambient mode: {}", selected_mode.map_or("—", listening_mode_label) ), - enabled: self.state.connected, + enabled: self.state.connected && self.state.has_ambient, submenu: vec![ ksni::menu::RadioGroup { selected: match selected_mode { @@ -382,11 +387,20 @@ mod tests { ..DeviceSnapshot::default() }; - let state = TrayState::connected("Test Earbuds", &snapshot); + let state = TrayState::connected("Test Earbuds", &snapshot, true); assert!(state.connected); + assert!(state.has_ambient); assert_eq!(state.device_label(), "Test Earbuds"); assert_eq!(state.listening_mode, Some(ListeningMode::Normal)); assert_eq!(battery_value(state.battery_left), "94%"); } + + #[test] + fn connected_state_without_ambient_options_disables_ambient_menu() { + let state = TrayState::connected("K20i", &DeviceSnapshot::default(), false); + + assert!(state.connected); + assert!(!state.has_ambient); + } }