diff options
| author | pml68 <contact@pml68.dev> | 2025-06-26 12:13:02 +0200 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-06-26 12:13:07 +0200 |
| commit | 21ea78574ca6e7a4cf890ba661a42e0ed1f88578 (patch) | |
| tree | e867e68d8bc85a234118e1b8f322f046e097a965 | |
| parent | feat: initial commit (diff) | |
| download | iced_material-21ea78574ca6e7a4cf890ba661a42e0ed1f88578.tar.gz | |
feat: make `System` variant non-reactive
| -rw-r--r-- | src/lib.rs | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::sync::LazyLock; use iced_widget::core::theme::{Base, Style}; use iced_widget::core::{color, Color}; @@ -44,6 +45,10 @@ macro_rules! from_argb { }}; } +#[cfg(feature = "system-theme")] +pub static SYSTEM_IS_DARK: LazyLock<bool> = + LazyLock::new(|| dark_light::detect().is_ok_and(|mode| mode == dark_light::Mode::Dark)); + #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -57,7 +62,12 @@ pub enum Theme { } impl Theme { - pub const ALL: &'static [Self] = &[Self::Dark, Self::Light]; + pub const ALL: &'static [Self] = &[ + Self::Dark, + Self::Light, + #[cfg(feature = "system-theme")] + Self::System, + ]; pub fn new(name: impl Into<Cow<'static, str>>, colorscheme: ColorScheme) -> Self { Self::Custom(Custom { @@ -90,9 +100,7 @@ impl Theme { Self::Dark => true, Self::Light => false, #[cfg(feature = "system-theme")] - Self::System => !dark_light::detect() - .ok() - .is_some_and(|mode| mode == dark_light::Mode::Light), + Self::System => *SYSTEM_IS_DARK, Self::Custom(custom) => custom.is_dark, } } @@ -103,13 +111,10 @@ impl Theme { Self::Light => ColorScheme::LIGHT, #[cfg(feature = "system-theme")] Self::System => { - if dark_light::detect() - .ok() - .is_some_and(|mode| mode == dark_light::Mode::Light) - { - ColorScheme::LIGHT - } else { + if *SYSTEM_IS_DARK { ColorScheme::DARK + } else { + ColorScheme::LIGHT } } Self::Custom(custom) => custom.colorscheme, |
