From 314b0f7dc52c844669c224060a7b03e842762370 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 16 Nov 2022 17:43:16 +0100 Subject: chore(examples): Add svg-style example --- examples/svg_style/Cargo.toml | 10 +++ examples/svg_style/resources/go-next-symbolic.svg | 1 + examples/svg_style/src/main.rs | 78 +++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 examples/svg_style/Cargo.toml create mode 100644 examples/svg_style/resources/go-next-symbolic.svg create mode 100644 examples/svg_style/src/main.rs (limited to 'examples') diff --git a/examples/svg_style/Cargo.toml b/examples/svg_style/Cargo.toml new file mode 100644 index 00000000..9ecda7c4 --- /dev/null +++ b/examples/svg_style/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "svg_style" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +iced = { path = "../..", features = ["svg"] } +iced_style = { path = "../../style" } \ No newline at end of file diff --git a/examples/svg_style/resources/go-next-symbolic.svg b/examples/svg_style/resources/go-next-symbolic.svg new file mode 100644 index 00000000..79c456b7 --- /dev/null +++ b/examples/svg_style/resources/go-next-symbolic.svg @@ -0,0 +1 @@ + diff --git a/examples/svg_style/src/main.rs b/examples/svg_style/src/main.rs new file mode 100644 index 00000000..905e1d86 --- /dev/null +++ b/examples/svg_style/src/main.rs @@ -0,0 +1,78 @@ +use iced::widget::{container, svg}; +use iced::{Color, Element, Length, Sandbox, Settings}; +use iced_style::svg::Appearance; +use iced_style::theme::{self, Theme}; + +pub fn main() -> iced::Result { + SvgStyleExample::run(Settings::default()) +} + +struct SvgStyleExample; + +impl Sandbox for SvgStyleExample { + type Message = (); + + fn new() -> Self { + SvgStyleExample + } + + fn theme(&self) -> Theme { + Theme::Light + } + + fn title(&self) -> String { + String::from("SVG - Iced") + } + + fn update(&mut self, _message: ()) {} + + fn view(&self) -> Element<()> { + let svg1: Element<_> = svg(svg::Handle::from_path(format!( + "{}/resources/go-next-symbolic.svg", + env!("CARGO_MANIFEST_DIR") + ))) + .width(Length::Fill) + .height(Length::Fill) + .into(); + + let svg2: Element<_> = svg(svg::Handle::from_path(format!( + "{}/resources/go-next-symbolic.svg", + env!("CARGO_MANIFEST_DIR") + ))) + .style(theme::Svg::Custom(|_theme| Appearance { + fill: Some(Color { + r: 0.0, + g: 0.28627452, + b: 0.42745098, + a: 1.0, + }), + })) + .width(Length::Fill) + .height(Length::Fill) + .into(); + + let svg3: Element<_> = svg(svg::Handle::from_path(format!( + "{}/resources/go-next-symbolic.svg", + env!("CARGO_MANIFEST_DIR") + ))) + .style(theme::Svg::Custom(|_theme| Appearance { + fill: Some(Color { + r: 0.5803922, + g: 0.92156863, + b: 0.92156863, + a: 1.0, + }), + })) + .width(Length::Fill) + .height(Length::Fill) + .into(); + + container(iced::widget::row!(svg1, svg2, svg3)) + .width(Length::Fill) + .height(Length::Fill) + .padding(20) + .center_x() + .center_y() + .into() + } +} -- 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. --- examples/svg_style/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/svg_style/src/main.rs b/examples/svg_style/src/main.rs index 905e1d86..0a1fa039 100644 --- a/examples/svg_style/src/main.rs +++ b/examples/svg_style/src/main.rs @@ -39,8 +39,8 @@ impl Sandbox for SvgStyleExample { "{}/resources/go-next-symbolic.svg", env!("CARGO_MANIFEST_DIR") ))) - .style(theme::Svg::Custom(|_theme| Appearance { - fill: Some(Color { + .style(theme::Svg::custom_fn(|_theme| Appearance { + color: Some(Color { r: 0.0, g: 0.28627452, b: 0.42745098, @@ -55,8 +55,8 @@ impl Sandbox for SvgStyleExample { "{}/resources/go-next-symbolic.svg", env!("CARGO_MANIFEST_DIR") ))) - .style(theme::Svg::Custom(|_theme| Appearance { - fill: Some(Color { + .style(theme::Svg::custom_fn(|_theme| Appearance { + color: Some(Color { r: 0.5803922, g: 0.92156863, b: 0.92156863, -- cgit From 1220ce55bc882cc4fef891c8b2d0fdc22e18a015 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Dec 2022 04:35:08 +0100 Subject: Showcase color filtering in existing `svg` example ... and remove the `svg_style` example --- examples/svg/src/main.rs | 71 ++++++++++++++++----- examples/svg_style/Cargo.toml | 10 --- examples/svg_style/resources/go-next-symbolic.svg | 1 - examples/svg_style/src/main.rs | 78 ----------------------- 4 files changed, 54 insertions(+), 106 deletions(-) delete mode 100644 examples/svg_style/Cargo.toml delete mode 100644 examples/svg_style/resources/go-next-symbolic.svg delete mode 100644 examples/svg_style/src/main.rs (limited to 'examples') diff --git a/examples/svg/src/main.rs b/examples/svg/src/main.rs index 27d175da..4dc92416 100644 --- a/examples/svg/src/main.rs +++ b/examples/svg/src/main.rs @@ -1,39 +1,76 @@ -use iced::widget::{container, svg}; -use iced::{Element, Length, Sandbox, Settings}; +use iced::theme; +use iced::widget::{checkbox, column, container, svg}; +use iced::{color, Element, Length, Sandbox, Settings}; pub fn main() -> iced::Result { Tiger::run(Settings::default()) } -struct Tiger; +#[derive(Debug, Default)] +struct Tiger { + apply_color_filter: bool, +} + +#[derive(Debug, Clone, Copy)] +pub enum Message { + ToggleColorFilter(bool), +} impl Sandbox for Tiger { - type Message = (); + type Message = Message; fn new() -> Self { - Tiger + Tiger::default() } fn title(&self) -> String { String::from("SVG - Iced") } - fn update(&mut self, _message: ()) {} + fn update(&mut self, message: Self::Message) { + match message { + Message::ToggleColorFilter(apply_color_filter) => { + self.apply_color_filter = apply_color_filter; + } + } + } - fn view(&self) -> Element<()> { - let svg = svg(svg::Handle::from_path(format!( + fn view(&self) -> Element { + let handle = svg::Handle::from_path(format!( "{}/resources/tiger.svg", env!("CARGO_MANIFEST_DIR") - ))) - .width(Length::Fill) - .height(Length::Fill); + )); + + let svg = svg(handle).width(Length::Fill).height(Length::Fill).style( + if self.apply_color_filter { + theme::Svg::custom_fn(|_theme| svg::Appearance { + color: Some(color!(0x0000ff)), + }) + } else { + theme::Svg::Default + }, + ); - container(svg) + let apply_color_filter = checkbox( + "Apply a color filter", + self.apply_color_filter, + Message::ToggleColorFilter, + ); + + container( + column![ + svg, + container(apply_color_filter).width(Length::Fill).center_x() + ] + .spacing(20) .width(Length::Fill) - .height(Length::Fill) - .padding(20) - .center_x() - .center_y() - .into() + .height(Length::Fill), + ) + .width(Length::Fill) + .height(Length::Fill) + .padding(20) + .center_x() + .center_y() + .into() } } diff --git a/examples/svg_style/Cargo.toml b/examples/svg_style/Cargo.toml deleted file mode 100644 index 9ecda7c4..00000000 --- a/examples/svg_style/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "svg_style" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -iced = { path = "../..", features = ["svg"] } -iced_style = { path = "../../style" } \ No newline at end of file diff --git a/examples/svg_style/resources/go-next-symbolic.svg b/examples/svg_style/resources/go-next-symbolic.svg deleted file mode 100644 index 79c456b7..00000000 --- a/examples/svg_style/resources/go-next-symbolic.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/examples/svg_style/src/main.rs b/examples/svg_style/src/main.rs deleted file mode 100644 index 0a1fa039..00000000 --- a/examples/svg_style/src/main.rs +++ /dev/null @@ -1,78 +0,0 @@ -use iced::widget::{container, svg}; -use iced::{Color, Element, Length, Sandbox, Settings}; -use iced_style::svg::Appearance; -use iced_style::theme::{self, Theme}; - -pub fn main() -> iced::Result { - SvgStyleExample::run(Settings::default()) -} - -struct SvgStyleExample; - -impl Sandbox for SvgStyleExample { - type Message = (); - - fn new() -> Self { - SvgStyleExample - } - - fn theme(&self) -> Theme { - Theme::Light - } - - fn title(&self) -> String { - String::from("SVG - Iced") - } - - fn update(&mut self, _message: ()) {} - - fn view(&self) -> Element<()> { - let svg1: Element<_> = svg(svg::Handle::from_path(format!( - "{}/resources/go-next-symbolic.svg", - env!("CARGO_MANIFEST_DIR") - ))) - .width(Length::Fill) - .height(Length::Fill) - .into(); - - let svg2: Element<_> = svg(svg::Handle::from_path(format!( - "{}/resources/go-next-symbolic.svg", - env!("CARGO_MANIFEST_DIR") - ))) - .style(theme::Svg::custom_fn(|_theme| Appearance { - color: Some(Color { - r: 0.0, - g: 0.28627452, - b: 0.42745098, - a: 1.0, - }), - })) - .width(Length::Fill) - .height(Length::Fill) - .into(); - - let svg3: Element<_> = svg(svg::Handle::from_path(format!( - "{}/resources/go-next-symbolic.svg", - env!("CARGO_MANIFEST_DIR") - ))) - .style(theme::Svg::custom_fn(|_theme| Appearance { - color: Some(Color { - r: 0.5803922, - g: 0.92156863, - b: 0.92156863, - a: 1.0, - }), - })) - .width(Length::Fill) - .height(Length::Fill) - .into(); - - container(iced::widget::row!(svg1, svg2, svg3)) - .width(Length::Fill) - .height(Length::Fill) - .padding(20) - .center_x() - .center_y() - .into() - } -} -- cgit