From 8a63774b24488f71147a728123551ae72c080d14 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 17:26:07 +0100 Subject: Try `Style` newtype instead of trait for `Svg` widget --- widget/src/helpers.rs | 2 +- widget/src/svg.rs | 36 ++++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'widget/src') diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index da9a5792..f63306c4 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -366,7 +366,7 @@ pub fn image(handle: impl Into) -> crate::Image { #[cfg(feature = "svg")] pub fn svg(handle: impl Into) -> crate::Svg where - Theme: crate::svg::Style, + crate::svg::Style: Default, { crate::Svg::new(handle) } diff --git a/widget/src/svg.rs b/widget/src/svg.rs index a0402288..8ac5a1cf 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -25,21 +25,21 @@ pub struct Svg { width: Length, height: Length, content_fit: ContentFit, - style: fn(&Theme, Status) -> Appearance, + style: Style, } impl Svg { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into) -> Self where - Theme: Style, + Style: Default, { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Theme::style(), + style: Style::default(), } } @@ -48,7 +48,7 @@ impl Svg { #[must_use] pub fn from_path(path: impl Into) -> Self where - Theme: Style, + Style: Default, { Self::new(Handle::from_path(path)) } @@ -163,7 +163,7 @@ where Status::Idle }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); renderer.draw( self.handle.clone(), @@ -213,14 +213,26 @@ pub struct Appearance { pub color: Option, } -/// The definiton of the default style of an [`Svg`]. -pub trait Style { - /// Returns the default style of an [`Svg`]. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of an [`Svg`]. +#[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(|_, _| Appearance::default()) + } } -impl Style for Theme { - fn style() -> fn(&Self, Status) -> Appearance { - |_, _| Appearance::default() +impl From Appearance> for Style { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) } } -- cgit