From 69bc1df252382a662228c8b0da6f60358e90f376 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 11:36:33 +0100 Subject: Simplify theming for `Svg` widget --- widget/src/svg.rs | 75 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 21 deletions(-) (limited to 'widget/src/svg.rs') diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 12ef3d92..a0402288 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -5,13 +5,13 @@ use crate::core::renderer; use crate::core::svg; use crate::core::widget::Tree; use crate::core::{ - ContentFit, Element, Layout, Length, Rectangle, Size, Vector, Widget, + Color, ContentFit, Element, Layout, Length, Rectangle, Size, Vector, Widget, }; +use crate::style::Theme; use std::path::PathBuf; -pub use crate::style::svg::{Appearance, StyleSheet}; -pub use svg::Handle; +pub use crate::core::svg::Handle; /// A vector graphics image. /// @@ -20,36 +20,36 @@ pub use svg::Handle; /// [`Svg`] images can have a considerable rendering cost when resized, /// specially when they are complex. #[allow(missing_debug_implementations)] -pub struct Svg -where - Theme: StyleSheet, -{ +pub struct Svg { handle: Handle, width: Length, height: Length, content_fit: ContentFit, - style: ::Style, + style: fn(&Theme, Status) -> Appearance, } -impl Svg -where - Theme: StyleSheet, -{ +impl Svg { /// Creates a new [`Svg`] from the given [`Handle`]. - pub fn new(handle: impl Into) -> Self { + pub fn new(handle: impl Into) -> Self + where + Theme: Style, + { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Default::default(), + style: Theme::style(), } } /// Creates a new [`Svg`] that will display the contents of the file at the /// provided path. #[must_use] - pub fn from_path(path: impl Into) -> Self { + pub fn from_path(path: impl Into) -> Self + where + Theme: Style, + { Self::new(Handle::from_path(path)) } @@ -80,7 +80,7 @@ where /// Sets the style variant of this [`Svg`]. #[must_use] - pub fn style(mut self, style: impl Into) -> Self { + pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { self.style = style.into(); self } @@ -88,7 +88,6 @@ where impl Widget for Svg where - Theme: iced_style::svg::StyleSheet, Renderer: svg::Renderer, { fn size(&self) -> Size { @@ -158,12 +157,14 @@ where ..bounds }; - let appearance = if is_mouse_over { - theme.hovered(&self.style) + let status = if is_mouse_over { + Status::Hovered } else { - theme.appearance(&self.style) + Status::Idle }; + let appearance = (self.style)(theme, status); + renderer.draw( self.handle.clone(), appearance.color, @@ -184,10 +185,42 @@ where impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where - Theme: iced_style::svg::StyleSheet + 'a, + Theme: 'a, Renderer: svg::Renderer + 'a, { fn from(icon: Svg) -> Element<'a, Message, Theme, Renderer> { Element::new(icon) } } + +/// The possible status of an [`Svg`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Status { + /// The [`Svg`] is idle. + Idle, + /// The [`Svg`] is being hovered. + Hovered, +} + +/// The appearance of an [`Svg`]. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct Appearance { + /// The [`Color`] filter of an [`Svg`]. + /// + /// Useful for coloring a symbolic icon. + /// + /// `None` keeps the original color. + 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; +} + +impl Style for Theme { + fn style() -> fn(&Self, Status) -> Appearance { + |_, _| Appearance::default() + } +} -- cgit 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/svg.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/svg.rs') 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 From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/svg.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/svg.rs') diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 8ac5a1cf..c80fa6b1 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -81,7 +81,7 @@ 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.into(); + self.style = Style(style); self } } -- cgit From 905f2160e6eb7504f52d9bd62c7bfa42c8ec2902 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:14:41 +0100 Subject: Move `Theme` type to `iced_core` --- widget/src/svg.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/svg.rs') diff --git a/widget/src/svg.rs b/widget/src/svg.rs index c80fa6b1..34fd9a7b 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -5,9 +5,9 @@ use crate::core::renderer; use crate::core::svg; use crate::core::widget::Tree; use crate::core::{ - Color, ContentFit, Element, Layout, Length, Rectangle, Size, Vector, Widget, + Color, ContentFit, Element, Layout, Length, Rectangle, Size, Theme, Vector, + Widget, }; -use crate::style::Theme; use std::path::PathBuf; -- cgit From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/svg.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'widget/src/svg.rs') diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 34fd9a7b..6e61d27a 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -32,14 +32,14 @@ impl Svg { /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into) -> Self where - Style: Default, + Theme: DefaultStyle, { Svg { handle: handle.into(), width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, - style: Style::default(), + style: Theme::default_style(), } } @@ -48,7 +48,7 @@ impl Svg { #[must_use] pub fn from_path(path: impl Into) -> Self where - Style: Default, + Theme: DefaultStyle, { Self::new(Handle::from_path(path)) } @@ -81,7 +81,7 @@ 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(style); + self.style = style; self } } @@ -163,7 +163,7 @@ where Status::Idle }; - let appearance = (self.style.0)(theme, status); + let appearance = (self.style)(theme, status); renderer.draw( self.handle.clone(), @@ -214,25 +214,22 @@ pub struct Appearance { } /// The style of an [`Svg`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(fn(&Theme, Status) -> Appearance); +pub type Style = fn(&Theme, Status) -> Appearance; -impl Clone for Style { - fn clone(&self) -> Self { - *self - } +/// The default style of an [`Svg`]. +pub trait DefaultStyle { + /// Returns the default style of an [`Svg`]. + fn default_style() -> Style; } -impl Copy for Style {} - -impl Default for Style { - fn default() -> Self { - Style(|_, _| Appearance::default()) +impl DefaultStyle for Theme { + fn default_style() -> Style { + |_theme, _status| Appearance::default() } } -impl From Appearance> for Style { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style { + |appearance, _status| *appearance } } -- cgit