From b9a471a5fb2234f6433d0ff2ae7faf7738fe805a Mon Sep 17 00:00:00 2001 From: Lucy Date: Mon, 7 Feb 2022 12:51:16 -0500 Subject: [PATCH] Wired network thingy works now. --- .../src/ui/current_networks.rs | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/applets/cosmic-applet-network/src/ui/current_networks.rs b/applets/cosmic-applet-network/src/ui/current_networks.rs index 56383a18..bf262feb 100644 --- a/applets/cosmic-applet-network/src/ui/current_networks.rs +++ b/applets/cosmic-applet-network/src/ui/current_networks.rs @@ -9,16 +9,81 @@ use cosmic_dbus_networkmanager::{ use gtk4::{ glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender}, prelude::*, + Image, Orientation, }; +use std::{cell::RefCell, rc::Rc}; use zbus::Connection; pub fn add_current_networks(target: >k4::Box) { - let our_box = gtk4::Box::new(gtk4::Orientation::Vertical, 0); + let our_box = gtk4::Box::new(Orientation::Vertical, 8); + let entries = Rc::>>::default(); let (tx, rx) = MainContext::channel::>(PRIORITY_DEFAULT); crate::task::spawn(handle_devices(tx)); + rx.attach( + None, + clone!(@weak our_box, @strong entries => @default-return Continue(true), move |connections| { + let mut entries = entries.borrow_mut(); + display_active_connections(connections, &our_box, &mut *entries); + Continue(true) + }), + ); target.append(&our_box); } +fn display_active_connections( + connections: Vec, + target: >k4::Box, + entries: &mut Vec, +) { + for old_entry in entries.drain(..) { + target.remove(&old_entry); + } + for connection in connections { + let entry = match connection { + ActiveConnectionInfo::Wired { + name, + hw_address, + speed, + } => render_wired_connection(name, speed), + ActiveConnectionInfo::WiFi { + name, + hw_address, + flags, + rsn_flags, + wpa_flags, + } => todo!(), + }; + target.append(&entry); + entries.push(entry); + } +} + +fn render_wired_connection(name: String, speed: u32) -> gtk4::Box { + view! { + entry = gtk4::Box { + set_orientation: Orientation::Horizontal, + set_spacing: 8, + append: wired_icon = &Image { + set_icon_name: Some("network-wired-symbolic"), + }, + append: wired_label_box = >k4::Box { + set_orientation: Orientation::Vertical, + append: wired_label = >k4::Label { + set_label: &name, + }, + append: wired_ip = >k4::Label { + set_label: "IP Address: N/A", + } + }, + append: wired_speed = >k4::Label { + set_label: &format!("Connected - {} Mbps", speed), + set_valign: gtk4::Align::Center, + }, + } + } + entry +} + async fn handle_devices(tx: Sender>) -> zbus::Result<()> { let conn = Connection::system().await?; let network_manager = NetworkManager::new(&conn).await?; @@ -49,6 +114,8 @@ async fn handle_devices(tx: Sender>) -> zbus::Result<( } } } + tx.send(info) + .expect("failed to send active connections back to main thread"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; } }