From 71b9b3c3b1242dd571170fb0f8dd22760a1b4c53 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Mar 2024 13:41:14 +0100 Subject: Use closures for `Svg::style` --- examples/svg/src/main.rs | 12 ++++++------ widget/src/helpers.rs | 6 ++++-- widget/src/svg.rs | 38 +++++++++++++++++++++----------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/examples/svg/src/main.rs b/examples/svg/src/main.rs index 4e238048..0870dce4 100644 --- a/examples/svg/src/main.rs +++ b/examples/svg/src/main.rs @@ -41,12 +41,12 @@ impl Sandbox for Tiger { )); let svg = svg(handle).width(Length::Fill).height(Length::Fill).style( - if self.apply_color_filter { - |_theme, _status| svg::Appearance { - color: Some(color!(0x0000ff)), - } - } else { - |_theme, _status| svg::Appearance::default() + |_theme, _status| svg::Appearance { + color: if self.apply_color_filter { + Some(color!(0x0000ff)) + } else { + None + }, }, ); diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index e30b2829..91d5ee80 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -362,9 +362,11 @@ pub fn image(handle: impl Into) -> crate::Image { /// [`Svg`]: crate::Svg /// [`Handle`]: crate::svg::Handle #[cfg(feature = "svg")] -pub fn svg(handle: impl Into) -> crate::Svg +pub fn svg<'a, Theme>( + handle: impl Into, +) -> crate::Svg<'a, Theme> where - Theme: crate::svg::DefaultStyle, + Theme: crate::svg::DefaultStyle + 'a, { crate::Svg::new(handle) } diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 6e61d27a..1ac07ade 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -20,26 +20,26 @@ pub use crate::core::svg::Handle; /// [`Svg`] images can have a considerable rendering cost when resized, /// specially when they are complex. #[allow(missing_debug_implementations)] -pub struct Svg { +pub struct Svg<'a, Theme = crate::Theme> { handle: Handle, width: Length, height: Length, content_fit: ContentFit, - style: Style, + style: Style<'a, Theme>, } -impl Svg { +impl<'a, Theme> Svg<'a, Theme> { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -48,7 +48,7 @@ impl Svg { #[must_use] pub fn from_path(path: impl Into) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self::new(Handle::from_path(path)) } @@ -80,13 +80,17 @@ impl Svg { /// Sets the style variant of this [`Svg`]. #[must_use] - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } } -impl Widget for Svg +impl<'a, Message, Theme, Renderer> Widget + for Svg<'a, Theme> where Renderer: svg::Renderer, { @@ -182,13 +186,13 @@ where } } -impl<'a, Message, Theme, Renderer> From> +impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Theme: 'a, Renderer: svg::Renderer + 'a, { - fn from(icon: Svg) -> Element<'a, Message, Theme, Renderer> { + fn from(icon: Svg<'a, Theme>) -> Element<'a, Message, Theme, Renderer> { Element::new(icon) } } @@ -214,22 +218,22 @@ pub struct Appearance { } /// The style of an [`Svg`]. -pub type Style = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box Appearance + 'a>; /// The default style of an [`Svg`]. pub trait DefaultStyle { /// Returns the default style of an [`Svg`]. - fn default_style() -> Style; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style { - |_theme, _status| Appearance::default() + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default() } } impl DefaultStyle for Appearance { - fn default_style() -> Style { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } -- cgit