diff options
Diffstat (limited to '')
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/material_theme/src/lib.rs | 60 | ||||
| -rw-r--r-- | theme_test/src/main.rs | 6 |
3 files changed, 29 insertions, 38 deletions
@@ -96,6 +96,7 @@ from_over_into = "deny" needless_borrow = "deny" new_without_default = "deny" useless_conversion = "deny" +clone_on_copy = "deny" [patch.crates-io] iced_anim = { git = "https://github.com/pml68/iced_anim" } diff --git a/crates/material_theme/src/lib.rs b/crates/material_theme/src/lib.rs index 13f13b1..25b6a9c 100644 --- a/crates/material_theme/src/lib.rs +++ b/crates/material_theme/src/lib.rs @@ -1,7 +1,7 @@ use std::sync::LazyLock; +use iced_widget::core::Color; use iced_widget::core::theme::{Base, Style}; -use iced_widget::core::{Color, color}; pub mod button; pub mod checkbox; @@ -29,55 +29,48 @@ pub mod text_input; pub mod toggler; pub mod utils; -pub static DARK: LazyLock<Theme> = - LazyLock::new(|| Theme::new("Dark", ColorScheme::DARK)); -pub static LIGHT: LazyLock<Theme> = - LazyLock::new(|| Theme::new("Light", ColorScheme::LIGHT)); - -#[derive(Debug, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Theme { - pub name: String, + pub name: &'static str, #[cfg_attr(feature = "serde", serde(flatten))] pub colorscheme: ColorScheme, } impl Theme { + pub const ALL: &'static [Self] = &[Self::DARK, Self::LIGHT]; + + pub const DARK: Self = Self { + name: "Dark", + colorscheme: ColorScheme::DARK, + }; + + pub const LIGHT: Self = Self { + name: "Light", + colorscheme: ColorScheme::LIGHT, + }; + pub fn new(name: impl Into<String>, colorscheme: ColorScheme) -> Self { Self { - name: name.into(), + name: Box::leak(name.into().into_boxed_str()), colorscheme, } } } -impl Clone for Theme { - fn clone(&self) -> Self { - Self { - name: self.name.clone(), - colorscheme: self.colorscheme, - } - } - - fn clone_from(&mut self, source: &Self) { - self.name.clone_from(&source.name); - self.colorscheme = source.colorscheme; - } -} - impl Default for Theme { fn default() -> Self { static DEFAULT: LazyLock<Theme> = LazyLock::new(|| { match dark_light::detect().unwrap_or(dark_light::Mode::Unspecified) { dark_light::Mode::Dark | dark_light::Mode::Unspecified => { - DARK.clone() + Theme::DARK } - dark_light::Mode::Light => LIGHT.clone(), + dark_light::Mode::Light => Theme::LIGHT, } }); - DEFAULT.clone() + *DEFAULT } } @@ -108,10 +101,8 @@ impl iced_anim::Animate for Theme { } fn update(&mut self, components: &mut impl Iterator<Item = f32>) { - let mut colors = self.colorscheme; - colors.update(components); - - *self = Theme::new("Animating Theme", colors); + self.colorscheme.update(components); + self.name = "Animating Theme"; } fn distance_to(&self, end: &Self) -> Vec<f32> { @@ -119,10 +110,9 @@ impl iced_anim::Animate for Theme { } fn lerp(&mut self, start: &Self, end: &Self, progress: f32) { - let mut colors = self.colorscheme; - colors.lerp(&start.colorscheme, &end.colorscheme, progress); - - *self = Theme::new("Animating Theme", colors); + self.colorscheme + .lerp(&start.colorscheme, &end.colorscheme, progress); + self.name = "Animating Theme"; } } @@ -153,7 +143,7 @@ macro_rules! from_argb { let g = (hex & 0x0000ff00) >> 8; let b = (hex & 0x000000ff); - color!(r as u8, g as u8, b as u8, a) + ::iced_widget::core::color!(r as u8, g as u8, b as u8, a) }}; } diff --git a/theme_test/src/main.rs b/theme_test/src/main.rs index 3331196..7ec7f15 100644 --- a/theme_test/src/main.rs +++ b/theme_test/src/main.rs @@ -5,6 +5,7 @@ use iced::widget::{ use iced::{Element, Length}; use iced_anim::{Animated, Animation, Event}; use iced_dialog::dialog; +use material_theme::Theme; use material_theme::button::{elevated, filled_tonal, outlined, text}; use material_theme::container::{ error, error_container, inverse_surface, primary, primary_container, @@ -13,11 +14,10 @@ use material_theme::container::{ surface_container_lowest, tertiary, tertiary_container, }; use material_theme::text::surface_variant; -use material_theme::{DARK, LIGHT, Theme}; fn main() -> iced::Result { iced::application(State::default, State::update, State::view) - .theme(|state| state.theme.value().clone()) + .theme(|state| *state.theme.value()) .run() } @@ -190,7 +190,7 @@ impl State { column![ // Pick List pick_list( - [LIGHT.clone(), DARK.clone()], + Theme::ALL, Some(self.theme.target()), |theme| Message::SwitchTheme(theme.into()) ) |
