1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-09-15 19:26:58 +02:00

fix(networkmanager): prevent race condition, support all device types

This commit is contained in:
Reinout Meliesie 2025-08-17 20:57:40 +02:00
commit 8bb95e2e3f
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
2 changed files with 8 additions and 22 deletions

View file

@ -33,7 +33,6 @@ impl Client {
let dbus_connection = Connection::system().await?; let dbus_connection = Connection::system().await?;
let root = DbusProxy::new(&dbus_connection).await?; let root = DbusProxy::new(&dbus_connection).await?;
// All device types get added to this, but not all types emit events
let mut devices = HashSet::new(); let mut devices = HashSet::new();
let mut devices_changes = root.receive_all_devices_changed().await; let mut devices_changes = root.receive_all_devices_changed().await;

View file

@ -43,8 +43,9 @@ impl Module<gtk::Box> for NetworkManagerModule {
let client = context.try_client::<Client>()?; let client = context.try_client::<Client>()?;
// Should we be using context.tx with ModuleUpdateEvent::Update instead? // Should we be using context.tx with ModuleUpdateEvent::Update instead?
let tx = context.update_tx.clone(); let tx = context.update_tx.clone();
spawn(async move { // Must be done here synchronously to avoid race condition
let mut client_rx = client.subscribe(); let mut client_rx = client.subscribe();
spawn(async move {
while let Result::Ok(event) = client_rx.recv().await { while let Result::Ok(event) = client_rx.recv().await {
tx.send(event)?; tx.send(event)?;
} }
@ -62,10 +63,11 @@ impl Module<gtk::Box> for NetworkManagerModule {
) -> Result<ModuleParts<gtk::Box>> { ) -> Result<ModuleParts<gtk::Box>> {
let container = gtk::Box::new(Orientation::Horizontal, 0); let container = gtk::Box::new(Orientation::Horizontal, 0);
// TODO: Check if passing the widget context in its entirety here is possible // Must be done here synchronously to avoid race condition
// We cannot use recv_glib_async() here because the lifetimes don't work out let rx = context.subscribe();
// We cannot use recv_glib_async here because the lifetimes don't work out
spawn_future_local(handle_update_events( spawn_future_local(handle_update_events(
context.subscribe(), rx,
container.clone(), container.clone(),
self.icon_size, self.icon_size,
context.ironbar.image_provider(), context.ironbar.image_provider(),
@ -84,13 +86,8 @@ async fn handle_update_events(
let mut icons = HashMap::<String, Image>::new(); let mut icons = HashMap::<String, Image>::new();
while let Result::Ok(event) = rx.recv().await { while let Result::Ok(event) = rx.recv().await {
println!("NM UI event: {:?}", event);
match event { match event {
Event::DeviceAdded { interface, r#type } => { Event::DeviceAdded { interface, .. } => {
if !is_supported_device_type(&r#type) {
continue;
}
let icon = Image::new(); let icon = Image::new();
icon.add_class("icon"); icon.add_class("icon");
container.add(&icon); container.add(&icon);
@ -101,9 +98,6 @@ async fn handle_update_events(
r#type, r#type,
state, state,
} => { } => {
if !is_supported_device_type(&r#type) {
continue;
}
let icon = icons let icon = icons
.get(&interface) .get(&interface)
.expect("the icon for the interface to be present"); .expect("the icon for the interface to be present");
@ -124,13 +118,6 @@ async fn handle_update_events(
} }
} }
fn is_supported_device_type(r#type: &DeviceType) -> bool {
matches!(
r#type,
DeviceType::Ethernet | DeviceType::Wifi | DeviceType::Tun | DeviceType::Wireguard
)
}
fn get_icon_for_device_state(r#type: &DeviceType, state: &DeviceState) -> Option<&'static str> { fn get_icon_for_device_state(r#type: &DeviceType, state: &DeviceState) -> Option<&'static str> {
match r#type { match r#type {
DeviceType::Ethernet => match state { DeviceType::Ethernet => match state {
@ -169,6 +156,6 @@ fn get_icon_for_device_state(r#type: &DeviceType, state: &DeviceState) -> Option
DeviceState::Activated => Some("icon:network-vpn-symbolic"), DeviceState::Activated => Some("icon:network-vpn-symbolic"),
_ => None, _ => None,
}, },
_ => panic!("Device type should be a supported one"), _ => None,
} }
} }