diff options
author | 2022-12-06 05:15:16 +0100 | |
---|---|---|
committer | 2022-12-06 05:15:16 +0100 | |
commit | f38e7fcac2e3505d11577ade1757a77ca2a544ea (patch) | |
tree | 84b3dc6c88aa555cf931482adcf4e7be35614b72 /native | |
parent | 28f0beee52f258af6bbaf8a0d9867863d7513c9b (diff) | |
parent | f99d24e0850b63194b7976ec66d547ea2ff6bfc8 (diff) | |
download | iced-f38e7fcac2e3505d11577ade1757a77ca2a544ea.tar.gz iced-f38e7fcac2e3505d11577ade1757a77ca2a544ea.tar.bz2 iced-f38e7fcac2e3505d11577ade1757a77ca2a544ea.zip |
Merge pull request #1578 from iced-rs/svg-styling
Svg styling
Diffstat (limited to 'native')
-rw-r--r-- | native/src/svg.rs | 6 | ||||
-rw-r--r-- | native/src/widget/helpers.rs | 8 | ||||
-rw-r--r-- | native/src/widget/svg.rs | 54 |
3 files changed, 54 insertions, 14 deletions
diff --git a/native/src/svg.rs b/native/src/svg.rs index a8e481d2..2168e409 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -1,5 +1,5 @@ //! Load and draw vector graphics. -use crate::{Hasher, Rectangle, Size}; +use crate::{Color, Hasher, Rectangle, Size}; use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; @@ -84,6 +84,6 @@ pub trait Renderer: crate::Renderer { /// Returns the default dimensions of an SVG for the given [`Handle`]. fn dimensions(&self, handle: &Handle) -> Size<u32>; - /// Draws an SVG with the given [`Handle`] and inside the provided `bounds`. - fn draw(&mut self, handle: Handle, bounds: Rectangle); + /// Draws an SVG with the given [`Handle`], an optional [`Color`] filter, and inside the provided `bounds`. + fn draw(&mut self, handle: Handle, color: Option<Color>, bounds: Rectangle); } diff --git a/native/src/widget/helpers.rs b/native/src/widget/helpers.rs index 3bce9e60..0bde288f 100644 --- a/native/src/widget/helpers.rs +++ b/native/src/widget/helpers.rs @@ -285,6 +285,12 @@ where /// /// [`Svg`]: widget::Svg /// [`Handle`]: widget::svg::Handle -pub fn svg(handle: impl Into<widget::svg::Handle>) -> widget::Svg { +pub fn svg<Renderer>( + handle: impl Into<widget::svg::Handle>, +) -> widget::Svg<Renderer> +where + Renderer: crate::svg::Renderer, + Renderer::Theme: widget::svg::StyleSheet, +{ widget::Svg::new(handle) } diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs index 1015ed0a..f83f5acf 100644 --- a/native/src/widget/svg.rs +++ b/native/src/widget/svg.rs @@ -9,6 +9,7 @@ use crate::{ use std::path::PathBuf; +pub use iced_style::svg::{Appearance, StyleSheet}; pub use svg::Handle; /// A vector graphics image. @@ -17,15 +18,24 @@ pub use svg::Handle; /// /// [`Svg`] images can have a considerable rendering cost when resized, /// specially when they are complex. -#[derive(Debug, Clone)] -pub struct Svg { +#[allow(missing_debug_implementations)] +pub struct Svg<Renderer> +where + Renderer: svg::Renderer, + Renderer::Theme: StyleSheet, +{ handle: Handle, width: Length, height: Length, content_fit: ContentFit, + style: <Renderer::Theme as StyleSheet>::Style, } -impl Svg { +impl<Renderer> Svg<Renderer> +where + Renderer: svg::Renderer, + Renderer::Theme: StyleSheet, +{ /// Creates a new [`Svg`] from the given [`Handle`]. pub fn new(handle: impl Into<Handle>) -> Self { Svg { @@ -33,22 +43,26 @@ impl Svg { width: Length::Fill, height: Length::Shrink, content_fit: ContentFit::Contain, + style: Default::default(), } } /// 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 { Self::new(Handle::from_path(path)) } /// Sets the width of the [`Svg`]. + #[must_use] pub fn width(mut self, width: Length) -> Self { self.width = width; self } /// Sets the height of the [`Svg`]. + #[must_use] pub fn height(mut self, height: Length) -> Self { self.height = height; self @@ -57,17 +71,29 @@ impl Svg { /// Sets the [`ContentFit`] of the [`Svg`]. /// /// Defaults to [`ContentFit::Contain`] + #[must_use] pub fn content_fit(self, content_fit: ContentFit) -> Self { Self { content_fit, ..self } } + + /// Sets the style variant of this [`Svg`]. + #[must_use] + pub fn style( + mut self, + style: <Renderer::Theme as StyleSheet>::Style, + ) -> Self { + self.style = style; + self + } } -impl<Message, Renderer> Widget<Message, Renderer> for Svg +impl<Message, Renderer> Widget<Message, Renderer> for Svg<Renderer> where Renderer: svg::Renderer, + Renderer::Theme: iced_style::svg::StyleSheet, { fn width(&self) -> Length { self.width @@ -114,7 +140,7 @@ where &self, _state: &Tree, renderer: &mut Renderer, - _theme: &Renderer::Theme, + theme: &Renderer::Theme, _style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, @@ -138,7 +164,13 @@ where ..bounds }; - renderer.draw(self.handle.clone(), drawing_bounds + offset) + let appearance = theme.appearance(&self.style); + + renderer.draw( + self.handle.clone(), + appearance.color, + drawing_bounds + offset, + ); }; if adjusted_fit.width > bounds.width @@ -146,16 +178,18 @@ where { renderer.with_layer(bounds, render); } else { - render(renderer) + render(renderer); } } } -impl<'a, Message, Renderer> From<Svg> for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From<Svg<Renderer>> + for Element<'a, Message, Renderer> where - Renderer: svg::Renderer, + Renderer: svg::Renderer + 'a, + Renderer::Theme: iced_style::svg::StyleSheet, { - fn from(icon: Svg) -> Element<'a, Message, Renderer> { + fn from(icon: Svg<Renderer>) -> Element<'a, Message, Renderer> { Element::new(icon) } } |