From 40af65c3aa4a96281db8f642cfa1641479314d27 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 11:24:51 +0100 Subject: Simplify theming for `Toggler` widget --- widget/src/toggler.rs | 131 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 20 deletions(-) (limited to 'widget/src/toggler.rs') diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 4e3925ba..1f19212d 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -9,11 +9,10 @@ use crate::core::touch; use crate::core::widget; use crate::core::widget::tree::{self, Tree}; use crate::core::{ - Border, Clipboard, Element, Event, Layout, Length, Pixels, Rectangle, - Shell, Size, Widget, + Border, Clipboard, Color, Element, Event, Layout, Length, Pixels, + Rectangle, Shell, Size, Widget, }; - -pub use crate::style::toggler::{Appearance, StyleSheet}; +use crate::style::Theme; /// A toggler widget. /// @@ -38,7 +37,6 @@ pub struct Toggler< Theme = crate::Theme, Renderer = crate::Renderer, > where - Theme: StyleSheet, Renderer: text::Renderer, { is_toggled: bool, @@ -52,12 +50,11 @@ pub struct Toggler< text_shaping: text::Shaping, spacing: f32, font: Option, - style: Theme::Style, + style: fn(&Theme, Status) -> Appearance, } impl<'a, Message, Theme, Renderer> Toggler<'a, Message, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { /// The default size of a [`Toggler`]. @@ -77,6 +74,7 @@ where f: F, ) -> Self where + Theme: Style, F: 'a + Fn(bool) -> Message, { Toggler { @@ -91,7 +89,7 @@ where text_shaping: text::Shaping::Basic, spacing: Self::DEFAULT_SIZE / 2.0, font: None, - style: Default::default(), + style: Theme::style(), } } @@ -149,7 +147,7 @@ where } /// Sets the style of the [`Toggler`]. - pub fn style(mut self, style: impl Into) -> Self { + pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { self.style = style.into(); self } @@ -158,7 +156,6 @@ where impl<'a, Message, Theme, Renderer> Widget for Toggler<'a, Message, Theme, Renderer> where - Theme: StyleSheet + crate::text::StyleSheet, Renderer: text::Renderer, { fn tag(&self) -> tree::Tag { @@ -294,12 +291,18 @@ where let bounds = toggler_layout.bounds(); let is_mouse_over = cursor.is_over(layout.bounds()); - let style = if is_mouse_over { - theme.hovered(&self.style, self.is_toggled) + let status = if is_mouse_over { + Status::Hovered { + is_toggled: self.is_toggled, + } } else { - theme.active(&self.style, self.is_toggled) + Status::Active { + is_toggled: self.is_toggled, + } }; + let appearance = (self.style)(theme, status); + let border_radius = bounds.height / BORDER_RADIUS_RATIO; let space = SPACE_RATIO * bounds.height; @@ -315,12 +318,12 @@ where bounds: toggler_background_bounds, border: Border { radius: border_radius.into(), - width: style.background_border_width, - color: style.background_border_color, + width: appearance.background_border_width, + color: appearance.background_border_color, }, ..renderer::Quad::default() }, - style.background, + appearance.background, ); let toggler_foreground_bounds = Rectangle { @@ -340,12 +343,12 @@ where bounds: toggler_foreground_bounds, border: Border { radius: border_radius.into(), - width: style.foreground_border_width, - color: style.foreground_border_color, + width: appearance.foreground_border_width, + color: appearance.foreground_border_color, }, ..renderer::Quad::default() }, - style.foreground, + appearance.foreground, ); } } @@ -354,7 +357,7 @@ impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: 'a, - Theme: StyleSheet + crate::text::StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from( @@ -363,3 +366,91 @@ where Element::new(toggler) } } + +/// The possible status of a [`Toggler`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Status { + /// The [`Toggler`] can be interacted with. + Active { + /// Indicates whether the [`Toggler`] is toggled. + is_toggled: bool, + }, + /// The [`Toggler`] is being hovered. + Hovered { + /// Indicates whether the [`Toggler`] is toggled. + is_toggled: bool, + }, +} + +/// The appearance of a toggler. +#[derive(Debug, Clone, Copy)] +pub struct Appearance { + /// The background [`Color`] of the toggler. + pub background: Color, + /// The width of the background border of the toggler. + pub background_border_width: f32, + /// The [`Color`] of the background border of the toggler. + pub background_border_color: Color, + /// The foreground [`Color`] of the toggler. + pub foreground: Color, + /// The width of the foreground border of the toggler. + pub foreground_border_width: f32, + /// The [`Color`] of the foreground border of the toggler. + pub foreground_border_color: Color, +} + +/// The definiton of the default style of a [`Toggler`]. +pub trait Style { + /// Returns the default style of a [`Toggler`]. + fn style() -> fn(&Self, Status) -> Appearance; +} + +impl Style for Theme { + fn style() -> fn(&Self, Status) -> Appearance { + default + } +} + +/// The default style of a [`Toggler`]. +pub fn default(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); + + let background = match status { + Status::Active { is_toggled } | Status::Hovered { is_toggled } => { + if is_toggled { + palette.primary.strong.color + } else { + palette.background.strong.color + } + } + }; + + let foreground = match status { + Status::Active { is_toggled } => { + if is_toggled { + palette.primary.strong.text + } else { + palette.background.base.color + } + } + Status::Hovered { is_toggled } => { + if is_toggled { + Color { + a: 0.5, + ..palette.primary.strong.text + } + } else { + palette.background.weak.color + } + } + }; + + Appearance { + background, + foreground, + foreground_border_width: 0.0, + foreground_border_color: Color::TRANSPARENT, + background_border_width: 0.0, + background_border_color: Color::TRANSPARENT, + } +} -- cgit From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/toggler.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'widget/src/toggler.rs') diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 1f19212d..cecd7b6c 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -50,7 +50,7 @@ pub struct Toggler< text_shaping: text::Shaping, spacing: f32, font: Option, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl<'a, Message, Theme, Renderer> Toggler<'a, Message, Theme, Renderer> @@ -74,7 +74,7 @@ where f: F, ) -> Self where - Theme: Style, + Style: Default, F: 'a + Fn(bool) -> Message, { Toggler { @@ -89,7 +89,7 @@ where text_shaping: text::Shaping::Basic, spacing: Self::DEFAULT_SIZE / 2.0, font: None, - style: Theme::style(), + style: Style::default(), } } @@ -301,7 +301,7 @@ where } }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); let border_radius = bounds.height / BORDER_RADIUS_RATIO; let space = SPACE_RATIO * bounds.height; @@ -399,15 +399,27 @@ pub struct Appearance { pub foreground_border_color: Color, } -/// The definiton of the default style of a [`Toggler`]. -pub trait Style { - /// Returns the default style of a [`Toggler`]. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of a [`Toggler`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme, Status) -> Appearance); + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style(default) + } } -impl Style for Theme { - fn style() -> fn(&Self, Status) -> Appearance { - default +impl From Appearance> for Style { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) } } -- cgit From 905f2160e6eb7504f52d9bd62c7bfa42c8ec2902 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:14:41 +0100 Subject: Move `Theme` type to `iced_core` --- widget/src/toggler.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'widget/src/toggler.rs') diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index cecd7b6c..adc82f73 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -10,17 +10,15 @@ use crate::core::widget; use crate::core::widget::tree::{self, Tree}; use crate::core::{ Border, Clipboard, Color, Element, Event, Layout, Length, Pixels, - Rectangle, Shell, Size, Widget, + Rectangle, Shell, Size, Theme, Widget, }; -use crate::style::Theme; /// A toggler widget. /// /// # Example /// /// ```no_run -/// # type Toggler<'a, Message> = -/// # iced_widget::Toggler<'a, Message, iced_widget::style::Theme, iced_widget::renderer::Renderer>; +/// # type Toggler<'a, Message> = iced_widget::Toggler<'a, Message>; /// # /// pub enum Message { /// TogglerToggled(bool), -- cgit From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/toggler.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'widget/src/toggler.rs') diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index adc82f73..6b540f1c 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -72,7 +72,7 @@ where f: F, ) -> Self where - Style: Default, + Theme: DefaultStyle, F: 'a + Fn(bool) -> Message, { Toggler { @@ -87,7 +87,7 @@ where text_shaping: text::Shaping::Basic, spacing: Self::DEFAULT_SIZE / 2.0, font: None, - style: Style::default(), + style: Theme::default_style(), } } @@ -299,7 +299,7 @@ where } }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); let border_radius = bounds.height / BORDER_RADIUS_RATIO; let space = SPACE_RATIO * bounds.height; @@ -398,26 +398,23 @@ pub struct Appearance { } /// The style of a [`Toggler`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(fn(&Theme, Status) -> Appearance); +pub type Style = fn(&Theme, Status) -> Appearance; -impl Clone for Style { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Toggler`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Toggler`]. + fn default_style() -> Style; } -impl Copy for Style {} - -impl Default for Style { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style() -> Style { + default } } -impl From Appearance> for Style { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style { + |appearance, _status| *appearance } } -- cgit From c99e5996478ee74e5328ef5aaa1d350fcc06933b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Mar 2024 00:25:42 +0100 Subject: Make `Checkbox`, `Radio`, and `Toggler` default sizes consistent --- widget/src/toggler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/toggler.rs') diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 6b540f1c..9e81ba33 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -56,7 +56,7 @@ where Renderer: text::Renderer, { /// The default size of a [`Toggler`]. - pub const DEFAULT_SIZE: f32 = 20.0; + pub const DEFAULT_SIZE: f32 = 16.0; /// Creates a new [`Toggler`]. /// -- cgit