Working WiFi toggle!
This commit is contained in:
parent
b9a471a5fb
commit
0d54744537
4 changed files with 55 additions and 6 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -10,6 +10,14 @@ where
|
|||
crate::RT.spawn(future)
|
||||
}
|
||||
|
||||
pub fn block_on<O, F>(future: F) -> O
|
||||
where
|
||||
F: Future<Output = O> + Send + 'static,
|
||||
O: Send + 'static,
|
||||
{
|
||||
crate::RT.block_on(future)
|
||||
}
|
||||
|
||||
pub fn spawn_local<F: Future<Output = ()> + 'static>(future: F) {
|
||||
gtk4::glib::MainContext::default().spawn_local(future);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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::<bool>(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<bool>) -> 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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue