diff options
author | 2024-03-12 13:41:14 +0100 | |
---|---|---|
committer | 2024-03-12 13:41:14 +0100 | |
commit | 71b9b3c3b1242dd571170fb0f8dd22760a1b4c53 (patch) | |
tree | 54a88a33d54eca8ce5919b793e1fcefe8e901341 | |
parent | 66dce4865eb00f7cc7b75e64cd8a96a496916285 (diff) | |
download | iced-71b9b3c3b1242dd571170fb0f8dd22760a1b4c53.tar.gz iced-71b9b3c3b1242dd571170fb0f8dd22760a1b4c53.tar.bz2 iced-71b9b3c3b1242dd571170fb0f8dd22760a1b4c53.zip |
Use closures for `Svg::style`
-rw-r--r-- | examples/svg/src/main.rs | 12 | ||||
-rw-r--r-- | widget/src/helpers.rs | 6 | ||||
-rw-r--r-- | 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>(handle: impl Into<Handle>) -> crate::Image<Handle> { /// [`Svg`]: crate::Svg /// [`Handle`]: crate::svg::Handle #[cfg(feature = "svg")] -pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme> +pub fn svg<'a, Theme>( + handle: impl Into<core::svg::Handle>, +) -> 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<Theme = crate::Theme> { +pub struct Svg<'a, Theme = crate::Theme> { handle: Handle, width: Length, height: Length, content_fit: ContentFit, - style: Style<Theme>, + style: Style<'a, Theme>, } -impl<Theme> Svg<Theme> { +impl<'a, Theme> Svg<'a, Theme> { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into<Handle>) -> 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<Theme> Svg<Theme> { #[must_use] pub fn from_path(path: impl Into<PathBuf>) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { Self::new(Handle::from_path(path)) } @@ -80,13 +80,17 @@ impl<Theme> Svg<Theme> { /// 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<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Svg<Theme> +impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> + for Svg<'a, Theme> where Renderer: svg::Renderer, { @@ -182,13 +186,13 @@ where } } -impl<'a, Message, Theme, Renderer> From<Svg<Theme>> +impl<'a, Message, Theme, Renderer> From<Svg<'a, Theme>> for Element<'a, Message, Theme, Renderer> where Theme: 'a, Renderer: svg::Renderer + 'a, { - fn from(icon: Svg<Theme>) -> 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<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; /// The default style of an [`Svg`]. pub trait DefaultStyle { /// Returns the default style of an [`Svg`]. - fn default_style() -> Style<Self>; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style<Self> { - |_theme, _status| Appearance::default() + fn default_style(&self, _status: Status) -> Appearance { + Appearance::default() } } impl DefaultStyle for Appearance { - fn default_style() -> Style<Self> { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } |