From 7d84c9c9c3619513519ac1ef7ea1c5f6e4e2cf5d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 21:55:24 +0100 Subject: Simplify theming for `Radio` widget --- widget/src/radio.rs | 106 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 21 deletions(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 68e9bc7e..c4283af8 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.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, Layout, Length, Pixels, Rectangle, Shell, Size, - Widget, + Background, Border, Clipboard, Color, Element, Layout, Length, Pixels, + Rectangle, Shell, Size, Widget, }; - -pub use iced_style::radio::{Appearance, StyleSheet}; +use crate::style::Theme; /// A circular button representing a choice. /// @@ -71,7 +70,6 @@ pub use iced_style::radio::{Appearance, StyleSheet}; #[allow(missing_debug_implementations)] pub struct Radio where - Theme: StyleSheet, Renderer: text::Renderer, { is_selected: bool, @@ -84,20 +82,19 @@ where text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, - style: Theme::Style, + style: fn(&Theme, Status) -> Appearance, } impl Radio where Message: Clone, - Theme: StyleSheet, Renderer: text::Renderer, { /// The default size of a [`Radio`] button. - pub const DEFAULT_SIZE: f32 = 28.0; + pub const DEFAULT_SIZE: f32 = 14.0; /// The default spacing of a [`Radio`] button. - pub const DEFAULT_SPACING: f32 = 15.0; + pub const DEFAULT_SPACING: f32 = 10.0; /// Creates a new [`Radio`] button. /// @@ -114,6 +111,7 @@ where f: F, ) -> Self where + Theme: Style, V: Eq + Copy, F: FnOnce(V) -> Message, { @@ -128,7 +126,7 @@ where text_line_height: text::LineHeight::default(), text_shaping: text::Shaping::Basic, font: None, - style: Default::default(), + style: Theme::style(), } } @@ -178,7 +176,7 @@ where } /// Sets the style of the [`Radio`] button. - 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 } @@ -188,7 +186,6 @@ impl Widget for Radio where Message: Clone, - Theme: StyleSheet + crate::text::StyleSheet, Renderer: text::Renderer, { fn tag(&self) -> tree::Tag { @@ -291,15 +288,18 @@ where viewport: &Rectangle, ) { let is_mouse_over = cursor.is_over(layout.bounds()); + let is_selected = self.is_selected; let mut children = layout.children(); - let custom_style = if is_mouse_over { - theme.hovered(&self.style, self.is_selected) + let status = if is_mouse_over { + Status::Hovered { is_selected } } else { - theme.active(&self.style, self.is_selected) + Status::Active { is_selected } }; + let appearance = (self.style)(theme, status); + { let layout = children.next().unwrap(); let bounds = layout.bounds(); @@ -312,12 +312,12 @@ where bounds, border: Border { radius: (size / 2.0).into(), - width: custom_style.border_width, - color: custom_style.border_color, + width: appearance.border_width, + color: appearance.border_color, }, ..renderer::Quad::default() }, - custom_style.background, + appearance.background, ); if self.is_selected { @@ -332,7 +332,7 @@ where border: Border::with_radius(dot_size / 2.0), ..renderer::Quad::default() }, - custom_style.dot_color, + appearance.dot_color, ); } } @@ -346,7 +346,7 @@ where label_layout, tree.state.downcast_ref(), crate::text::Appearance { - color: custom_style.text_color, + color: appearance.text_color, }, viewport, ); @@ -358,7 +358,7 @@ impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: 'a + Clone, - Theme: StyleSheet + crate::text::StyleSheet + 'a, + Theme: 'a, Renderer: 'a + text::Renderer, { fn from( @@ -367,3 +367,67 @@ where Element::new(radio) } } + +/// The possible status of a [`TextInput`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Status { + /// The [`Radio`] button can be interacted with. + Active { + /// Indicates whether the [`Radio`] button is currently selected. + is_selected: bool, + }, + /// The [`Radio`] button is being hovered. + Hovered { + /// Indicates whether the [`Radio`] button is currently selected. + is_selected: bool, + }, +} + +/// The appearance of a radio button. +#[derive(Debug, Clone, Copy)] +pub struct Appearance { + /// The [`Background`] of the radio button. + pub background: Background, + /// The [`Color`] of the dot of the radio button. + pub dot_color: Color, + /// The border width of the radio button. + pub border_width: f32, + /// The border [`Color`] of the radio button. + pub border_color: Color, + /// The text [`Color`] of the radio button. + pub text_color: Option, +} + +/// The definiton of the default style of a [`Radio`] button. +pub trait Style { + /// Returns the default style of a [`Radio`] button. + fn style() -> fn(&Self, Status) -> Appearance; +} + +impl Style for Theme { + fn style() -> fn(&Self, Status) -> Appearance { + default + } +} + +/// The default style of a [`Radio`] button. +pub fn default(theme: &Theme, status: Status) -> Appearance { + let palette = theme.extended_palette(); + + let active = Appearance { + background: Color::TRANSPARENT.into(), + dot_color: palette.primary.strong.color, + border_width: 1.0, + border_color: palette.primary.strong.color, + text_color: None, + }; + + match status { + Status::Active { .. } => active, + Status::Hovered { .. } => Appearance { + dot_color: palette.primary.strong.color, + background: palette.primary.weak.color.into(), + ..active + }, + } +} -- cgit From 87d16a090b14fa206bb87041a32d66348bc294e4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 22:03:10 +0100 Subject: Reduce default size of `checkbox` to `15.0` --- widget/src/radio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index c4283af8..90a10a0b 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -91,7 +91,7 @@ where Renderer: text::Renderer, { /// The default size of a [`Radio`] button. - pub const DEFAULT_SIZE: f32 = 14.0; + pub const DEFAULT_SIZE: f32 = 15.0; /// The default spacing of a [`Radio`] button. pub const DEFAULT_SPACING: f32 = 10.0; -- 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/radio.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 90a10a0b..83d17f01 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -82,7 +82,7 @@ where text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl Radio @@ -111,7 +111,7 @@ where f: F, ) -> Self where - Theme: Style, + Style: Default, V: Eq + Copy, F: FnOnce(V) -> Message, { @@ -126,7 +126,7 @@ where text_line_height: text::LineHeight::default(), text_shaping: text::Shaping::Basic, font: None, - style: Theme::style(), + style: Style::default(), } } @@ -177,7 +177,7 @@ where /// Sets the style of the [`Radio`] button. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + self.style = Style(style); self } } @@ -298,7 +298,7 @@ where Status::Active { is_selected } }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); { let layout = children.next().unwrap(); @@ -398,15 +398,27 @@ pub struct Appearance { pub text_color: Option, } -/// The definiton of the default style of a [`Radio`] button. -pub trait Style { - /// Returns the default style of a [`Radio`] button. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of a [`Radio`] button. +#[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/radio.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 83d17f01..556d8ac9 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -10,16 +10,15 @@ use crate::core::widget; use crate::core::widget::tree::{self, Tree}; use crate::core::{ Background, Border, Clipboard, Color, Element, Layout, Length, Pixels, - Rectangle, Shell, Size, Widget, + Rectangle, Shell, Size, Theme, Widget, }; -use crate::style::Theme; /// A circular button representing a choice. /// /// # Example /// ```no_run /// # type Radio = -/// # iced_widget::Radio; +/// # iced_widget::Radio; /// # /// # use iced_widget::column; /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -- cgit From 6785a452eea5f6b6f69bac123789245dacbc936e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:19:24 +0100 Subject: Fix broken links in documentation --- widget/src/radio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 556d8ac9..6bb72650 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -367,7 +367,7 @@ where } } -/// The possible status of a [`TextInput`]. +/// The possible status of a [`Radio`] button. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Status { /// The [`Radio`] button can be interacted with. -- 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/radio.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 6bb72650..e8f1eb1f 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -110,7 +110,7 @@ where f: F, ) -> Self where - Style: Default, + Theme: DefaultStyle, V: Eq + Copy, F: FnOnce(V) -> Message, { @@ -125,7 +125,7 @@ where text_line_height: text::LineHeight::default(), text_shaping: text::Shaping::Basic, font: None, - style: Style::default(), + style: Theme::default_style(), } } @@ -176,7 +176,7 @@ where /// Sets the style of the [`Radio`] button. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -297,7 +297,7 @@ where Status::Active { is_selected } }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); { let layout = children.next().unwrap(); @@ -398,26 +398,23 @@ pub struct Appearance { } /// The style of a [`Radio`] button. -#[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 [`Radio`] button. +pub trait DefaultStyle { + /// Returns the default style of a [`Radio`] button. + 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 7ece5eea509f3595432babfc7729701f2e063b21 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 21:02:17 +0100 Subject: Implement additional helpers for `Border` and `container::Appearance` --- widget/src/radio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index e8f1eb1f..34c3b3a0 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -328,7 +328,7 @@ where width: bounds.width - dot_size, height: bounds.height - dot_size, }, - border: Border::with_radius(dot_size / 2.0), + border: Border::rounded(dot_size / 2.0), ..renderer::Quad::default() }, appearance.dot_color, -- 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/radio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/radio.rs') diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 34c3b3a0..5e4a3c1f 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -90,10 +90,10 @@ where Renderer: text::Renderer, { /// The default size of a [`Radio`] button. - pub const DEFAULT_SIZE: f32 = 15.0; + pub const DEFAULT_SIZE: f32 = 16.0; /// The default spacing of a [`Radio`] button. - pub const DEFAULT_SPACING: f32 = 10.0; + pub const DEFAULT_SPACING: f32 = 8.0; /// Creates a new [`Radio`] button. /// -- cgit