Browse Source

system-settings: Refactor

af/unable-to-decryt-styling
Kévin Commaille 12 months ago
parent
commit
b2dd3acd08
No known key found for this signature in database
GPG Key ID: C971D9DBC9D678D
  1. 155
      src/system_settings/linux.rs
  2. 9
      src/system_settings/mod.rs

155
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::<ClockFormat>(GNOME_DESKTOP_NAMESPACE, CLOCK_FORMAT_KEY)
.await
})
.await
.expect("task was not aborted")
{
Ok(clock_format) => obj
.upcast_ref::<SystemSettings>()
.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::<ClockFormat>(
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::<SystemSettings>().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::<ClockFormat>(GNOME_DESKTOP_NAMESPACE, CLOCK_FORMAT_KEY)
.await
})
.await
.unwrap()
{
Ok(clock_format) => self
.upcast_ref::<SystemSettings>()
.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::<ClockFormat>(
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::<SystemSettings>().set_clock_format(clock_format);
} else {
error!("Could not update clock format setting: could not upgrade weak reference");
}
}
}).await;
}
}
impl Default for LinuxSystemSettings {

9
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<glib::Object>,
parent_class: glib::object::Class<glib::Object>,
}
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<ClockFormat>,
pub(super) clock_format: Cell<ClockFormat>,
}
#[glib::object_subclass]

Loading…
Cancel
Save