diff options
| -rw-r--r-- | Cargo.toml | 15 | ||||
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | examples/styling.rs | 46 | ||||
| -rw-r--r-- | src/lib.rs | 144 |
4 files changed, 38 insertions, 171 deletions
@@ -11,9 +11,9 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"] rust-version = "1.88" [features] -default = ["system-theme"] -# Adds a `System` theme variant that follows the system theme mode. -system-theme = ["dep:mundy", "dep:arc-swap"] +default = ["linux-theme-detection"] +# Enables Linux system theme detection +linux-theme-detection = ["iced_winit/linux-theme-detection"] # Provides `serde` support. serde = ["dep:serde"] # Provides support for animating with `iced_anim`. @@ -31,7 +31,7 @@ qr_code = ["iced_widget/qr_code"] [dependencies] iced_widget = "0.14.0-dev" -arc-swap = { version = "1.7.1", optional = true } +iced_winit = "0.14.0-dev" serde = { version = "1.0", optional = true } [dependencies.iced_dialog] @@ -45,12 +45,6 @@ branch = "iced/master" features = ["derive"] optional = true -[dependencies.mundy] -version = "0.2.0" -default-features = false -features = ["async-io", "color-scheme"] -optional = true - [dev-dependencies] iced = "0.14.0-dev" @@ -79,5 +73,6 @@ broken_intra_doc_links = "forbid" [patch.crates-io] iced_widget = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_winit = { git = "https://github.com/iced-rs/iced", branch = "master" } iced_core = { git = "https://github.com/iced-rs/iced", branch = "master" } iced = { git = "https://github.com/iced-rs/iced", branch = "master" } @@ -14,8 +14,8 @@ ## Features -- `default`: `system-theme` -- `system-theme`: Adds a `System` theme variant that follows the system theme mode. +- `default`: `linux-theme-detection` +- `linux-theme-detection`: Enables Linux system theme detection. - `serde`: Provides [`serde`](https://docs.rs/serde) support. - `animate`: Provides support for animating with [`iced_anim`](https://github.com/bradysimon/iced_anim/tree/iced/master). - `crisp`: Enables pixel snapping for crisp edges by default (can cause jitter!). diff --git a/examples/styling.rs b/examples/styling.rs index 127f615..6b8a381 100644 --- a/examples/styling.rs +++ b/examples/styling.rs @@ -1,8 +1,7 @@ use iced::keyboard; use iced::widget::{ - Button, center, checkbox, column, container, horizontal_rule, pick_list, - progress_bar, row, scrollable, slider, text, text_input, toggler, - vertical_rule, vertical_space, + Button, center, checkbox, column, container, pick_list, progress_bar, row, + rule, scrollable, slider, space, text, text_input, toggler, }; use iced::{Center, Fill, Subscription}; use iced_material::{Theme, button}; @@ -35,7 +34,6 @@ enum Message { TogglerToggled(bool), PreviousTheme, NextTheme, - SystemThemeChanged(Theme), } impl Styling { @@ -66,9 +64,6 @@ impl Styling { }; } } - Message::SystemThemeChanged(theme) => { - Theme::update_system_theme(theme) - } } } @@ -104,7 +99,7 @@ impl Styling { let progress_bar = || progress_bar(0.0..=100.0, self.slider_value); let scrollable = scrollable( - column!["Scroll me!", vertical_space().height(800), "You did it!"] + column!["Scroll me!", space::vertical().height(800), "You did it!"] .padding(10), ) .width(Fill) @@ -144,7 +139,7 @@ impl Styling { let content = column![ choose_theme, - horizontal_rule(1), + rule::horizontal(1), text_input, row![filled, elevated, filled_tonal, outlined, text_button] .spacing(10) @@ -153,7 +148,7 @@ impl Styling { progress_bar(), row![ scrollable, - row![vertical_rule(1), column![checkbox, toggler].spacing(20)] + row![rule::vertical(1), column![checkbox, toggler].spacing(20)] .spacing(20) ] .spacing(10) @@ -169,26 +164,19 @@ impl Styling { } fn subscription(&self) -> Subscription<Message> { - let theme_toggle = - keyboard::on_key_press(|key, _modifiers| match key { - keyboard::Key::Named( - keyboard::key::Named::ArrowUp - | keyboard::key::Named::ArrowLeft, - ) => Some(Message::PreviousTheme), - keyboard::Key::Named( - keyboard::key::Named::ArrowDown - | keyboard::key::Named::ArrowRight, - ) => Some(Message::NextTheme), - _ => None, - }); - - let system_theme = - Theme::subscription().map(Message::SystemThemeChanged); - - Subscription::batch([theme_toggle, system_theme]) + keyboard::on_key_press(|key, _modifiers| match key { + keyboard::Key::Named( + keyboard::key::Named::ArrowUp | keyboard::key::Named::ArrowLeft, + ) => Some(Message::PreviousTheme), + keyboard::Key::Named( + keyboard::key::Named::ArrowDown + | keyboard::key::Named::ArrowRight, + ) => Some(Message::NextTheme), + _ => None, + }) } - fn theme(&self) -> Theme { - self.theme.clone() + fn theme(&self) -> Option<Theme> { + None } } @@ -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 } } } |
