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/checkbox.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'widget/src/checkbox.rs') diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index 91838291..e4fc2232 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -53,13 +53,12 @@ pub struct Checkbox< text_shaping: text::Shaping, font: Option, icon: Icon, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer> where Renderer: text::Renderer, - Theme: Style, { /// The default size of a [`Checkbox`]. const DEFAULT_SIZE: f32 = 15.0; @@ -72,7 +71,10 @@ where /// It expects: /// * the label of the [`Checkbox`] /// * a boolean describing whether the [`Checkbox`] is checked or not - pub fn new(label: impl Into, is_checked: bool) -> Self { + pub fn new(label: impl Into, is_checked: bool) -> Self + where + Style: Default, + { Checkbox { is_checked, on_toggle: None, @@ -91,7 +93,7 @@ where line_height: text::LineHeight::default(), shaping: text::Shaping::Basic, }, - style: Theme::style(), + style: Style::default(), } } @@ -175,7 +177,7 @@ where /// Sets the style of the [`Checkbox`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + self.style = Style(style); self } } @@ -301,7 +303,7 @@ where Status::Active { is_checked } }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); { let layout = children.next().unwrap(); @@ -423,15 +425,27 @@ pub struct Appearance { pub text_color: Option, } -/// A set of rules that dictate the style of a checkbox. -pub trait Style { - /// The supported style of the [`StyleSheet`]. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of a [`Checkbox`]. +#[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(primary) + } } -impl Style for Theme { - fn style() -> fn(&Self, Status) -> Appearance { - primary +impl From Appearance> for Style { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) } } -- cgit