From 9966c6f8834220e62fd66766d2e11d8a36e334e2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 03:21:26 +0100 Subject: Make `Theme::Custom` fields opaque --- examples/styling/src/main.rs | 89 ++++++++++++++++++++------------------------ style/src/theme.rs | 28 +++++++++++--- 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 784ea581..e16860ad 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -1,28 +1,17 @@ -use iced::theme::Palette; -use iced::theme::palette::Extended; +use iced::theme::{self, Theme}; use iced::widget::{ button, checkbox, column, container, horizontal_rule, progress_bar, radio, row, scrollable, slider, text, text_input, toggler, vertical_rule, vertical_space, }; -use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color}; +use iced::{Alignment, Color, Element, Length, Sandbox, Settings}; pub fn main() -> iced::Result { - Styling::run(Settings::default()) } - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -enum ThemeType { - Light, - Dark, - Custom, -} - #[derive(Default)] struct Styling { - custom_theme: Theme, theme: Theme, input_value: String, slider_value: f32, @@ -30,6 +19,13 @@ struct Styling { toggler_value: bool, } +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +enum ThemeType { + Light, + Dark, + Custom, +} + #[derive(Debug, Clone)] enum Message { ThemeChanged(ThemeType), @@ -44,21 +40,7 @@ impl Sandbox for Styling { type Message = Message; fn new() -> Self { - let palette = Palette { - background: Color::from_rgb(1.0, 0.9, 1.0), - text: Color::BLACK, - primary: Color::from_rgb(0.5, 0.5, 0.0), - success: Color::from_rgb(0.0, 1.0, 0.0), - danger: Color::from_rgb(1.0, 0.0, 0.0), - }; - let extended = Extended::generate(palette); - Styling { - custom_theme: Theme::Custom { - palette: Box::new(palette), - extended: Box::new(extended) - }, - ..Default::default() - } + Styling::default() } fn title(&self) -> String { @@ -67,11 +49,19 @@ impl Sandbox for Styling { fn update(&mut self, message: Message) { match message { - Message::ThemeChanged(theme) => self.theme = match theme { - ThemeType::Light => Theme::Light, - ThemeType::Dark => Theme::Dark, - ThemeType::Custom => self.custom_theme.clone(), - }, + Message::ThemeChanged(theme) => { + self.theme = match theme { + ThemeType::Light => Theme::Light, + ThemeType::Dark => Theme::Dark, + ThemeType::Custom => Theme::custom(theme::Palette { + background: Color::from_rgb(1.0, 0.9, 1.0), + text: Color::BLACK, + primary: Color::from_rgb(0.5, 0.5, 0.0), + success: Color::from_rgb(0.0, 1.0, 0.0), + danger: Color::from_rgb(1.0, 0.0, 0.0), + }), + } + } Message::InputChanged(value) => self.input_value = value, Message::ButtonPressed => {} Message::SliderChanged(value) => self.slider_value = value, @@ -81,21 +71,24 @@ impl Sandbox for Styling { } fn view(&self) -> Element { - let choose_theme = [ThemeType::Light, ThemeType::Dark, ThemeType::Custom].iter().fold( - column![text("Choose a theme:")].spacing(10), - |column, theme| { - column.push(radio( - format!("{:?}", theme), - *theme, - Some(match self.theme { - Theme::Light => ThemeType::Light, - Theme::Dark => ThemeType::Dark, - Theme::Custom { .. } => ThemeType::Custom, - }), - Message::ThemeChanged, - )) - }, - ); + let choose_theme = + [ThemeType::Light, ThemeType::Dark, ThemeType::Custom] + .iter() + .fold( + column![text("Choose a theme:")].spacing(10), + |column, theme| { + column.push(radio( + format!("{:?}", theme), + *theme, + Some(match self.theme { + Theme::Light => ThemeType::Light, + Theme::Dark => ThemeType::Dark, + Theme::Custom { .. } => ThemeType::Custom, + }), + Message::ThemeChanged, + )) + }, + ); let text_input = text_input( "Type something...", diff --git a/style/src/theme.rs b/style/src/theme.rs index 4e83758b..35945aca 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -25,18 +25,19 @@ use iced_core::{Background, Color}; pub enum Theme { Light, Dark, - Custom { - palette: Box, - extended: Box, - } + Custom(Custom), } impl Theme { + pub fn custom(palette: Palette) -> Self { + Self::Custom(Custom::new(palette)) + } + pub fn palette(self) -> Palette { match self { Self::Light => Palette::LIGHT, Self::Dark => Palette::DARK, - Self::Custom { palette, .. } => *palette + Self::Custom(custom) => custom.palette, } } @@ -44,7 +45,7 @@ impl Theme { match self { Self::Light => &palette::EXTENDED_LIGHT, Self::Dark => &palette::EXTENDED_DARK, - Self::Custom { extended, .. } => extended, + Self::Custom(custom) => &custom.extended, } } } @@ -55,6 +56,21 @@ impl Default for Theme { } } +#[derive(Debug, Clone, PartialEq)] +pub struct Custom { + palette: Palette, + extended: Extended, +} + +impl Custom { + pub fn new(palette: Palette) -> Self { + Self { + palette, + extended: Extended::generate(palette), + } + } +} + #[derive(Debug, Clone, Copy)] pub enum Application { Default, -- cgit