diff options
author | 2024-03-06 11:36:33 +0100 | |
---|---|---|
committer | 2024-03-06 11:36:49 +0100 | |
commit | 69bc1df252382a662228c8b0da6f60358e90f376 (patch) | |
tree | 6365e00118e78f24f2f4c308358360477be6258a /widget | |
parent | 40af65c3aa4a96281db8f642cfa1641479314d27 (diff) | |
download | iced-69bc1df252382a662228c8b0da6f60358e90f376.tar.gz iced-69bc1df252382a662228c8b0da6f60358e90f376.tar.bz2 iced-69bc1df252382a662228c8b0da6f60358e90f376.zip |
Simplify theming for `Svg` widget
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/helpers.rs | 2 | ||||
-rw-r--r-- | widget/src/svg.rs | 75 |
2 files changed, 55 insertions, 22 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 8cb03691..60e7d34f 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -371,7 +371,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::StyleSheet, + Theme: crate::svg::Style, { crate::Svg::new(handle) } 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<Theme = crate::Theme> -where - Theme: StyleSheet, -{ +pub struct Svg<Theme = crate::Theme> { handle: Handle, width: Length, height: Length, content_fit: ContentFit, - style: <Theme as StyleSheet>::Style, + style: fn(&Theme, Status) -> Appearance, } -impl<Theme> Svg<Theme> -where - Theme: StyleSheet, -{ +impl<Theme> Svg<Theme> { /// Creates a new [`Svg`] from the given [`Handle`]. - pub fn new(handle: impl Into<Handle>) -> Self { + pub fn new(handle: impl Into<Handle>) -> 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<PathBuf>) -> Self { + pub fn from_path(path: impl Into<PathBuf>) -> 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<Theme::Style>) -> Self { + pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { self.style = style.into(); self } @@ -88,7 +88,6 @@ where impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Svg<Theme> where - Theme: iced_style::svg::StyleSheet, Renderer: svg::Renderer, { fn size(&self) -> Size<Length> { @@ -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<Svg<Theme>> for Element<'a, Message, Theme, Renderer> where - Theme: iced_style::svg::StyleSheet + 'a, + Theme: 'a, Renderer: svg::Renderer + 'a, { fn from(icon: Svg<Theme>) -> 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<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; +} + +impl Style for Theme { + fn style() -> fn(&Self, Status) -> Appearance { + |_, _| Appearance::default() + } +} |