From 66f81c3429c736c5ca17d022ed1d6cdcc15e4f94 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Mar 2024 13:44:03 +0100 Subject: Use closures for `Rule::style` --- widget/src/helpers.rs | 8 ++++---- widget/src/rule.rs | 37 +++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 91d5ee80..4bff0f16 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -315,9 +315,9 @@ pub fn vertical_space() -> Space { /// Creates a horizontal [`Rule`] with the given height. /// /// [`Rule`]: crate::Rule -pub fn horizontal_rule(height: impl Into) -> Rule +pub fn horizontal_rule<'a, Theme>(height: impl Into) -> Rule<'a, Theme> where - Theme: rule::DefaultStyle, + Theme: rule::DefaultStyle + 'a, { Rule::horizontal(height) } @@ -325,9 +325,9 @@ where /// Creates a vertical [`Rule`] with the given width. /// /// [`Rule`]: crate::Rule -pub fn vertical_rule(width: impl Into) -> Rule +pub fn vertical_rule<'a, Theme>(width: impl Into) -> Rule<'a, Theme> where - Theme: rule::DefaultStyle, + Theme: rule::DefaultStyle + 'a, { Rule::vertical(width) } diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 8580d4c7..9fa5f74f 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -10,48 +10,49 @@ use crate::core::{ /// Display a horizontal or vertical rule for dividing content. #[allow(missing_debug_implementations)] -pub struct Rule { +pub struct Rule<'a, Theme = crate::Theme> { width: Length, height: Length, is_horizontal: bool, - style: Style, + style: Style<'a, Theme>, } -impl Rule { +impl<'a, Theme> Rule<'a, Theme> { /// Creates a horizontal [`Rule`] with the given height. pub fn horizontal(height: impl Into) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Rule { width: Length::Fill, height: Length::Fixed(height.into().0), is_horizontal: true, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } /// Creates a vertical [`Rule`] with the given width. pub fn vertical(width: impl Into) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Rule { width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } /// Sets the style of the [`Rule`]. - pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self { + self.style = Box::new(style); self } } -impl Widget for Rule +impl<'a, Message, Theme, Renderer> Widget + for Rule<'a, Theme> where Renderer: crate::core::Renderer, { @@ -126,14 +127,14 @@ where } } -impl<'a, Message, Theme, Renderer> From> +impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: 'a, Theme: 'a, Renderer: 'a + crate::core::Renderer, { - fn from(rule: Rule) -> Element<'a, Message, Theme, Renderer> { + fn from(rule: Rule<'a, Theme>) -> Element<'a, Message, Theme, Renderer> { Element::new(rule) } } @@ -216,23 +217,23 @@ impl FillMode { } /// The style of a [`Rule`]. -pub type Style = fn(&Theme) -> Appearance; +pub type Style<'a, Theme> = Box Appearance + 'a>; /// The default style of a [`Rule`]. pub trait DefaultStyle { /// Returns the default style of a [`Rule`]. - fn default_style() -> Style; + fn default_style(&self) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style { - default + fn default_style(&self) -> Appearance { + default(self) } } impl DefaultStyle for Appearance { - fn default_style() -> Style { - |appearance| *appearance + fn default_style(&self) -> Appearance { + *self } } -- cgit