diff --git a/src/clients/networkmanager/mod.rs b/src/clients/networkmanager/mod.rs index c6faba7..52c1329 100644 --- a/src/clients/networkmanager/mod.rs +++ b/src/clients/networkmanager/mod.rs @@ -33,7 +33,6 @@ impl Client { let dbus_connection = Connection::system().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_changes = root.receive_all_devices_changed().await; diff --git a/src/modules/networkmanager.rs b/src/modules/networkmanager.rs index ba1b116..a19f22a 100644 --- a/src/modules/networkmanager.rs +++ b/src/modules/networkmanager.rs @@ -43,8 +43,9 @@ impl Module for NetworkManagerModule { let client = context.try_client::()?; // Should we be using context.tx with ModuleUpdateEvent::Update instead? let tx = context.update_tx.clone(); + // Must be done here synchronously to avoid race condition + let mut client_rx = client.subscribe(); spawn(async move { - let mut client_rx = client.subscribe(); while let Result::Ok(event) = client_rx.recv().await { tx.send(event)?; } @@ -62,10 +63,11 @@ impl Module for NetworkManagerModule { ) -> Result> { let container = gtk::Box::new(Orientation::Horizontal, 0); - // TODO: Check if passing the widget context in its entirety here is possible - // We cannot use recv_glib_async() here because the lifetimes don't work out + // Must be done here synchronously to avoid race condition + let rx = context.subscribe(); + // We cannot use recv_glib_async here because the lifetimes don't work out spawn_future_local(handle_update_events( - context.subscribe(), + rx, container.clone(), self.icon_size, context.ironbar.image_provider(), @@ -84,13 +86,8 @@ async fn handle_update_events( let mut icons = HashMap::::new(); while let Result::Ok(event) = rx.recv().await { - println!("NM UI event: {:?}", event); - match event { - Event::DeviceAdded { interface, r#type } => { - if !is_supported_device_type(&r#type) { - continue; - } + Event::DeviceAdded { interface, .. } => { let icon = Image::new(); icon.add_class("icon"); container.add(&icon); @@ -101,9 +98,6 @@ async fn handle_update_events( r#type, state, } => { - if !is_supported_device_type(&r#type) { - continue; - } let icon = icons .get(&interface) .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> { match r#type { 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"), _ => None, }, - _ => panic!("Device type should be a supported one"), + _ => None, } }