From 0f4667fc47c49727239fe6663118e834818ae6aa Mon Sep 17 00:00:00 2001 From: Alexander Bachmann Date: Mon, 10 Feb 2025 13:13:14 +0100 Subject: [PATCH] improve(bluetooth): ignore devices with unknown type and without alias --- cosmic-applet-bluetooth/src/app.rs | 1 + cosmic-applet-bluetooth/src/bluetooth.rs | 36 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cosmic-applet-bluetooth/src/app.rs b/cosmic-applet-bluetooth/src/app.rs index 715a1e96..4b89a9cd 100644 --- a/cosmic-applet-bluetooth/src/app.rs +++ b/cosmic-applet-bluetooth/src/app.rs @@ -528,6 +528,7 @@ impl cosmic::Application for CosmicBluetoothApplet { .request_confirmation .as_ref() .map_or(false, |(dev, _, _)| d.address == dev.address) + && (d.has_name() || d.is_known_device_type()) }) { let row = row![ icon::from_name(dev.icon.as_str()).size(16).symbolic(true), diff --git a/cosmic-applet-bluetooth/src/bluetooth.rs b/cosmic-applet-bluetooth/src/bluetooth.rs index 98bfb94a..a7560271 100644 --- a/cosmic-applet-bluetooth/src/bluetooth.rs +++ b/cosmic-applet-bluetooth/src/bluetooth.rs @@ -29,6 +29,26 @@ use tokio::{ time::timeout, }; +// Copied from https://github.com/bluez/bluez/blob/39467578207889fd015775cbe81a3db9dd26abea/src/dbus-common.c#L53 +fn device_type_to_icon(device_type: &str) -> &'static str { + match device_type { + "computer" => "laptop-symbolic", + "phone" => "smartphone-symbolic", + "network-wireless" => "network-wireless-symbolic", + "audio-headset" => "audio-headset-symbolic", + "audio-headphones" => "audio-headphones-symbolic", + "camera-video" => "camera-video-symbolic", + "audio-card" => "audio-card-symbolic", + "input-gaming" => "input-gaming-symbolic", + "input-keyboard" => "input-keyboard-symbolic", + "input-tablet" => "input-tablet-symbolic", + "input-mouse" => "input-mouse-symbolic", + "printer" => "printer-network-symbolic", + "camera-photo" => "camera-photo-symbolic", + _ => default_device_icon, + } +} + pub fn bluetooth_subscription( id: I, ) -> iced::Subscription { @@ -222,6 +242,8 @@ impl PartialEq for BluerDevice { } } +const default_device_icon: &str = "bluetooth-symbolic"; + impl BluerDevice { pub async fn from_device(device: &bluer::Device) -> Self { let mut name = device @@ -246,12 +268,12 @@ impl BluerDevice { .iter() .find_map(|p| { if let DeviceProperty::Icon(icon) = p { - Some(icon.clone()) + Some(device_type_to_icon(icon.clone().as_str()).to_string()) } else { None } }) - .unwrap_or_else(|| "bluetooth-symbolic".into()); + .unwrap_or_else(|| device_type_to_icon(default_device_icon).to_string()); Self { name, @@ -274,6 +296,16 @@ impl BluerDevice { .count() == 2 } + + #[must_use] + pub fn is_known_device_type(&self) -> bool { + self.icon != default_device_icon + } + + #[must_use] + pub fn has_name(&self) -> bool { + self.name != self.address.to_string() + } } #[derive(Debug, Clone)]