diff options
| author | 2024-01-10 08:15:05 +0100 | |
|---|---|---|
| committer | 2024-01-10 10:01:51 +0100 | |
| commit | 3850a46db6e13f2948f5731f4ceec42764391f5d (patch) | |
| tree | 032c2cf31412cbc87beef56b7654bbc1a1909c81 | |
| parent | d76705df29f1960124bd06277683448e18f788b0 (diff) | |
| download | iced-3850a46db6e13f2948f5731f4ceec42764391f5d.tar.gz iced-3850a46db6e13f2948f5731f4ceec42764391f5d.tar.bz2 iced-3850a46db6e13f2948f5731f4ceec42764391f5d.zip | |
Add `Theme` selector to `layout` example
| -rw-r--r-- | examples/layout/src/main.rs | 20 | ||||
| -rw-r--r-- | examples/styling/src/main.rs | 17 | ||||
| -rw-r--r-- | style/src/theme.rs | 36 | ||||
| -rw-r--r-- | widget/src/helpers.rs | 2 | ||||
| -rw-r--r-- | widget/src/pick_list.rs | 6 | 
5 files changed, 60 insertions, 21 deletions
| diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index e23b2218..c1ff3951 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -1,8 +1,8 @@  use iced::executor;  use iced::keyboard;  use iced::widget::{ -    button, checkbox, column, container, horizontal_space, row, text, -    vertical_rule, +    button, checkbox, column, container, horizontal_space, pick_list, row, +    text, vertical_rule,  };  use iced::{      color, Alignment, Application, Color, Command, Element, Font, Length, @@ -17,13 +17,15 @@ pub fn main() -> iced::Result {  struct Layout {      example: Example,      explain: bool, +    theme: Theme,  } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)]  enum Message {      Next,      Previous,      ExplainToggled(bool), +    ThemeSelected(Theme),  }  impl Application for Layout { @@ -37,6 +39,7 @@ impl Application for Layout {              Self {                  example: Example::default(),                  explain: false, +                theme: Theme::Light,              },              Command::none(),          ) @@ -57,6 +60,9 @@ impl Application for Layout {              Message::ExplainToggled(explain) => {                  self.explain = explain;              } +            Message::ThemeSelected(theme) => { +                self.theme = theme; +            }          }          Command::none() @@ -75,7 +81,13 @@ impl Application for Layout {              text(self.example.title).size(20).font(Font::MONOSPACE),              horizontal_space(Length::Fill),              checkbox("Explain", self.explain, Message::ExplainToggled), +            pick_list( +                Theme::ALL, +                Some(self.theme.clone()), +                Message::ThemeSelected +            ),          ] +        .spacing(20)          .align_items(Alignment::Center);          let example = container(if self.explain { @@ -115,7 +127,7 @@ impl Application for Layout {      }      fn theme(&self) -> Theme { -        Theme::Dark +        self.theme.clone()      }  } diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index f14f6a8f..10f3c79d 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -53,13 +53,16 @@ impl Sandbox for Styling {                  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), -                    }), +                    ThemeType::Custom => Theme::custom( +                        String::from("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, diff --git a/style/src/theme.rs b/style/src/theme.rs index eafb0b47..deccf455 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -23,6 +23,7 @@ use crate::toggler;  use iced_core::{Background, Color, Vector}; +use std::fmt;  use std::rc::Rc;  /// A built-in theme. @@ -38,18 +39,22 @@ pub enum Theme {  }  impl Theme { +    /// A list with all the defined themes. +    pub const ALL: &'static [Self] = &[Self::Light, Self::Dark]; +      /// Creates a new custom [`Theme`] from the given [`Palette`]. -    pub fn custom(palette: Palette) -> Self { -        Self::custom_with_fn(palette, palette::Extended::generate) +    pub fn custom(name: String, palette: Palette) -> Self { +        Self::custom_with_fn(name, palette, palette::Extended::generate)      }      /// Creates a new custom [`Theme`] from the given [`Palette`], with      /// a custom generator of a [`palette::Extended`].      pub fn custom_with_fn( +        name: String,          palette: Palette,          generate: impl FnOnce(Palette) -> palette::Extended,      ) -> Self { -        Self::Custom(Box::new(Custom::with_fn(palette, generate))) +        Self::Custom(Box::new(Custom::with_fn(name, palette, generate)))      }      /// Returns the [`Palette`] of the [`Theme`]. @@ -71,32 +76,51 @@ impl Theme {      }  } +impl fmt::Display for Theme { +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +        match self { +            Self::Light => write!(f, "Light"), +            Self::Dark => write!(f, "Dark"), +            Self::Custom(custom) => custom.fmt(f), +        } +    } +} +  /// A [`Theme`] with a customized [`Palette`]. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)]  pub struct Custom { +    name: String,      palette: Palette,      extended: palette::Extended,  }  impl Custom {      /// Creates a [`Custom`] theme from the given [`Palette`]. -    pub fn new(palette: Palette) -> Self { -        Self::with_fn(palette, palette::Extended::generate) +    pub fn new(name: String, palette: Palette) -> Self { +        Self::with_fn(name, palette, palette::Extended::generate)      }      /// Creates a [`Custom`] theme from the given [`Palette`] with      /// a custom generator of a [`palette::Extended`].      pub fn with_fn( +        name: String,          palette: Palette,          generate: impl FnOnce(Palette) -> palette::Extended,      ) -> Self {          Self { +            name,              palette,              extended: generate(palette),          }      }  } +impl fmt::Display for Custom { +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +        write!(f, "{}", self.name) +    } +} +  /// The style of an application.  #[derive(Default)]  pub enum Application { diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 4b988ae3..498dd76c 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -271,7 +271,7 @@ pub fn pick_list<'a, Message, Renderer, T>(      on_selected: impl Fn(T) -> Message + 'a,  ) -> PickList<'a, T, Message, Renderer>  where -    T: ToString + Eq + 'static, +    T: ToString + PartialEq + 'static,      [T]: ToOwned<Owned = Vec<T>>,      Renderer: core::text::Renderer,      Renderer::Theme: pick_list::StyleSheet diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 9f6a371a..2e3aab6f 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -45,7 +45,7 @@ where  impl<'a, T: 'a, Message, Renderer> PickList<'a, T, Message, Renderer>  where -    T: ToString + Eq, +    T: ToString + PartialEq,      [T]: ToOwned<Owned = Vec<T>>,      Renderer: text::Renderer,      Renderer::Theme: StyleSheet @@ -145,7 +145,7 @@ where  impl<'a, T: 'a, Message, Renderer> Widget<Message, Renderer>      for PickList<'a, T, Message, Renderer>  where -    T: Clone + ToString + Eq + 'static, +    T: Clone + ToString + PartialEq + 'static,      [T]: ToOwned<Owned = Vec<T>>,      Message: 'a,      Renderer: text::Renderer + 'a, @@ -281,7 +281,7 @@ where  impl<'a, T: 'a, Message, Renderer> From<PickList<'a, T, Message, Renderer>>      for Element<'a, Message, Renderer>  where -    T: Clone + ToString + Eq + 'static, +    T: Clone + ToString + PartialEq + 'static,      [T]: ToOwned<Owned = Vec<T>>,      Message: 'a,      Renderer: text::Renderer + 'a, | 
