diff options
Diffstat (limited to 'style')
| -rw-r--r-- | style/src/application.rs | 4 | ||||
| -rw-r--r-- | style/src/button.rs | 10 | ||||
| -rw-r--r-- | style/src/checkbox.rs | 6 | ||||
| -rw-r--r-- | style/src/container.rs | 4 | ||||
| -rw-r--r-- | style/src/menu.rs | 4 | ||||
| -rw-r--r-- | style/src/pane_grid.rs | 6 | ||||
| -rw-r--r-- | style/src/pick_list.rs | 15 | ||||
| -rw-r--r-- | style/src/progress_bar.rs | 4 | ||||
| -rw-r--r-- | style/src/radio.rs | 6 | ||||
| -rw-r--r-- | style/src/rule.rs | 4 | ||||
| -rw-r--r-- | style/src/scrollable.rs | 8 | ||||
| -rw-r--r-- | style/src/slider.rs | 8 | ||||
| -rw-r--r-- | style/src/text_input.rs | 14 | ||||
| -rw-r--r-- | style/src/theme.rs | 696 | ||||
| -rw-r--r-- | style/src/toggler.rs | 6 | 
15 files changed, 509 insertions, 286 deletions
| diff --git a/style/src/application.rs b/style/src/application.rs index d48c6a34..a7f8d11f 100644 --- a/style/src/application.rs +++ b/style/src/application.rs @@ -1,9 +1,9 @@  use iced_core::Color;  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default; -    fn appearance(&self, style: Self::Style) -> Appearance; +    fn appearance(&self, style: &Self::Style) -> Appearance;  }  #[derive(Debug, Clone, Copy, PartialEq)] diff --git a/style/src/button.rs b/style/src/button.rs index c63a6b71..858a9ab2 100644 --- a/style/src/button.rs +++ b/style/src/button.rs @@ -27,11 +27,11 @@ impl std::default::Default for Appearance {  /// A set of rules that dictate the style of a button.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default; -    fn active(&self, style: Self::Style) -> Appearance; +    fn active(&self, style: &Self::Style) -> Appearance; -    fn hovered(&self, style: Self::Style) -> Appearance { +    fn hovered(&self, style: &Self::Style) -> Appearance {          let active = self.active(style);          Appearance { @@ -40,14 +40,14 @@ pub trait StyleSheet {          }      } -    fn pressed(&self, style: Self::Style) -> Appearance { +    fn pressed(&self, style: &Self::Style) -> Appearance {          Appearance {              shadow_offset: Vector::default(),              ..self.active(style)          }      } -    fn disabled(&self, style: Self::Style) -> Appearance { +    fn disabled(&self, style: &Self::Style) -> Appearance {          let active = self.active(style);          Appearance { diff --git a/style/src/checkbox.rs b/style/src/checkbox.rs index ba54b0a2..a8b4a11d 100644 --- a/style/src/checkbox.rs +++ b/style/src/checkbox.rs @@ -14,9 +14,9 @@ pub struct Appearance {  /// A set of rules that dictate the style of a checkbox.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default; -    fn active(&self, style: Self::Style, is_checked: bool) -> Appearance; +    fn active(&self, style: &Self::Style, is_checked: bool) -> Appearance; -    fn hovered(&self, style: Self::Style, is_checked: bool) -> Appearance; +    fn hovered(&self, style: &Self::Style, is_checked: bool) -> Appearance;  } diff --git a/style/src/container.rs b/style/src/container.rs index 184310fa..53c59589 100644 --- a/style/src/container.rs +++ b/style/src/container.rs @@ -25,8 +25,8 @@ impl std::default::Default for Appearance {  /// A set of rules that dictate the [`Appearance`] of a container.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default;      /// Produces the [`Appearance`] of a container. -    fn appearance(&self, style: Self::Style) -> Appearance; +    fn appearance(&self, style: &Self::Style) -> Appearance;  } diff --git a/style/src/menu.rs b/style/src/menu.rs index 6ef3e2a2..409831a9 100644 --- a/style/src/menu.rs +++ b/style/src/menu.rs @@ -13,7 +13,7 @@ pub struct Appearance {  }  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default + Clone; -    fn appearance(&self, style: Self::Style) -> Appearance; +    fn appearance(&self, style: &Self::Style) -> Appearance;  } diff --git a/style/src/pane_grid.rs b/style/src/pane_grid.rs index 5bae353f..be541434 100644 --- a/style/src/pane_grid.rs +++ b/style/src/pane_grid.rs @@ -4,13 +4,13 @@ use iced_core::Color;  /// A set of rules that dictate the style of a container.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default;      /// The [`Line`] to draw when a split is picked. -    fn picked_split(&self, style: Self::Style) -> Option<Line>; +    fn picked_split(&self, style: &Self::Style) -> Option<Line>;      /// The [`Line`] to draw when a split is hovered. -    fn hovered_split(&self, style: Self::Style) -> Option<Line>; +    fn hovered_split(&self, style: &Self::Style) -> Option<Line>;  }  /// A line. diff --git a/style/src/pick_list.rs b/style/src/pick_list.rs index 2bafe932..a09772d7 100644 --- a/style/src/pick_list.rs +++ b/style/src/pick_list.rs @@ -1,9 +1,5 @@  use iced_core::{Background, Color}; -use crate::container; -use crate::menu; -use crate::scrollable; -  /// The appearance of a pick list.  #[derive(Debug, Clone, Copy)]  pub struct Appearance { @@ -17,13 +13,10 @@ pub struct Appearance {  }  /// A set of rules that dictate the style of a container. -pub trait StyleSheet: -    container::StyleSheet + menu::StyleSheet + scrollable::StyleSheet -{ -    type Style: Default + Copy + Into<<Self as menu::StyleSheet>::Style>; +pub trait StyleSheet { +    type Style: Default + Clone; -    fn active(&self, style: <Self as StyleSheet>::Style) -> Appearance; +    fn active(&self, style: &<Self as StyleSheet>::Style) -> Appearance; -    /// Produces the style of a container. -    fn hovered(&self, style: <Self as StyleSheet>::Style) -> Appearance; +    fn hovered(&self, style: &<Self as StyleSheet>::Style) -> Appearance;  } diff --git a/style/src/progress_bar.rs b/style/src/progress_bar.rs index 768e7c9c..506d634f 100644 --- a/style/src/progress_bar.rs +++ b/style/src/progress_bar.rs @@ -11,7 +11,7 @@ pub struct Appearance {  /// A set of rules that dictate the style of a progress bar.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default; -    fn appearance(&self, style: Self::Style) -> Appearance; +    fn appearance(&self, style: &Self::Style) -> Appearance;  } diff --git a/style/src/radio.rs b/style/src/radio.rs index d14ea33e..370d483e 100644 --- a/style/src/radio.rs +++ b/style/src/radio.rs @@ -13,9 +13,9 @@ pub struct Appearance {  /// A set of rules that dictate the style of a radio button.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default; -    fn active(&self, style: Self::Style, is_selected: bool) -> Appearance; +    fn active(&self, style: &Self::Style, is_selected: bool) -> Appearance; -    fn hovered(&self, style: Self::Style, is_selected: bool) -> Appearance; +    fn hovered(&self, style: &Self::Style, is_selected: bool) -> Appearance;  } diff --git a/style/src/rule.rs b/style/src/rule.rs index af334912..bcad655c 100644 --- a/style/src/rule.rs +++ b/style/src/rule.rs @@ -16,10 +16,10 @@ pub struct Appearance {  /// A set of rules that dictate the style of a rule.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default;      /// Produces the style of a rule. -    fn style(&self, style: Self::Style) -> Appearance; +    fn appearance(&self, style: &Self::Style) -> Appearance;  }  /// The fill mode of a rule. diff --git a/style/src/scrollable.rs b/style/src/scrollable.rs index 8da7409c..4352930c 100644 --- a/style/src/scrollable.rs +++ b/style/src/scrollable.rs @@ -22,16 +22,16 @@ pub struct Scroller {  /// A set of rules that dictate the style of a scrollable.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default;      /// Produces the style of an active scrollbar. -    fn active(&self, style: Self::Style) -> Scrollbar; +    fn active(&self, style: &Self::Style) -> Scrollbar;      /// Produces the style of an hovered scrollbar. -    fn hovered(&self, style: Self::Style) -> Scrollbar; +    fn hovered(&self, style: &Self::Style) -> Scrollbar;      /// Produces the style of a scrollbar that is being dragged. -    fn dragging(&self, style: Self::Style) -> Scrollbar { +    fn dragging(&self, style: &Self::Style) -> Scrollbar {          self.hovered(style)      }  } diff --git a/style/src/slider.rs b/style/src/slider.rs index 0ff0449b..73ee097b 100644 --- a/style/src/slider.rs +++ b/style/src/slider.rs @@ -26,14 +26,14 @@ pub enum HandleShape {  /// A set of rules that dictate the style of a slider.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default;      /// Produces the style of an active slider. -    fn active(&self, style: Self::Style) -> Appearance; +    fn active(&self, style: &Self::Style) -> Appearance;      /// Produces the style of an hovered slider. -    fn hovered(&self, style: Self::Style) -> Appearance; +    fn hovered(&self, style: &Self::Style) -> Appearance;      /// Produces the style of a slider that is being dragged. -    fn dragging(&self, style: Self::Style) -> Appearance; +    fn dragging(&self, style: &Self::Style) -> Appearance;  } diff --git a/style/src/text_input.rs b/style/src/text_input.rs index af86617b..1be9ccea 100644 --- a/style/src/text_input.rs +++ b/style/src/text_input.rs @@ -12,22 +12,22 @@ pub struct Appearance {  /// A set of rules that dictate the style of a text input.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default;      /// Produces the style of an active text input. -    fn active(&self, style: Self::Style) -> Appearance; +    fn active(&self, style: &Self::Style) -> Appearance;      /// Produces the style of a focused text input. -    fn focused(&self, style: Self::Style) -> Appearance; +    fn focused(&self, style: &Self::Style) -> Appearance; -    fn placeholder_color(&self, style: Self::Style) -> Color; +    fn placeholder_color(&self, style: &Self::Style) -> Color; -    fn value_color(&self, style: Self::Style) -> Color; +    fn value_color(&self, style: &Self::Style) -> Color; -    fn selection_color(&self, style: Self::Style) -> Color; +    fn selection_color(&self, style: &Self::Style) -> Color;      /// Produces the style of an hovered text input. -    fn hovered(&self, style: Self::Style) -> Appearance { +    fn hovered(&self, style: &Self::Style) -> Appearance {          self.focused(style)      }  } diff --git a/style/src/theme.rs b/style/src/theme.rs index a253e990..ca2d1904 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -19,10 +19,13 @@ 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; + +#[derive(Debug, Clone, PartialEq, Default)]  pub enum Theme { +    #[default]      Light,      Dark,      Custom(Box<Custom>), @@ -50,12 +53,6 @@ impl Theme {      }  } -impl Default for Theme { -    fn default() -> Self { -        Self::Light -    } -} -  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Custom {      palette: Palette, @@ -71,22 +68,17 @@ impl Custom {      }  } -#[derive(Debug, Clone, Copy)] +#[derive(Default)]  pub enum Application { +    #[default]      Default, -    Custom(fn(Theme) -> application::Appearance), -} - -impl Default for Application { -    fn default() -> Self { -        Self::Default -    } +    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 +86,43 @@ 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),          }      }  } +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)) +    } +} +  /*   * Button   */ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Default)]  pub enum Button { +    #[default]      Primary,      Secondary,      Positive,      Destructive,      Text, -} - -impl Default for Button { -    fn default() -> Self { -        Self::Primary -    } +    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 +145,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 +171,53 @@ 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)] +#[derive(Default)]  pub enum Checkbox { +    #[default]      Primary,      Secondary,      Success,      Danger, -} - -impl Default for Checkbox { -    fn default() -> Self { -        Self::Primary -    } +    Custom(Box<dyn checkbox::StyleSheet<Style = Theme>>),  }  impl checkbox::StyleSheet for Theme { @@ -187,7 +225,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 +255,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 +291,7 @@ impl checkbox::StyleSheet for Theme {                  palette.danger.base,                  is_checked,              ), +            Checkbox::Custom(custom) => custom.hovered(self, is_checked),          }      }  } @@ -279,29 +319,24 @@ fn checkbox_appearance(  /*   * Container   */ -#[derive(Clone, Copy)] +#[derive(Default)]  pub enum Container { +    #[default]      Transparent,      Box, -    Custom(fn(&Theme) -> container::Appearance), -} - -impl Default for Container { -    fn default() -> Self { -        Self::Transparent -    } +    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,63 +350,96 @@ impl container::StyleSheet for Theme {                      border_color: Color::TRANSPARENT,                  }              } -            Container::Custom(f) => f(self), +            Container::Custom(custom) => custom.appearance(self),          }      }  } +impl container::StyleSheet for fn(&Theme) -> container::Appearance { +    type Style = Theme; + +    fn appearance(&self, style: &Self::Style) -> container::Appearance { +        (self)(style) +    } +} +  /*   * Slider   */ -impl slider::StyleSheet for Theme { -    type Style = (); +#[derive(Default)] +pub enum Slider { +    #[default] +    Default, +    Custom(Box<dyn slider::StyleSheet<Style = Theme>>), +} -    fn active(&self, _style: Self::Style) -> slider::Appearance { -        let palette = self.extended_palette(); +impl slider::StyleSheet for Theme { +    type Style = Slider; -        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, -        }; +    fn active(&self, style: &Self::Style) -> slider::Appearance { +        match style { +            Slider::Default => { +                let palette = self.extended_palette(); -        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 -            }, +                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),          }      }  } @@ -379,20 +447,41 @@ impl slider::StyleSheet for Theme {  /*   * Menu   */ +#[derive(Clone, Default)] +pub enum Menu { +    #[default] +    Default, +    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),          }      }  } @@ -400,34 +489,54 @@ impl menu::StyleSheet for Theme {  /*   * Pick List   */ +#[derive(Clone, Default)] +pub enum PickList { +    #[default] +    Default, +    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),          }      }  } @@ -435,37 +544,54 @@ impl pick_list::StyleSheet for Theme {  /*   * Radio   */ +#[derive(Default)] +pub enum Radio { +    #[default] +    Default, +    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),          }      }  } @@ -473,49 +599,66 @@ impl radio::StyleSheet for Theme {  /*   * Toggler   */ +#[derive(Default)] +pub enum Toggler { +    #[default] +    Default, +    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),          }      }  } @@ -523,49 +666,71 @@ impl toggler::StyleSheet for Theme {  /*   * Pane Grid   */ +#[derive(Default)] +pub enum PaneGrid { +    #[default] +    Default, +    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)] +#[derive(Default)]  pub enum ProgressBar { +    #[default]      Primary,      Success,      Danger, -    Custom(fn(&Theme) -> progress_bar::Appearance), +    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 +743,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),          }      }  } +impl progress_bar::StyleSheet for fn(&Theme) -> progress_bar::Appearance { +    type Style = Theme; + +    fn appearance(&self, style: &Self::Style) -> progress_bar::Appearance { +        (self)(style) +    } +} +  /*   * Rule   */ -#[derive(Clone, Copy)] +#[derive(Default)]  pub enum Rule { +    #[default]      Default, -    Custom(fn(&Theme) -> rule::Appearance), +    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,48 +785,80 @@ impl rule::StyleSheet for Theme {                  radius: 0.0,                  fill_mode: rule::FillMode::Full,              }, -            Rule::Custom(f) => f(self), +            Rule::Custom(custom) => custom.appearance(self),          }      }  } +impl rule::StyleSheet for fn(&Theme) -> rule::Appearance { +    type Style = Theme; + +    fn appearance(&self, style: &Self::Style) -> rule::Appearance { +        (self)(style) +    } +} +  /*   * Scrollable   */ +#[derive(Default)] +pub enum Scrollable { +    #[default] +    Default, +    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),          }      }  } @@ -660,17 +866,11 @@ impl scrollable::StyleSheet for Theme {  /*   * Text   */ -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Default)]  pub enum Text { +    #[default]      Default,      Color(Color), -    Custom(fn(&Theme) -> text::Appearance), -} - -impl Default for Text { -    fn default() -> Self { -        Self::Default -    }  }  impl From<Color> for Text { @@ -686,7 +886,6 @@ impl text::StyleSheet for Theme {          match style {              Text::Default => Default::default(),              Text::Color(c) => text::Appearance { color: Some(c) }, -            Text::Custom(f) => f(self),          }      }  } @@ -694,10 +893,21 @@ impl text::StyleSheet for Theme {  /*   * Text Input   */ +#[derive(Default)] +pub enum TextInput { +    #[default] +    Default, +    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 +918,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 +933,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 +948,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/toggler.rs b/style/src/toggler.rs index 0acf8e97..fcc7e45b 100644 --- a/style/src/toggler.rs +++ b/style/src/toggler.rs @@ -12,9 +12,9 @@ pub struct Appearance {  /// A set of rules that dictate the style of a toggler.  pub trait StyleSheet { -    type Style: Default + Copy; +    type Style: Default; -    fn active(&self, style: Self::Style, is_active: bool) -> Appearance; +    fn active(&self, style: &Self::Style, is_active: bool) -> Appearance; -    fn hovered(&self, style: Self::Style, is_active: bool) -> Appearance; +    fn hovered(&self, style: &Self::Style, is_active: bool) -> Appearance;  } | 
