Wired network thingy works now.
This commit is contained in:
parent
d7db1243e4
commit
b9a471a5fb
1 changed files with 68 additions and 1 deletions
|
|
@ -9,16 +9,81 @@ use cosmic_dbus_networkmanager::{
|
||||||
use gtk4::{
|
use gtk4::{
|
||||||
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
|
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
Image, Orientation,
|
||||||
};
|
};
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use zbus::Connection;
|
use zbus::Connection;
|
||||||
|
|
||||||
pub fn add_current_networks(target: >k4::Box) {
|
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::<RefCell<Vec<gtk4::Box>>>::default();
|
||||||
let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT);
|
let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT);
|
||||||
crate::task::spawn(handle_devices(tx));
|
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);
|
target.append(&our_box);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn display_active_connections(
|
||||||
|
connections: Vec<ActiveConnectionInfo>,
|
||||||
|
target: >k4::Box,
|
||||||
|
entries: &mut Vec<gtk4::Box>,
|
||||||
|
) {
|
||||||
|
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<Vec<ActiveConnectionInfo>>) -> zbus::Result<()> {
|
async fn handle_devices(tx: Sender<Vec<ActiveConnectionInfo>>) -> zbus::Result<()> {
|
||||||
let conn = Connection::system().await?;
|
let conn = Connection::system().await?;
|
||||||
let network_manager = NetworkManager::new(&conn).await?;
|
let network_manager = NetworkManager::new(&conn).await?;
|
||||||
|
|
@ -49,6 +114,8 @@ async fn handle_devices(tx: Sender<Vec<ActiveConnectionInfo>>) -> 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;
|
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue