Skip to content

Commit

Permalink
improve(bluetooth): ignore devices with unknown type and without alias
Browse files Browse the repository at this point in the history
  • Loading branch information
abachmann authored Feb 10, 2025
1 parent fd15399 commit 0f4667f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions cosmic-applet-bluetooth/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
36 changes: 34 additions & 2 deletions cosmic-applet-bluetooth/src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
) -> iced::Subscription<BluerEvent> {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)]
Expand Down

0 comments on commit 0f4667f

Please sign in to comment.