aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPolesznyák Márk <contact@pml68.dev>2025-09-29 12:56:17 +0200
committerPolesznyák Márk <contact@pml68.dev>2025-09-29 12:56:17 +0200
commite703a81b042fd79dcc262472eb075cc5ded0aebb (patch)
tree6021f84dad08b5a6137a49c56a98613b5faccd21 /src
parentdocs(README): add builds.sr.ht status badge (diff)
downloadiced_material-e703a81b042fd79dcc262472eb075cc5ded0aebb.tar.gz
feat!: use iced's built-in theme preference detection
Diffstat (limited to '')
-rw-r--r--src/lib.rs144
1 files changed, 14 insertions, 130 deletions
diff --git a/src/lib.rs b/src/lib.rs
index d70501b..0b14649 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,21 +1,9 @@
use std::borrow::Cow;
-#[cfg(feature = "system-theme")]
-use std::sync::{Arc, LazyLock};
-#[cfg(feature = "system-theme")]
-use arc_swap::ArcSwap;
use iced_widget::core::{
Color, color,
- theme::{Base, Style},
+ theme::{Base, Mode, Style},
};
-#[cfg(feature = "system-theme")]
-use iced_widget::runtime::futures::{
- BoxStream,
- futures::StreamExt,
- subscription::{EventStream, Hasher, Recipe, Subscription, from_recipe},
-};
-#[cfg(feature = "system-theme")]
-use mundy::{Interest, Preferences};
use utils::{lightness, mix};
pub mod button;
@@ -59,19 +47,6 @@ macro_rules! from_argb {
}};
}
-#[cfg(feature = "system-theme")]
-static SYSTEM_THEME: LazyLock<ArcSwap<Theme>> = LazyLock::new(|| {
- use std::time::Duration;
-
- ArcSwap::new(Arc::new(
- Preferences::once_blocking(
- Interest::ColorScheme,
- Duration::from_millis(1200),
- )
- .map_or(Theme::Dark, Theme::from),
- ))
-});
-
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -79,18 +54,11 @@ static SYSTEM_THEME: LazyLock<ArcSwap<Theme>> = LazyLock::new(|| {
pub enum Theme {
Dark,
Light,
- #[cfg(feature = "system-theme")]
- System,
Custom(Custom),
}
impl Theme {
- pub const ALL: &'static [Self] = &[
- #[cfg(feature = "system-theme")]
- Self::System,
- Self::Dark,
- Self::Light,
- ];
+ pub const ALL: &'static [Self] = &[Self::Dark, Self::Light];
pub fn new(
name: impl Into<Cow<'static, str>>,
@@ -118,8 +86,6 @@ impl Theme {
match self {
Self::Dark => "Dark".into(),
Self::Light => "Light".into(),
- #[cfg(feature = "system-theme")]
- Self::System => "System".into(),
Self::Custom(custom) => custom.name.clone(),
}
}
@@ -128,8 +94,6 @@ impl Theme {
match self {
Self::Dark => true,
Self::Light => false,
- #[cfg(feature = "system-theme")]
- Self::System => SYSTEM_THEME.load().is_dark(),
Self::Custom(custom) => custom.is_dark,
}
}
@@ -138,98 +102,14 @@ impl Theme {
match self {
Self::Dark => ColorScheme::DARK,
Self::Light => ColorScheme::LIGHT,
- #[cfg(feature = "system-theme")]
- Self::System => SYSTEM_THEME.load().colors(),
Self::Custom(custom) => custom.colorscheme,
}
}
-
- /// A subscription that responds to the user's theme preference changing and returns the
- /// corresponding [`Theme`] variant.
- #[cfg(feature = "system-theme")]
- pub fn subscription() -> Subscription<Self> {
- struct ThemeSubscription;
-
- impl Recipe for ThemeSubscription {
- type Output = Theme;
-
- fn hash(&self, state: &mut Hasher) {
- use std::hash::Hash;
- std::any::TypeId::of::<Self::Output>().hash(state);
- }
-
- fn stream(
- self: Box<Self>,
- _input: EventStream,
- ) -> BoxStream<Self::Output> {
- Preferences::stream(Interest::ColorScheme)
- .map(Theme::from)
- .boxed()
- }
- }
-
- from_recipe(ThemeSubscription)
- }
-
- /// Updates the [`System`] variant with the given theme.
- ///
- /// Meant to be used in conjunction with [`Theme::subscription`].
- ///
- /// # Example
- ///
- /// ```no_run
- /// use iced_material::Theme;
- ///
- /// fn main() -> iced::Result {
- /// iced::application(State::default, State::update, State::view)
- /// .theme(State::theme)
- /// .subscription(State::subscription)
- /// .run()
- /// }
- ///
- /// #[derive(Debug, Default)]
- /// struct State;
- ///
- /// #[derive(Debug, Clone)]
- /// enum Message {
- /// SystemThemeChanged(Theme)
- /// }
- ///
- /// impl State {
- /// fn view(&self) -> iced::Element<Message, Theme> {
- /// iced::widget::text("Hello!").into()
- /// }
- ///
- /// fn update(&mut self, message: Message) {
- /// match message {
- /// Message::SystemThemeChanged(theme) => Theme::update_system_theme(theme)
- /// }
- /// }
- ///
- /// fn theme(&self) -> Theme {
- /// Theme::System
- /// }
- ///
- /// fn subscription(&self) -> iced::Subscription<Message> {
- /// Theme::subscription().map(Message::SystemThemeChanged)
- /// }
- /// }
- /// ```
- ///
- /// [`System`]: Theme::System
- #[cfg(feature = "system-theme")]
- pub fn update_system_theme(this: Self) {
- SYSTEM_THEME.store(Arc::new(this));
- }
}
impl Default for Theme {
fn default() -> Self {
- if cfg!(feature = "system-theme") {
- Theme::System
- } else {
- Theme::Dark
- }
+ Self::Dark
}
}
@@ -259,15 +139,19 @@ impl Base for Theme {
danger: colors.error.color,
})
}
-}
-#[cfg(feature = "system-theme")]
-impl From<Preferences> for Theme {
- fn from(preference: Preferences) -> Self {
- if preference.color_scheme.is_light() {
- Theme::Light
+ fn default(preference: Mode) -> Self {
+ match preference {
+ Mode::None | Mode::Dark => Self::Dark,
+ Mode::Light => Self::Light,
+ }
+ }
+
+ fn mode(&self) -> Mode {
+ if self.is_dark() {
+ Mode::Dark
} else {
- Theme::Dark
+ Mode::Light
}
}
}