From 60b5822b67e569e99efc9e4176c3e6d3f859d0bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Mar 2024 13:30:47 +0100 Subject: Use closures for `Button::style` --- widget/src/button.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'widget/src/button.rs') diff --git a/widget/src/button.rs b/widget/src/button.rs index e265aa1f..5790f811 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -56,7 +56,7 @@ where height: Length, padding: Padding, clip: bool, - style: Style, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> @@ -68,7 +68,7 @@ where content: impl Into>, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { let content = content.into(); let size = content.as_widget().size_hint(); @@ -80,7 +80,7 @@ where height: size.height.fluid(), padding: DEFAULT_PADDING, clip: false, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -120,8 +120,11 @@ where } /// Sets the style variant of this [`Button`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -439,29 +442,29 @@ impl std::default::Default for Appearance { } /// The style of a [`Button`]. -pub type Style = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box Appearance + 'a>; /// The default style of a [`Button`]. pub trait DefaultStyle { /// Returns the default style of a [`Button`]. - fn default_style() -> Style; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style { - primary + fn default_style(&self, status: Status) -> Appearance { + primary(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } impl DefaultStyle for Color { - fn default_style() -> Style { - |color, _status| Appearance::default().with_background(*color) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default().with_background(*self) } } -- cgit