diff options
author | 2024-03-06 17:26:07 +0100 | |
---|---|---|
committer | 2024-03-06 17:26:07 +0100 | |
commit | 8a63774b24488f71147a728123551ae72c080d14 (patch) | |
tree | 377c16d68cc70a510e0f2b911a43a792b0c91892 /widget | |
parent | 597a41cea73f078eda04eb3ff40cfda5d37d6135 (diff) | |
download | iced-8a63774b24488f71147a728123551ae72c080d14.tar.gz iced-8a63774b24488f71147a728123551ae72c080d14.tar.bz2 iced-8a63774b24488f71147a728123551ae72c080d14.zip |
Try `Style` newtype instead of trait for `Svg` widget
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/helpers.rs | 2 | ||||
-rw-r--r-- | widget/src/svg.rs | 36 |
2 files changed, 25 insertions, 13 deletions
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>(handle: impl Into<Handle>) -> crate::Image<Handle> { #[cfg(feature = "svg")] pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme> where - Theme: crate::svg::Style, + crate::svg::Style<Theme>: 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<Theme = crate::Theme> { width: Length, height: Length, content_fit: ContentFit, - style: fn(&Theme, Status) -> Appearance, + style: Style<Theme>, } impl<Theme> Svg<Theme> { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into<Handle>) -> Self where - Theme: Style, + Style<Theme>: 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<Theme> Svg<Theme> { #[must_use] pub fn from_path(path: impl Into<PathBuf>) -> Self where - Theme: Style, + Style<Theme>: 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<Color>, } -/// 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<Theme>(fn(&Theme, Status) -> 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(|_, _| Appearance::default()) + } } -impl Style for Theme { - fn style() -> fn(&Self, Status) -> Appearance { - |_, _| Appearance::default() +impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) } } |