From c4c95df8d3dadbaa3bad93bba08c60c7a7c3593e Mon Sep 17 00:00:00 2001 From: takotori Date: Mon, 30 Oct 2023 11:48:33 +0100 Subject: [PATCH 1/2] reorganize wifi (wip) --- src/components/wifi/mod.rs | 90 +--------------------------- src/components/wifi/wifiBox.rs | 90 +++++++++++++--------------- src/components/wifi/wifiBoxImpl.rs | 55 +++++++++++++++++ src/components/wifi/wifiEntry.rs | 78 ++++++++---------------- src/components/wifi/wifiEntryImpl.rs | 57 ++++++++++++++++++ 5 files changed, 182 insertions(+), 188 deletions(-) create mode 100644 src/components/wifi/wifiBoxImpl.rs create mode 100644 src/components/wifi/wifiEntryImpl.rs diff --git a/src/components/wifi/mod.rs b/src/components/wifi/mod.rs index 34ee916..b01145e 100644 --- a/src/components/wifi/mod.rs +++ b/src/components/wifi/mod.rs @@ -1,89 +1,5 @@ #![allow(non_snake_case)] - -use std::thread; -use std::time::Duration; - -use adw::glib::{Object, PropertySet}; -use adw::glib::clone; -use adw::subclass::prelude::ObjectSubclassIsExt; -use dbus::blocking::Connection; -use dbus::Error; -use gtk::glib; -use gtk::prelude::WidgetExt; - -use crate::components::wifi::wifiEntry::WifiStrength; - mod wifiBox; -mod wifiEntry; - -glib::wrapper! { - pub struct WifiBox(ObjectSubclass) - @extends gtk::Box, gtk::Widget, - @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable; -} - -glib::wrapper! { - pub struct WifiEntry(ObjectSubclass) - @extends gtk::ListBoxRow, gtk::Widget, - @implements gtk::Accessible, gtk::Buildable, gtk::Actionable, gtk::ConstraintTarget; -} - -impl WifiBox { - pub fn new() -> Self { - Object::builder().build() - } - - pub fn setupCallbacks(&self) { - let selfImp = self.imp(); - - selfImp.resetWifiDetails.connect_row_activated(clone!(@ weak selfImp as window => move |_, y| { - // let result = y.downcast_ref()::().unwrap(); no worky smh - })); - } - - pub fn scanForWifi(&self) { - let selfImp = self.imp(); - let mut wifiEntries = selfImp.wifiEntries.borrow_mut(); - wifiEntries.push(WifiEntry::new(WifiStrength::Excellent, "ina internet", true)); - wifiEntries.push(WifiEntry::new(WifiStrength::Excellent, "watch ina", true)); - wifiEntries.push(WifiEntry::new(WifiStrength::Ok, "INANET", true)); - wifiEntries.push(WifiEntry::new(WifiStrength::Weak, "ina best waifu", false)); - - for wifiEntry in wifiEntries.iter() { - selfImp.resetWifiList.append(wifiEntry); - } - } - - pub fn donotdisturb() { - thread::spawn(|| { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.freedesktop.Notifications", - "/org/freedesktop/Notifications", - Duration::from_millis(1000), - ); - let _: Result<(), Error> = proxy.method_call("org.freedesktop.Notifications", "DoNotDisturb", ()); - }); - } -} - -impl WifiEntry { - pub fn new(strength: WifiStrength, name: &str, isEncrypted: bool) -> Self { - let entry: WifiEntry = Object::builder().build(); - let entryImp = entry.imp(); - entryImp.wifiStrength.set(strength); - entryImp.resetWifiLabel.get().set_text(name); - entryImp.resetWifiEncrypted.set_visible(isEncrypted); - entryImp.resetWifiStrength.get().set_from_icon_name(match strength { - WifiStrength::Excellent => Some("network-wireless-signal-excellent-symbolic"), - WifiStrength::Ok => Some("network-wireless-signal-ok-symbolic"), - WifiStrength::Weak => Some("network-wireless-signal-weak-symbolic"), - WifiStrength::None => Some("network-wireless-signal-none-symbolic"), - }); - { - let mut wifiName = entryImp.wifiName.borrow_mut(); - *wifiName = String::from(name); - } - entry - } -} \ No newline at end of file +mod wifiBoxImpl; +mod wifiEntryImpl; +mod wifiEntry; \ No newline at end of file diff --git a/src/components/wifi/wifiBox.rs b/src/components/wifi/wifiBox.rs index 2a43174..094dd4c 100644 --- a/src/components/wifi/wifiBox.rs +++ b/src/components/wifi/wifiBox.rs @@ -1,55 +1,49 @@ -use std::cell::RefCell; -use gtk::{CompositeTemplate, glib, ListBox, ListBoxRow, Switch}; -use gtk::prelude::*; -use gtk::subclass::prelude::*; +use adw::glib; +use dbus::blocking::Connection; +use crate::components::wifi::wifiEntry::WifiEntry; +use crate::components::wifi::wifiEntryImpl::WifiStrength; -use crate::components::wifi::WifiEntry; - -#[allow(non_snake_case)] -#[derive(Default, CompositeTemplate)] -#[template(resource = "/org/Xetibo/ReSet/resetWiFi.ui")] -pub struct WifiBox { - #[template_child] - pub resetWifiDetails: TemplateChild, - #[template_child] - pub resetWifiSwitchRow: TemplateChild, - #[template_child] - pub resetWifiSwitch: TemplateChild, - #[template_child] - pub resetWifiList: TemplateChild, - pub wifiEntries: RefCell>, +glib::wrapper! { + pub struct WifiBox(ObjectSubclass) + @extends gtk::Box, gtk::Widget, + @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable; } -#[glib::object_subclass] -impl ObjectSubclass for WifiBox { - const NAME: &'static str = "resetWifi"; - type Type = super::WifiBox; - type ParentType = gtk::Box; - - fn class_init(klass: &mut Self::Class) { - WifiEntry::ensure_type(); - klass.bind_template(); +impl WifiBox { + pub fn new() -> Self { + Object::builder().build() } - fn instance_init(obj: &glib::subclass::InitializingObject) { - obj.init_template(); + pub fn setupCallbacks(&self) { + let selfImp = self.imp(); + + selfImp.resetWifiDetails.connect_row_activated(clone!(@ weak selfImp as window => move |_, y| { + // let result = y.downcast_ref()::().unwrap(); no worky smh + })); + } + + pub fn scanForWifi(&self) { + let selfImp = self.imp(); + let mut wifiEntries = selfImp.wifiEntries.borrow_mut(); + wifiEntries.push(WifiEntry::new(WifiStrength::Excellent, "ina internet", true)); + wifiEntries.push(WifiEntry::new(WifiStrength::Excellent, "watch ina", true)); + wifiEntries.push(WifiEntry::new(WifiStrength::Ok, "INANET", true)); + wifiEntries.push(WifiEntry::new(WifiStrength::Weak, "ina best waifu", false)); + + for wifiEntry in wifiEntries.iter() { + selfImp.resetWifiList.append(wifiEntry); + } + } + + pub fn donotdisturb() { + thread::spawn(|| { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.freedesktop.Notifications", + "/org/freedesktop/Notifications", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = proxy.method_call("org.freedesktop.Notifications", "DoNotDisturb", ()); + }); } } - -impl ObjectImpl for WifiBox { - fn constructed(&self) { - self.parent_constructed(); - - let obj = self.obj(); - obj.setupCallbacks(); - obj.scanForWifi(); - } -} - -impl BoxImpl for WifiBox {} - -impl WidgetImpl for WifiBox {} - -impl WindowImpl for WifiBox {} - -impl ApplicationWindowImpl for WifiBox {} diff --git a/src/components/wifi/wifiBoxImpl.rs b/src/components/wifi/wifiBoxImpl.rs new file mode 100644 index 0000000..2a43174 --- /dev/null +++ b/src/components/wifi/wifiBoxImpl.rs @@ -0,0 +1,55 @@ +use std::cell::RefCell; +use gtk::{CompositeTemplate, glib, ListBox, ListBoxRow, Switch}; +use gtk::prelude::*; +use gtk::subclass::prelude::*; + +use crate::components::wifi::WifiEntry; + +#[allow(non_snake_case)] +#[derive(Default, CompositeTemplate)] +#[template(resource = "/org/Xetibo/ReSet/resetWiFi.ui")] +pub struct WifiBox { + #[template_child] + pub resetWifiDetails: TemplateChild, + #[template_child] + pub resetWifiSwitchRow: TemplateChild, + #[template_child] + pub resetWifiSwitch: TemplateChild, + #[template_child] + pub resetWifiList: TemplateChild, + pub wifiEntries: RefCell>, +} + +#[glib::object_subclass] +impl ObjectSubclass for WifiBox { + const NAME: &'static str = "resetWifi"; + type Type = super::WifiBox; + type ParentType = gtk::Box; + + fn class_init(klass: &mut Self::Class) { + WifiEntry::ensure_type(); + klass.bind_template(); + } + + fn instance_init(obj: &glib::subclass::InitializingObject) { + obj.init_template(); + } +} + +impl ObjectImpl for WifiBox { + fn constructed(&self) { + self.parent_constructed(); + + let obj = self.obj(); + obj.setupCallbacks(); + obj.scanForWifi(); + } +} + +impl BoxImpl for WifiBox {} + +impl WidgetImpl for WifiBox {} + +impl WindowImpl for WifiBox {} + +impl ApplicationWindowImpl for WifiBox {} diff --git a/src/components/wifi/wifiEntry.rs b/src/components/wifi/wifiEntry.rs index d7fb28a..41a8be9 100644 --- a/src/components/wifi/wifiEntry.rs +++ b/src/components/wifi/wifiEntry.rs @@ -1,57 +1,29 @@ -use std::cell::RefCell; -use gtk::{Button, CompositeTemplate, glib, Image, Label}; -use gtk::subclass::prelude::*; +use adw::glib; +use crate::components::wifi::wifiEntryImpl::WifiStrength; -#[derive(Default, Copy, Clone)] -pub enum WifiStrength { - Excellent, - Ok, - Weak, - #[default] - None, +glib::wrapper! { + pub struct WifiEntry(ObjectSubclass) + @extends gtk::ListBoxRow, gtk::Widget, + @implements gtk::Accessible, gtk::Buildable, gtk::Actionable, gtk::ConstraintTarget; } -#[allow(non_snake_case)] -#[derive(Default, CompositeTemplate)] -#[template(resource = "/org/Xetibo/ReSet/resetWifiEntry.ui")] -pub struct WifiEntry { - #[template_child] - pub resetWifiStrength: TemplateChild, - #[template_child] - pub resetWifiEncrypted: TemplateChild, - #[template_child] - pub resetWifiLabel: TemplateChild