Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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");
Expand Down
20 changes: 17 additions & 3 deletions src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct TrayState {
battery_case: Option<u8>,
battery_single: Option<u8>,
listening_mode: Option<ListeningMode>,
/// 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 {
Expand Down Expand Up @@ -64,6 +67,7 @@ impl TrayState {
battery_case: None,
battery_single: None,
listening_mode: None,
has_ambient: false,
}
}

Expand All @@ -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(),
Expand All @@ -86,6 +90,7 @@ impl TrayState {
battery_case: snapshot.battery_case,
battery_single: snapshot.battery_single,
listening_mode: Some(snapshot.listening_mode),
has_ambient,
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}