diff --git a/src/system_settings/linux.rs b/src/system_settings/linux.rs index 11d2016a..84c29df4 100644 --- a/src/system_settings/linux.rs +++ b/src/system_settings/linux.rs @@ -27,19 +27,95 @@ mod imp { impl ObjectImpl for LinuxSystemSettings { fn constructed(&self) { self.parent_constructed(); - let obj = self.obj(); spawn!(clone!( - #[weak] - obj, + #[weak(rename_to = imp)] + self, async move { - obj.init().await; + imp.init().await; } )); } } impl SystemSettingsImpl for LinuxSystemSettings {} + + impl LinuxSystemSettings { + /// Initialize the system settings. + async fn init(&self) { + let obj = self.obj(); + + let proxy = match spawn_tokio!(async move { SettingsProxy::new().await }) + .await + .expect("task was not aborted") + { + Ok(proxy) => proxy, + Err(error) => { + error!("Could not access settings portal: {error}"); + return; + } + }; + let proxy = Arc::new(proxy); + + let proxy_clone = proxy.clone(); + match spawn_tokio!(async move { + proxy_clone + .read::(GNOME_DESKTOP_NAMESPACE, CLOCK_FORMAT_KEY) + .await + }) + .await + .expect("task was not aborted") + { + Ok(clock_format) => obj + .upcast_ref::() + .set_clock_format(clock_format), + Err(error) => { + error!("Could not access clock format system setting: {error}"); + return; + } + } + + let clock_format_changed_stream = match spawn_tokio!(async move { + proxy + .receive_setting_changed_with_args::( + GNOME_DESKTOP_NAMESPACE, + CLOCK_FORMAT_KEY, + ) + .await + }) + .await + .expect("task was not aborted") + { + Ok(stream) => stream, + Err(error) => { + error!( + "Could not listen to changes of the clock format system setting: {error}" + ); + return; + } + }; + + let obj_weak = obj.downgrade(); + clock_format_changed_stream.for_each(move |value| { + let obj_weak = obj_weak.clone(); + async move { + let clock_format = match value { + Ok(clock_format) => clock_format, + Err(error) => { + error!("Could not update clock format setting: {error}"); + return; + } + }; + + if let Some(obj) = obj_weak.upgrade() { + obj.upcast_ref::().set_clock_format(clock_format); + } else { + error!("Could not update clock format setting: could not upgrade weak reference"); + } + } + }).await; + } + } } glib::wrapper! { @@ -52,77 +128,6 @@ impl LinuxSystemSettings { pub fn new() -> Self { glib::Object::new() } - - /// Initialize the system settings. - async fn init(&self) { - let proxy = match spawn_tokio!(async move { SettingsProxy::new().await }) - .await - .unwrap() - { - Ok(proxy) => proxy, - Err(error) => { - error!("Could not access settings portal: {error}"); - return; - } - }; - let proxy = Arc::new(proxy); - - let proxy_clone = proxy.clone(); - match spawn_tokio!(async move { - proxy_clone - .read::(GNOME_DESKTOP_NAMESPACE, CLOCK_FORMAT_KEY) - .await - }) - .await - .unwrap() - { - Ok(clock_format) => self - .upcast_ref::() - .set_clock_format(clock_format), - Err(error) => { - error!("Could not access clock format system setting: {error}"); - return; - } - } - - let clock_format_changed_stream = match spawn_tokio!(async move { - proxy - .receive_setting_changed_with_args::( - GNOME_DESKTOP_NAMESPACE, - CLOCK_FORMAT_KEY, - ) - .await - }) - .await - .unwrap() - { - Ok(stream) => stream, - Err(error) => { - error!("Could not listen to changes of the clock format system setting: {error}"); - return; - } - }; - - let obj_weak = self.downgrade(); - clock_format_changed_stream.for_each(move |value| { - let obj_weak = obj_weak.clone(); - async move { - let clock_format = match value { - Ok(clock_format) => clock_format, - Err(error) => { - error!("Could not update clock format setting: {error}"); - return; - } - }; - - if let Some(obj) = obj_weak.upgrade() { - obj.upcast_ref::().set_clock_format(clock_format); - } else { - error!("Could not update clock format setting: could not upgrade weak reference"); - } - } - }).await; - } } impl Default for LinuxSystemSettings { diff --git a/src/system_settings/mod.rs b/src/system_settings/mod.rs index e2c5e56f..4d12f366 100644 --- a/src/system_settings/mod.rs +++ b/src/system_settings/mod.rs @@ -6,13 +6,12 @@ mod linux; /// The clock format setting. #[derive(Debug, Clone, Copy, PartialEq, Eq, glib::Enum)] -#[repr(u32)] #[enum_type(name = "ClockFormat")] pub enum ClockFormat { /// The 12h format, i.e. AM/PM. - TwelveHours = 0, + TwelveHours, /// The 24h format. - TwentyFourHours = 1, + TwentyFourHours, } impl Default for ClockFormat { @@ -39,7 +38,7 @@ mod imp { #[repr(C)] pub struct SystemSettingsClass { - pub parent_class: glib::object::Class, + parent_class: glib::object::Class, } unsafe impl ClassStruct for SystemSettingsClass { @@ -51,7 +50,7 @@ mod imp { pub struct SystemSettings { /// The clock format setting. #[property(get, builder(ClockFormat::default()))] - pub clock_format: Cell, + pub(super) clock_format: Cell, } #[glib::object_subclass]