diff options
author | 2022-05-31 05:13:57 +0200 | |
---|---|---|
committer | 2022-06-01 01:46:04 +0200 | |
commit | 3e2b6247f72815b6e928237f242c2d66478cf15d (patch) | |
tree | 694eba8c2bbf28565d598e02627b0b8e026e1e78 /style | |
parent | 28d09bfff1dde55190986bab10d7aaeb0ceb49de (diff) | |
download | iced-3e2b6247f72815b6e928237f242c2d66478cf15d.tar.gz iced-3e2b6247f72815b6e928237f242c2d66478cf15d.tar.bz2 iced-3e2b6247f72815b6e928237f242c2d66478cf15d.zip |
Implement theme styling for `Toggler`
... and wire up theming to the `styling` example.
Diffstat (limited to 'style')
-rw-r--r-- | style/src/theme.rs | 60 | ||||
-rw-r--r-- | style/src/toggler.rs | 45 |
2 files changed, 64 insertions, 41 deletions
diff --git a/style/src/theme.rs b/style/src/theme.rs index e3b151aa..2f9fd4fa 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -6,6 +6,7 @@ use crate::application; use crate::button; use crate::radio; use crate::slider; +use crate::toggler; use iced_core::{Background, Color}; @@ -51,6 +52,9 @@ impl application::StyleSheet for Theme { } } +/* + * Button + */ #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Button { Primary, @@ -114,6 +118,9 @@ impl button::StyleSheet for Theme { } } +/* + * Slider + */ impl slider::StyleSheet for Theme { type Style = (); @@ -167,6 +174,9 @@ impl slider::StyleSheet for Theme { } } +/* + * Radio + */ impl radio::StyleSheet for Theme { type Style = (); @@ -193,3 +203,53 @@ impl radio::StyleSheet for Theme { } } } + +/* + * Toggler + */ +impl toggler::StyleSheet for Theme { + type Style = (); + + fn active( + &self, + _style: Self::Style, + is_active: bool, + ) -> toggler::Appearance { + 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, + } + } + + fn hovered( + &self, + style: Self::Style, + is_active: bool, + ) -> toggler::Appearance { + let palette = self.extended_palette(); + + toggler::Appearance { + foreground: if is_active { + Color { + a: 0.5, + ..palette.primary.strong.text + } + } else { + palette.background.weak.color + }, + ..self.active(style, is_active) + } + } +} diff --git a/style/src/toggler.rs b/style/src/toggler.rs index c06a8cd1..4ee7db46 100644 --- a/style/src/toggler.rs +++ b/style/src/toggler.rs @@ -3,7 +3,7 @@ use iced_core::Color; /// The appearance of a toggler. #[derive(Debug)] -pub struct Style { +pub struct Appearance { pub background: Color, pub background_border: Option<Color>, pub foreground: Color, @@ -12,46 +12,9 @@ pub struct Style { /// A set of rules that dictate the style of a toggler. pub trait StyleSheet { - fn active(&self, is_active: bool) -> Style; + type Style: Default + Copy; - fn hovered(&self, is_active: bool) -> Style; -} - -struct Default; - -impl StyleSheet for Default { - fn active(&self, is_active: bool) -> Style { - Style { - background: if is_active { - Color::from_rgb(0.0, 1.0, 0.0) - } else { - Color::from_rgb(0.7, 0.7, 0.7) - }, - background_border: None, - foreground: Color::WHITE, - foreground_border: None, - } - } - - fn hovered(&self, is_active: bool) -> Style { - Style { - foreground: Color::from_rgb(0.95, 0.95, 0.95), - ..self.active(is_active) - } - } -} - -impl<'a> std::default::Default for Box<dyn StyleSheet + 'a> { - fn default() -> Self { - Box::new(Default) - } -} + fn active(&self, style: Self::Style, is_active: bool) -> Appearance; -impl<'a, T> From<T> for Box<dyn StyleSheet + 'a> -where - T: 'a + StyleSheet, -{ - fn from(style: T) -> Self { - Box::new(style) - } + fn hovered(&self, style: Self::Style, is_active: bool) -> Appearance; } |