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