From 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- examples/arc/src/main.rs | 14 ++-- examples/clock/src/main.rs | 54 +++++++++------ examples/geometry/src/main.rs | 41 ++++++----- examples/modern_art/Cargo.toml | 10 +++ examples/modern_art/src/main.rs | 141 ++++++++++++++++++++++++++++++++++++++ examples/solar_system/src/main.rs | 13 ++-- 6 files changed, 220 insertions(+), 53 deletions(-) create mode 100644 examples/modern_art/Cargo.toml create mode 100644 examples/modern_art/src/main.rs (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 0c619dc9..6029a69c 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -52,11 +52,6 @@ impl Application for Arc { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(10)) - .map(|_| Message::Tick) - } - fn view(&self) -> Element { Canvas::new(self) .width(Length::Fill) @@ -67,6 +62,11 @@ impl Application for Arc { fn theme(&self) -> Theme { Theme::Dark } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(10)) + .map(|_| Message::Tick) + } } impl canvas::Program for Arc { @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - color: palette.text, + style: StrokeStyle::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8818fb54..51f25a3f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,7 @@ use iced::executor; -use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke}; +use iced::widget::canvas::{ + Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, +}; use iced::widget::{canvas, container}; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -24,9 +26,9 @@ enum Message { } impl Application for Clock { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -59,15 +61,6 @@ impl Application for Clock { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(500)).map(|_| { - Message::Tick( - time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - ) - }) - } - fn view(&self) -> Element { let canvas = canvas(self as &Self) .width(Length::Fill) @@ -79,6 +72,15 @@ impl Application for Clock { .padding(20) .into() } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(500)).map(|_| { + Message::Tick( + time::OffsetDateTime::now_local() + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), + ) + }) + } } impl canvas::Program for Clock { @@ -104,33 +106,41 @@ impl canvas::Program for Clock { let long_hand = Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius)); - let thin_stroke = Stroke { - width: radius / 100.0, - color: Color::WHITE, - line_cap: LineCap::Round, - ..Stroke::default() + let width = radius / 100.0; + + let thin_stroke = || -> Stroke { + Stroke { + width, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; - let wide_stroke = Stroke { - width: thin_stroke.width * 3.0, - ..thin_stroke + let wide_stroke = || -> Stroke { + Stroke { + width: width * 3.0, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; frame.translate(Vector::new(center.x, center.y)); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.hour(), 12)); - frame.stroke(&short_hand, wide_stroke); + frame.stroke(&short_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.minute(), 60)); - frame.stroke(&long_hand, wide_stroke); + frame.stroke(&long_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.second(), 60)); - frame.stroke(&long_hand, thin_stroke); + frame.stroke(&long_hand, thin_stroke()); }) }); diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index d8b99ab3..a8ce26f8 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -1,6 +1,9 @@ //! This example showcases a simple native custom widget that renders using //! arbitrary low-level geometry. +//! +//TODO need to update this now that vertex data doesn't contain color data mod rainbow { + use iced::Color; // For now, to implement a custom native widget you will need to add // `iced_native` and `iced_wgpu` to your dependencies. // @@ -12,6 +15,7 @@ mod rainbow { // implemented by `iced_wgpu` and other renderers. use iced_graphics::renderer::{self, Renderer}; use iced_graphics::{Backend, Primitive}; + use iced_graphics::shader::Shader; use iced_native::widget::{self, Widget}; use iced_native::{ @@ -63,20 +67,20 @@ mod rainbow { cursor_position: Point, _viewport: &Rectangle, ) { - use iced_graphics::triangle::{Mesh2D, Vertex2D}; + use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; use iced_native::Renderer as _; let b = layout.bounds(); // R O Y G B I V - let color_r = [1.0, 0.0, 0.0, 1.0]; - let color_o = [1.0, 0.5, 0.0, 1.0]; - let color_y = [1.0, 1.0, 0.0, 1.0]; - let color_g = [0.0, 1.0, 0.0, 1.0]; - let color_gb = [0.0, 1.0, 0.5, 1.0]; - let color_b = [0.0, 0.2, 1.0, 1.0]; - let color_i = [0.5, 0.0, 1.0, 1.0]; - let color_v = [0.75, 0.0, 0.5, 1.0]; + // let color_r = [1.0, 0.0, 0.0, 1.0]; + // let color_o = [1.0, 0.5, 0.0, 1.0]; + // let color_y = [1.0, 1.0, 0.0, 1.0]; + // let color_g = [0.0, 1.0, 0.0, 1.0]; + // let color_gb = [0.0, 1.0, 0.5, 1.0]; + // let color_b = [0.0, 0.2, 1.0, 1.0]; + // let color_i = [0.5, 0.0, 1.0, 1.0]; + // let color_v = [0.75, 0.0, 0.5, 1.0]; let posn_center = { if b.contains(cursor_position) { @@ -101,39 +105,39 @@ mod rainbow { vertices: vec![ Vertex2D { position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], + // color: [1.0, 1.0, 1.0, 1.0], }, Vertex2D { position: posn_tl, - color: color_r, + // color: color_r, }, Vertex2D { position: posn_t, - color: color_o, + // color: color_o, }, Vertex2D { position: posn_tr, - color: color_y, + // color: color_y, }, Vertex2D { position: posn_r, - color: color_g, + // color: color_g, }, Vertex2D { position: posn_br, - color: color_gb, + // color: color_gb, }, Vertex2D { position: posn_b, - color: color_b, + // color: color_b, }, Vertex2D { position: posn_bl, - color: color_i, + // color: color_i, }, Vertex2D { position: posn_l, - color: color_v, + // color: color_v, }, ], indices: vec![ @@ -147,6 +151,7 @@ mod rainbow { 0, 8, 1, // L ], }, + shader: Shader::Solid(Color::BLACK), }; renderer.with_translation(Vector::new(b.x, b.y), |renderer| { diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml new file mode 100644 index 00000000..4995e9a7 --- /dev/null +++ b/examples/modern_art/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "modern_art" +version = "0.1.0" +authors = ["Bingus "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs new file mode 100644 index 00000000..17149cbe --- /dev/null +++ b/examples/modern_art/src/main.rs @@ -0,0 +1,141 @@ +use rand::{Rng, thread_rng}; +use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::{canvas, Canvas}; +use iced::Settings; +use iced::{ + executor, Application, Color, Command, Element, Length, Point, Rectangle, + Renderer, Size, Theme, +}; + +fn main() -> iced::Result { + ModernArt::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +#[derive(Debug, Clone, Copy)] +enum Message {} + +struct ModernArt { + cache: Cache, +} + +impl Application for ModernArt { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + ModernArt { + cache: Default::default(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Modern Art") + } + + fn update(&mut self, _message: Self::Message) -> Command { + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + Canvas::new(self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } +} + +impl canvas::Program for ModernArt { + type State = (); + + fn draw( + &self, + _state: &Self::State, + _theme: &Theme, + bounds: Rectangle, + _cursor: Cursor, + ) -> Vec { + let geometry = self.cache.draw(bounds.size(), |frame| { + let num_squares = thread_rng().gen_range(0..1200); + + let mut i = 0; + while i <= num_squares { + generate_box(frame, bounds.size()); + i += 1; + } + }); + + vec![geometry] + } +} + +fn generate_box(frame: &mut Frame, bounds: Size) -> bool { + let solid = rand::random::(); + + let random_color = || -> Color { + Color::from_rgb( + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + ) + }; + + let gradient = |top_left: Point, bottom_right: Point| -> Gradient { + let mut builder = Gradient::linear(top_left, bottom_right); + let stops = thread_rng().gen_range(1..64u32); + + let mut i = 0; + while i <= stops { + builder = builder.add_stop( + i as f32 / stops as f32, + random_color() + ); + i += 1; + } + + builder.build().unwrap() + }; + + let top_left = Point::new( + thread_rng().gen_range(0.0..bounds.width), + thread_rng().gen_range(0.0..bounds.height) + ); + + let size = Size::new( + thread_rng().gen_range(50.0..200.0), + thread_rng().gen_range(50.0..200.0), + ); + + if solid { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Solid(random_color()), + .. Default::default() + } + ); + } else { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Gradient(&gradient( + top_left, + Point::new(top_left.x + size.width, top_left.y + size.height) + )), + .. Default::default() + } + ); + }; + + solid +} \ No newline at end of file diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index c59d73a8..fcd20561 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::StrokeStyle; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -37,9 +38,9 @@ enum Message { } impl Application for SolarSystem { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -65,10 +66,6 @@ impl Application for SolarSystem { Command::none() } - fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(10)).map(Message::Tick) - } - fn view(&self) -> Element { canvas(&self.state) .width(Length::Fill) @@ -86,6 +83,10 @@ impl Application for SolarSystem { text_color: Color::WHITE, }) } + + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(10)).map(Message::Tick) + } } #[derive(Debug)] @@ -178,8 +179,8 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { + style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, - color: Color::from_rgba8(0, 153, 255, 0.1), line_dash: canvas::LineDash { offset: 0, segments: &[3.0, 6.0], -- cgit From e25f3d3dea2d2c1ba7d2778948443df2745084be Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 17:13:32 -0700 Subject: Fixed issue where OpenGL would not render both shaders at once under certain circumstances. --- examples/modern_art/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 17149cbe..7b04b581 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -90,7 +90,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let gradient = |top_left: Point, bottom_right: Point| -> Gradient { let mut builder = Gradient::linear(top_left, bottom_right); - let stops = thread_rng().gen_range(1..64u32); + let stops = thread_rng().gen_range(1..10u32); let mut i = 0; while i <= stops { -- cgit From 5d0fffc626928177239336757507b986b081b878 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 30 Sep 2022 10:27:00 -0700 Subject: Fixed some importing issues since you can use a Shader::Gradient outside a Canvas widget, where it was previously only accessible. --- examples/modern_art/Cargo.toml | 1 + examples/modern_art/src/main.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml index 4995e9a7..8242f7e4 100644 --- a/examples/modern_art/Cargo.toml +++ b/examples/modern_art/Cargo.toml @@ -7,4 +7,5 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +iced_graphics = { path = "../../graphics" } rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 7b04b581..c7945012 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use crate::canvas::{Cursor, FillStyle, Geometry}; use iced::widget::canvas::{Cache, Fill, Frame}; use iced::widget::{canvas, Canvas}; use iced::Settings; @@ -7,6 +7,7 @@ use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; +use iced_graphics::gradient::Gradient; fn main() -> iced::Result { ModernArt::run(Settings { -- cgit From 6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 4 Oct 2022 18:24:46 -0700 Subject: Reworked wgpu buffers, updated glow side to have proper transform location storage, attempting to fix visibility modifiers, implemented some of the feedback received in initial PR. --- examples/arc/src/main.rs | 4 ++-- examples/clock/src/main.rs | 6 +++--- examples/modern_art/src/main.rs | 7 ++++--- examples/solar_system/src/main.rs | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 6029a69c..bc7e49c6 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, Style, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - style: StrokeStyle::Solid(palette.text), + style: Style::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 51f25a3f..06ed44f0 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, + Cache, Cursor, Geometry, LineCap, Path, Stroke, Style, }; use iced::widget::{canvas, container}; use iced::{ @@ -111,7 +111,7 @@ impl canvas::Program for Clock { let thin_stroke = || -> Stroke { Stroke { width, - style: StrokeStyle::Solid(Color::WHITE), + style: Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } @@ -120,7 +120,7 @@ impl canvas::Program for Clock { let wide_stroke = || -> Stroke { Stroke { width: width * 3.0, - style: StrokeStyle::Solid(Color::WHITE), + style: Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index c7945012..238c9a0f 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, FillStyle, Geometry}; +use crate::canvas::{Cursor, Geometry}; use iced::widget::canvas::{Cache, Fill, Frame}; use iced::widget::{canvas, Canvas}; use iced::Settings; @@ -8,6 +8,7 @@ use iced::{ Renderer, Size, Theme, }; use iced_graphics::gradient::Gradient; +use iced_graphics::widget::canvas::Style; fn main() -> iced::Result { ModernArt::run(Settings { @@ -120,7 +121,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: FillStyle::Solid(random_color()), + style: Style::Solid(random_color()), .. Default::default() } ); @@ -129,7 +130,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: FillStyle::Gradient(&gradient( + style: Style::Gradient(&gradient( top_left, Point::new(top_left.x + size.width, top_left.y + size.height) )), diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index fcd20561..8d713ce0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,7 +19,7 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::StrokeStyle; +use crate::canvas::Style; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -179,7 +179,7 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, line_dash: canvas::LineDash { offset: 0, -- cgit From 30432cbade3d9b25c4df62656a7494db3f4ea82a Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 10:49:58 -0700 Subject: Readjusted namespaces, removed Geometry example as it's no longer relevant. --- examples/arc/src/main.rs | 4 +- examples/geometry/Cargo.toml | 11 -- examples/geometry/README.md | 18 ---- examples/geometry/src/main.rs | 222 ---------------------------------------- examples/modern_art/src/main.rs | 9 +- 5 files changed, 6 insertions(+), 258 deletions(-) delete mode 100644 examples/geometry/Cargo.toml delete mode 100644 examples/geometry/README.md delete mode 100644 examples/geometry/src/main.rs (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index bc7e49c6..7b6ea0e1 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, Style, + self, stroke, Cache, Canvas, Cursor, Geometry, Path, Stroke, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - style: Style::Solid(palette.text), + style: stroke::Style::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/geometry/Cargo.toml b/examples/geometry/Cargo.toml deleted file mode 100644 index 22ede0e0..00000000 --- a/examples/geometry/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "geometry" -version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../.." } -iced_native = { path = "../../native" } -iced_graphics = { path = "../../graphics" } diff --git a/examples/geometry/README.md b/examples/geometry/README.md deleted file mode 100644 index 4d5c81cb..00000000 --- a/examples/geometry/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Geometry - -A custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../../wgpu). - -The __[`main`]__ file contains all the code of the example. - - - -You can run it with `cargo run`: -``` -cargo run --package geometry -``` - -[`main`]: src/main.rs diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs deleted file mode 100644 index a8ce26f8..00000000 --- a/examples/geometry/src/main.rs +++ /dev/null @@ -1,222 +0,0 @@ -//! This example showcases a simple native custom widget that renders using -//! arbitrary low-level geometry. -//! -//TODO need to update this now that vertex data doesn't contain color data -mod rainbow { - use iced::Color; - // For now, to implement a custom native widget you will need to add - // `iced_native` and `iced_wgpu` to your dependencies. - // - // Then, you simply need to define your widget type and implement the - // `iced_native::Widget` trait with the `iced_wgpu::Renderer`. - // - // Of course, you can choose to make the implementation renderer-agnostic, - // if you wish to, by creating your own `Renderer` trait, which could be - // implemented by `iced_wgpu` and other renderers. - use iced_graphics::renderer::{self, Renderer}; - use iced_graphics::{Backend, Primitive}; - use iced_graphics::shader::Shader; - - use iced_native::widget::{self, Widget}; - use iced_native::{ - layout, Element, Layout, Length, Point, Rectangle, Size, Vector, - }; - - #[derive(Default)] - pub struct Rainbow; - - impl Rainbow { - pub fn new() -> Self { - Self - } - } - - pub fn rainbow() -> Rainbow { - Rainbow - } - - impl Widget> for Rainbow - where - B: Backend, - { - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Shrink - } - - fn layout( - &self, - _renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let size = limits.width(Length::Fill).resolve(Size::ZERO); - - layout::Node::new(Size::new(size.width, size.width)) - } - - fn draw( - &self, - _tree: &widget::Tree, - renderer: &mut Renderer, - _theme: &T, - _style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - ) { - use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; - use iced_native::Renderer as _; - - let b = layout.bounds(); - - // R O Y G B I V - // let color_r = [1.0, 0.0, 0.0, 1.0]; - // let color_o = [1.0, 0.5, 0.0, 1.0]; - // let color_y = [1.0, 1.0, 0.0, 1.0]; - // let color_g = [0.0, 1.0, 0.0, 1.0]; - // let color_gb = [0.0, 1.0, 0.5, 1.0]; - // let color_b = [0.0, 0.2, 1.0, 1.0]; - // let color_i = [0.5, 0.0, 1.0, 1.0]; - // let color_v = [0.75, 0.0, 0.5, 1.0]; - - let posn_center = { - if b.contains(cursor_position) { - [cursor_position.x - b.x, cursor_position.y - b.y] - } else { - [b.width / 2.0, b.height / 2.0] - } - }; - - let posn_tl = [0.0, 0.0]; - let posn_t = [b.width / 2.0, 0.0]; - let posn_tr = [b.width, 0.0]; - let posn_r = [b.width, b.height / 2.0]; - let posn_br = [b.width, b.height]; - let posn_b = [(b.width / 2.0), b.height]; - let posn_bl = [0.0, b.height]; - let posn_l = [0.0, b.height / 2.0]; - - let mesh = Primitive::Mesh2D { - size: b.size(), - buffers: Mesh2D { - vertices: vec![ - Vertex2D { - position: posn_center, - // color: [1.0, 1.0, 1.0, 1.0], - }, - Vertex2D { - position: posn_tl, - // color: color_r, - }, - Vertex2D { - position: posn_t, - // color: color_o, - }, - Vertex2D { - position: posn_tr, - // color: color_y, - }, - Vertex2D { - position: posn_r, - // color: color_g, - }, - Vertex2D { - position: posn_br, - // color: color_gb, - }, - Vertex2D { - position: posn_b, - // color: color_b, - }, - Vertex2D { - position: posn_bl, - // color: color_i, - }, - Vertex2D { - position: posn_l, - // color: color_v, - }, - ], - indices: vec![ - 0, 1, 2, // TL - 0, 2, 3, // T - 0, 3, 4, // TR - 0, 4, 5, // R - 0, 5, 6, // BR - 0, 6, 7, // B - 0, 7, 8, // BL - 0, 8, 1, // L - ], - }, - shader: Shader::Solid(Color::BLACK), - }; - - renderer.with_translation(Vector::new(b.x, b.y), |renderer| { - renderer.draw_primitive(mesh); - }); - } - } - - impl<'a, Message, B, T> From for Element<'a, Message, Renderer> - where - B: Backend, - { - fn from(rainbow: Rainbow) -> Self { - Self::new(rainbow) - } - } -} - -use iced::widget::{column, container, scrollable}; -use iced::{Alignment, Element, Length, Sandbox, Settings}; -use rainbow::rainbow; - -pub fn main() -> iced::Result { - Example::run(Settings::default()) -} - -struct Example; - -impl Sandbox for Example { - type Message = (); - - fn new() -> Self { - Example - } - - fn title(&self) -> String { - String::from("Custom 2D geometry - Iced") - } - - fn update(&mut self, _: ()) {} - - fn view(&self) -> Element<()> { - let content = column![ - rainbow(), - "In this example we draw a custom widget Rainbow, using \ - the Mesh2D primitive. This primitive supplies a list of \ - triangles, expressed as vertices and indices.", - "Move your cursor over it, and see the center vertex \ - follow you!", - "Every Vertex2D defines its own color. You could use the \ - Mesh2D primitive to render virtually any two-dimensional \ - geometry for your widget.", - ] - .padding(20) - .spacing(20) - .max_width(500) - .align_items(Alignment::Start); - - let scrollable = - scrollable(container(content).width(Length::Fill).center_x()); - - container(scrollable) - .width(Length::Fill) - .height(Length::Fill) - .center_y() - .into() - } -} diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 238c9a0f..14e117b3 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,14 +1,13 @@ use rand::{Rng, thread_rng}; use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::canvas::{Cache, Fill, Frame, Gradient}; use iced::widget::{canvas, Canvas}; use iced::Settings; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; -use iced_graphics::gradient::Gradient; -use iced_graphics::widget::canvas::Style; +use iced_graphics::widget::canvas::fill; fn main() -> iced::Result { ModernArt::run(Settings { @@ -121,7 +120,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: Style::Solid(random_color()), + style: fill::Style::Solid(random_color()), .. Default::default() } ); @@ -130,7 +129,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: Style::Gradient(&gradient( + style: fill::Style::Gradient(&gradient( top_left, Point::new(top_left.x + size.width, top_left.y + size.height) )), -- cgit From 1eb8d972ba60592da7bfc27fe7ec80138e64dd7b Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 16:07:43 -0700 Subject: Reduced memory transfer of OpenGL gradient uniform upload. Rearranged gradient uniforms on OpenGL side to be more performant. --- examples/modern_art/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 14e117b3..a7aa2d8a 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -91,7 +91,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let gradient = |top_left: Point, bottom_right: Point| -> Gradient { let mut builder = Gradient::linear(top_left, bottom_right); - let stops = thread_rng().gen_range(1..10u32); + let stops = thread_rng().gen_range(1..15u32); let mut i = 0; while i <= stops { -- cgit From cb7c4676543cd508dfae8d4dcbd9cc8b61b1a94e Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 07:28:05 -0700 Subject: Fixed lint issues & cleaned up some documentation. --- examples/clock/src/main.rs | 6 +++--- examples/modern_art/Cargo.toml | 3 +-- examples/modern_art/src/main.rs | 5 ++--- examples/solar_system/src/main.rs | 7 +++---- 4 files changed, 9 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 06ed44f0..be614201 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, Style, + Cache, Cursor, Geometry, LineCap, Path, Stroke, stroke, }; use iced::widget::{canvas, container}; use iced::{ @@ -111,7 +111,7 @@ impl canvas::Program for Clock { let thin_stroke = || -> Stroke { Stroke { width, - style: Style::Solid(Color::WHITE), + style: stroke::Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } @@ -120,7 +120,7 @@ impl canvas::Program for Clock { let wide_stroke = || -> Stroke { Stroke { width: width * 3.0, - style: Style::Solid(Color::WHITE), + style: stroke::Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml index 8242f7e4..a48361ae 100644 --- a/examples/modern_art/Cargo.toml +++ b/examples/modern_art/Cargo.toml @@ -7,5 +7,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -iced_graphics = { path = "../../graphics" } -rand = "0.8.5" \ No newline at end of file +rand = "0.8.5" diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index a7aa2d8a..6a22b27f 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,13 +1,12 @@ use rand::{Rng, thread_rng}; use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame, Gradient}; +use iced::widget::canvas::{Cache, Fill, Frame, Gradient, fill}; use iced::widget::{canvas, Canvas}; use iced::Settings; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; -use iced_graphics::widget::canvas::fill; fn main() -> iced::Result { ModernArt::run(Settings { @@ -139,4 +138,4 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { }; solid -} \ No newline at end of file +} diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8d713ce0..5f90245c 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,7 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke}; +use iced::widget::canvas::{Cursor, Path, Stroke, stroke}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -19,7 +19,6 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::Style; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -85,7 +84,7 @@ impl Application for SolarSystem { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(10)).map(Message::Tick) + time::every(time::Duration::from_millis(10)).map(Message::Tick) } } @@ -179,7 +178,7 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: stroke::Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, line_dash: canvas::LineDash { offset: 0, -- cgit From 9c7bf417ac9c1ac72bcc55aa3cd5e8eb962243a2 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 16:57:38 -0700 Subject: Added support for gradients to respect current frame transform. --- examples/modern_art/src/main.rs | 33 ++++++++++++++++---------------- examples/solar_system/src/main.rs | 40 ++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 36 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 6a22b27f..0601fc44 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,12 +1,11 @@ -use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame, Gradient, fill}; -use iced::widget::{canvas, Canvas}; -use iced::Settings; +use iced::widget::canvas::{ + self, fill, Cache, Canvas, Cursor, Fill, Frame, Geometry, Gradient, +}; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, - Renderer, Size, Theme, + Renderer, Size, Theme, Settings }; +use rand::{thread_rng, Rng}; fn main() -> iced::Result { ModernArt::run(Settings { @@ -41,7 +40,7 @@ impl Application for ModernArt { String::from("Modern Art") } - fn update(&mut self, _message: Self::Message) -> Command { + fn update(&mut self, _message: Message) -> Command { Command::none() } @@ -94,10 +93,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let mut i = 0; while i <= stops { - builder = builder.add_stop( - i as f32 / stops as f32, - random_color() - ); + builder = builder.add_stop(i as f32 / stops as f32, random_color()); i += 1; } @@ -106,7 +102,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let top_left = Point::new( thread_rng().gen_range(0.0..bounds.width), - thread_rng().gen_range(0.0..bounds.height) + thread_rng().gen_range(0.0..bounds.height), ); let size = Size::new( @@ -120,8 +116,8 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { size, Fill { style: fill::Style::Solid(random_color()), - .. Default::default() - } + ..Default::default() + }, ); } else { frame.fill_rectangle( @@ -130,10 +126,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { Fill { style: fill::Style::Gradient(&gradient( top_left, - Point::new(top_left.x + size.width, top_left.y + size.height) + Point::new( + top_left.x + size.width, + top_left.y + size.height, + ), )), - .. Default::default() - } + ..Default::default() + }, ); }; diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 5f90245c..73ddb01e 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -10,14 +10,14 @@ use iced::application; use iced::executor; use iced::theme::{self, Theme}; use iced::time; -use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, stroke}; +use iced::widget::canvas::{self, stroke, Cursor, Path, Stroke}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector, }; +use crate::canvas::{fill, Fill, Gradient}; use std::time::Instant; pub fn main() -> iced::Result { @@ -178,7 +178,9 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: stroke::Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: stroke::Style::Solid(Color::from_rgba8( + 0, 153, 255, 0.1, + )), width: 1.0, line_dash: canvas::LineDash { offset: 0, @@ -198,15 +200,23 @@ impl canvas::Program for State { frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0)); let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); - let shadow = Path::rectangle( - Point::new(0.0, -Self::EARTH_RADIUS), - Size::new( - Self::EARTH_RADIUS * 4.0, - Self::EARTH_RADIUS * 2.0, - ), - ); - frame.fill(&earth, Color::from_rgb8(0x6B, 0x93, 0xD6)); + let earth_fill = Gradient::linear( + Point::new(-Self::EARTH_RADIUS, 0.0), + Point::new(Self::EARTH_RADIUS, 0.0), + ) + .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) + .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) + .build() + .unwrap(); + + frame.fill( + &earth, + Fill { + style: fill::Style::Gradient(&earth_fill), + ..Default::default() + }, + ); frame.with_save(|frame| { frame.rotate(rotation * 10.0); @@ -215,14 +225,6 @@ impl canvas::Program for State { let moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS); frame.fill(&moon, Color::WHITE); }); - - frame.fill( - &shadow, - Color { - a: 0.7, - ..Color::BLACK - }, - ); }); }); -- cgit From 72feba51bed41db0bc04b43167d5d3b43007fd44 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 19:13:40 -0700 Subject: Fixed some imports/documentation. --- examples/solar_system/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 73ddb01e..6f38f480 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -10,14 +10,14 @@ use iced::application; use iced::executor; use iced::theme::{self, Theme}; use iced::time; -use iced::widget::canvas::{self, stroke, Cursor, Path, Stroke}; +use iced::widget::canvas; +use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector, }; -use crate::canvas::{fill, Fill, Gradient}; use std::time::Instant; pub fn main() -> iced::Result { -- cgit From 12a87c54eb68b992676060c80e518ffb29445cfc Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 11:41:50 -0700 Subject: Added support for relative positioning of gradient fills. Addressed some PR feedback. --- examples/modern_art/src/main.rs | 41 +++++++++++++++++++++++---------------- examples/solar_system/src/main.rs | 7 +++++-- 2 files changed, 29 insertions(+), 19 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 0601fc44..362e4ad1 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use iced::widget::canvas::{ - self, fill, Cache, Canvas, Cursor, Fill, Frame, Geometry, Gradient, + self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, Position, Location }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, @@ -76,6 +76,20 @@ impl canvas::Program for ModernArt { } } +fn random_direction() -> Location { + match thread_rng().gen_range(0..8) { + 0 => Location::TopLeft, + 1 => Location::Top, + 2 => Location::TopRight, + 3 => Location::Right, + 4 => Location::BottomRight, + 5 => Location::Bottom, + 6 => Location::BottomLeft, + 7 => Location::Left, + _ => Location::TopLeft + } +} + fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let solid = rand::random::(); @@ -87,8 +101,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { ) }; - let gradient = |top_left: Point, bottom_right: Point| -> Gradient { - let mut builder = Gradient::linear(top_left, bottom_right); + let gradient = |top_left: Point, size: Size| -> Gradient { + let mut builder = Gradient::linear(Position::Relative { + top_left, + size, + start: random_direction(), + end: random_direction() + }); let stops = thread_rng().gen_range(1..15u32); let mut i = 0; @@ -114,25 +133,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { frame.fill_rectangle( top_left, size, - Fill { - style: fill::Style::Solid(random_color()), - ..Default::default() - }, + random_color(), ); } else { frame.fill_rectangle( top_left, size, - Fill { - style: fill::Style::Gradient(&gradient( - top_left, - Point::new( - top_left.x + size.width, - top_left.y + size.height, - ), - )), - ..Default::default() - }, + &gradient(top_left, size), ); }; diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 6f38f480..9200391b 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::Position; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -202,8 +203,10 @@ impl canvas::Program for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); let earth_fill = Gradient::linear( - Point::new(-Self::EARTH_RADIUS, 0.0), - Point::new(Self::EARTH_RADIUS, 0.0), + Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0) + } ) .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) -- cgit From 3e600fe7754645ffdfca595060273b1e96c9a162 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 13:10:37 -0700 Subject: Adjusted reexports for clarity. --- examples/modern_art/src/main.rs | 2 +- examples/solar_system/src/main.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 362e4ad1..fc203cff 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, Position, Location + self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, gradient::Position, gradient::Location }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 9200391b..10154621 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,7 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke}; +use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke, gradient::Position}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -19,7 +19,6 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::Position; pub fn main() -> iced::Result { SolarSystem::run(Settings { -- cgit From a4a1262fa2d625be5ad7a37e409e0e9c399b09b6 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 16:28:13 -0700 Subject: Fixed import issue with canvas in the gradient mod for situations where canvas feature is not enabled. --- examples/solar_system/src/main.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 10154621..b638c6d0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,7 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke, gradient::Position}; +use iced::widget::canvas::{Cursor, Path, Stroke, Gradient, stroke, gradient::Position}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -212,13 +212,7 @@ impl canvas::Program for State { .build() .unwrap(); - frame.fill( - &earth, - Fill { - style: fill::Style::Gradient(&earth_fill), - ..Default::default() - }, - ); + frame.fill(&earth, &earth_fill); frame.with_save(|frame| { frame.rotate(rotation * 10.0); -- cgit From e28c441c693ce9f42d60c98ba6892c6fc78d6724 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 2 Nov 2022 17:05:58 -0700 Subject: Update pane_grid example w/ maximize / restore --- examples/pane_grid/src/main.rs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index ae8fa22b..c9f1376c 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -29,6 +29,8 @@ enum Message { Dragged(pane_grid::DragEvent), Resized(pane_grid::ResizeEvent), TogglePin(pane_grid::Pane), + Maximize(pane_grid::Pane), + Restore, Close(pane_grid::Pane), CloseFocused, } @@ -114,6 +116,10 @@ impl Application for Example { *is_pinned = !*is_pinned; } } + Message::Maximize(pane) => self.panes.maximize(&pane), + Message::Restore => { + self.panes.restore(); + } Message::Close(pane) => { if let Some((_, sibling)) = self.panes.close(&pane) { self.focus = Some(sibling); @@ -157,7 +163,7 @@ impl Application for Example { let focus = self.focus; let total_panes = self.panes.len(); - let pane_grid = PaneGrid::new(&self.panes, |id, pane| { + let pane_grid = PaneGrid::new(&self.panes, |id, pane, is_maximized| { let is_focused = focus == Some(id); let pin_button = button( @@ -178,7 +184,12 @@ impl Application for Example { .spacing(5); let title_bar = pane_grid::TitleBar::new(title) - .controls(view_controls(id, total_panes, pane.is_pinned)) + .controls(view_controls( + id, + total_panes, + pane.is_pinned, + is_maximized, + )) .padding(10) .style(if is_focused { style::title_bar_focused @@ -314,16 +325,35 @@ fn view_controls<'a>( pane: pane_grid::Pane, total_panes: usize, is_pinned: bool, + is_maximized: bool, ) -> Element<'a, Message> { - let mut button = button(text("Close").size(14)) + let mut row = row![].spacing(5); + + if total_panes > 1 { + let toggle = { + let (content, message) = if is_maximized { + ("Restore", Message::Restore) + } else { + ("Maximize", Message::Maximize(pane)) + }; + button(text(content).size(14)) + .style(theme::Button::Secondary) + .padding(3) + .on_press(message) + }; + + row = row.push(toggle); + } + + let mut close = button(text("Close").size(14)) .style(theme::Button::Destructive) .padding(3); if total_panes > 1 && !is_pinned { - button = button.on_press(Message::Close(pane)); + close = close.on_press(Message::Close(pane)); } - button.into() + row.push(close).into() } mod style { -- cgit From 50eb9e34b8ea939c263c1f548ef3f228400d4bda Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 5 Aug 2022 15:40:55 -0700 Subject: add example --- examples/cached/Cargo.toml | 11 ++++ examples/cached/src/main.rs | 141 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 examples/cached/Cargo.toml create mode 100644 examples/cached/src/main.rs (limited to 'examples') diff --git a/examples/cached/Cargo.toml b/examples/cached/Cargo.toml new file mode 100644 index 00000000..21f59886 --- /dev/null +++ b/examples/cached/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pure_cached" +version = "0.1.0" +authors = ["Nick Senger "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } +iced_native = { path = "../../native" } +iced_lazy = { path = "../../lazy" } diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs new file mode 100644 index 00000000..d7787979 --- /dev/null +++ b/examples/cached/src/main.rs @@ -0,0 +1,141 @@ +use iced::widget::{ + button, column, horizontal_rule, horizontal_space, row, scrollable, text, + text_input, +}; +use iced::{Element, Sandbox}; +use iced::{Length, Settings}; +use iced_lazy::Cached; + +use std::collections::HashSet; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Hash)] +enum SortOrder { + Ascending, + Descending, +} + +impl std::fmt::Display for SortOrder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Ascending => "Ascending", + Self::Descending => "Descending", + } + ) + } +} + +struct App { + options: HashSet, + input: String, + sort_order: SortOrder, +} + +impl Default for App { + fn default() -> Self { + Self { + options: ["Foo", "Bar", "Baz", "Qux", "Corge", "Waldo", "Fred"] + .into_iter() + .map(ToString::to_string) + .collect(), + input: Default::default(), + sort_order: SortOrder::Ascending, + } + } +} + +#[derive(Debug, Clone)] +enum Message { + InputChanged(String), + ToggleSortOrder, + DeleteOption(String), + AddOption(String), +} + +impl Sandbox for App { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Cached - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::InputChanged(input) => { + self.input = input; + } + Message::ToggleSortOrder => { + self.sort_order = match self.sort_order { + SortOrder::Ascending => SortOrder::Descending, + SortOrder::Descending => SortOrder::Ascending, + } + } + Message::AddOption(option) => { + self.options.insert(option); + self.input.clear(); + } + Message::DeleteOption(option) => { + self.options.remove(&option); + } + } + } + + fn view(&self) -> Element { + let options = + Cached::new((&self.sort_order, self.options.len()), || { + let mut options = self.options.iter().collect::>(); + options.sort_by(|a, b| match self.sort_order { + SortOrder::Ascending => { + a.to_lowercase().cmp(&b.to_lowercase()) + } + SortOrder::Descending => { + b.to_lowercase().cmp(&a.to_lowercase()) + } + }); + + options.into_iter().fold( + column![horizontal_rule(1)], + |column, option| { + column + .push(row![ + text(option), + horizontal_space(Length::Fill), + button("Delete").on_press( + Message::DeleteOption(option.to_string(),), + ) + ]) + .push(horizontal_rule(1)) + }, + ) + }); + + scrollable( + column![ + button(text(format!( + "Toggle Sort Order ({})", + self.sort_order + ))) + .on_press(Message::ToggleSortOrder), + options, + text_input( + "Add a new option", + &self.input, + Message::InputChanged, + ) + .on_submit(Message::AddOption(self.input.clone())), + ] + .spacing(20), + ) + .into() + } +} -- cgit From 4f83500bb8f522504a7ec0875a6f134eac733175 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:31:04 +0100 Subject: Rename `pure_cached` example to `cached` --- examples/cached/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/cached/Cargo.toml b/examples/cached/Cargo.toml index 21f59886..4d4013e6 100644 --- a/examples/cached/Cargo.toml +++ b/examples/cached/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "pure_cached" +name = "cached" version = "0.1.0" authors = ["Nick Senger "] edition = "2021" -- cgit From 1fb84ae5d3732ed51b35fb5419a5ad014e22ca5b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:32:23 +0100 Subject: Remove `iced_native` dependency from `cached` example --- examples/cached/Cargo.toml | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/cached/Cargo.toml b/examples/cached/Cargo.toml index 4d4013e6..2c7edde2 100644 --- a/examples/cached/Cargo.toml +++ b/examples/cached/Cargo.toml @@ -7,5 +7,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["debug"] } -iced_native = { path = "../../native" } iced_lazy = { path = "../../lazy" } -- cgit From 0e295be8917a543e3641f8c4657db87fed0ce91b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:32:38 +0100 Subject: Move declaration of `SortOrder` in `cached` example --- examples/cached/src/main.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'examples') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index d7787979..b900ff36 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -12,25 +12,6 @@ pub fn main() -> iced::Result { App::run(Settings::default()) } -#[derive(Hash)] -enum SortOrder { - Ascending, - Descending, -} - -impl std::fmt::Display for SortOrder { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - Self::Ascending => "Ascending", - Self::Descending => "Descending", - } - ) - } -} - struct App { options: HashSet, input: String, @@ -139,3 +120,22 @@ impl Sandbox for App { .into() } } + +#[derive(Debug, Hash)] +enum SortOrder { + Ascending, + Descending, +} + +impl std::fmt::Display for SortOrder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Ascending => "Ascending", + Self::Descending => "Descending", + } + ) + } +} -- cgit From 0478df9fd61c67255b0ea213aa83f56f5698ae7d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:35:05 +0100 Subject: Add `padding` to main `column` in `cached` example --- examples/cached/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index b900ff36..85a4a4f0 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -115,7 +115,8 @@ impl Sandbox for App { ) .on_submit(Message::AddOption(self.input.clone())), ] - .spacing(20), + .spacing(20) + .padding(20), ) .into() } -- cgit From adf541d4325df22d577342f870f0f95fa357797a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:40:51 +0100 Subject: Improve layout of `cached` example --- examples/cached/src/main.rs | 59 ++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'examples') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 85a4a4f0..39364dc9 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -1,9 +1,8 @@ +use iced::theme; use iced::widget::{ - button, column, horizontal_rule, horizontal_space, row, scrollable, text, - text_input, + button, column, horizontal_space, row, scrollable, text, text_input, }; -use iced::{Element, Sandbox}; -use iced::{Length, Settings}; +use iced::{Element, Length, Sandbox, Settings}; use iced_lazy::Cached; use std::collections::HashSet; @@ -74,7 +73,8 @@ impl Sandbox for App { fn view(&self) -> Element { let options = Cached::new((&self.sort_order, self.options.len()), || { - let mut options = self.options.iter().collect::>(); + let mut options: Vec<_> = self.options.iter().collect(); + options.sort_by(|a, b| match self.sort_order { SortOrder::Ascending => { a.to_lowercase().cmp(&b.to_lowercase()) @@ -84,40 +84,45 @@ impl Sandbox for App { } }); - options.into_iter().fold( - column![horizontal_rule(1)], - |column, option| { - column - .push(row![ + column( + options + .into_iter() + .map(|option| { + row![ text(option), horizontal_space(Length::Fill), - button("Delete").on_press( - Message::DeleteOption(option.to_string(),), - ) - ]) - .push(horizontal_rule(1)) - }, + button("Delete") + .on_press(Message::DeleteOption( + option.to_string(), + ),) + .style(theme::Button::Destructive) + ] + .into() + }) + .collect(), ) + .spacing(10) }); - scrollable( - column![ - button(text(format!( - "Toggle Sort Order ({})", - self.sort_order - ))) - .on_press(Message::ToggleSortOrder), - options, + column![ + scrollable(options).height(Length::Fill), + row![ text_input( "Add a new option", &self.input, Message::InputChanged, ) .on_submit(Message::AddOption(self.input.clone())), + button(text(format!( + "Toggle Sort Order ({})", + self.sort_order + ))) + .on_press(Message::ToggleSortOrder) ] - .spacing(20) - .padding(20), - ) + .spacing(10) + ] + .spacing(20) + .padding(20) .into() } } -- cgit From 1cdc1fcd0669bfea096237c07b32742c1a3f2158 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:46:31 +0100 Subject: Rename `iced_lazy::Cached` to `Lazy` :tada: --- examples/cached/src/main.rs | 59 +++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'examples') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 39364dc9..7c8b06f0 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -3,7 +3,7 @@ use iced::widget::{ button, column, horizontal_space, row, scrollable, text, text_input, }; use iced::{Element, Length, Sandbox, Settings}; -use iced_lazy::Cached; +use iced_lazy::lazy; use std::collections::HashSet; @@ -71,39 +71,36 @@ impl Sandbox for App { } fn view(&self) -> Element { - let options = - Cached::new((&self.sort_order, self.options.len()), || { - let mut options: Vec<_> = self.options.iter().collect(); + let options = lazy((&self.sort_order, self.options.len()), || { + let mut options: Vec<_> = self.options.iter().collect(); - options.sort_by(|a, b| match self.sort_order { - SortOrder::Ascending => { - a.to_lowercase().cmp(&b.to_lowercase()) - } - SortOrder::Descending => { - b.to_lowercase().cmp(&a.to_lowercase()) - } - }); - - column( - options - .into_iter() - .map(|option| { - row![ - text(option), - horizontal_space(Length::Fill), - button("Delete") - .on_press(Message::DeleteOption( - option.to_string(), - ),) - .style(theme::Button::Destructive) - ] - .into() - }) - .collect(), - ) - .spacing(10) + options.sort_by(|a, b| match self.sort_order { + SortOrder::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), + SortOrder::Descending => { + b.to_lowercase().cmp(&a.to_lowercase()) + } }); + column( + options + .into_iter() + .map(|option| { + row![ + text(option), + horizontal_space(Length::Fill), + button("Delete") + .on_press(Message::DeleteOption( + option.to_string(), + ),) + .style(theme::Button::Destructive) + ] + .into() + }) + .collect(), + ) + .spacing(10) + }); + column![ scrollable(options).height(Length::Fill), row![ -- cgit From 6efda2457e7b80e9d3d145ceb9910bfbb5af9994 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:47:43 +0100 Subject: Rename `SortOrder` to `Order` in `cached` example --- examples/cached/src/main.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'examples') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 7c8b06f0..8845b874 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -14,7 +14,7 @@ pub fn main() -> iced::Result { struct App { options: HashSet, input: String, - sort_order: SortOrder, + order: Order, } impl Default for App { @@ -25,7 +25,7 @@ impl Default for App { .map(ToString::to_string) .collect(), input: Default::default(), - sort_order: SortOrder::Ascending, + order: Order::Ascending, } } } @@ -33,7 +33,7 @@ impl Default for App { #[derive(Debug, Clone)] enum Message { InputChanged(String), - ToggleSortOrder, + ToggleOrder, DeleteOption(String), AddOption(String), } @@ -54,10 +54,10 @@ impl Sandbox for App { Message::InputChanged(input) => { self.input = input; } - Message::ToggleSortOrder => { - self.sort_order = match self.sort_order { - SortOrder::Ascending => SortOrder::Descending, - SortOrder::Descending => SortOrder::Ascending, + Message::ToggleOrder => { + self.order = match self.order { + Order::Ascending => Order::Descending, + Order::Descending => Order::Ascending, } } Message::AddOption(option) => { @@ -71,14 +71,12 @@ impl Sandbox for App { } fn view(&self) -> Element { - let options = lazy((&self.sort_order, self.options.len()), || { + let options = lazy((&self.order, self.options.len()), || { let mut options: Vec<_> = self.options.iter().collect(); - options.sort_by(|a, b| match self.sort_order { - SortOrder::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), - SortOrder::Descending => { - b.to_lowercase().cmp(&a.to_lowercase()) - } + options.sort_by(|a, b| match self.order { + Order::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), + Order::Descending => b.to_lowercase().cmp(&a.to_lowercase()), }); column( @@ -110,11 +108,8 @@ impl Sandbox for App { Message::InputChanged, ) .on_submit(Message::AddOption(self.input.clone())), - button(text(format!( - "Toggle Sort Order ({})", - self.sort_order - ))) - .on_press(Message::ToggleSortOrder) + button(text(format!("Toggle Order ({})", self.order))) + .on_press(Message::ToggleOrder) ] .spacing(10) ] @@ -125,12 +120,12 @@ impl Sandbox for App { } #[derive(Debug, Hash)] -enum SortOrder { +enum Order { Ascending, Descending, } -impl std::fmt::Display for SortOrder { +impl std::fmt::Display for Order { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, -- cgit From 415978b80771fdd065b65e115d1dcc6aaa9d792c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:55:22 +0100 Subject: Rename `cached` example to `lazy` --- examples/lazy/Cargo.toml | 10 ++++ examples/lazy/src/main.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 examples/lazy/Cargo.toml create mode 100644 examples/lazy/src/main.rs (limited to 'examples') diff --git a/examples/lazy/Cargo.toml b/examples/lazy/Cargo.toml new file mode 100644 index 00000000..79255c25 --- /dev/null +++ b/examples/lazy/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "lazy" +version = "0.1.0" +authors = ["Nick Senger "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } +iced_lazy = { path = "../../lazy" } diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs new file mode 100644 index 00000000..8845b874 --- /dev/null +++ b/examples/lazy/src/main.rs @@ -0,0 +1,139 @@ +use iced::theme; +use iced::widget::{ + button, column, horizontal_space, row, scrollable, text, text_input, +}; +use iced::{Element, Length, Sandbox, Settings}; +use iced_lazy::lazy; + +use std::collections::HashSet; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +struct App { + options: HashSet, + input: String, + order: Order, +} + +impl Default for App { + fn default() -> Self { + Self { + options: ["Foo", "Bar", "Baz", "Qux", "Corge", "Waldo", "Fred"] + .into_iter() + .map(ToString::to_string) + .collect(), + input: Default::default(), + order: Order::Ascending, + } + } +} + +#[derive(Debug, Clone)] +enum Message { + InputChanged(String), + ToggleOrder, + DeleteOption(String), + AddOption(String), +} + +impl Sandbox for App { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Cached - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::InputChanged(input) => { + self.input = input; + } + Message::ToggleOrder => { + self.order = match self.order { + Order::Ascending => Order::Descending, + Order::Descending => Order::Ascending, + } + } + Message::AddOption(option) => { + self.options.insert(option); + self.input.clear(); + } + Message::DeleteOption(option) => { + self.options.remove(&option); + } + } + } + + fn view(&self) -> Element { + let options = lazy((&self.order, self.options.len()), || { + let mut options: Vec<_> = self.options.iter().collect(); + + options.sort_by(|a, b| match self.order { + Order::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), + Order::Descending => b.to_lowercase().cmp(&a.to_lowercase()), + }); + + column( + options + .into_iter() + .map(|option| { + row![ + text(option), + horizontal_space(Length::Fill), + button("Delete") + .on_press(Message::DeleteOption( + option.to_string(), + ),) + .style(theme::Button::Destructive) + ] + .into() + }) + .collect(), + ) + .spacing(10) + }); + + column![ + scrollable(options).height(Length::Fill), + row![ + text_input( + "Add a new option", + &self.input, + Message::InputChanged, + ) + .on_submit(Message::AddOption(self.input.clone())), + button(text(format!("Toggle Order ({})", self.order))) + .on_press(Message::ToggleOrder) + ] + .spacing(10) + ] + .spacing(20) + .padding(20) + .into() + } +} + +#[derive(Debug, Hash)] +enum Order { + Ascending, + Descending, +} + +impl std::fmt::Display for Order { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Ascending => "Ascending", + Self::Descending => "Descending", + } + ) + } +} -- cgit From e2166ecad020662d246b364637efc4e6ee3bc1db Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Wed, 7 Sep 2022 11:56:11 -0400 Subject: wip: Custom palette for built in theme --- examples/styling/Cargo.toml | 1 + examples/styling/src/main.rs | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/styling/Cargo.toml b/examples/styling/Cargo.toml index f771708c..344cd0d6 100644 --- a/examples/styling/Cargo.toml +++ b/examples/styling/Cargo.toml @@ -7,3 +7,4 @@ publish = false [dependencies] iced = { path = "../.." } +once_cell = "1.14.0" \ No newline at end of file diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index cda53e87..36ab0c0c 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -1,14 +1,28 @@ +use iced::theme::Palette; +use iced::theme::palette::Extended; use iced::widget::{ button, checkbox, column, container, horizontal_rule, progress_bar, radio, row, scrollable, slider, text, text_input, toggler, vertical_rule, vertical_space, }; -use iced::{Alignment, Element, Length, Sandbox, Settings, Theme}; +use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color}; +use once_cell::sync::OnceCell; pub fn main() -> iced::Result { + let palette = Palette { + background: Color::from_rgb(1.0, 0.9, 1.0), + text: Color::BLACK, + primary: Color::from_rgb(0.5, 0.5, 0.0), + success: Color::from_rgb(0.0, 1.0, 0.0), + danger: Color::from_rgb(1.0, 0.0, 0.0), + }; + let extended = Extended::generate(palette); + CUSTOM_THEME.set(Theme::Custom { palette, extended }).unwrap(); Styling::run(Settings::default()) } +static CUSTOM_THEME: OnceCell = OnceCell::new(); + #[derive(Default)] struct Styling { theme: Theme, @@ -51,11 +65,15 @@ impl Sandbox for Styling { } fn view(&self) -> Element { - let choose_theme = [Theme::Light, Theme::Dark].iter().fold( + let choose_theme = [Theme::Light, Theme::Dark, *CUSTOM_THEME.get().unwrap()].iter().fold( column![text("Choose a theme:")].spacing(10), |column, theme| { column.push(radio( - format!("{:?}", theme), + match theme { + Theme::Light => "Light", + Theme::Dark => "Dark", + Theme::Custom { .. } => "Custom", + }, *theme, Some(self.theme), Message::ThemeChanged, -- cgit From bc26dff2ca1d3bcc431d489c5958df5e864f1863 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Wed, 7 Sep 2022 12:04:56 -0400 Subject: refactor: undo changes to radio --- examples/styling/src/main.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 36ab0c0c..69827130 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -23,6 +23,13 @@ pub fn main() -> iced::Result { static CUSTOM_THEME: OnceCell = OnceCell::new(); +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +enum ThemeType { + Light, + Dark, + Custom, +} + #[derive(Default)] struct Styling { theme: Theme, @@ -34,7 +41,7 @@ struct Styling { #[derive(Debug, Clone)] enum Message { - ThemeChanged(Theme), + ThemeChanged(ThemeType), InputChanged(String), ButtonPressed, SliderChanged(f32), @@ -55,7 +62,11 @@ impl Sandbox for Styling { fn update(&mut self, message: Message) { match message { - Message::ThemeChanged(theme) => self.theme = theme, + Message::ThemeChanged(theme) => self.theme = match theme { + ThemeType::Light => Theme::Light, + ThemeType::Dark => Theme::Dark, + ThemeType::Custom => *CUSTOM_THEME.get().unwrap(), + }, Message::InputChanged(value) => self.input_value = value, Message::ButtonPressed => {} Message::SliderChanged(value) => self.slider_value = value, @@ -65,17 +76,17 @@ impl Sandbox for Styling { } fn view(&self) -> Element { - let choose_theme = [Theme::Light, Theme::Dark, *CUSTOM_THEME.get().unwrap()].iter().fold( + let choose_theme = [ThemeType::Light, ThemeType::Dark, ThemeType::Custom].iter().fold( column![text("Choose a theme:")].spacing(10), |column, theme| { column.push(radio( - match theme { - Theme::Light => "Light", - Theme::Dark => "Dark", - Theme::Custom { .. } => "Custom", - }, + format!("{:?}", theme), *theme, - Some(self.theme), + Some(match self.theme { + Theme::Light => ThemeType::Light, + Theme::Dark => ThemeType::Dark, + Theme::Custom { .. } => ThemeType::Custom, + }), Message::ThemeChanged, )) }, -- cgit From 269d6f9a3f47861ff6fe67d65baf226192a3c22f Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 8 Sep 2022 17:36:59 -0400 Subject: fix: scrollable example --- examples/scrollable/src/main.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index b7b3dedc..f0cd60f4 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -14,9 +14,15 @@ struct ScrollableDemo { variants: Vec, } +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +enum ThemeType { + Light, + Dark, +} + #[derive(Debug, Clone)] enum Message { - ThemeChanged(Theme), + ThemeChanged(ThemeType), ScrollToTop(usize), ScrollToBottom(usize), Scrolled(usize, f32), @@ -45,7 +51,10 @@ impl Application for ScrollableDemo { fn update(&mut self, message: Message) -> Command { match message { Message::ThemeChanged(theme) => { - self.theme = theme; + self.theme = match theme { + ThemeType::Light => Theme::Light, + ThemeType::Dark => Theme::Dark, + }; Command::none() } @@ -79,16 +88,16 @@ impl Application for ScrollableDemo { fn view(&self) -> Element { let ScrollableDemo { - theme, variants, .. + variants, .. } = self; - let choose_theme = [Theme::Light, Theme::Dark].iter().fold( + let choose_theme = [ThemeType::Light, ThemeType::Dark].iter().fold( column!["Choose a theme:"].spacing(10), |column, option| { column.push(radio( format!("{:?}", option), *option, - Some(*theme), + Some(*option), Message::ThemeChanged, )) }, -- cgit From d5a933b047a955158a31a35266bbc9b25307b0fc Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 8 Sep 2022 17:39:59 -0400 Subject: refactor: remove once_cell from styling example --- examples/styling/Cargo.toml | 1 - examples/styling/src/main.rs | 28 +++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'examples') diff --git a/examples/styling/Cargo.toml b/examples/styling/Cargo.toml index 344cd0d6..f771708c 100644 --- a/examples/styling/Cargo.toml +++ b/examples/styling/Cargo.toml @@ -7,4 +7,3 @@ publish = false [dependencies] iced = { path = "../.." } -once_cell = "1.14.0" \ No newline at end of file diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 69827130..625a2b9a 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -6,22 +6,12 @@ use iced::widget::{ vertical_space, }; use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color}; -use once_cell::sync::OnceCell; pub fn main() -> iced::Result { - let palette = Palette { - background: Color::from_rgb(1.0, 0.9, 1.0), - text: Color::BLACK, - primary: Color::from_rgb(0.5, 0.5, 0.0), - success: Color::from_rgb(0.0, 1.0, 0.0), - danger: Color::from_rgb(1.0, 0.0, 0.0), - }; - let extended = Extended::generate(palette); - CUSTOM_THEME.set(Theme::Custom { palette, extended }).unwrap(); + Styling::run(Settings::default()) } -static CUSTOM_THEME: OnceCell = OnceCell::new(); #[derive(Debug, PartialEq, Eq, Clone, Copy)] enum ThemeType { @@ -32,6 +22,7 @@ enum ThemeType { #[derive(Default)] struct Styling { + custom_theme: Theme, theme: Theme, input_value: String, slider_value: f32, @@ -53,7 +44,18 @@ impl Sandbox for Styling { type Message = Message; fn new() -> Self { - Styling::default() + let palette = Palette { + background: Color::from_rgb(1.0, 0.9, 1.0), + text: Color::BLACK, + primary: Color::from_rgb(0.5, 0.5, 0.0), + success: Color::from_rgb(0.0, 1.0, 0.0), + danger: Color::from_rgb(1.0, 0.0, 0.0), + }; + let extended = Extended::generate(palette); + Styling { + custom_theme: Theme::Custom { palette, extended }, + ..Default::default() + } } fn title(&self) -> String { @@ -65,7 +67,7 @@ impl Sandbox for Styling { Message::ThemeChanged(theme) => self.theme = match theme { ThemeType::Light => Theme::Light, ThemeType::Dark => Theme::Dark, - ThemeType::Custom => *CUSTOM_THEME.get().unwrap(), + ThemeType::Custom => self.custom_theme, }, Message::InputChanged(value) => self.input_value = value, Message::ButtonPressed => {} -- cgit From 4f3215f48e72ed36cb77efcb746db1f6adabf84f Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Sat, 10 Sep 2022 22:15:25 -0400 Subject: fix: clippy lint https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant --- examples/scrollable/src/main.rs | 2 +- examples/styling/src/main.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index f0cd60f4..d5cf19cc 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -207,7 +207,7 @@ impl Application for ScrollableDemo { } fn theme(&self) -> Theme { - self.theme + self.theme.clone() } } diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 625a2b9a..784ea581 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -53,7 +53,10 @@ impl Sandbox for Styling { }; let extended = Extended::generate(palette); Styling { - custom_theme: Theme::Custom { palette, extended }, + custom_theme: Theme::Custom { + palette: Box::new(palette), + extended: Box::new(extended) + }, ..Default::default() } } @@ -67,7 +70,7 @@ impl Sandbox for Styling { Message::ThemeChanged(theme) => self.theme = match theme { ThemeType::Light => Theme::Light, ThemeType::Dark => Theme::Dark, - ThemeType::Custom => self.custom_theme, + ThemeType::Custom => self.custom_theme.clone(), }, Message::InputChanged(value) => self.input_value = value, Message::ButtonPressed => {} @@ -163,6 +166,6 @@ impl Sandbox for Styling { } fn theme(&self) -> Theme { - self.theme + self.theme.clone() } } -- cgit From 09a531cd4427ff5dc972621094f33de0f94a0919 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Sat, 10 Sep 2022 23:18:57 -0400 Subject: fix: arc example --- examples/arc/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 0c619dc9..69712240 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -80,7 +80,7 @@ impl canvas::Program for Arc { _cursor: Cursor, ) -> Vec { let geometry = self.cache.draw(bounds.size(), |frame| { - let palette = theme.palette(); + let palette = theme.clone().palette(); let center = frame.center(); let radius = frame.width().min(frame.height()) / 5.0; -- cgit From 9966c6f8834220e62fd66766d2e11d8a36e334e2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 03:21:26 +0100 Subject: Make `Theme::Custom` fields opaque --- examples/styling/src/main.rs | 89 ++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 48 deletions(-) (limited to 'examples') diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 784ea581..e16860ad 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -1,28 +1,17 @@ -use iced::theme::Palette; -use iced::theme::palette::Extended; +use iced::theme::{self, Theme}; use iced::widget::{ button, checkbox, column, container, horizontal_rule, progress_bar, radio, row, scrollable, slider, text, text_input, toggler, vertical_rule, vertical_space, }; -use iced::{Alignment, Element, Length, Sandbox, Settings, Theme, Color}; +use iced::{Alignment, Color, Element, Length, Sandbox, Settings}; pub fn main() -> iced::Result { - Styling::run(Settings::default()) } - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -enum ThemeType { - Light, - Dark, - Custom, -} - #[derive(Default)] struct Styling { - custom_theme: Theme, theme: Theme, input_value: String, slider_value: f32, @@ -30,6 +19,13 @@ struct Styling { toggler_value: bool, } +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +enum ThemeType { + Light, + Dark, + Custom, +} + #[derive(Debug, Clone)] enum Message { ThemeChanged(ThemeType), @@ -44,21 +40,7 @@ impl Sandbox for Styling { type Message = Message; fn new() -> Self { - let palette = Palette { - background: Color::from_rgb(1.0, 0.9, 1.0), - text: Color::BLACK, - primary: Color::from_rgb(0.5, 0.5, 0.0), - success: Color::from_rgb(0.0, 1.0, 0.0), - danger: Color::from_rgb(1.0, 0.0, 0.0), - }; - let extended = Extended::generate(palette); - Styling { - custom_theme: Theme::Custom { - palette: Box::new(palette), - extended: Box::new(extended) - }, - ..Default::default() - } + Styling::default() } fn title(&self) -> String { @@ -67,11 +49,19 @@ impl Sandbox for Styling { fn update(&mut self, message: Message) { match message { - Message::ThemeChanged(theme) => self.theme = match theme { - ThemeType::Light => Theme::Light, - ThemeType::Dark => Theme::Dark, - ThemeType::Custom => self.custom_theme.clone(), - }, + Message::ThemeChanged(theme) => { + self.theme = match theme { + ThemeType::Light => Theme::Light, + ThemeType::Dark => Theme::Dark, + ThemeType::Custom => Theme::custom(theme::Palette { + background: Color::from_rgb(1.0, 0.9, 1.0), + text: Color::BLACK, + primary: Color::from_rgb(0.5, 0.5, 0.0), + success: Color::from_rgb(0.0, 1.0, 0.0), + danger: Color::from_rgb(1.0, 0.0, 0.0), + }), + } + } Message::InputChanged(value) => self.input_value = value, Message::ButtonPressed => {} Message::SliderChanged(value) => self.slider_value = value, @@ -81,21 +71,24 @@ impl Sandbox for Styling { } fn view(&self) -> Element { - let choose_theme = [ThemeType::Light, ThemeType::Dark, ThemeType::Custom].iter().fold( - column![text("Choose a theme:")].spacing(10), - |column, theme| { - column.push(radio( - format!("{:?}", theme), - *theme, - Some(match self.theme { - Theme::Light => ThemeType::Light, - Theme::Dark => ThemeType::Dark, - Theme::Custom { .. } => ThemeType::Custom, - }), - Message::ThemeChanged, - )) - }, - ); + let choose_theme = + [ThemeType::Light, ThemeType::Dark, ThemeType::Custom] + .iter() + .fold( + column![text("Choose a theme:")].spacing(10), + |column, theme| { + column.push(radio( + format!("{:?}", theme), + *theme, + Some(match self.theme { + Theme::Light => ThemeType::Light, + Theme::Dark => ThemeType::Dark, + Theme::Custom { .. } => ThemeType::Custom, + }), + Message::ThemeChanged, + )) + }, + ); let text_input = text_input( "Type something...", -- cgit From 708be32e3da9700afb61ab2f8a6da45b0e2df8ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 03:22:55 +0100 Subject: Derive `Copy` for `Theme` --- examples/arc/src/main.rs | 2 +- examples/styling/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 69712240..0c619dc9 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -80,7 +80,7 @@ impl canvas::Program for Arc { _cursor: Cursor, ) -> Vec { let geometry = self.cache.draw(bounds.size(), |frame| { - let palette = theme.clone().palette(); + let palette = theme.palette(); let center = frame.center(); let radius = frame.width().min(frame.height()) / 5.0; diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index e16860ad..6bcfa5f1 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -159,6 +159,6 @@ impl Sandbox for Styling { } fn theme(&self) -> Theme { - self.theme.clone() + self.theme } } -- cgit From 3517dece26e33a627cee8957e4ac3d60be3cfb76 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 03:25:27 +0100 Subject: Run `cargo fmt` --- examples/scrollable/src/main.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index d5cf19cc..6eba34e2 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -87,9 +87,7 @@ impl Application for ScrollableDemo { } fn view(&self) -> Element { - let ScrollableDemo { - variants, .. - } = self; + let ScrollableDemo { variants, .. } = self; let choose_theme = [ThemeType::Light, ThemeType::Dark].iter().fold( column!["Choose a theme:"].spacing(10), -- cgit From f04336dd5cf5e15bbcf48cd54b7e97c569c07a92 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 03:26:10 +0100 Subject: Remove unnecessary `clone` in `scrollable` example --- examples/scrollable/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 6eba34e2..7e1716cf 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -205,7 +205,7 @@ impl Application for ScrollableDemo { } fn theme(&self) -> Theme { - self.theme.clone() + self.theme } } -- cgit From df7877767567cc0c7f48d2d6da4680a55f0f7b6d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 03:27:55 +0100 Subject: Box `Custom` in `Theme` --- examples/scrollable/src/main.rs | 2 +- examples/styling/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 7e1716cf..6eba34e2 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -205,7 +205,7 @@ impl Application for ScrollableDemo { } fn theme(&self) -> Theme { - self.theme + self.theme.clone() } } diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 6bcfa5f1..e16860ad 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -159,6 +159,6 @@ impl Sandbox for Styling { } fn theme(&self) -> Theme { - self.theme + self.theme.clone() } } -- cgit From b95745340441835bd25b5cadc2342254631f8c05 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:35:16 +0100 Subject: Run `cargo fmt` --- examples/clock/src/main.rs | 2 +- examples/modern_art/src/main.rs | 21 +++++++-------------- examples/solar_system/src/main.rs | 14 +++++++------- 3 files changed, 15 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index be614201..a389c54f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, stroke, + stroke, Cache, Cursor, Geometry, LineCap, Path, Stroke, }; use iced::widget::{canvas, container}; use iced::{ diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index fc203cff..db9b0491 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,9 +1,10 @@ use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, gradient::Position, gradient::Location + self, gradient::Location, gradient::Position, Cache, Canvas, Cursor, Frame, + Geometry, Gradient, }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, - Renderer, Size, Theme, Settings + Renderer, Settings, Size, Theme, }; use rand::{thread_rng, Rng}; @@ -86,7 +87,7 @@ fn random_direction() -> Location { 5 => Location::Bottom, 6 => Location::BottomLeft, 7 => Location::Left, - _ => Location::TopLeft + _ => Location::TopLeft, } } @@ -106,7 +107,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, start: random_direction(), - end: random_direction() + end: random_direction(), }); let stops = thread_rng().gen_range(1..15u32); @@ -130,17 +131,9 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { ); if solid { - frame.fill_rectangle( - top_left, - size, - random_color(), - ); + frame.fill_rectangle(top_left, size, random_color()); } else { - frame.fill_rectangle( - top_left, - size, - &gradient(top_left, size), - ); + frame.fill_rectangle(top_left, size, &gradient(top_left, size)); }; solid diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index b638c6d0..09ac508d 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,9 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, Gradient, stroke, gradient::Position}; +use iced::widget::canvas::{ + gradient::Position, stroke, Cursor, Gradient, Path, Stroke, +}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -201,12 +203,10 @@ impl canvas::Program for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); - let earth_fill = Gradient::linear( - Position::Absolute { - start: Point::new(-Self::EARTH_RADIUS, 0.0), - end: Point::new(Self::EARTH_RADIUS, 0.0) - } - ) + let earth_fill = Gradient::linear(Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0), + }) .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) .build() -- cgit From edce457365aae5e1aa20863389cdd28357234908 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:42:46 +0100 Subject: Fix `multitouch` example --- examples/multitouch/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/multitouch/src/main.rs b/examples/multitouch/src/main.rs index 0345ceb7..f5faae0f 100644 --- a/examples/multitouch/src/main.rs +++ b/examples/multitouch/src/main.rs @@ -2,7 +2,8 @@ //! a circle around each fingertip. This only works on touch-enabled //! computers like Microsoft Surface. use iced::widget::canvas::event; -use iced::widget::canvas::{self, Canvas, Cursor, Geometry, Stroke}; +use iced::widget::canvas::stroke::{self, Stroke}; +use iced::widget::canvas::{self, Canvas, Cursor, Geometry}; use iced::{ executor, touch, window, Application, Color, Command, Element, Length, Point, Rectangle, Settings, Subscription, Theme, @@ -186,7 +187,7 @@ impl canvas::Program for State { frame.stroke( &path, Stroke { - color: Color::BLACK, + style: stroke::Style::Solid(Color::BLACK), width: 3.0, ..Stroke::default() }, -- cgit From e07344428de93055ac4b4f7e02505bbcbc43ab98 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:19:58 +0100 Subject: Refactor imports in `solar_system` example --- examples/solar_system/src/main.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 09ac508d..8d27d154 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,9 +11,9 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{ - gradient::Position, stroke, Cursor, Gradient, Path, Stroke, -}; +use iced::widget::canvas::gradient::{self, Gradient}; +use iced::widget::canvas::stroke::{self, Stroke}; +use iced::widget::canvas::{Cursor, Path}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -203,14 +203,15 @@ impl canvas::Program for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); - let earth_fill = Gradient::linear(Position::Absolute { - start: Point::new(-Self::EARTH_RADIUS, 0.0), - end: Point::new(Self::EARTH_RADIUS, 0.0), - }) - .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) - .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) - .build() - .unwrap(); + let earth_fill = + Gradient::linear(gradient::Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0), + }) + .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) + .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) + .build() + .expect("Build Earth fill gradient"); frame.fill(&earth, &earth_fill); -- cgit From 84d1b79fefc88534835fdfbe79bc0eb3b43627cf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:50:53 +0100 Subject: Move `mesh::Style` to `triangle` and reuse it in `fill` and `stroke` --- examples/modern_art/src/main.rs | 2 +- examples/solar_system/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index db9b0491..0dd21c74 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -133,7 +133,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { if solid { frame.fill_rectangle(top_left, size, random_color()); } else { - frame.fill_rectangle(top_left, size, &gradient(top_left, size)); + frame.fill_rectangle(top_left, size, gradient(top_left, size)); }; solid diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8d27d154..56787a99 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -213,7 +213,7 @@ impl canvas::Program for State { .build() .expect("Build Earth fill gradient"); - frame.fill(&earth, &earth_fill); + frame.fill(&earth, earth_fill); frame.with_save(|frame| { frame.rotate(rotation * 10.0); -- cgit From c0596179bd8582e4f4b5289cdeee8de4fa3de464 Mon Sep 17 00:00:00 2001 From: Robert Krahn Date: Thu, 3 Nov 2022 00:35:01 +0100 Subject: non uniform border radius for quads --- examples/custom_widget/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index c37a1a12..f6bb3b1e 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -61,7 +61,7 @@ mod circle { renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_radius: self.radius, + border_radius: self.radius.into(), border_width: 0.0, border_color: Color::TRANSPARENT, }, -- cgit From 9841d1938142cb453495a50e4a88059c6eae3074 Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Tue, 8 Nov 2022 11:32:27 -0800 Subject: Fixed issues with old GL versions ( <= 2.1 ) --- examples/modern_art/Cargo.toml | 1 + examples/modern_art/src/main.rs | 2 ++ 2 files changed, 3 insertions(+) (limited to 'examples') diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml index a48361ae..4242d209 100644 --- a/examples/modern_art/Cargo.toml +++ b/examples/modern_art/Cargo.toml @@ -8,3 +8,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } rand = "0.8.5" +env_logger = "0.9" diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 0dd21c74..28ed3e21 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -9,6 +9,8 @@ use iced::{ use rand::{thread_rng, Rng}; fn main() -> iced::Result { + env_logger::builder().format_timestamp(None).init(); + ModernArt::run(Settings { antialiasing: true, ..Settings::default() -- cgit From 18fb74f20092b2703a90afdb01f39754445998da Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Nov 2022 04:05:31 +0100 Subject: Introduce `Custom` variants for every style in the built-in `Theme` --- examples/solar_system/src/main.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 56787a99..9e303576 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -79,10 +79,14 @@ impl Application for SolarSystem { } fn style(&self) -> theme::Application { - theme::Application::Custom(|_theme| application::Appearance { - background_color: Color::BLACK, - text_color: Color::WHITE, - }) + fn dark_background(_theme: &Theme) -> application::Appearance { + application::Appearance { + background_color: Color::BLACK, + text_color: Color::WHITE, + } + } + + theme::Application::from(dark_background as fn(&Theme) -> _) } fn subscription(&self) -> Subscription { -- cgit From a7a4a92466a4447f8d2e9b5ae2fc715ba6262de0 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 11 Nov 2022 09:08:52 -0800 Subject: Update todos example to select_all input on edit --- examples/todos/src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index be48ae8c..690d9c09 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -131,7 +131,11 @@ impl Application for Todos { task.update(task_message); if should_focus { - text_input::focus(Task::text_input_id(i)) + let id = Task::text_input_id(i); + Command::batch(vec![ + text_input::focus(id.clone()), + text_input::select_all(id), + ]) } else { Command::none() } -- cgit From ee263cbb3c90a9adce7608686fbe5881c81f1c91 Mon Sep 17 00:00:00 2001 From: Leo Lee Date: Fri, 25 Nov 2022 17:36:42 +0800 Subject: docs: Remove invalid links. --- examples/README.md | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/README.md b/examples/README.md index bb15dc2e..8d9718a4 100644 --- a/examples/README.md +++ b/examples/README.md @@ -92,7 +92,6 @@ A bunch of simpler examples exist: - [`custom_widget`](custom_widget), a demonstration of how to build a custom widget that draws a circle. - [`download_progress`](download_progress), a basic application that asynchronously downloads a dummy file of 100 MB and tracks the download progress. - [`events`](events), a log of native events displayed using a conditional `Subscription`. -- [`geometry`](geometry), a custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../wgpu). - [`integration_opengl`](integration_opengl), a demonstration of how to integrate Iced in an existing OpenGL application. - [`integration_wgpu`](integration_wgpu), a demonstration of how to integrate Iced in an existing [`wgpu`] application. - [`pane_grid`](pane_grid), a grid of panes that can be split, resized, and reorganized. -- cgit From 324d60db6319017304bf0b514b76db98b3637929 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 28 Nov 2022 19:42:53 +0100 Subject: Re-introduce the `geometry` example --- examples/README.md | 1 + examples/geometry/Cargo.toml | 11 +++ examples/geometry/README.md | 18 ++++ examples/geometry/src/main.rs | 213 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 243 insertions(+) create mode 100644 examples/geometry/Cargo.toml create mode 100644 examples/geometry/README.md create mode 100644 examples/geometry/src/main.rs (limited to 'examples') diff --git a/examples/README.md b/examples/README.md index 8d9718a4..bb15dc2e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -92,6 +92,7 @@ A bunch of simpler examples exist: - [`custom_widget`](custom_widget), a demonstration of how to build a custom widget that draws a circle. - [`download_progress`](download_progress), a basic application that asynchronously downloads a dummy file of 100 MB and tracks the download progress. - [`events`](events), a log of native events displayed using a conditional `Subscription`. +- [`geometry`](geometry), a custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../wgpu). - [`integration_opengl`](integration_opengl), a demonstration of how to integrate Iced in an existing OpenGL application. - [`integration_wgpu`](integration_wgpu), a demonstration of how to integrate Iced in an existing [`wgpu`] application. - [`pane_grid`](pane_grid), a grid of panes that can be split, resized, and reorganized. diff --git a/examples/geometry/Cargo.toml b/examples/geometry/Cargo.toml new file mode 100644 index 00000000..22ede0e0 --- /dev/null +++ b/examples/geometry/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "geometry" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_native = { path = "../../native" } +iced_graphics = { path = "../../graphics" } diff --git a/examples/geometry/README.md b/examples/geometry/README.md new file mode 100644 index 00000000..4d5c81cb --- /dev/null +++ b/examples/geometry/README.md @@ -0,0 +1,18 @@ +## Geometry + +A custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../../wgpu). + +The __[`main`]__ file contains all the code of the example. + + + +You can run it with `cargo run`: +``` +cargo run --package geometry +``` + +[`main`]: src/main.rs diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs new file mode 100644 index 00000000..4de281fe --- /dev/null +++ b/examples/geometry/src/main.rs @@ -0,0 +1,213 @@ +//! This example showcases a simple native custom widget that renders using +//! arbitrary low-level geometry. +mod rainbow { + // For now, to implement a custom native widget you will need to add + // `iced_native` and `iced_wgpu` to your dependencies. + // + // Then, you simply need to define your widget type and implement the + // `iced_native::Widget` trait with the `iced_wgpu::Renderer`. + // + // Of course, you can choose to make the implementation renderer-agnostic, + // if you wish to, by creating your own `Renderer` trait, which could be + // implemented by `iced_wgpu` and other renderers. + use iced_graphics::renderer::{self, Renderer}; + use iced_graphics::triangle::ColoredVertex2D; + use iced_graphics::{Backend, Primitive}; + + use iced_native::layout; + use iced_native::widget::{self, Widget}; + use iced_native::{ + Element, Layout, Length, Point, Rectangle, Size, Vector, + }; + + pub struct Rainbow; + + impl Rainbow { + pub fn new() -> Self { + Self + } + } + + impl Widget> for Rainbow + where + B: Backend, + { + fn width(&self) -> Length { + Length::Fill + } + + fn height(&self) -> Length { + Length::Shrink + } + + fn layout( + &self, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let size = limits.width(Length::Fill).resolve(Size::ZERO); + + layout::Node::new(Size::new(size.width, size.width)) + } + + fn draw( + &self, + _tree: &widget::Tree, + renderer: &mut Renderer, + _theme: &T, + _style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + ) { + use iced_graphics::triangle::Mesh2D; + use iced_native::Renderer as _; + + let b = layout.bounds(); + + // R O Y G B I V + let color_r = [1.0, 0.0, 0.0, 1.0]; + let color_o = [1.0, 0.5, 0.0, 1.0]; + let color_y = [1.0, 1.0, 0.0, 1.0]; + let color_g = [0.0, 1.0, 0.0, 1.0]; + let color_gb = [0.0, 1.0, 0.5, 1.0]; + let color_b = [0.0, 0.2, 1.0, 1.0]; + let color_i = [0.5, 0.0, 1.0, 1.0]; + let color_v = [0.75, 0.0, 0.5, 1.0]; + + let posn_center = { + if b.contains(cursor_position) { + [cursor_position.x - b.x, cursor_position.y - b.y] + } else { + [b.width / 2.0, b.height / 2.0] + } + }; + + let posn_tl = [0.0, 0.0]; + let posn_t = [b.width / 2.0, 0.0]; + let posn_tr = [b.width, 0.0]; + let posn_r = [b.width, b.height / 2.0]; + let posn_br = [b.width, b.height]; + let posn_b = [(b.width / 2.0), b.height]; + let posn_bl = [0.0, b.height]; + let posn_l = [0.0, b.height / 2.0]; + + let mesh = Primitive::SolidMesh { + size: b.size(), + buffers: Mesh2D { + vertices: vec![ + ColoredVertex2D { + position: posn_center, + color: [1.0, 1.0, 1.0, 1.0], + }, + ColoredVertex2D { + position: posn_tl, + color: color_r, + }, + ColoredVertex2D { + position: posn_t, + color: color_o, + }, + ColoredVertex2D { + position: posn_tr, + color: color_y, + }, + ColoredVertex2D { + position: posn_r, + color: color_g, + }, + ColoredVertex2D { + position: posn_br, + color: color_gb, + }, + ColoredVertex2D { + position: posn_b, + color: color_b, + }, + ColoredVertex2D { + position: posn_bl, + color: color_i, + }, + ColoredVertex2D { + position: posn_l, + color: color_v, + }, + ], + indices: vec![ + 0, 1, 2, // TL + 0, 2, 3, // T + 0, 3, 4, // TR + 0, 4, 5, // R + 0, 5, 6, // BR + 0, 6, 7, // B + 0, 7, 8, // BL + 0, 8, 1, // L + ], + }, + }; + + renderer.with_translation(Vector::new(b.x, b.y), |renderer| { + renderer.draw_primitive(mesh); + }); + } + } + + impl<'a, Message, B, T> From for Element<'a, Message, Renderer> + where + B: Backend, + { + fn from(rainbow: Rainbow) -> Self { + Self::new(rainbow) + } + } +} + +use iced::widget::{column, container, scrollable}; +use iced::{Element, Length, Sandbox, Settings}; +use rainbow::Rainbow; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +struct Example; + +impl Sandbox for Example { + type Message = (); + + fn new() -> Self { + Self + } + + fn title(&self) -> String { + String::from("Custom 2D geometry - Iced") + } + + fn update(&mut self, _: ()) {} + + fn view(&self) -> Element<()> { + let content = column![ + Rainbow::new(), + "In this example we draw a custom widget Rainbow, using \ + the Mesh2D primitive. This primitive supplies a list of \ + triangles, expressed as vertices and indices.", + "Move your cursor over it, and see the center vertex \ + follow you!", + "Every Vertex2D defines its own color. You could use the \ + Mesh2D primitive to render virtually any two-dimensional \ + geometry for your widget.", + ] + .padding(20) + .spacing(20) + .max_width(500); + + let scrollable = + scrollable(container(content).width(Length::Fill).center_x()); + + container(scrollable) + .width(Length::Fill) + .height(Length::Fill) + .center_y() + .into() + } +} -- cgit From bb2bf063b472396d44f9f3114a87ba79dfd5f62e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 28 Nov 2022 19:49:23 +0100 Subject: Derive `Default` for `Rainbow` in `geometry` example --- examples/geometry/src/main.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 4de281fe..9bacce7f 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -20,12 +20,11 @@ mod rainbow { Element, Layout, Length, Point, Rectangle, Size, Vector, }; + #[derive(Debug, Clone, Copy, Default)] pub struct Rainbow; - impl Rainbow { - pub fn new() -> Self { - Self - } + pub fn rainbow() -> Rainbow { + Rainbow } impl Widget> for Rainbow @@ -164,7 +163,7 @@ mod rainbow { use iced::widget::{column, container, scrollable}; use iced::{Element, Length, Sandbox, Settings}; -use rainbow::Rainbow; +use rainbow::rainbow; pub fn main() -> iced::Result { Example::run(Settings::default()) @@ -187,7 +186,7 @@ impl Sandbox for Example { fn view(&self) -> Element<()> { let content = column![ - Rainbow::new(), + rainbow(), "In this example we draw a custom widget Rainbow, using \ the Mesh2D primitive. This primitive supplies a list of \ triangles, expressed as vertices and indices.", -- cgit From 3814fda9d22875bdbc9e11ccfc6dce1f0e7ca5d3 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 18 Nov 2022 14:16:02 -0800 Subject: Add modal example --- examples/modal/Cargo.toml | 10 + examples/modal/src/main.rs | 454 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 464 insertions(+) create mode 100644 examples/modal/Cargo.toml create mode 100644 examples/modal/src/main.rs (limited to 'examples') diff --git a/examples/modal/Cargo.toml b/examples/modal/Cargo.toml new file mode 100644 index 00000000..8770acac --- /dev/null +++ b/examples/modal/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "modal" +version = "0.1.0" +authors = ["tarkah "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = [] } +iced_native = { path = "../../native" } diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs new file mode 100644 index 00000000..37ac16fd --- /dev/null +++ b/examples/modal/src/main.rs @@ -0,0 +1,454 @@ +use iced::widget::{ + self, button, column, container, horizontal_space, row, text, text_input, +}; +use iced::{ + executor, keyboard, subscription, theme, Alignment, Application, Command, + Element, Event, Length, Settings, Subscription, +}; + +use self::modal::Modal; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Default)] +struct App { + show_modal: bool, + email: String, + password: String, +} + +#[derive(Debug, Clone)] +enum Message { + ShowModal, + HideModal, + Email(String), + Password(String), + Event(Event), +} + +impl Application for App { + type Executor = executor::Default; + type Message = Message; + type Theme = iced::Theme; + type Flags = (); + + fn new(_flags: ()) -> (Self, Command) { + (App::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Modal - Iced") + } + + fn subscription(&self) -> Subscription { + subscription::events().map(Message::Event) + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::ShowModal => { + self.show_modal = true; + widget::focus_next() + } + Message::HideModal => { + self.show_modal = false; + self.email.clear(); + self.password.clear(); + Command::none() + } + Message::Email(email) => { + self.email = email; + Command::none() + } + Message::Password(password) => { + self.password = password; + Command::none() + } + Message::Event(event) => { + if let Event::Keyboard(keyboard::Event::KeyPressed { + key_code: keyboard::KeyCode::Tab, + modifiers, + }) = event + { + if modifiers.shift() { + widget::focus_previous() + } else { + widget::focus_next() + } + } else { + Command::none() + } + } + } + } + + fn view(&self) -> Element { + let content = container( + column![ + row![ + text("Top Left"), + horizontal_space(Length::Fill), + text("Top Right") + ] + .align_items(Alignment::Start) + .height(Length::Fill), + container( + button(text("Show Modal")).on_press(Message::ShowModal) + ) + .center_x() + .center_y() + .width(Length::Fill) + .height(Length::Fill), + row![ + text("Bottom Left"), + horizontal_space(Length::Fill), + text("Bottom Right") + ] + .align_items(Alignment::End) + .height(Length::Fill), + ] + .height(Length::Fill), + ) + .padding(10) + .width(Length::Fill) + .height(Length::Fill); + + if self.show_modal { + let modal = container( + column![ + text("Sign Up").size(24), + column![ + column![ + text("Email").size(12), + text_input( + "abc@123.com", + &self.email, + Message::Email + ) + .padding(5), + ] + .spacing(5), + column![ + text("Password").size(12), + text_input("", &self.password, Message::Password) + .password() + .padding(5), + ] + .spacing(5), + button(text("Submit")).on_press(Message::HideModal), + ] + .spacing(10) + ] + .spacing(20), + ) + .width(Length::Units(300)) + .padding(10) + .style(theme::Container::Box); + + Modal::new(content, modal) + .on_blur(Message::HideModal) + .into() + } else { + content.into() + } + } +} + +mod modal { + use iced_native::alignment::Alignment; + use iced_native::widget::{self, Tree}; + use iced_native::{ + event, layout, mouse, overlay, renderer, Clipboard, Color, Element, + Event, Layout, Length, Point, Rectangle, Shell, Size, Widget, + }; + + /// A widget that centers a modal element over some base element + pub struct Modal<'a, Message, Renderer> { + base: Element<'a, Message, Renderer>, + modal: Element<'a, Message, Renderer>, + on_blur: Option, + } + + impl<'a, Message, Renderer> Modal<'a, Message, Renderer> { + /// Returns a new [`Modal`] + pub fn new( + base: impl Into>, + modal: impl Into>, + ) -> Self { + Self { + base: base.into(), + modal: modal.into(), + on_blur: None, + } + } + + /// Sets the message that will be produces when the background + /// of the [`Modal`] is pressed + pub fn on_blur(self, on_blur: Message) -> Self { + Self { + on_blur: Some(on_blur), + ..self + } + } + } + + impl<'a, Message, Renderer> Widget + for Modal<'a, Message, Renderer> + where + Renderer: iced_native::Renderer, + Message: Clone, + { + fn children(&self) -> Vec { + vec![Tree::new(&self.base), Tree::new(&self.modal)] + } + + fn diff(&self, tree: &mut Tree) { + tree.diff_children(&[&self.base, &self.modal]); + } + + fn width(&self) -> Length { + self.base.as_widget().width() + } + + fn height(&self) -> Length { + self.base.as_widget().height() + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + self.base.as_widget().layout(renderer, limits) + } + + fn on_event( + &mut self, + state: &mut Tree, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + self.base.as_widget_mut().on_event( + &mut state.children[0], + event, + layout, + cursor_position, + renderer, + clipboard, + shell, + ) + } + + fn draw( + &self, + state: &Tree, + renderer: &mut Renderer, + theme: &::Theme, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + ) { + self.base.as_widget().draw( + &state.children[0], + renderer, + theme, + style, + layout, + cursor_position, + viewport, + ); + } + + fn overlay<'b>( + &'b mut self, + state: &'b mut Tree, + layout: Layout<'_>, + _renderer: &Renderer, + ) -> Option> { + Some(overlay::Element::new( + layout.position(), + Box::new(Overlay { + content: &mut self.modal, + tree: &mut state.children[1], + size: layout.bounds().size(), + on_blur: self.on_blur.clone(), + }), + )) + } + + fn mouse_interaction( + &self, + state: &Tree, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + self.base.as_widget().mouse_interaction( + &state.children[0], + layout, + cursor_position, + viewport, + renderer, + ) + } + + fn operate( + &self, + state: &mut Tree, + layout: Layout<'_>, + operation: &mut dyn widget::Operation, + ) { + self.base.as_widget().operate( + &mut state.children[0], + layout, + operation, + ); + } + } + + struct Overlay<'a, 'b, Message, Renderer> { + content: &'b mut Element<'a, Message, Renderer>, + tree: &'b mut Tree, + size: Size, + on_blur: Option, + } + + impl<'a, 'b, Message, Renderer> overlay::Overlay + for Overlay<'a, 'b, Message, Renderer> + where + Renderer: iced_native::Renderer, + Message: Clone, + { + fn layout( + &self, + renderer: &Renderer, + _bounds: Size, + position: Point, + ) -> layout::Node { + let limits = layout::Limits::new(Size::ZERO, self.size) + .width(Length::Fill) + .height(Length::Fill); + + let mut child = self.content.as_widget().layout(renderer, &limits); + child.align(Alignment::Center, Alignment::Center, limits.max()); + + let mut node = layout::Node::with_children(self.size, vec![child]); + node.move_to(position); + + node + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + let content_bounds = layout.children().next().unwrap().bounds(); + + if let Some(message) = self.on_blur.as_ref() { + if let Event::Mouse(mouse::Event::ButtonPressed( + mouse::Button::Left, + )) = &event + { + if !content_bounds.contains(cursor_position) { + shell.publish(message.clone()); + return event::Status::Captured; + } + } + } + + self.content.as_widget_mut().on_event( + self.tree, + event, + layout.children().next().unwrap(), + cursor_position, + renderer, + clipboard, + shell, + ) + } + + fn draw( + &self, + renderer: &mut Renderer, + theme: &Renderer::Theme, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + ) { + renderer.fill_quad( + renderer::Quad { + bounds: layout.bounds(), + border_radius: 0.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }, + Color { + a: 0.80, + ..Color::BLACK + }, + ); + + self.content.as_widget().draw( + self.tree, + renderer, + theme, + style, + layout.children().next().unwrap(), + cursor_position, + &layout.bounds(), + ); + } + + fn operate( + &mut self, + layout: Layout<'_>, + operation: &mut dyn widget::Operation, + ) { + self.content.as_widget().operate( + self.tree, + layout.children().next().unwrap(), + operation, + ); + } + + fn mouse_interaction( + &self, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + self.content.as_widget().mouse_interaction( + self.tree, + layout.children().next().unwrap(), + cursor_position, + viewport, + renderer, + ) + } + } + + impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> + where + Renderer: 'a + iced_native::Renderer, + Message: 'a + Clone, + { + fn from(modal: Modal<'a, Message, Renderer>) -> Self { + Element::new(modal) + } + } +} -- cgit From 899bd45700f0fe7a5dee5fb71400820981f154e0 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 18 Nov 2022 15:00:10 -0800 Subject: Add on_submit & esc for example --- examples/modal/src/main.rs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 37ac16fd..25483cf3 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -25,6 +25,7 @@ enum Message { HideModal, Email(String), Password(String), + Submit, Event(Event), } @@ -53,9 +54,7 @@ impl Application for App { widget::focus_next() } Message::HideModal => { - self.show_modal = false; - self.email.clear(); - self.password.clear(); + self.hide_modal(); Command::none() } Message::Email(email) => { @@ -66,21 +65,33 @@ impl Application for App { self.password = password; Command::none() } - Message::Event(event) => { - if let Event::Keyboard(keyboard::Event::KeyPressed { + Message::Submit => { + if !self.email.is_empty() && !self.password.is_empty() { + self.hide_modal(); + } + + Command::none() + } + Message::Event(event) => match event { + Event::Keyboard(keyboard::Event::KeyPressed { key_code: keyboard::KeyCode::Tab, modifiers, - }) = event - { + }) => { if modifiers.shift() { widget::focus_previous() } else { widget::focus_next() } - } else { + } + Event::Keyboard(keyboard::Event::KeyPressed { + key_code: keyboard::KeyCode::Escape, + .. + }) => { + self.hide_modal(); Command::none() } - } + _ => Command::none(), + }, } } @@ -127,12 +138,14 @@ impl Application for App { &self.email, Message::Email ) + .on_submit(Message::Submit) .padding(5), ] .spacing(5), column![ text("Password").size(12), text_input("", &self.password, Message::Password) + .on_submit(Message::Submit) .password() .padding(5), ] @@ -156,6 +169,14 @@ impl Application for App { } } +impl App { + fn hide_modal(&mut self) { + self.show_modal = false; + self.email.clear(); + self.password.clear(); + } +} + mod modal { use iced_native::alignment::Alignment; use iced_native::widget::{self, Tree}; -- cgit From 6d39aebec61fa50ab7a5f58056adce1b87ac25e8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 2 Dec 2022 18:55:10 +0100 Subject: Fix `border_radius` in `modal` example --- examples/modal/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 25483cf3..2f20795c 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -412,7 +412,7 @@ mod modal { renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_radius: 0.0, + border_radius: renderer::BorderRadius::from(0.0), border_width: 0.0, border_color: Color::TRANSPARENT, }, -- cgit From 18fd5300854522a091a2bcccb47b65f1d1d8f063 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 2 Dec 2022 19:06:42 +0100 Subject: Add `custom_quad` example Thanks to @rksm :bow: --- examples/custom_quad/Cargo.toml | 10 +++ examples/custom_quad/src/main.rs | 159 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 examples/custom_quad/Cargo.toml create mode 100644 examples/custom_quad/src/main.rs (limited to 'examples') diff --git a/examples/custom_quad/Cargo.toml b/examples/custom_quad/Cargo.toml new file mode 100644 index 00000000..39154786 --- /dev/null +++ b/examples/custom_quad/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "custom_quad" +version = "0.1.0" +authors = ["Robert Krahn"] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_native = { path = "../../native" } diff --git a/examples/custom_quad/src/main.rs b/examples/custom_quad/src/main.rs new file mode 100644 index 00000000..2a5fe798 --- /dev/null +++ b/examples/custom_quad/src/main.rs @@ -0,0 +1,159 @@ +//! This example showcases a drawing a quad. +mod quad { + use iced_native::layout::{self, Layout}; + use iced_native::renderer; + use iced_native::widget::{self, Widget}; + use iced_native::{Color, Element, Length, Point, Rectangle, Size}; + + pub struct CustomQuad { + size: f32, + radius: [f32; 4], + border_width: f32, + } + + impl CustomQuad { + pub fn new(size: f32, radius: [f32; 4], border_width: f32) -> Self { + Self { + size, + radius, + border_width, + } + } + } + + impl Widget for CustomQuad + where + Renderer: renderer::Renderer, + { + fn width(&self) -> Length { + Length::Shrink + } + + fn height(&self) -> Length { + Length::Shrink + } + + fn layout( + &self, + _renderer: &Renderer, + _limits: &layout::Limits, + ) -> layout::Node { + layout::Node::new(Size::new(self.size, self.size)) + } + + fn draw( + &self, + _state: &widget::Tree, + renderer: &mut Renderer, + _theme: &Renderer::Theme, + _style: &renderer::Style, + layout: Layout<'_>, + _cursor_position: Point, + _viewport: &Rectangle, + ) { + renderer.fill_quad( + renderer::Quad { + bounds: layout.bounds(), + border_radius: self.radius.into(), + border_width: self.border_width, + border_color: Color::from_rgb(1.0, 0.0, 0.0), + }, + Color::BLACK, + ); + } + } + + impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> + where + Renderer: renderer::Renderer, + { + fn from(circle: CustomQuad) -> Self { + Self::new(circle) + } + } +} + +use iced::widget::{column, container, slider, text}; +use iced::{Alignment, Element, Length, Sandbox, Settings}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +struct Example { + radius: [f32; 4], + border_width: f32, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + RadiusTopLeftChanged(f32), + RadiusTopRightChanged(f32), + RadiusBottomRightChanged(f32), + RadiusBottomLeftChanged(f32), + BorderWidthChanged(f32), +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Self { + radius: [50.0; 4], + border_width: 0.0, + } + } + + fn title(&self) -> String { + String::from("Custom widget - Iced") + } + + fn update(&mut self, message: Message) { + let [tl, tr, br, bl] = self.radius; + match message { + Message::RadiusTopLeftChanged(radius) => { + self.radius = [radius, tr, br, bl]; + } + Message::RadiusTopRightChanged(radius) => { + self.radius = [tl, radius, br, bl]; + } + Message::RadiusBottomRightChanged(radius) => { + self.radius = [tl, tr, radius, bl]; + } + Message::RadiusBottomLeftChanged(radius) => { + self.radius = [tl, tr, br, radius]; + } + Message::BorderWidthChanged(width) => { + self.border_width = width; + } + } + } + + fn view(&self) -> Element { + let [tl, tr, br, bl] = self.radius; + + let content = column![ + quad::CustomQuad::new(200.0, self.radius, self.border_width), + text(format!("Radius: {tl:.2}/{tr:.2}/{br:.2}/{bl:.2}")), + slider(1.0..=100.0, tl, Message::RadiusTopLeftChanged).step(0.01), + slider(1.0..=100.0, tr, Message::RadiusTopRightChanged).step(0.01), + slider(1.0..=100.0, br, Message::RadiusBottomRightChanged) + .step(0.01), + slider(1.0..=100.0, bl, Message::RadiusBottomLeftChanged) + .step(0.01), + slider(1.0..=10.0, self.border_width, Message::BorderWidthChanged) + .step(0.01), + ] + .padding(20) + .spacing(20) + .max_width(500) + .align_items(Alignment::Center); + + container(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} -- cgit From 60e41666d0e203d9777de981121c39f569bc3a7b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 2 Dec 2022 19:13:04 +0100 Subject: Ask `clippy` to behave --- examples/custom_quad/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/custom_quad/src/main.rs b/examples/custom_quad/src/main.rs index 2a5fe798..6509887c 100644 --- a/examples/custom_quad/src/main.rs +++ b/examples/custom_quad/src/main.rs @@ -86,6 +86,7 @@ struct Example { } #[derive(Debug, Clone, Copy)] +#[allow(clippy::enum_variant_names)] enum Message { RadiusTopLeftChanged(f32), RadiusTopRightChanged(f32), -- cgit 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