diff --git a/applets/cosmic-applet-network/src/main.rs b/applets/cosmic-applet-network/src/main.rs index 2d0a5319..8d83e2fb 100644 --- a/applets/cosmic-applet-network/src/main.rs +++ b/applets/cosmic-applet-network/src/main.rs @@ -7,10 +7,10 @@ pub mod task; pub mod ui; pub mod widgets; -use gtk4::{glib, gio::ApplicationFlags, prelude::*, Orientation, Separator}; +use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig}; +use gtk4::{gio::ApplicationFlags, glib, prelude::*, Orientation, Separator}; use once_cell::sync::Lazy; use tokio::runtime::Runtime; -use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig}; static RT: Lazy = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime")); diff --git a/applets/cosmic-applet-network/src/ui/available_wifi.rs b/applets/cosmic-applet-network/src/ui/available_wifi.rs index 76a0a748..ae14c559 100644 --- a/applets/cosmic-applet-network/src/ui/available_wifi.rs +++ b/applets/cosmic-applet-network/src/ui/available_wifi.rs @@ -12,7 +12,11 @@ use gtk4::{ Image, ListBox, ListBoxRow, Separator, }; use libcosmic_widgets::{relm4::RelmContainerExt, LabeledItem}; -use std::{cell::RefCell, rc::Rc}; +use std::{ + cell::RefCell, + collections::{BTreeMap, HashMap}, + rc::Rc, +}; use zbus::Connection; pub fn add_available_wifi(target: >k4::Box, separator: Separator) { @@ -23,7 +27,7 @@ pub fn add_available_wifi(target: >k4::Box, separator: Separator) { eprintln!("scan_for_wifi failed: {}", err); } }); - + let scrolled_window = gtk4::ScrolledWindow::new(); scrolled_window.set_hscrollbar_policy(gtk4::PolicyType::Never); scrolled_window.set_vscrollbar_policy(gtk4::PolicyType::Automatic); @@ -90,9 +94,7 @@ async fn handle_wireless_device( device: WirelessDevice<'_>, tx: Sender>, ) -> zbus::Result<()> { - device - .request_scan(std::collections::HashMap::new()) - .await?; + device.request_scan(HashMap::new()).await?; let mut scan_changed = device.receive_last_scan_changed().await; if let Some(t) = scan_changed.next().await { if let Ok(-1) = t.get().await { @@ -101,13 +103,19 @@ async fn handle_wireless_device( } } let access_points = device.get_access_points().await?; - let mut aps = Vec::with_capacity(access_points.len()); + // Sort by SSID and remove duplicates + let mut aps = BTreeMap::::new(); for ap in access_points { - aps.push(AccessPoint { - ssid: String::from_utf8_lossy(&ap.ssid().await?.clone()).into_owned(), - strength: ap.strength().await?, - }); + let ssid = String::from_utf8_lossy(&ap.ssid().await?.clone()).into_owned(); + let strength = ap.strength().await?; + if let Some(access_point) = aps.get(&ssid) { + if access_point.strength > strength { + continue; + } + } + aps.insert(ssid.clone(), AccessPoint { ssid, strength }); } + let aps = aps.into_iter().map(|(_, x)| x).collect(); tx.send(aps).expect("failed to send back to main thread"); Ok(()) } diff --git a/applets/cosmic-applet-network/src/ui/current_networks.rs b/applets/cosmic-applet-network/src/ui/current_networks.rs index eae467f5..0f389ece 100644 --- a/applets/cosmic-applet-network/src/ui/current_networks.rs +++ b/applets/cosmic-applet-network/src/ui/current_networks.rs @@ -53,7 +53,7 @@ fn display_active_connections( } => { icon_image.set_icon_name(Some("network-wired-symbolic")); render_wired_connection(name, speed, ip_addresses) - }, + } ActiveConnectionInfo::WiFi { name, hw_address, @@ -64,7 +64,7 @@ fn display_active_connections( ActiveConnectionInfo::Vpn { name, ip_addresses } => { icon_image.set_icon_name(Some("network-vpn-symbolic")); render_vpn(name, ip_addresses) - }, + } }; let entry = ListBoxRow::builder().child(&entry).build(); target.append(&entry);