From 66dce4865eb00f7cc7b75e64cd8a96a496916285 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Mar 2024 13:34:51 +0100 Subject: Use closures for `Scrollable::style` --- widget/src/scrollable.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 34312752..c03bbb7d 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -36,7 +36,7 @@ pub struct Scrollable< direction: Direction, content: Element<'a, Message, Theme, Renderer>, on_scroll: Option Message + 'a>>, - style: Style, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Scrollable<'a, Message, Theme, Renderer> @@ -48,7 +48,7 @@ where content: impl Into>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self::with_direction(content, Direction::default()) } @@ -59,20 +59,16 @@ where direction: Direction, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { - Self::with_direction_and_style( - content, - direction, - Theme::default_style(), - ) + Self::with_direction_and_style(content, direction, Theme::default_style) } /// Creates a new [`Scrollable`] with the given [`Direction`] and style. pub fn with_direction_and_style( content: impl Into>, direction: Direction, - style: fn(&Theme, Status) -> Appearance, + style: impl Fn(&Theme, Status) -> Appearance + 'a, ) -> Self { let content = content.into(); @@ -95,7 +91,7 @@ where direction, content, on_scroll: None, - style: style.into(), + style: Box::new(style), } } @@ -126,8 +122,11 @@ where } /// Sets the style of the [`Scrollable`] . - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } @@ -1603,23 +1602,23 @@ pub struct Scroller { } /// The style of a [`Scrollable`]. -pub type Style = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box Appearance + 'a>; /// The default style of a [`Scrollable`]. pub trait DefaultStyle { /// Returns the default style of a [`Scrollable`]. - fn default_style() -> Style; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } -- cgit