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/scrollable.rs | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'widget/src/scrollable.rs') diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 9772855e..19a80ee2 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -37,7 +37,7 @@ pub struct Scrollable< direction: Direction, content: Element<'a, Message, Theme, Renderer>, on_scroll: Option Message + 'a>>, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl<'a, Message, Theme, Renderer> Scrollable<'a, Message, Theme, Renderer> @@ -49,7 +49,7 @@ where content: impl Into>, ) -> Self where - Theme: Style, + Style: Default, { Self::with_direction(content, Direction::default()) } @@ -60,8 +60,17 @@ where direction: Direction, ) -> Self where - Theme: Style, + Style: Default, { + Self::with_direction_and_style(content, direction, Style::default().0) + } + + /// Creates a new [`Scrollable`] with the given [`Direction`] and style. + pub fn with_direction_and_style( + content: impl Into>, + direction: Direction, + style: fn(&Theme, Status) -> Appearance, + ) -> Self { let content = content.into(); debug_assert!( @@ -83,7 +92,7 @@ where direction, content, on_scroll: None, - style: Theme::style(), + style: style.into(), } } @@ -115,7 +124,7 @@ where /// Sets the style of the [`Scrollable`] . pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + self.style = style.into(); self } } @@ -399,7 +408,7 @@ where Status::Active }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); container::draw_background( renderer, @@ -1653,15 +1662,27 @@ pub struct Scroller { pub border: Border, } -/// The definition of the default style of a [`Scrollable`]. -pub trait Style { - /// Returns the default style of a [`Scrollable`]. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of a [`Scrollable`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(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