fix: Use globals instead of thread-locals

Better support for multi-threaded applications,
especially cosmic-comp rendering in parallel on
multiple threads, each potentially accessing
global configurations such as the active theme,
icon_theme and more...
This commit is contained in:
Victoria Brekenfeld 2024-08-02 20:00:16 +02:00 committed by Michael Murphy
parent f655710d55
commit b40839638a
16 changed files with 183 additions and 216 deletions

View file

@ -218,15 +218,14 @@ where
self.app
.core()
.watch_config::<cosmic_theme::Theme>(
if THEME
.with(|t| {
if let ThemeType::System { prefer_dark, .. } = t.borrow().theme_type {
prefer_dark
} else {
None
}
})
.unwrap_or_else(|| self.app.core().system_theme_mode.is_dark)
if if let ThemeType::System { prefer_dark, .. } =
THEME.lock().unwrap().theme_type
{
prefer_dark
} else {
None
}
.unwrap_or_else(|| self.app.core().system_theme_mode.is_dark)
{
cosmic_theme::DARK_THEME_ID
} else {
@ -427,14 +426,11 @@ impl<T: Application> Cosmic<T> {
};
}
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
cosmic_theme.set_theme(theme.theme_type);
});
THEME.lock().unwrap().set_theme(theme.theme_type);
}
Message::SystemThemeChange(keys, theme) => {
let cur_is_dark = THEME.with(|t| t.borrow().theme_type.is_dark());
let cur_is_dark = THEME.lock().unwrap().theme_type.is_dark();
// Ignore updates if the current theme mode does not match.
if cur_is_dark != theme.cosmic().is_dark {
return iced::Command::none();
@ -443,8 +439,8 @@ impl<T: Application> Cosmic<T> {
// Record the last-known system theme in event that the current theme is custom.
self.app.core_mut().system_theme = theme.clone();
let portal_accent = self.app.core().portal_accent;
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
{
let mut cosmic_theme = THEME.lock().unwrap();
// Only apply update if the theme is set to load a system theme
if let ThemeType::System {
@ -466,7 +462,7 @@ impl<T: Application> Cosmic<T> {
cosmic_theme.set_theme(new_theme.theme_type);
}
});
}
return cmd;
}
@ -485,13 +481,13 @@ impl<T: Application> Cosmic<T> {
if !keys.contains(&"is_dark") {
return iced::Command::none();
}
if THEME.with(|t| match t.borrow().theme_type {
if match THEME.lock().unwrap().theme_type {
ThemeType::System {
theme: _,
prefer_dark,
} => prefer_dark.is_some(),
_ => false,
}) {
} {
return iced::Command::none();
}
let mut cmds = vec![self.app.system_theme_mode_update(&keys, &mode)];
@ -523,13 +519,13 @@ impl<T: Application> Cosmic<T> {
};
core.system_theme = new_theme.clone();
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
{
let mut cosmic_theme = THEME.lock().unwrap();
// Only apply update if the theme is set to load a system theme
if let ThemeType::System { theme: _, .. } = cosmic_theme.theme_type {
cosmic_theme.set_theme(new_theme.theme_type);
}
});
}
}
return Command::batch(cmds);
}
@ -552,13 +548,13 @@ impl<T: Application> Cosmic<T> {
#[cfg(feature = "xdg-portal")]
Message::DesktopSettings(crate::theme::portal::Desktop::ColorScheme(s)) => {
use ashpd::desktop::settings::ColorScheme;
if THEME.with(|t| match t.borrow().theme_type {
if match THEME.lock().unwrap().theme_type {
ThemeType::System {
theme: _,
prefer_dark,
} => prefer_dark.is_some(),
_ => false,
}) {
} {
return iced::Command::none();
}
let is_dark = match s {
@ -579,14 +575,14 @@ impl<T: Application> Cosmic<T> {
crate::theme::system_light()
};
core.system_theme = new_theme.clone();
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
{
let mut cosmic_theme = THEME.lock().unwrap();
// Only apply update if the theme is set to load a system theme
if let ThemeType::System { theme: _, .. } = cosmic_theme.theme_type {
cosmic_theme.set_theme(new_theme.theme_type);
}
});
}
}
}
#[cfg(feature = "xdg-portal")]
@ -602,8 +598,8 @@ impl<T: Application> Cosmic<T> {
return iced::Command::none();
}
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
{
let mut cosmic_theme = THEME.lock().unwrap();
// Only apply update if the theme is set to load a system theme
if let ThemeType::System {
@ -616,7 +612,7 @@ impl<T: Application> Cosmic<T> {
prefer_dark,
});
}
});
}
}
#[cfg(feature = "xdg-portal")]
Message::DesktopSettings(crate::theme::portal::Desktop::Contrast(_)) => {
@ -631,7 +627,7 @@ impl<T: Application> Cosmic<T> {
crate::icon_theme::set_default(config.icon_theme.clone());
}
crate::config::COSMIC_TK.with(|tk| *tk.borrow_mut() = config);
*crate::config::COSMIC_TK.lock().unwrap() = config;
}
Message::Focus(f) => {

View file

@ -48,7 +48,6 @@ pub mod message {
pub use self::command::Command;
pub use self::core::Core;
pub use self::settings::Settings;
use crate::config::CosmicTk;
use crate::prelude::*;
use crate::theme::THEME;
use crate::widget::{context_drawer, id_container, menu, nav_bar, popover};
@ -58,7 +57,6 @@ use iced::Subscription;
use iced::{multi_window::Application as IcedApplication, window};
#[cfg(any(not(feature = "winit"), not(feature = "multi-window")))]
use iced::{window, Application as IcedApplication};
use iced_core::mouse;
pub use message::Message;
use url::Url;
#[cfg(feature = "single-instance")]
@ -87,10 +85,7 @@ pub(crate) fn iced_settings<App: Application>(
crate::icon_theme::set_default(crate::config::icon_theme());
}
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
cosmic_theme.set_theme(settings.theme.theme_type);
});
THEME.lock().unwrap().set_theme(settings.theme.theme_type);
let mut iced = iced::Settings::with_flags((core, flags));