diff options
author | 2024-03-06 20:30:58 +0100 | |
---|---|---|
committer | 2024-03-06 20:30:58 +0100 | |
commit | 34e7c6593a9e0f56cee5db18b7258717cf6bc11b (patch) | |
tree | 7c65a58e9052f2f95a0025355679b13c7002eeab /widget/src/progress_bar.rs | |
parent | 8a63774b24488f71147a728123551ae72c080d14 (diff) | |
download | iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.tar.gz iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.tar.bz2 iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.zip |
Use `Style` struct pattern instead of trait for all widgets
Diffstat (limited to 'widget/src/progress_bar.rs')
-rw-r--r-- | widget/src/progress_bar.rs | 36 |
1 files changed, 24 insertions, 12 deletions
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<Theme = crate::Theme> { value: f32, width: Length, height: Option<Length>, - style: fn(&Theme) -> Appearance, + style: Style<Theme>, } impl<Theme> ProgressBar<Theme> { @@ -42,14 +42,14 @@ impl<Theme> ProgressBar<Theme> { /// * the current value of the [`ProgressBar`] pub fn new(range: RangeInclusive<f32>, value: f32) -> Self where - Theme: Style, + Style<Theme>: 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<Theme> ProgressBar<Theme> { /// 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<Theme>(fn(&Theme) -> Appearance); + +impl<Theme> Clone for Style<Theme> { + fn clone(&self) -> Self { + *self + } +} + +impl<Theme> Copy for Style<Theme> {} + +impl Default for Style<Theme> { + fn default() -> Self { + Style(primary) + } } -impl Style for Theme { - fn style() -> fn(&Self) -> Appearance { - primary +impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style(f) } } |