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/rule.rs | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'widget/src/rule.rs') diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 19ad43f6..384baed4 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -21,32 +21,32 @@ impl Rule { /// Creates a horizontal [`Rule`] with the given height. pub fn horizontal(height: impl Into) -> Self where - Style: Default, + Theme: DefaultStyle, { Rule { width: Length::Fill, height: Length::Fixed(height.into().0), is_horizontal: true, - style: Style::default(), + style: Theme::default_style(), } } /// Creates a vertical [`Rule`] with the given width. pub fn vertical(width: impl Into) -> Self where - Style: Default, + Theme: DefaultStyle, { Rule { width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, - style: Style::default(), + style: Theme::default_style(), } } /// Sets the style of the [`Rule`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } } @@ -82,7 +82,7 @@ where _viewport: &Rectangle, ) { let bounds = layout.bounds(); - let appearance = (self.style.0)(theme); + let appearance = (self.style)(theme); let bounds = if self.is_horizontal { let line_y = (bounds.y + (bounds.height / 2.0) @@ -216,26 +216,23 @@ impl FillMode { } /// The style of a [`Rule`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(fn(&Theme) -> Appearance); +pub type Style = fn(&Theme) -> Appearance; -impl Clone for Style { - fn clone(&self) -> Self { - *self - } +/// The default style of a [`Rule`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Rule`]. + 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) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style { + |appearance| *appearance } } -- cgit