diff options
| author | 2022-12-02 18:53:21 +0100 | |
|---|---|---|
| committer | 2022-12-02 18:53:21 +0100 | |
| commit | 4029a1cdaaac1abbdcc141b20469a49670cd99b6 (patch) | |
| tree | 71fa9d9c4aa1f02ce05771db43a4bb7bc6570e77 /style/src/theme | |
| parent | 676d8efe03ebdbeeb95aef96b8097395b788b1ab (diff) | |
| parent | 8b55e9b9e6ba0b83038dd491dd34d95b4f9a381b (diff) | |
| download | iced-4029a1cdaaac1abbdcc141b20469a49670cd99b6.tar.gz iced-4029a1cdaaac1abbdcc141b20469a49670cd99b6.tar.bz2 iced-4029a1cdaaac1abbdcc141b20469a49670cd99b6.zip | |
Merge branch 'master' into non-uniform-border-radius-for-quads
Diffstat (limited to '')
| -rw-r--r-- | style/src/theme.rs | 805 | ||||
| -rw-r--r-- | style/src/theme/palette.rs | 53 | 
2 files changed, 583 insertions, 275 deletions
| diff --git a/style/src/theme.rs b/style/src/theme.rs index a253e990..d7ebb827 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -1,3 +1,4 @@ +//! Use the built-in theme and styles.  pub mod palette;  use self::palette::Extended; @@ -19,20 +20,29 @@ use crate::text;  use crate::text_input;  use crate::toggler; -use iced_core::{Background, Color}; +use iced_core::{Background, Color, Vector}; -#[derive(Debug, Clone, PartialEq)] +use std::rc::Rc; + +/// A built-in theme. +#[derive(Debug, Clone, PartialEq, Default)]  pub enum Theme { +    /// The built-in light variant. +    #[default]      Light, +    /// The built-in dark variant.      Dark, +    /// A [`Theme`] that uses a [`Custom`] palette.      Custom(Box<Custom>),  }  impl Theme { +    /// Creates a new custom [`Theme`] from the given [`Palette`].      pub fn custom(palette: Palette) -> Self {          Self::Custom(Box::new(Custom::new(palette)))      } +    /// Returns the [`Palette`] of the [`Theme`].      pub fn palette(&self) -> Palette {          match self {              Self::Light => Palette::LIGHT, @@ -41,6 +51,7 @@ impl Theme {          }      } +    /// Returns the [`palette::Extended`] of the [`Theme`].      pub fn extended_palette(&self) -> &palette::Extended {          match self {              Self::Light => &palette::EXTENDED_LIGHT, @@ -50,12 +61,7 @@ impl Theme {      }  } -impl Default for Theme { -    fn default() -> Self { -        Self::Light -    } -} - +/// A [`Theme`] with a customized [`Palette`].  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Custom {      palette: Palette, @@ -63,6 +69,7 @@ pub struct Custom {  }  impl Custom { +    /// Creates a [`Custom`] theme from the given [`Palette`].      pub fn new(palette: Palette) -> Self {          Self {              palette, @@ -71,22 +78,20 @@ impl Custom {      }  } -#[derive(Debug, Clone, Copy)] +/// The style of an application. +#[derive(Default)]  pub enum Application { +    /// The default style. +    #[default]      Default, -    Custom(fn(Theme) -> application::Appearance), -} - -impl Default for Application { -    fn default() -> Self { -        Self::Default -    } +    /// A custom style. +    Custom(Box<dyn application::StyleSheet<Style = Theme>>),  }  impl application::StyleSheet for Theme {      type Style = Application; -    fn appearance(&self, style: Self::Style) -> application::Appearance { +    fn appearance(&self, style: &Self::Style) -> application::Appearance {          let palette = self.extended_palette();          match style { @@ -94,33 +99,49 @@ impl application::StyleSheet for Theme {                  background_color: palette.background.base.color,                  text_color: palette.background.base.text,              }, -            Application::Custom(f) => f(self.clone()), +            Application::Custom(custom) => custom.appearance(self),          }      }  } -/* - * Button - */ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +impl application::StyleSheet for fn(&Theme) -> application::Appearance { +    type Style = Theme; + +    fn appearance(&self, style: &Self::Style) -> application::Appearance { +        (self)(style) +    } +} + +impl From<fn(&Theme) -> application::Appearance> for Application { +    fn from(f: fn(&Theme) -> application::Appearance) -> Self { +        Self::Custom(Box::new(f)) +    } +} + +/// The style of a button. +#[derive(Default)]  pub enum Button { +    /// The primary style. +    #[default]      Primary, +    /// The secondary style.      Secondary, +    /// The positive style.      Positive, +    /// The destructive style.      Destructive, +    /// The text style. +    /// +    /// Useful for links!      Text, -} - -impl Default for Button { -    fn default() -> Self { -        Self::Primary -    } +    /// A custom style. +    Custom(Box<dyn button::StyleSheet<Style = Theme>>),  }  impl button::StyleSheet for Theme {      type Style = Button; -    fn active(&self, style: Self::Style) -> button::Appearance { +    fn active(&self, style: &Self::Style) -> button::Appearance {          let palette = self.extended_palette();          let appearance = button::Appearance { @@ -143,19 +164,25 @@ impl button::StyleSheet for Theme {                  text_color: palette.background.base.text,                  ..appearance              }, +            Button::Custom(custom) => custom.active(self),          }      } -    fn hovered(&self, style: Self::Style) -> button::Appearance { -        let active = self.active(style); +    fn hovered(&self, style: &Self::Style) -> button::Appearance {          let palette = self.extended_palette(); +        if let Button::Custom(custom) = style { +            return custom.hovered(self); +        } + +        let active = self.active(style); +          let background = match style {              Button::Primary => Some(palette.primary.base.color),              Button::Secondary => Some(palette.background.strong.color),              Button::Positive => Some(palette.success.strong.color),              Button::Destructive => Some(palette.danger.strong.color), -            Button::Text => None, +            Button::Text | Button::Custom(_) => None,          };          button::Appearance { @@ -163,23 +190,56 @@ impl button::StyleSheet for Theme {              ..active          }      } + +    fn pressed(&self, style: &Self::Style) -> button::Appearance { +        if let Button::Custom(custom) = style { +            return custom.pressed(self); +        } + +        button::Appearance { +            shadow_offset: Vector::default(), +            ..self.active(style) +        } +    } + +    fn disabled(&self, style: &Self::Style) -> button::Appearance { +        if let Button::Custom(custom) = style { +            return custom.disabled(self); +        } + +        let active = self.active(style); + +        button::Appearance { +            shadow_offset: Vector::default(), +            background: active.background.map(|background| match background { +                Background::Color(color) => Background::Color(Color { +                    a: color.a * 0.5, +                    ..color +                }), +            }), +            text_color: Color { +                a: active.text_color.a * 0.5, +                ..active.text_color +            }, +            ..active +        } +    }  } -/* - * Checkbox - */ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// The style of a checkbox. +#[derive(Default)]  pub enum Checkbox { +    /// The primary style. +    #[default]      Primary, +    /// The secondary style.      Secondary, +    /// The success style.      Success, +    /// The danger style.      Danger, -} - -impl Default for Checkbox { -    fn default() -> Self { -        Self::Primary -    } +    /// A custom style. +    Custom(Box<dyn checkbox::StyleSheet<Style = Theme>>),  }  impl checkbox::StyleSheet for Theme { @@ -187,7 +247,7 @@ impl checkbox::StyleSheet for Theme {      fn active(          &self, -        style: Self::Style, +        style: &Self::Style,          is_checked: bool,      ) -> checkbox::Appearance {          let palette = self.extended_palette(); @@ -217,12 +277,13 @@ impl checkbox::StyleSheet for Theme {                  palette.danger.base,                  is_checked,              ), +            Checkbox::Custom(custom) => custom.active(self, is_checked),          }      }      fn hovered(          &self, -        style: Self::Style, +        style: &Self::Style,          is_checked: bool,      ) -> checkbox::Appearance {          let palette = self.extended_palette(); @@ -252,6 +313,7 @@ impl checkbox::StyleSheet for Theme {                  palette.danger.base,                  is_checked,              ), +            Checkbox::Custom(custom) => custom.hovered(self, is_checked),          }      }  } @@ -276,32 +338,28 @@ fn checkbox_appearance(      }  } -/* - * Container - */ -#[derive(Clone, Copy)] +/// The style of a container. +#[derive(Default)]  pub enum Container { +    /// No style. +    #[default]      Transparent, +    /// A simple box.      Box, -    Custom(fn(&Theme) -> container::Appearance), -} - -impl Default for Container { -    fn default() -> Self { -        Self::Transparent -    } +    /// A custom style. +    Custom(Box<dyn container::StyleSheet<Style = Theme>>),  }  impl From<fn(&Theme) -> container::Appearance> for Container {      fn from(f: fn(&Theme) -> container::Appearance) -> Self { -        Self::Custom(f) +        Self::Custom(Box::new(f))      }  }  impl container::StyleSheet for Theme {      type Style = Container; -    fn appearance(&self, style: Self::Style) -> container::Appearance { +    fn appearance(&self, style: &Self::Style) -> container::Appearance {          match style {              Container::Transparent => Default::default(),              Container::Box => { @@ -315,257 +373,389 @@ impl container::StyleSheet for Theme {                      border_color: Color::TRANSPARENT,                  }              } -            Container::Custom(f) => f(self), +            Container::Custom(custom) => custom.appearance(self),          }      }  } -/* - * Slider - */ -impl slider::StyleSheet for Theme { -    type Style = (); +impl container::StyleSheet for fn(&Theme) -> container::Appearance { +    type Style = Theme; -    fn active(&self, _style: Self::Style) -> slider::Appearance { -        let palette = self.extended_palette(); +    fn appearance(&self, style: &Self::Style) -> container::Appearance { +        (self)(style) +    } +} -        let handle = slider::Handle { -            shape: slider::HandleShape::Rectangle { -                width: 8, -                border_radius: 4.0, -            }, -            color: Color::WHITE, -            border_color: Color::WHITE, -            border_width: 1.0, -        }; +/// The style of a slider. +#[derive(Default)] +pub enum Slider { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn slider::StyleSheet<Style = Theme>>), +} -        slider::Appearance { -            rail_colors: (palette.primary.base.color, Color::TRANSPARENT), -            handle: slider::Handle { -                color: palette.background.base.color, -                border_color: palette.primary.base.color, -                ..handle -            }, +impl slider::StyleSheet for Theme { +    type Style = Slider; + +    fn active(&self, style: &Self::Style) -> slider::Appearance { +        match style { +            Slider::Default => { +                let palette = self.extended_palette(); + +                let handle = slider::Handle { +                    shape: slider::HandleShape::Rectangle { +                        width: 8, +                        border_radius: 4.0, +                    }, +                    color: Color::WHITE, +                    border_color: Color::WHITE, +                    border_width: 1.0, +                }; + +                slider::Appearance { +                    rail_colors: ( +                        palette.primary.base.color, +                        Color::TRANSPARENT, +                    ), +                    handle: slider::Handle { +                        color: palette.background.base.color, +                        border_color: palette.primary.base.color, +                        ..handle +                    }, +                } +            } +            Slider::Custom(custom) => custom.active(self),          }      } -    fn hovered(&self, style: Self::Style) -> slider::Appearance { -        let active = self.active(style); -        let palette = self.extended_palette(); +    fn hovered(&self, style: &Self::Style) -> slider::Appearance { +        match style { +            Slider::Default => { +                let active = self.active(style); +                let palette = self.extended_palette(); -        slider::Appearance { -            handle: slider::Handle { -                color: palette.primary.weak.color, -                ..active.handle -            }, -            ..active +                slider::Appearance { +                    handle: slider::Handle { +                        color: palette.primary.weak.color, +                        ..active.handle +                    }, +                    ..active +                } +            } +            Slider::Custom(custom) => custom.hovered(self),          }      } -    fn dragging(&self, style: Self::Style) -> slider::Appearance { -        let active = self.active(style); -        let palette = self.extended_palette(); +    fn dragging(&self, style: &Self::Style) -> slider::Appearance { +        match style { +            Slider::Default => { +                let active = self.active(style); +                let palette = self.extended_palette(); -        slider::Appearance { -            handle: slider::Handle { -                color: palette.primary.base.color, -                ..active.handle -            }, -            ..active +                slider::Appearance { +                    handle: slider::Handle { +                        color: palette.primary.base.color, +                        ..active.handle +                    }, +                    ..active +                } +            } +            Slider::Custom(custom) => custom.dragging(self),          }      }  } -/* - * Menu - */ +/// The style of a menu. +#[derive(Clone, Default)] +pub enum Menu { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Rc<dyn menu::StyleSheet<Style = Theme>>), +} +  impl menu::StyleSheet for Theme { -    type Style = (); +    type Style = Menu; -    fn appearance(&self, _style: Self::Style) -> menu::Appearance { -        let palette = self.extended_palette(); +    fn appearance(&self, style: &Self::Style) -> menu::Appearance { +        match style { +            Menu::Default => { +                let palette = self.extended_palette(); -        menu::Appearance { -            text_color: palette.background.weak.text, -            background: palette.background.weak.color.into(), -            border_width: 1.0, -            border_radius: 0.0, -            border_color: palette.background.strong.color, -            selected_text_color: palette.primary.strong.text, -            selected_background: palette.primary.strong.color.into(), +                menu::Appearance { +                    text_color: palette.background.weak.text, +                    background: palette.background.weak.color.into(), +                    border_width: 1.0, +                    border_radius: 0.0, +                    border_color: palette.background.strong.color, +                    selected_text_color: palette.primary.strong.text, +                    selected_background: palette.primary.strong.color.into(), +                } +            } +            Menu::Custom(custom) => custom.appearance(self), +        } +    } +} + +impl From<PickList> for Menu { +    fn from(pick_list: PickList) -> Self { +        match pick_list { +            PickList::Default => Self::Default, +            PickList::Custom(_, menu) => Self::Custom(menu),          }      }  } -/* - * Pick List - */ +/// The style of a pick list. +#[derive(Clone, Default)] +pub enum PickList { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom( +        Rc<dyn pick_list::StyleSheet<Style = Theme>>, +        Rc<dyn menu::StyleSheet<Style = Theme>>, +    ), +} +  impl pick_list::StyleSheet for Theme { -    type Style = (); +    type Style = PickList; -    fn active(&self, _style: ()) -> pick_list::Appearance { -        let palette = self.extended_palette(); +    fn active(&self, style: &Self::Style) -> pick_list::Appearance { +        match style { +            PickList::Default => { +                let palette = self.extended_palette(); -        pick_list::Appearance { -            text_color: palette.background.weak.text, -            background: palette.background.weak.color.into(), -            placeholder_color: palette.background.strong.color, -            border_radius: 2.0, -            border_width: 1.0, -            border_color: palette.background.strong.color, -            icon_size: 0.7, +                pick_list::Appearance { +                    text_color: palette.background.weak.text, +                    background: palette.background.weak.color.into(), +                    placeholder_color: palette.background.strong.color, +                    border_radius: 2.0, +                    border_width: 1.0, +                    border_color: palette.background.strong.color, +                    icon_size: 0.7, +                } +            } +            PickList::Custom(custom, _) => custom.active(self),          }      } -    fn hovered(&self, _style: ()) -> pick_list::Appearance { -        let palette = self.extended_palette(); +    fn hovered(&self, style: &Self::Style) -> pick_list::Appearance { +        match style { +            PickList::Default => { +                let palette = self.extended_palette(); -        pick_list::Appearance { -            text_color: palette.background.weak.text, -            background: palette.background.weak.color.into(), -            placeholder_color: palette.background.strong.color, -            border_radius: 2.0, -            border_width: 1.0, -            border_color: palette.primary.strong.color, -            icon_size: 0.7, +                pick_list::Appearance { +                    text_color: palette.background.weak.text, +                    background: palette.background.weak.color.into(), +                    placeholder_color: palette.background.strong.color, +                    border_radius: 2.0, +                    border_width: 1.0, +                    border_color: palette.primary.strong.color, +                    icon_size: 0.7, +                } +            } +            PickList::Custom(custom, _) => custom.active(self),          }      }  } -/* - * Radio - */ +/// The style of a radio button. +#[derive(Default)] +pub enum Radio { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn radio::StyleSheet<Style = Theme>>), +} +  impl radio::StyleSheet for Theme { -    type Style = (); +    type Style = Radio;      fn active(          &self, -        _style: Self::Style, -        _is_selected: bool, +        style: &Self::Style, +        is_selected: bool,      ) -> radio::Appearance { -        let palette = self.extended_palette(); +        match style { +            Radio::Default => { +                let palette = self.extended_palette(); -        radio::Appearance { -            background: Color::TRANSPARENT.into(), -            dot_color: palette.primary.strong.color, -            border_width: 1.0, -            border_color: palette.primary.strong.color, -            text_color: None, +                radio::Appearance { +                    background: Color::TRANSPARENT.into(), +                    dot_color: palette.primary.strong.color, +                    border_width: 1.0, +                    border_color: palette.primary.strong.color, +                    text_color: None, +                } +            } +            Radio::Custom(custom) => custom.active(self, is_selected),          }      }      fn hovered(          &self, -        style: Self::Style, +        style: &Self::Style,          is_selected: bool,      ) -> radio::Appearance { -        let active = self.active(style, is_selected); -        let palette = self.extended_palette(); +        match style { +            Radio::Default => { +                let active = self.active(style, is_selected); +                let palette = self.extended_palette(); -        radio::Appearance { -            dot_color: palette.primary.strong.color, -            background: palette.primary.weak.color.into(), -            ..active +                radio::Appearance { +                    dot_color: palette.primary.strong.color, +                    background: palette.primary.weak.color.into(), +                    ..active +                } +            } +            Radio::Custom(custom) => custom.hovered(self, is_selected),          }      }  } -/* - * Toggler - */ +/// The style of a toggler. +#[derive(Default)] +pub enum Toggler { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn toggler::StyleSheet<Style = Theme>>), +} +  impl toggler::StyleSheet for Theme { -    type Style = (); +    type Style = Toggler;      fn active(          &self, -        _style: Self::Style, +        style: &Self::Style,          is_active: bool,      ) -> toggler::Appearance { -        let palette = self.extended_palette(); +        match style { +            Toggler::Default => { +                let palette = self.extended_palette(); -        toggler::Appearance { -            background: if is_active { -                palette.primary.strong.color -            } else { -                palette.background.strong.color -            }, -            background_border: None, -            foreground: if is_active { -                palette.primary.strong.text -            } else { -                palette.background.base.color -            }, -            foreground_border: None, +                toggler::Appearance { +                    background: if is_active { +                        palette.primary.strong.color +                    } else { +                        palette.background.strong.color +                    }, +                    background_border: None, +                    foreground: if is_active { +                        palette.primary.strong.text +                    } else { +                        palette.background.base.color +                    }, +                    foreground_border: None, +                } +            } +            Toggler::Custom(custom) => custom.active(self, is_active),          }      }      fn hovered(          &self, -        style: Self::Style, +        style: &Self::Style,          is_active: bool,      ) -> toggler::Appearance { -        let palette = self.extended_palette(); +        match style { +            Toggler::Default => { +                let palette = self.extended_palette(); -        toggler::Appearance { -            foreground: if is_active { -                Color { -                    a: 0.5, -                    ..palette.primary.strong.text +                toggler::Appearance { +                    foreground: if is_active { +                        Color { +                            a: 0.5, +                            ..palette.primary.strong.text +                        } +                    } else { +                        palette.background.weak.color +                    }, +                    ..self.active(style, is_active)                  } -            } else { -                palette.background.weak.color -            }, -            ..self.active(style, is_active) +            } +            Toggler::Custom(custom) => custom.hovered(self, is_active),          }      }  } -/* - * Pane Grid - */ +/// The style of a pane grid. +#[derive(Default)] +pub enum PaneGrid { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn pane_grid::StyleSheet<Style = Theme>>), +} +  impl pane_grid::StyleSheet for Theme { -    type Style = (); +    type Style = PaneGrid; -    fn picked_split(&self, _style: Self::Style) -> Option<pane_grid::Line> { -        let palette = self.extended_palette(); +    fn picked_split(&self, style: &Self::Style) -> Option<pane_grid::Line> { +        match style { +            PaneGrid::Default => { +                let palette = self.extended_palette(); -        Some(pane_grid::Line { -            color: palette.primary.strong.color, -            width: 2.0, -        }) +                Some(pane_grid::Line { +                    color: palette.primary.strong.color, +                    width: 2.0, +                }) +            } +            PaneGrid::Custom(custom) => custom.picked_split(self), +        }      } -    fn hovered_split(&self, _style: Self::Style) -> Option<pane_grid::Line> { -        let palette = self.extended_palette(); +    fn hovered_split(&self, style: &Self::Style) -> Option<pane_grid::Line> { +        match style { +            PaneGrid::Default => { +                let palette = self.extended_palette(); -        Some(pane_grid::Line { -            color: palette.primary.base.color, -            width: 2.0, -        }) +                Some(pane_grid::Line { +                    color: palette.primary.base.color, +                    width: 2.0, +                }) +            } +            PaneGrid::Custom(custom) => custom.hovered_split(self), +        }      }  } -/* - * Progress Bar - */ -#[derive(Clone, Copy)] +/// The style of a progress bar. +#[derive(Default)]  pub enum ProgressBar { +    /// The primary style. +    #[default]      Primary, +    /// The success style.      Success, +    /// The danger style.      Danger, -    Custom(fn(&Theme) -> progress_bar::Appearance), +    /// A custom style. +    Custom(Box<dyn progress_bar::StyleSheet<Style = Theme>>),  } -impl Default for ProgressBar { -    fn default() -> Self { -        Self::Primary +impl From<fn(&Theme) -> progress_bar::Appearance> for ProgressBar { +    fn from(f: fn(&Theme) -> progress_bar::Appearance) -> Self { +        Self::Custom(Box::new(f))      }  }  impl progress_bar::StyleSheet for Theme {      type Style = ProgressBar; -    fn appearance(&self, style: Self::Style) -> progress_bar::Appearance { +    fn appearance(&self, style: &Self::Style) -> progress_bar::Appearance { +        if let ProgressBar::Custom(custom) = style { +            return custom.appearance(self); +        } +          let palette = self.extended_palette();          let from_palette = |bar: Color| progress_bar::Appearance { @@ -578,30 +768,39 @@ impl progress_bar::StyleSheet for Theme {              ProgressBar::Primary => from_palette(palette.primary.base.color),              ProgressBar::Success => from_palette(palette.success.base.color),              ProgressBar::Danger => from_palette(palette.danger.base.color), -            ProgressBar::Custom(f) => f(self), +            ProgressBar::Custom(custom) => custom.appearance(self),          }      }  } -/* - * Rule - */ -#[derive(Clone, Copy)] +impl progress_bar::StyleSheet for fn(&Theme) -> progress_bar::Appearance { +    type Style = Theme; + +    fn appearance(&self, style: &Self::Style) -> progress_bar::Appearance { +        (self)(style) +    } +} + +/// The style of a rule. +#[derive(Default)]  pub enum Rule { +    /// The default style. +    #[default]      Default, -    Custom(fn(&Theme) -> rule::Appearance), +    /// A custom style. +    Custom(Box<dyn rule::StyleSheet<Style = Theme>>),  } -impl Default for Rule { -    fn default() -> Self { -        Self::Default +impl From<fn(&Theme) -> rule::Appearance> for Rule { +    fn from(f: fn(&Theme) -> rule::Appearance) -> Self { +        Self::Custom(Box::new(f))      }  }  impl rule::StyleSheet for Theme {      type Style = Rule; -    fn style(&self, style: Self::Style) -> rule::Appearance { +    fn appearance(&self, style: &Self::Style) -> rule::Appearance {          let palette = self.extended_palette();          match style { @@ -611,66 +810,92 @@ impl rule::StyleSheet for Theme {                  radius: 0.0,                  fill_mode: rule::FillMode::Full,              }, -            Rule::Custom(f) => f(self), +            Rule::Custom(custom) => custom.appearance(self),          }      }  } -/* - * Scrollable - */ +impl rule::StyleSheet for fn(&Theme) -> rule::Appearance { +    type Style = Theme; + +    fn appearance(&self, style: &Self::Style) -> rule::Appearance { +        (self)(style) +    } +} + +/// The style of a scrollable. +#[derive(Default)] +pub enum Scrollable { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn scrollable::StyleSheet<Style = Theme>>), +} +  impl scrollable::StyleSheet for Theme { -    type Style = (); +    type Style = Scrollable; -    fn active(&self, _style: Self::Style) -> scrollable::Scrollbar { -        let palette = self.extended_palette(); +    fn active(&self, style: &Self::Style) -> scrollable::Scrollbar { +        match style { +            Scrollable::Default => { +                let palette = self.extended_palette(); -        scrollable::Scrollbar { -            background: palette.background.weak.color.into(), -            border_radius: 2.0, -            border_width: 0.0, -            border_color: Color::TRANSPARENT, -            scroller: scrollable::Scroller { -                color: palette.background.strong.color, -                border_radius: 2.0, -                border_width: 0.0, -                border_color: Color::TRANSPARENT, -            }, +                scrollable::Scrollbar { +                    background: palette.background.weak.color.into(), +                    border_radius: 2.0, +                    border_width: 0.0, +                    border_color: Color::TRANSPARENT, +                    scroller: scrollable::Scroller { +                        color: palette.background.strong.color, +                        border_radius: 2.0, +                        border_width: 0.0, +                        border_color: Color::TRANSPARENT, +                    }, +                } +            } +            Scrollable::Custom(custom) => custom.active(self),          }      } -    fn hovered(&self, _style: Self::Style) -> scrollable::Scrollbar { -        let palette = self.extended_palette(); +    fn hovered(&self, style: &Self::Style) -> scrollable::Scrollbar { +        match style { +            Scrollable::Default => { +                let palette = self.extended_palette(); -        scrollable::Scrollbar { -            background: palette.background.weak.color.into(), -            border_radius: 2.0, -            border_width: 0.0, -            border_color: Color::TRANSPARENT, -            scroller: scrollable::Scroller { -                color: palette.primary.strong.color, -                border_radius: 2.0, -                border_width: 0.0, -                border_color: Color::TRANSPARENT, -            }, +                scrollable::Scrollbar { +                    background: palette.background.weak.color.into(), +                    border_radius: 2.0, +                    border_width: 0.0, +                    border_color: Color::TRANSPARENT, +                    scroller: scrollable::Scroller { +                        color: palette.primary.strong.color, +                        border_radius: 2.0, +                        border_width: 0.0, +                        border_color: Color::TRANSPARENT, +                    }, +                } +            } +            Scrollable::Custom(custom) => custom.hovered(self), +        } +    } + +    fn dragging(&self, style: &Self::Style) -> scrollable::Scrollbar { +        match style { +            Scrollable::Default => self.hovered(style), +            Scrollable::Custom(custom) => custom.dragging(self),          }      }  } -/* - * Text - */ -#[derive(Clone, Copy)] +/// The style of text. +#[derive(Clone, Copy, Default)]  pub enum Text { +    /// The default style. +    #[default]      Default, +    /// Colored text.      Color(Color), -    Custom(fn(&Theme) -> text::Appearance), -} - -impl Default for Text { -    fn default() -> Self { -        Self::Default -    }  }  impl From<Color> for Text { @@ -686,18 +911,28 @@ impl text::StyleSheet for Theme {          match style {              Text::Default => Default::default(),              Text::Color(c) => text::Appearance { color: Some(c) }, -            Text::Custom(f) => f(self),          }      }  } -/* - * Text Input - */ +/// The style of a text input. +#[derive(Default)] +pub enum TextInput { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn text_input::StyleSheet<Style = Theme>>), +} +  impl text_input::StyleSheet for Theme { -    type Style = (); +    type Style = TextInput; + +    fn active(&self, style: &Self::Style) -> text_input::Appearance { +        if let TextInput::Custom(custom) = style { +            return custom.active(self); +        } -    fn active(&self, _style: Self::Style) -> text_input::Appearance {          let palette = self.extended_palette();          text_input::Appearance { @@ -708,7 +943,11 @@ impl text_input::StyleSheet for Theme {          }      } -    fn hovered(&self, _style: Self::Style) -> text_input::Appearance { +    fn hovered(&self, style: &Self::Style) -> text_input::Appearance { +        if let TextInput::Custom(custom) = style { +            return custom.hovered(self); +        } +          let palette = self.extended_palette();          text_input::Appearance { @@ -719,7 +958,11 @@ impl text_input::StyleSheet for Theme {          }      } -    fn focused(&self, _style: Self::Style) -> text_input::Appearance { +    fn focused(&self, style: &Self::Style) -> text_input::Appearance { +        if let TextInput::Custom(custom) = style { +            return custom.focused(self); +        } +          let palette = self.extended_palette();          text_input::Appearance { @@ -730,19 +973,31 @@ impl text_input::StyleSheet for Theme {          }      } -    fn placeholder_color(&self, _style: Self::Style) -> Color { +    fn placeholder_color(&self, style: &Self::Style) -> Color { +        if let TextInput::Custom(custom) = style { +            return custom.placeholder_color(self); +        } +          let palette = self.extended_palette();          palette.background.strong.color      } -    fn value_color(&self, _style: Self::Style) -> Color { +    fn value_color(&self, style: &Self::Style) -> Color { +        if let TextInput::Custom(custom) = style { +            return custom.value_color(self); +        } +          let palette = self.extended_palette();          palette.background.base.text      } -    fn selection_color(&self, _style: Self::Style) -> Color { +    fn selection_color(&self, style: &Self::Style) -> Color { +        if let TextInput::Custom(custom) = style { +            return custom.selection_color(self); +        } +          let palette = self.extended_palette();          palette.primary.weak.color diff --git a/style/src/theme/palette.rs b/style/src/theme/palette.rs index b3a10d28..0f15494b 100644 --- a/style/src/theme/palette.rs +++ b/style/src/theme/palette.rs @@ -1,18 +1,26 @@ +//! Define the colors of a theme.  use iced_core::Color;  use once_cell::sync::Lazy;  use palette::{FromColor, Hsl, Mix, RelativeContrast, Srgb}; +/// A color palette.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Palette { +    /// The background [`Color`] of the [`Palette`].      pub background: Color, +    /// The text [`Color`] of the [`Palette`].      pub text: Color, +    /// The primary [`Color`] of the [`Palette`].      pub primary: Color, +    /// The success [`Color`] of the [`Palette`].      pub success: Color, +    /// The danger [`Color`] of the [`Palette`].      pub danger: Color,  }  impl Palette { +    /// The built-in light variant of a [`Palette`].      pub const LIGHT: Self = Self {          background: Color::WHITE,          text: Color::BLACK, @@ -33,6 +41,7 @@ impl Palette {          ),      }; +    /// The built-in dark variant of a [`Palette`].      pub const DARK: Self = Self {          background: Color::from_rgb(              0x20 as f32 / 255.0, @@ -58,21 +67,31 @@ impl Palette {      };  } +/// An extended set of colors generated from a [`Palette`].  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Extended { +    /// The set of background colors.      pub background: Background, +    /// The set of primary colors.      pub primary: Primary, +    /// The set of secondary colors.      pub secondary: Secondary, +    /// The set of success colors.      pub success: Success, +    /// The set of danger colors.      pub danger: Danger,  } +/// The built-in light variant of an [`Extended`] palette.  pub static EXTENDED_LIGHT: Lazy<Extended> =      Lazy::new(|| Extended::generate(Palette::LIGHT)); + +/// The built-in dark variant of an [`Extended`] palette.  pub static EXTENDED_DARK: Lazy<Extended> =      Lazy::new(|| Extended::generate(Palette::DARK));  impl Extended { +    /// Generates an [`Extended`] palette from a simple [`Palette`].      pub fn generate(palette: Palette) -> Self {          Self {              background: Background::new(palette.background, palette.text), @@ -96,13 +115,22 @@ impl Extended {      }  } +/// A pair of background and text colors.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Pair { +    /// The background color.      pub color: Color, + +    /// The text color. +    /// +    /// It's guaranteed to be readable on top of the background [`color`]. +    /// +    /// [`color`]: Self::color      pub text: Color,  }  impl Pair { +    /// Creates a new [`Pair`] from a background [`Color`] and some text [`Color`].      pub fn new(color: Color, text: Color) -> Self {          Self {              color, @@ -111,14 +139,19 @@ impl Pair {      }  } +/// A set of background colors.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Background { +    /// The base background color.      pub base: Pair, +    /// A weaker version of the base background color.      pub weak: Pair, +    /// A stronger version of the base background color.      pub strong: Pair,  }  impl Background { +    /// Generates a set of [`Background`] colors from the base and text colors.      pub fn new(base: Color, text: Color) -> Self {          let weak = mix(base, text, 0.15);          let strong = mix(base, text, 0.40); @@ -131,14 +164,19 @@ impl Background {      }  } +/// A set of primary colors.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Primary { +    /// The base primary color.      pub base: Pair, +    /// A weaker version of the base primary color.      pub weak: Pair, +    /// A stronger version of the base primary color.      pub strong: Pair,  }  impl Primary { +    /// Generates a set of [`Primary`] colors from the base, background, and text colors.      pub fn generate(base: Color, background: Color, text: Color) -> Self {          let weak = mix(base, background, 0.4);          let strong = deviate(base, 0.1); @@ -151,14 +189,19 @@ impl Primary {      }  } +/// A set of secondary colors.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Secondary { +    /// The base secondary color.      pub base: Pair, +    /// A weaker version of the base secondary color.      pub weak: Pair, +    /// A stronger version of the base secondary color.      pub strong: Pair,  }  impl Secondary { +    /// Generates a set of [`Secondary`] colors from the base and text colors.      pub fn generate(base: Color, text: Color) -> Self {          let base = mix(base, text, 0.2);          let weak = mix(base, text, 0.1); @@ -172,14 +215,19 @@ impl Secondary {      }  } +/// A set of success colors.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Success { +    /// The base success color.      pub base: Pair, +    /// A weaker version of the base success color.      pub weak: Pair, +    /// A stronger version of the base success color.      pub strong: Pair,  }  impl Success { +    /// Generates a set of [`Success`] colors from the base, background, and text colors.      pub fn generate(base: Color, background: Color, text: Color) -> Self {          let weak = mix(base, background, 0.4);          let strong = deviate(base, 0.1); @@ -192,14 +240,19 @@ impl Success {      }  } +/// A set of danger colors.  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Danger { +    /// The base danger color.      pub base: Pair, +    /// A weaker version of the base danger color.      pub weak: Pair, +    /// A stronger version of the base danger color.      pub strong: Pair,  }  impl Danger { +    /// Generates a set of [`Danger`] colors from the base, background, and text colors.      pub fn generate(base: Color, background: Color, text: Color) -> Self {          let weak = mix(base, background, 0.4);          let strong = deviate(base, 0.1); | 
