refactor: introduce thread local THEME variable and distinguish between custom and system theme settings
This commit is contained in:
parent
40efcbbe31
commit
6c57e04e36
8 changed files with 80 additions and 36 deletions
|
|
@ -40,7 +40,7 @@ pub struct Core {
|
|||
/// Scaling factor used by the application
|
||||
scale_factor: f32,
|
||||
|
||||
pub theme: Theme,
|
||||
pub system_theme: Theme,
|
||||
pub(crate) title: String,
|
||||
pub window: Window,
|
||||
}
|
||||
|
|
@ -56,7 +56,7 @@ impl Default for Core {
|
|||
toggled_condensed: true,
|
||||
},
|
||||
scale_factor: 1.0,
|
||||
theme: theme::theme(),
|
||||
system_theme: theme::theme(),
|
||||
title: String::new(),
|
||||
window: Window {
|
||||
can_fullscreen: false,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use super::{command, Application, ApplicationExt, Core, Subscription};
|
||||
use crate::theme::{self, Theme};
|
||||
use crate::theme::{self, Theme, ThemeType, THEME};
|
||||
use crate::widget::nav_bar;
|
||||
use crate::{keyboard_nav, Element};
|
||||
#[cfg(feature = "wayland")]
|
||||
|
|
@ -36,6 +36,8 @@ pub enum Message {
|
|||
ScaleFactor(f32),
|
||||
/// Requests theme changes.
|
||||
ThemeChange(Theme),
|
||||
/// Notification of system theme changes.
|
||||
SystemThemeChange(Theme),
|
||||
/// Toggles visibility of the nav bar.
|
||||
ToggleNavBar,
|
||||
/// Toggles the condensed status of the nav bar.
|
||||
|
|
@ -146,14 +148,14 @@ where
|
|||
.map(Message::KeyboardNav)
|
||||
.map(super::Message::Cosmic),
|
||||
theme::subscription(0)
|
||||
.map(Message::ThemeChange)
|
||||
.map(Message::SystemThemeChange)
|
||||
.map(super::Message::Cosmic),
|
||||
window_events.map(super::Message::Cosmic),
|
||||
])
|
||||
}
|
||||
|
||||
fn theme(&self) -> Self::Theme {
|
||||
self.app.core().theme.clone()
|
||||
THEME.with(|t| t.borrow().clone())
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
|
|
@ -267,12 +269,32 @@ impl<T: Application> Cosmic<T> {
|
|||
}
|
||||
|
||||
Message::ThemeChange(theme) => {
|
||||
self.app.core_mut().theme = theme;
|
||||
// our system theme is always receiving updates so we should use it instead
|
||||
let theme = if matches!(theme.theme_type, ThemeType::System(_)) {
|
||||
self.app.core().system_theme.clone()
|
||||
} else {
|
||||
theme
|
||||
};
|
||||
|
||||
THEME.with(move |t| {
|
||||
let mut cosmic_theme = t.borrow_mut();
|
||||
cosmic_theme.set_theme(theme.theme_type);
|
||||
});
|
||||
}
|
||||
|
||||
Message::ScaleFactor(factor) => {
|
||||
self.app.core_mut().set_scale_factor(factor);
|
||||
}
|
||||
Message::SystemThemeChange(theme) => {
|
||||
self.app.core_mut().system_theme = theme.clone();
|
||||
THEME.with(move |t| {
|
||||
let mut cosmic_theme = t.borrow_mut();
|
||||
// only apply update if the theme is set to load a system theme
|
||||
if matches!(cosmic_theme.theme_type, ThemeType::System(_)) {
|
||||
cosmic_theme.set_theme(theme.theme_type);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
iced::Command::none()
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ pub mod message {
|
|||
|
||||
pub use self::core::Core;
|
||||
pub use self::settings::Settings;
|
||||
use crate::theme::THEME;
|
||||
use crate::widget::nav_bar;
|
||||
use crate::{Element, ElementExt};
|
||||
use apply::Apply;
|
||||
|
|
@ -58,7 +59,10 @@ pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Res
|
|||
core.set_scale_factor(settings.scale_factor);
|
||||
core.set_window_width(settings.size.0);
|
||||
core.set_window_height(settings.size.1);
|
||||
core.theme = settings.theme;
|
||||
THEME.with(move |t| {
|
||||
let mut cosmic_theme = t.borrow_mut();
|
||||
cosmic_theme.set_theme(settings.theme.theme_type);
|
||||
});
|
||||
|
||||
let mut iced = iced::Settings::with_flags((core, flags));
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
pub mod expander;
|
||||
mod segmented_button;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::f32::consts::PI;
|
||||
use std::hash::Hash;
|
||||
use std::hash::Hasher;
|
||||
|
|
@ -64,6 +65,10 @@ lazy_static::lazy_static! {
|
|||
};
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
pub(crate) static THEME: RefCell<Theme> = RefCell::new(Theme { theme_type: ThemeType::Dark, layer: cosmic_theme::Layer::Background });
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub enum ThemeType {
|
||||
#[default]
|
||||
|
|
@ -72,6 +77,7 @@ pub enum ThemeType {
|
|||
HighContrastDark,
|
||||
HighContrastLight,
|
||||
Custom(Arc<CosmicTheme>),
|
||||
System(Arc<CosmicTheme>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
|
|
@ -88,7 +94,7 @@ impl Theme {
|
|||
ThemeType::Light => &COSMIC_LIGHT,
|
||||
ThemeType::HighContrastDark => &COSMIC_HC_DARK,
|
||||
ThemeType::HighContrastLight => &COSMIC_HC_LIGHT,
|
||||
ThemeType::Custom(ref t) => t.as_ref(),
|
||||
ThemeType::Custom(ref t) | ThemeType::System(ref t) => t.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -132,6 +138,14 @@ impl Theme {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn system(theme: Arc<CosmicTheme>) -> Self {
|
||||
Self {
|
||||
theme_type: ThemeType::System(theme),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// get current container
|
||||
/// can be used in a component that is intended to be a child of a `CosmicContainer`
|
||||
#[must_use]
|
||||
|
|
@ -142,6 +156,11 @@ impl Theme {
|
|||
cosmic_theme::Layer::Secondary => &self.cosmic().secondary,
|
||||
}
|
||||
}
|
||||
|
||||
/// set the theme
|
||||
pub fn set_theme(&mut self, theme: ThemeType) {
|
||||
self.theme_type = theme;
|
||||
}
|
||||
}
|
||||
|
||||
impl LayeredTheme for Theme {
|
||||
|
|
@ -1215,7 +1234,7 @@ pub fn theme() -> Theme {
|
|||
}
|
||||
theme
|
||||
});
|
||||
crate::theme::Theme::custom(Arc::new(t))
|
||||
crate::theme::Theme::system(Arc::new(t))
|
||||
}
|
||||
|
||||
pub fn subscription(id: u64) -> Subscription<crate::theme::Theme> {
|
||||
|
|
@ -1231,7 +1250,8 @@ pub fn subscription(id: u64) -> Subscription<crate::theme::Theme> {
|
|||
}
|
||||
theme
|
||||
});
|
||||
crate::theme::Theme::custom(Arc::new(theme))
|
||||
|
||||
crate::theme::Theme::system(Arc::new(theme))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue