1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-09-18 04:36:57 +02:00

refactor(networkmanager): identify devices by their number outside of the client

This commit is contained in:
Reinout Meliesie 2025-09-04 14:54:23 +02:00
commit db88e12b8e
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
3 changed files with 26 additions and 20 deletions

View file

@ -3,12 +3,12 @@ use crate::clients::networkmanager::dbus::{DeviceState, DeviceType};
#[derive(Debug, Clone)]
pub enum ClientToModuleEvent {
DeviceChanged {
interface: String,
number: u32,
r#type: DeviceType,
new_state: DeviceState,
},
DeviceRemoved {
interface: String,
number: u32,
},
}

View file

@ -121,12 +121,9 @@ impl ClientInner {
watcher.state_watcher.abort();
watchers.remove(removed_device_path);
// TODO: Replace the identifier sent to modules with the dbus device number (last segment of its path)
let dbus_connection = &self.dbus_connection().await?;
let device = DeviceDbusProxy::new(dbus_connection, removed_device_path).await?;
let interface = device.interface().await?.to_string();
let number = get_number_from_dbus_path(removed_device_path);
self.controller_sender
.send(ClientToModuleEvent::DeviceRemoved { interface })?;
.send(ClientToModuleEvent::DeviceRemoved { number })?;
debug!("D-bus device watchers for {} stopped", removed_device_path);
}
@ -148,12 +145,12 @@ impl ClientInner {
let dbus_connection = &self.dbus_connection().await?;
let device = DeviceDbusProxy::new(dbus_connection, device_path).await?;
let interface = device.interface().await?.to_string();
let number = get_number_from_dbus_path(device_path);
let r#type = device.device_type().await?;
let new_state = device.state().await?;
self.controller_sender
.send(ClientToModuleEvent::DeviceChanged {
interface,
number,
r#type,
new_state,
})?;
@ -177,14 +174,14 @@ impl ClientInner {
let dbus_connection = Connection::system().await?;
let device = DeviceDbusProxy::new(&dbus_connection, path.clone()).await?;
let interface = device.interface().await?;
let number = get_number_from_dbus_path(&path);
let r#type = device.device_type().await?;
// Send an event communicating the initial state
let new_state = device.state().await?;
self.controller_sender
.send(ClientToModuleEvent::DeviceChanged {
interface: interface.to_string(),
number,
r#type: r#type.clone(),
new_state,
})?;
@ -194,7 +191,7 @@ impl ClientInner {
let new_state = state_change.get().await?;
self.controller_sender
.send(ClientToModuleEvent::DeviceChanged {
interface: interface.to_string(),
number,
r#type: r#type.clone(),
new_state,
})?;
@ -225,4 +222,13 @@ pub fn create_client() -> ClientResult<Client> {
Ok(client)
}
fn get_number_from_dbus_path(path: &ObjectPath) -> u32 {
let (_, number_str) = path
.rsplit_once('/')
.expect("Path must have at least two segments to contain an object number");
number_str
.parse()
.expect("Last segment was not a positive integer")
}
register_fallible_client!(Client, network_manager);

View file

@ -91,17 +91,17 @@ async fn handle_update_events(
image_provider: Provider,
) -> Result<()> {
// TODO: Ensure the visible icons are always in the same order
let mut icons = HashMap::<String, Image>::new();
let mut icons = HashMap::<u32, Image>::new();
while let Result::Ok(event) = widget_receiver.recv().await {
match event {
ClientToModuleEvent::DeviceChanged {
interface,
number,
r#type,
new_state,
} => {
let icon: &_ = icons.entry(interface.clone()).or_insert_with(|| {
debug!("Adding icon for {}", interface);
let icon: &_ = icons.entry(number).or_insert_with(|| {
debug!("Adding icon for device {}", number);
let icon = Image::new();
icon.add_class("icon");
@ -123,14 +123,14 @@ async fn handle_update_events(
}
}
}
ClientToModuleEvent::DeviceRemoved { interface } => {
ClientToModuleEvent::DeviceRemoved { number } => {
let icon = icons
.get(interface.as_str())
.get(&number)
.expect("The icon for {} was about to be removed but was not present");
container.remove(icon);
icons.remove(interface.as_str());
icons.remove(&number);
debug!("Removed icon for {}", interface);
debug!("Removed icon for device {}", number);
}
}
}