From f8a6e09866e5cf42a43dc57ce8b70425ec43b2af Mon Sep 17 00:00:00 2001 From: Erica Z Date: Sun, 24 Nov 2024 18:19:07 +0100 Subject: [PATCH] make gsettings a thread local --- src/application.rs | 2 +- src/lib.rs | 10 ++++++++++ src/main.rs | 4 ---- src/mpris.rs | 2 +- src/subsonic.rs | 4 ++-- src/ui/setup.rs | 4 ++-- src/ui/window.rs | 37 +++++++++++++++++++------------------ 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/application.rs b/src/application.rs index 0195919..d2a0e0a 100644 --- a/src/application.rs +++ b/src/application.rs @@ -57,7 +57,7 @@ glib::wrapper! { impl Default for Application { fn default() -> Self { glib::Object::builder::() - .property("application-id", crate::APP_ID) + .property("application-id", audrey::APP_ID) .property("flags", gio::ApplicationFlags::default()) .build() } diff --git a/src/lib.rs b/src/lib.rs index c9ad65e..3205686 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,17 @@ +pub const VERSION: &str = "0.1.0"; // AUDREY_VERSION +pub const USER_AGENT: &str = "audrey/linux"; +pub const APP_ID: &str = "eu.callcc.audrey"; + pub mod globals { + use adw::gio; use std::path::PathBuf; use tokio::sync::Mutex; + // can't use LazyLock sinze glib objects aren't thread safe + thread_local! { + pub static SETTINGS: gio::Settings = gio::Settings::new(crate::APP_ID); + } + pub fn xdg_dirs() -> &'static xdg::BaseDirectories { static DIRS: std::sync::LazyLock = std::sync::LazyLock::new(|| { xdg::BaseDirectories::with_prefix("audrey").expect("failed to get xdg dirs") diff --git a/src/main.rs b/src/main.rs index 02c3baf..c839b84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,3 @@ -pub const VERSION: &str = "0.1.0"; // AUDREY_VERSION -pub const USER_AGENT: &str = "audrey/linux"; -pub const APP_ID: &str = "eu.callcc.audrey"; - mod application; pub use application::Application; diff --git a/src/mpris.rs b/src/mpris.rs index 619024f..94bd9cf 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -79,7 +79,7 @@ impl Mpris { #[zbus(property)] fn desktop_entry(&self) -> String { - crate::APP_ID.to_string() + audrey::APP_ID.to_string() } #[zbus(property)] diff --git a/src/subsonic.rs b/src/subsonic.rs index 437fdf5..5c2318e 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -107,7 +107,7 @@ impl Client { ("t", token), ("s", salt), ("v", "1.16.1"), - ("c", crate::APP_ID), + ("c", audrey::APP_ID), ("f", "json"), ], ) @@ -139,7 +139,7 @@ impl Client { }); let reqwest_client = reqwest::Client::builder() - .user_agent(crate::USER_AGENT) + .user_agent(audrey::USER_AGENT) .build()?; let client = reqwest_middleware::ClientBuilder::new(reqwest_client) diff --git a/src/ui/setup.rs b/src/ui/setup.rs index 1ca5da1..6acad85 100644 --- a/src/ui/setup.rs +++ b/src/ui/setup.rs @@ -98,7 +98,7 @@ mod imp { // FIXME: remove unwraps let keyring = oo7::Keyring::new().await.unwrap(); - let mut attributes = vec![("xdg:schema", crate::APP_ID.to_string())]; + let mut attributes = vec![("xdg:schema", audrey::APP_ID.to_string())]; // clear previous passwords keyring.delete(&attributes).await.unwrap(); @@ -149,7 +149,7 @@ impl Setup { let keyring = oo7::Keyring::new().await.unwrap(); // make sure the keyring is unlocked keyring.unlock().await.unwrap(); - let attributes = vec![("xdg:schema", crate::APP_ID)]; + let attributes = vec![("xdg:schema", audrey::APP_ID)]; let items = keyring.search_items(&attributes).await.unwrap(); if items.is_empty() { diff --git a/src/ui/window.rs b/src/ui/window.rs index 43db767..fa9b6f5 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -93,7 +93,7 @@ mod imp { fn default() -> Self { let mpv = mpv::Handle::new(); mpv.set_property("audio-client-name", "audrey").unwrap(); - mpv.set_property("user-agent", crate::USER_AGENT).unwrap(); + mpv.set_property("user-agent", audrey::USER_AGENT).unwrap(); mpv.set_property("vid", false).unwrap(); mpv.set_property("prefetch-playlist", true).unwrap(); @@ -226,17 +226,18 @@ mod imp { None::<&glib::Object>, ); - let settings = gio::Settings::new(crate::APP_ID); - settings.bind("mute", self.obj().as_ref(), "mute").build(); - settings - .bind("volume", self.obj().as_ref(), "volume") - .build(); + audrey::globals::SETTINGS.with(|settings| { + settings.bind("mute", self.obj().as_ref(), "mute").build(); + settings + .bind("volume", self.obj().as_ref(), "volume") + .build(); - self.obj() - .set_default_size(settings.int("window-width"), settings.int("window-height")); - if settings.boolean("is-maximized") { - self.obj().maximize(); - } + self.obj() + .set_default_size(settings.int("window-width"), settings.int("window-height")); + if settings.boolean("is-maximized") { + self.obj().maximize(); + } + }); // update time-pos every 100 ms let window = self.obj().downgrade(); @@ -362,13 +363,13 @@ mod imp { impl WindowImpl for Window { fn close_request(&self) -> glib::Propagation { let size = self.obj().default_size(); - // TODO: don't instantiate this so many times maybe? OnceCell in imp struct? - let settings = gio::Settings::new(crate::APP_ID); - settings.set_int("window-width", size.0).unwrap(); - settings.set_int("window-height", size.1).unwrap(); - settings - .set_boolean("is-maximized", self.obj().is_maximized()) - .unwrap(); + audrey::globals::SETTINGS.with(|settings| { + settings.set_int("window-width", size.0).unwrap(); + settings.set_int("window-height", size.1).unwrap(); + settings + .set_boolean("is-maximized", self.obj().is_maximized()) + .unwrap(); + }); glib::Propagation::Proceed }