make gsettings a thread local

This commit is contained in:
Erica Z 2024-11-24 18:19:07 +01:00
parent d2d1cee3a6
commit f8a6e09866
7 changed files with 35 additions and 28 deletions

View file

@ -57,7 +57,7 @@ glib::wrapper! {
impl Default for Application { impl Default for Application {
fn default() -> Self { fn default() -> Self {
glib::Object::builder::<Self>() glib::Object::builder::<Self>()
.property("application-id", crate::APP_ID) .property("application-id", audrey::APP_ID)
.property("flags", gio::ApplicationFlags::default()) .property("flags", gio::ApplicationFlags::default())
.build() .build()
} }

View file

@ -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 { pub mod globals {
use adw::gio;
use std::path::PathBuf; use std::path::PathBuf;
use tokio::sync::Mutex; 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 { pub fn xdg_dirs() -> &'static xdg::BaseDirectories {
static DIRS: std::sync::LazyLock<xdg::BaseDirectories> = std::sync::LazyLock::new(|| { static DIRS: std::sync::LazyLock<xdg::BaseDirectories> = std::sync::LazyLock::new(|| {
xdg::BaseDirectories::with_prefix("audrey").expect("failed to get xdg dirs") xdg::BaseDirectories::with_prefix("audrey").expect("failed to get xdg dirs")

View file

@ -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; mod application;
pub use application::Application; pub use application::Application;

View file

@ -79,7 +79,7 @@ impl Mpris {
#[zbus(property)] #[zbus(property)]
fn desktop_entry(&self) -> String { fn desktop_entry(&self) -> String {
crate::APP_ID.to_string() audrey::APP_ID.to_string()
} }
#[zbus(property)] #[zbus(property)]

View file

@ -107,7 +107,7 @@ impl Client {
("t", token), ("t", token),
("s", salt), ("s", salt),
("v", "1.16.1"), ("v", "1.16.1"),
("c", crate::APP_ID), ("c", audrey::APP_ID),
("f", "json"), ("f", "json"),
], ],
) )
@ -139,7 +139,7 @@ impl Client {
}); });
let reqwest_client = reqwest::Client::builder() let reqwest_client = reqwest::Client::builder()
.user_agent(crate::USER_AGENT) .user_agent(audrey::USER_AGENT)
.build()?; .build()?;
let client = reqwest_middleware::ClientBuilder::new(reqwest_client) let client = reqwest_middleware::ClientBuilder::new(reqwest_client)

View file

@ -98,7 +98,7 @@ mod imp {
// FIXME: remove unwraps // FIXME: remove unwraps
let keyring = oo7::Keyring::new().await.unwrap(); 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 // clear previous passwords
keyring.delete(&attributes).await.unwrap(); keyring.delete(&attributes).await.unwrap();
@ -149,7 +149,7 @@ impl Setup {
let keyring = oo7::Keyring::new().await.unwrap(); let keyring = oo7::Keyring::new().await.unwrap();
// make sure the keyring is unlocked // make sure the keyring is unlocked
keyring.unlock().await.unwrap(); 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(); let items = keyring.search_items(&attributes).await.unwrap();
if items.is_empty() { if items.is_empty() {

View file

@ -93,7 +93,7 @@ mod imp {
fn default() -> Self { fn default() -> Self {
let mpv = mpv::Handle::new(); let mpv = mpv::Handle::new();
mpv.set_property("audio-client-name", "audrey").unwrap(); 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("vid", false).unwrap();
mpv.set_property("prefetch-playlist", true).unwrap(); mpv.set_property("prefetch-playlist", true).unwrap();
@ -226,17 +226,18 @@ mod imp {
None::<&glib::Object>, None::<&glib::Object>,
); );
let settings = gio::Settings::new(crate::APP_ID); audrey::globals::SETTINGS.with(|settings| {
settings.bind("mute", self.obj().as_ref(), "mute").build(); settings.bind("mute", self.obj().as_ref(), "mute").build();
settings settings
.bind("volume", self.obj().as_ref(), "volume") .bind("volume", self.obj().as_ref(), "volume")
.build(); .build();
self.obj() self.obj()
.set_default_size(settings.int("window-width"), settings.int("window-height")); .set_default_size(settings.int("window-width"), settings.int("window-height"));
if settings.boolean("is-maximized") { if settings.boolean("is-maximized") {
self.obj().maximize(); self.obj().maximize();
} }
});
// update time-pos every 100 ms // update time-pos every 100 ms
let window = self.obj().downgrade(); let window = self.obj().downgrade();
@ -362,13 +363,13 @@ mod imp {
impl WindowImpl for Window { impl WindowImpl for Window {
fn close_request(&self) -> glib::Propagation { fn close_request(&self) -> glib::Propagation {
let size = self.obj().default_size(); let size = self.obj().default_size();
// TODO: don't instantiate this so many times maybe? OnceCell in imp struct? audrey::globals::SETTINGS.with(|settings| {
let settings = gio::Settings::new(crate::APP_ID); settings.set_int("window-width", size.0).unwrap();
settings.set_int("window-width", size.0).unwrap(); settings.set_int("window-height", size.1).unwrap();
settings.set_int("window-height", size.1).unwrap(); settings
settings .set_boolean("is-maximized", self.obj().is_maximized())
.set_boolean("is-maximized", self.obj().is_maximized()) .unwrap();
.unwrap(); });
glib::Propagation::Proceed glib::Propagation::Proceed
} }