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/progress_bar.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/progress_bar.rs') diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index 889c5558..62d319f4 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -28,7 +28,7 @@ pub struct ProgressBar { value: f32, width: Length, height: Option, - style: fn(&Theme) -> Appearance, + style: Style, } impl ProgressBar { @@ -42,14 +42,14 @@ impl ProgressBar { /// * the current value of the [`ProgressBar`] pub fn new(range: RangeInclusive, value: f32) -> Self where - Theme: Style, + Style: Default, { ProgressBar { value: value.clamp(*range.start(), *range.end()), range, width: Length::Fill, height: None, - style: Theme::style(), + style: Style::default(), } } @@ -67,7 +67,7 @@ impl ProgressBar { /// Sets the style of the [`ProgressBar`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + self.style = style.into(); self } } @@ -117,7 +117,7 @@ where / (range_end - range_start) }; - let appearance = (self.style)(theme); + let appearance = (self.style.0)(theme); renderer.fill_quad( renderer::Quad { @@ -169,15 +169,27 @@ pub struct Appearance { pub border: Border, } -/// The definiton of the default style of a [`ProgressBar`]. -pub trait Style { - /// Returns the default style of a [`ProgressBar`]. - fn style() -> fn(&Self) -> Appearance; +/// The style of a [`ProgressBar`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme) -> Appearance); + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style(primary) + } } -impl Style for Theme { - fn style() -> fn(&Self) -> Appearance { - primary +impl From Appearance> for Style { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style(f) } } -- cgit