diff options
author | 2024-03-12 13:30:47 +0100 | |
---|---|---|
committer | 2024-03-12 13:31:25 +0100 | |
commit | 60b5822b67e569e99efc9e4176c3e6d3f859d0bb (patch) | |
tree | 248029a84bca042e387f8bd3df53d6cb6e47329f /widget | |
parent | 2088e5d66117dd481e4c60ba6afe9ab8f3a2d4c1 (diff) | |
download | iced-60b5822b67e569e99efc9e4176c3e6d3f859d0bb.tar.gz iced-60b5822b67e569e99efc9e4176c3e6d3f859d0bb.tar.bz2 iced-60b5822b67e569e99efc9e4176c3e6d3f859d0bb.zip |
Use closures for `Button::style`
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/button.rs | 29 | ||||
-rw-r--r-- | widget/src/helpers.rs | 2 |
2 files changed, 17 insertions, 14 deletions
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<Theme>, + style: Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> @@ -68,7 +68,7 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> 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<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of a [`Button`]. pub trait DefaultStyle { /// Returns the default style of a [`Button`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - primary + fn default_style(&self, status: Status) -> Appearance { + primary(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } impl DefaultStyle for Color { - fn default_style() -> Style<Self> { - |color, _status| Appearance::default().with_background(*color) + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default().with_background(*self) } } diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 89d6ef62..db21c8e9 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -117,7 +117,7 @@ pub fn button<'a, Message, Theme, Renderer>( content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Button<'a, Message, Theme, Renderer> where - Theme: button::DefaultStyle, + Theme: button::DefaultStyle + 'a, Renderer: core::Renderer, { Button::new(content) |