From 75ae0de9bdd3376b6e537bf59030059c926114ee Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 16 Nov 2022 17:42:41 +0100 Subject: feat: SVG styling with icon fill color --- style/src/lib.rs | 1 + style/src/svg.rs | 21 +++++++++++++++++++++ style/src/theme.rs | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 style/src/svg.rs (limited to 'style/src') diff --git a/style/src/lib.rs b/style/src/lib.rs index 3242602c..59eb1eb8 100644 --- a/style/src/lib.rs +++ b/style/src/lib.rs @@ -32,6 +32,7 @@ pub mod radio; pub mod rule; pub mod scrollable; pub mod slider; +pub mod svg; pub mod text; pub mod text_input; pub mod theme; diff --git a/style/src/svg.rs b/style/src/svg.rs new file mode 100644 index 00000000..66791d04 --- /dev/null +++ b/style/src/svg.rs @@ -0,0 +1,21 @@ +//! Change the appearance of a svg. + +use iced_core::Color; + +/// The appearance of a svg. +#[derive(Debug, Default, Clone, Copy)] +pub struct Appearance { + /// Changes the fill color + /// + /// Useful for coloring a symbolic icon. + pub fill: Option, +} + +/// The stylesheet of a svg. +pub trait StyleSheet { + /// The supported style of the [`StyleSheet`]. + type Style: Default + Copy; + + /// Produces the [`Appearance`] of the svg. + fn appearance(&self, style: Self::Style) -> Appearance; +} diff --git a/style/src/theme.rs b/style/src/theme.rs index dde0df5d..d825b086 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -16,6 +16,7 @@ use crate::radio; use crate::rule; use crate::scrollable; use crate::slider; +use crate::svg; use crate::text; use crate::text_input; use crate::toggler; @@ -797,6 +798,29 @@ impl From rule::Appearance> for Rule { } } +/** + * SVG + */ +#[derive(Default, Clone, Copy)] +pub enum Svg { + /// No filtering to the rendered SVG. + #[default] + Default, + /// Apply custom filtering to the SVG. + Custom(fn(&Theme) -> svg::Appearance), +} + +impl svg::StyleSheet for Theme { + type Style = Svg; + + fn appearance(&self, style: Self::Style) -> svg::Appearance { + match style { + Svg::Default => Default::default(), + Svg::Custom(appearance) => appearance(self), + } + } +} + impl rule::StyleSheet for Theme { type Style = Rule; -- cgit From b205a663471a8170d7b30cc59894425c09bea563 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Dec 2022 04:34:00 +0100 Subject: Remove `appearance` from `Handle` ... and pass it directly to `Renderer::draw` instead. --- style/src/svg.rs | 12 ++++++----- style/src/theme.rs | 61 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 28 deletions(-) (limited to 'style/src') diff --git a/style/src/svg.rs b/style/src/svg.rs index 66791d04..9378c1a7 100644 --- a/style/src/svg.rs +++ b/style/src/svg.rs @@ -2,20 +2,22 @@ use iced_core::Color; -/// The appearance of a svg. +/// The appearance of an SVG. #[derive(Debug, Default, Clone, Copy)] pub struct Appearance { - /// Changes the fill color + /// The [`Color`] filter of an SVG. /// /// Useful for coloring a symbolic icon. - pub fill: Option, + /// + /// `None` keeps the original color. + pub color: Option, } /// The stylesheet of a svg. pub trait StyleSheet { /// The supported style of the [`StyleSheet`]. - type Style: Default + Copy; + type Style: Default; /// Produces the [`Appearance`] of the svg. - fn appearance(&self, style: Self::Style) -> Appearance; + fn appearance(&self, style: &Self::Style) -> Appearance; } diff --git a/style/src/theme.rs b/style/src/theme.rs index d825b086..d2b583ed 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -798,29 +798,6 @@ impl From rule::Appearance> for Rule { } } -/** - * SVG - */ -#[derive(Default, Clone, Copy)] -pub enum Svg { - /// No filtering to the rendered SVG. - #[default] - Default, - /// Apply custom filtering to the SVG. - Custom(fn(&Theme) -> svg::Appearance), -} - -impl svg::StyleSheet for Theme { - type Style = Svg; - - fn appearance(&self, style: Self::Style) -> svg::Appearance { - match style { - Svg::Default => Default::default(), - Svg::Custom(appearance) => appearance(self), - } - } -} - impl rule::StyleSheet for Theme { type Style = Rule; @@ -847,6 +824,44 @@ impl rule::StyleSheet for fn(&Theme) -> rule::Appearance { } } +/** + * SVG + */ +#[derive(Default)] +pub enum Svg { + /// No filtering to the rendered SVG. + #[default] + Default, + /// A custom style. + Custom(Box>), +} + +impl Svg { + /// Creates a custom [`Svg`] style. + pub fn custom_fn(f: fn(&Theme) -> svg::Appearance) -> Self { + Self::Custom(Box::new(f)) + } +} + +impl svg::StyleSheet for Theme { + type Style = Svg; + + fn appearance(&self, style: &Self::Style) -> svg::Appearance { + match style { + Svg::Default => Default::default(), + Svg::Custom(custom) => custom.appearance(self), + } + } +} + +impl svg::StyleSheet for fn(&Theme) -> svg::Appearance { + type Style = Theme; + + fn appearance(&self, style: &Self::Style) -> svg::Appearance { + (self)(style) + } +} + /// The style of a scrollable. #[derive(Default)] pub enum Scrollable { -- cgit From f99d24e0850b63194b7976ec66d547ea2ff6bfc8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Dec 2022 04:44:37 +0100 Subject: Fix casing in `theme` --- style/src/theme.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'style/src') diff --git a/style/src/theme.rs b/style/src/theme.rs index d2b583ed..271d9a29 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -825,7 +825,7 @@ impl rule::StyleSheet for fn(&Theme) -> rule::Appearance { } /** - * SVG + * Svg */ #[derive(Default)] pub enum Svg { -- cgit