From 0d547445379ea13bb1b11c5e1be3eaaa5c7346a6 Mon Sep 17 00:00:00 2001 From: Lucy Date: Mon, 7 Feb 2022 14:14:55 -0500 Subject: [PATCH] Working WiFi toggle! --- Cargo.lock | 2 +- applets/cosmic-applet-network/src/main.rs | 4 +- applets/cosmic-applet-network/src/task.rs | 8 ++++ .../cosmic-applet-network/src/ui/toggles.rs | 47 +++++++++++++++++-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bf0daf5..2d347a44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,7 +236,7 @@ dependencies = [ [[package]] name = "cosmic-dbus-networkmanager" version = "0.1.0" -source = "git+https://github.com/pop-os/dbus-settings-bindings#52b613d2849ec34108aede515352a58f4b91bf38" +source = "git+https://github.com/pop-os/dbus-settings-bindings#4704a1672daeab75e83035449895862dfab3f816" dependencies = [ "bitflags", "derive_builder", diff --git a/applets/cosmic-applet-network/src/main.rs b/applets/cosmic-applet-network/src/main.rs index 90c9796e..6fddafc9 100644 --- a/applets/cosmic-applet-network/src/main.rs +++ b/applets/cosmic-applet-network/src/main.rs @@ -40,9 +40,9 @@ fn build_ui(application: >k4::Application) { set_margin_end: 24 } } - ui::toggles::add_toggles(&main_box); - main_box.append(&Separator::new(Orientation::Horizontal)); ui::current_networks::add_current_networks(&main_box); + main_box.append(&Separator::new(Orientation::Horizontal)); + ui::toggles::add_toggles(&main_box); let available_wifi_separator = Separator::new(Orientation::Horizontal); main_box.append(&available_wifi_separator); available_wifi_separator.hide(); diff --git a/applets/cosmic-applet-network/src/task.rs b/applets/cosmic-applet-network/src/task.rs index 37078fbb..39fc9073 100644 --- a/applets/cosmic-applet-network/src/task.rs +++ b/applets/cosmic-applet-network/src/task.rs @@ -10,6 +10,14 @@ where crate::RT.spawn(future) } +pub fn block_on(future: F) -> O +where + F: Future + Send + 'static, + O: Send + 'static, +{ + crate::RT.block_on(future) +} + pub fn spawn_local + 'static>(future: F) { gtk4::glib::MainContext::default().spawn_local(future); } diff --git a/applets/cosmic-applet-network/src/ui/toggles.rs b/applets/cosmic-applet-network/src/ui/toggles.rs index d68bf0cf..d215ef9c 100644 --- a/applets/cosmic-applet-network/src/ui/toggles.rs +++ b/applets/cosmic-applet-network/src/ui/toggles.rs @@ -1,5 +1,11 @@ -use crate::widgets::SettingsEntry; -use gtk4::{prelude::*, Orientation, Separator, Switch}; +use crate::{task, widgets::SettingsEntry}; +use cosmic_dbus_networkmanager::nm::NetworkManager; +use gtk4::{ + glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender}, + prelude::*, + Inhibit, Orientation, Separator, Switch, +}; +use zbus::Connection; pub fn add_toggles(target: >k4::Box) { view! { @@ -14,9 +20,44 @@ pub fn add_toggles(target: >k4::Box) { set_child: wifi_switch = &Switch {} } } - target.append(&airplane_mode); target.append(&Separator::new(Orientation::Horizontal)); target.append(&wifi); target.append(&Separator::new(Orientation::Horizontal)); + + let (wifi_tx, wifi_rx) = MainContext::channel::(PRIORITY_DEFAULT); + wifi_switch.connect_state_set( + clone!(@strong wifi_tx => @default-return Inhibit(false), move |_switch, state| { + match task::block_on(set_wifi_mode(state)) { + Ok(()) => Inhibit(false), + Err(err) => { + eprintln!("set_wifi_mode failed: {}", err); + Inhibit(true) + } + } + }), + ); + wifi_rx.attach( + None, + clone!(@weak wifi_switch => @default-return Continue(true), move |wifi| { + wifi_switch.set_active(wifi); + Continue(true) + }), + ); + task::spawn(get_wifi_mode(wifi_tx)); +} + +async fn get_wifi_mode(tx: Sender) -> zbus::Result<()> { + let connection = Connection::system().await?; + let network_manager = NetworkManager::new(&connection).await?; + let wireless_enabled = network_manager.wireless_enabled().await?; + tx.send(wireless_enabled) + .expect("Failed to send wifi enablement back to main thread"); + Ok(()) +} + +async fn set_wifi_mode(state: bool) -> zbus::Result<()> { + let connection = Connection::system().await?; + let network_manager = NetworkManager::new(&connection).await?; + network_manager.set_wireless_enabled(state).await }