From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/slider.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/slider.rs') diff --git a/widget/src/slider.rs b/widget/src/slider.rs index a40f8792..c48fe143 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -53,7 +53,7 @@ pub struct Slider<'a, T, Message, Theme = crate::Theme> { on_release: Option, width: Length, height: f32, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme> @@ -74,7 +74,7 @@ where /// `Message`. pub fn new(range: RangeInclusive, value: T, on_change: F) -> Self where - Theme: Style, + Style: Default, F: 'a + Fn(T) -> Message, { let value = if value >= *range.start() { @@ -99,7 +99,7 @@ where on_release: None, width: Length::Fill, height: Self::DEFAULT_HEIGHT, - style: Theme::style(), + style: Style::default(), } } @@ -136,7 +136,7 @@ where /// Sets the style of the [`Slider`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + self.style = style.into(); self } @@ -350,7 +350,7 @@ where let bounds = layout.bounds(); let is_mouse_over = cursor.is_over(bounds); - let style = (self.style)( + let style = (self.style.0)( theme, if state.is_dragging { Status::Dragged @@ -550,15 +550,27 @@ pub enum HandleShape { }, } -/// The definiton of the default style of a [`TextInput`]. -pub trait Style { - /// Returns the default style of a [`TextInput`]. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of a [`Slider`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(pub(crate) fn(&Theme, Status) -> Appearance); + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style(default) + } } -impl Style for Theme { - fn style() -> fn(&Self, Status) -> Appearance { - default +impl From Appearance> for Style { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) } } -- cgit