diff options
author | 2024-03-12 14:47:36 +0100 | |
---|---|---|
committer | 2024-03-12 14:52:29 +0100 | |
commit | afd138dba697976f61e335555b69417c4c84f7f2 (patch) | |
tree | ec2c5713fd5beca8abe4f1a9f96e643ca0d7ac4d /widget/src/slider.rs | |
parent | 58a0d5b7fff3000c46f5f2c468fce9442c78ef20 (diff) | |
download | iced-afd138dba697976f61e335555b69417c4c84f7f2.tar.gz iced-afd138dba697976f61e335555b69417c4c84f7f2.tar.bz2 iced-afd138dba697976f61e335555b69417c4c84f7f2.zip |
Use closures for `Slider::style` and `VerticalSlider::style`
Diffstat (limited to 'widget/src/slider.rs')
-rw-r--r-- | widget/src/slider.rs | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/widget/src/slider.rs b/widget/src/slider.rs index f3ea9bfd..d3b46a98 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -49,7 +49,7 @@ pub struct Slider<'a, T, Message, Theme = crate::Theme> { on_release: Option<Message>, width: Length, height: f32, - style: Style<Theme>, + style: Style<'a, Theme>, } impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme> @@ -70,7 +70,7 @@ where /// `Message`. pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, F: 'a + Fn(T) -> Message, { let value = if value >= *range.start() { @@ -95,7 +95,7 @@ where on_release: None, width: Length::Fill, height: Self::DEFAULT_HEIGHT, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -131,8 +131,11 @@ where } /// Sets the style of the [`Slider`]. - 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 } @@ -547,23 +550,23 @@ pub enum HandleShape { } /// The style of a [`Slider`]. -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 [`Slider`]. pub trait DefaultStyle { /// Returns the default style of a [`Slider`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } |