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/rule.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'widget/src/rule.rs') diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 1a1ba106..53a077aa 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -15,39 +15,39 @@ pub struct Rule { width: Length, height: Length, is_horizontal: bool, - style: fn(&Theme) -> Appearance, + style: Style, } impl Rule { /// Creates a horizontal [`Rule`] with the given height. pub fn horizontal(height: impl Into) -> Self where - Theme: Style, + Style: Default, { Rule { width: Length::Fill, height: Length::Fixed(height.into().0), is_horizontal: true, - style: Theme::style(), + style: Style::default(), } } /// Creates a vertical [`Rule`] with the given width. pub fn vertical(width: impl Into) -> Self where - Theme: Style, + Style: Default, { Rule { width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, - style: Theme::style(), + style: Style::default(), } } /// Sets the style of the [`Rule`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + self.style = Style(style); self } } @@ -83,7 +83,7 @@ where _viewport: &Rectangle, ) { let bounds = layout.bounds(); - let appearance = (self.style)(theme); + let appearance = (self.style.0)(theme); let bounds = if self.is_horizontal { let line_y = (bounds.y + (bounds.height / 2.0) @@ -216,15 +216,27 @@ impl FillMode { } } -/// The definiton of the default style of a [`Rule`]. -pub trait Style { - /// Returns the default style of a [`Rule`]. - fn style() -> fn(&Self) -> Appearance; +/// The style of a [`Rule`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme) -> 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) -> Appearance { - default +impl From Appearance> for Style { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style(f) } } -- cgit