diff options
author | 2024-03-12 18:17:19 +0100 | |
---|---|---|
committer | 2024-03-12 18:17:19 +0100 | |
commit | 3d915d3cb30e5d08829aa2928676a53c505a601e (patch) | |
tree | 4acb46e00ef3037aad6a8273ab67d4278d560784 /widget/src/button.rs | |
parent | 34317bba5db0a0f9e3ffdbbac0d7136a32bd0f95 (diff) | |
parent | 98621aa344a7a0e1b23f320d21a4687af559998e (diff) | |
download | iced-3d915d3cb30e5d08829aa2928676a53c505a601e.tar.gz iced-3d915d3cb30e5d08829aa2928676a53c505a601e.tar.bz2 iced-3d915d3cb30e5d08829aa2928676a53c505a601e.zip |
Merge pull request #2326 from iced-rs/closure-styles
Use closures for widget styling
Diffstat (limited to '')
-rw-r--r-- | widget/src/button.rs | 29 |
1 files changed, 16 insertions, 13 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) } } |