From dc0ebdc525dcb234fb754248eb1ee1606f91e839 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 13 Jul 2023 16:40:47 +0200 Subject: Fix new `clippy` lint in `pokedex` example --- examples/pokedex/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 1873b674..4482814c 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -151,7 +151,7 @@ impl Pokemon { } let id = { - let mut rng = rand::rngs::OsRng::default(); + let mut rng = rand::rngs::OsRng; rng.gen_range(0, Pokemon::TOTAL) }; -- cgit From 42c423b4a89613c4e1c552c891c1391a34837122 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Sat, 15 Jul 2023 10:04:25 -0700 Subject: Add viewport to Widget::on_event --- examples/loading_spinners/src/circular.rs | 1 + examples/loading_spinners/src/linear.rs | 1 + examples/modal/src/main.rs | 3 +++ examples/toast/src/main.rs | 5 +++++ 4 files changed, 10 insertions(+) (limited to 'examples') diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 3a35e029..3898d76e 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -272,6 +272,7 @@ where _renderer: &iced::Renderer, _clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + _viewport: &Rectangle, ) -> event::Status { const FRAME_RATE: u64 = 60; diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index 3d95729b..20fbe9f3 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -193,6 +193,7 @@ where _renderer: &Renderer, _clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + _viewport: &Rectangle, ) -> event::Status { const FRAME_RATE: u64 = 60; diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 7fcbbfe4..8a48f830 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -300,6 +300,7 @@ mod modal { renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + viewport: &Rectangle, ) -> event::Status { self.base.as_widget_mut().on_event( &mut state.children[0], @@ -309,6 +310,7 @@ mod modal { renderer, clipboard, shell, + viewport, ) } @@ -446,6 +448,7 @@ mod modal { renderer, clipboard, shell, + &layout.bounds(), ) } diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 4282ddcf..5d29e895 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -400,6 +400,7 @@ mod toast { renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + viewport: &Rectangle, ) -> event::Status { self.content.as_widget_mut().on_event( &mut state.children[0], @@ -409,6 +410,7 @@ mod toast { renderer, clipboard, shell, + viewport, ) } @@ -559,6 +561,8 @@ mod toast { } } + let viewport = layout.bounds(); + self.toasts .iter_mut() .zip(self.state.iter_mut()) @@ -576,6 +580,7 @@ mod toast { renderer, clipboard, &mut local_shell, + &viewport, ); if !local_shell.is_empty() { -- cgit From dd5ef8b90895f626d4b8f0466c4457c5abf451a0 Mon Sep 17 00:00:00 2001 From: Joao Freitas <51237625+jhff@users.noreply.github.com> Date: Thu, 13 Jul 2023 13:51:29 +0100 Subject: Add ComboBox widget - Widget implementation - Widget helper - Example --- examples/combo_box/Cargo.toml | 9 +++ examples/combo_box/README.md | 18 ++++++ examples/combo_box/src/main.rs | 143 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 examples/combo_box/Cargo.toml create mode 100644 examples/combo_box/README.md create mode 100644 examples/combo_box/src/main.rs (limited to 'examples') diff --git a/examples/combo_box/Cargo.toml b/examples/combo_box/Cargo.toml new file mode 100644 index 00000000..be1b5e32 --- /dev/null +++ b/examples/combo_box/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "combo_box" +version = "0.1.0" +authors = ["Joao Freitas "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } diff --git a/examples/combo_box/README.md b/examples/combo_box/README.md new file mode 100644 index 00000000..5fc87469 --- /dev/null +++ b/examples/combo_box/README.md @@ -0,0 +1,18 @@ +## Combo-Box + +A dropdown list of searchable and selectable options. + +It displays and positions an overlay based on the window position of the widget. + +The __[`main`]__ file contains all the code of the example. + +
+ +
+ +You can run it with `cargo run`: +``` +cargo run --package combo_box +``` + +[`main`]: src/main.rs diff --git a/examples/combo_box/src/main.rs b/examples/combo_box/src/main.rs new file mode 100644 index 00000000..22d05132 --- /dev/null +++ b/examples/combo_box/src/main.rs @@ -0,0 +1,143 @@ +use iced::widget::{ + column, combo_box, container, scrollable, text, vertical_space, +}; +use iced::{Alignment, Element, Length, Sandbox, Settings}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +struct Example { + languages: combo_box::State, + selected_language: Option, + text: String, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + LanguageSelected(Language), + LanguagePreview(Language), + LanguageBlurred, +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Self { + languages: combo_box::State::new(Language::ALL.to_vec()), + selected_language: None, + text: String::new(), + } + } + + fn title(&self) -> String { + String::from("Combo box - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::LanguageSelected(language) => { + self.selected_language = Some(language); + self.text = language.hello().to_string(); + self.languages.unfocus(); + } + Message::LanguagePreview(language) => { + self.text = language.hello().to_string(); + } + Message::LanguageBlurred => { + self.text = self + .selected_language + .map(|language| language.hello().to_string()) + .unwrap_or_default(); + } + } + } + + fn view(&self) -> Element { + let combo_box = combo_box( + &self.languages, + "Type a language...", + self.selected_language.as_ref(), + Message::LanguageSelected, + ) + .on_selection(Message::LanguagePreview) + .on_blur(Message::LanguageBlurred) + .width(250); + + let content = column![ + "What is your language?", + combo_box, + vertical_space(150), + text(&self.text), + ] + .width(Length::Fill) + .align_items(Alignment::Center) + .spacing(10); + + container(scrollable(content)) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Language { + Danish, + #[default] + English, + French, + German, + Italian, + Portuguese, + Spanish, + Other, +} + +impl Language { + const ALL: [Language; 8] = [ + Language::Danish, + Language::English, + Language::French, + Language::German, + Language::Italian, + Language::Portuguese, + Language::Spanish, + Language::Other, + ]; + + fn hello(&self) -> &str { + match self { + Language::Danish => "Halloy!", + Language::English => "Hello!", + Language::French => "Salut!", + Language::German => "Hallo!", + Language::Italian => "Ciao!", + Language::Portuguese => "Olá!", + Language::Spanish => "¡Hola!", + Language::Other => "... hello?", + } + } +} + +impl std::fmt::Display for Language { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Language::Danish => "Danish", + Language::English => "English", + Language::French => "French", + Language::German => "German", + Language::Italian => "Italian", + Language::Portuguese => "Portuguese", + Language::Spanish => "Spanish", + Language::Other => "Some other language", + } + ) + } +} -- cgit From 470e13c806f4239ca3f2e90e9c0a794a81e354d8 Mon Sep 17 00:00:00 2001 From: Joao Freitas <51237625+jhff@users.noreply.github.com> Date: Thu, 13 Jul 2023 14:18:57 +0100 Subject: Add gif to example --- examples/combo_box/README.md | 2 +- examples/combo_box/combobox.gif | Bin 0 -> 1414629 bytes 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 examples/combo_box/combobox.gif (limited to 'examples') diff --git a/examples/combo_box/README.md b/examples/combo_box/README.md index 5fc87469..9cd224ad 100644 --- a/examples/combo_box/README.md +++ b/examples/combo_box/README.md @@ -7,7 +7,7 @@ It displays and positions an overlay based on the window position of the widget. The __[`main`]__ file contains all the code of the example.
- +
You can run it with `cargo run`: diff --git a/examples/combo_box/combobox.gif b/examples/combo_box/combobox.gif new file mode 100644 index 00000000..f216c026 Binary files /dev/null and b/examples/combo_box/combobox.gif differ -- cgit From 28d32a8b6463b5756aa7cc497c1e26e173f70bee Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Jul 2023 22:34:56 +0200 Subject: Fix `on_option_hovered` support in `ComboBox` --- examples/combo_box/src/main.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/combo_box/src/main.rs b/examples/combo_box/src/main.rs index 22d05132..2e6f95d5 100644 --- a/examples/combo_box/src/main.rs +++ b/examples/combo_box/src/main.rs @@ -15,9 +15,9 @@ struct Example { #[derive(Debug, Clone, Copy)] enum Message { - LanguageSelected(Language), - LanguagePreview(Language), - LanguageBlurred, + Selected(Language), + OptionHovered(Language), + Closed, } impl Sandbox for Example { @@ -37,15 +37,15 @@ impl Sandbox for Example { fn update(&mut self, message: Message) { match message { - Message::LanguageSelected(language) => { + Message::Selected(language) => { self.selected_language = Some(language); self.text = language.hello().to_string(); self.languages.unfocus(); } - Message::LanguagePreview(language) => { + Message::OptionHovered(language) => { self.text = language.hello().to_string(); } - Message::LanguageBlurred => { + Message::Closed => { self.text = self .selected_language .map(|language| language.hello().to_string()) @@ -59,17 +59,17 @@ impl Sandbox for Example { &self.languages, "Type a language...", self.selected_language.as_ref(), - Message::LanguageSelected, + Message::Selected, ) - .on_selection(Message::LanguagePreview) - .on_blur(Message::LanguageBlurred) + .on_option_hovered(Message::OptionHovered) + .on_close(Message::Closed) .width(250); let content = column![ + text(&self.text), "What is your language?", combo_box, vertical_space(150), - text(&self.text), ] .width(Length::Fill) .align_items(Alignment::Center) -- cgit From e2ba7ece83f141c149659747977147392df008f4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 27 Jul 2023 01:02:28 +0200 Subject: Introduce `visible_bounds` operation for `Container` --- examples/toast/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 5d29e895..42f6c348 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -381,7 +381,7 @@ mod toast { renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { + operation.container(None, layout.bounds(), &mut |operation| { self.content.as_widget().operate( &mut state.children[0], layout, @@ -622,7 +622,7 @@ mod toast { renderer: &Renderer, operation: &mut dyn widget::Operation, ) { - operation.container(None, &mut |operation| { + operation.container(None, layout.bounds(), &mut |operation| { self.toasts .iter() .zip(self.state.iter_mut()) -- cgit From 09f2887da582ed7d39a56b2c0b37d0b24b6c1e57 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 27 Jul 2023 01:02:47 +0200 Subject: Create `visible_bounds` example --- examples/visible_bounds/Cargo.toml | 10 +++ examples/visible_bounds/src/main.rs | 151 ++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 examples/visible_bounds/Cargo.toml create mode 100644 examples/visible_bounds/src/main.rs (limited to 'examples') diff --git a/examples/visible_bounds/Cargo.toml b/examples/visible_bounds/Cargo.toml new file mode 100644 index 00000000..cfa56dd2 --- /dev/null +++ b/examples/visible_bounds/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "visible_bounds" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } +once_cell = "1" diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs new file mode 100644 index 00000000..bd7ccdc0 --- /dev/null +++ b/examples/visible_bounds/src/main.rs @@ -0,0 +1,151 @@ +use iced::executor; +use iced::mouse; +use iced::subscription::{self, Subscription}; +use iced::theme::{self, Theme}; +use iced::widget::{column, container, scrollable, text, vertical_space}; +use iced::{ + Application, Command, Element, Event, Length, Point, Rectangle, Settings, +}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +struct Example { + mouse_position: Option, + outer_bounds: Option, + inner_bounds: Option, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + MouseMoved(Point), + Scrolled(scrollable::Viewport), + OuterBoundsFetched(Option), + InnerBoundsFetched(Option), +} + +impl Application for Example { + type Message = Message; + type Theme = Theme; + type Flags = (); + type Executor = executor::Default; + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + Self { + mouse_position: None, + outer_bounds: None, + inner_bounds: None, + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Visible bounds - Iced") + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::MouseMoved(position) => { + self.mouse_position = Some(position); + + Command::none() + } + Message::Scrolled(_) => Command::batch(vec![ + container::visible_bounds(OUTER_CONTAINER.clone()) + .map(Message::OuterBoundsFetched), + container::visible_bounds(INNER_CONTAINER.clone()) + .map(Message::InnerBoundsFetched), + ]), + Message::OuterBoundsFetched(outer_bounds) => { + self.outer_bounds = outer_bounds; + + Command::none() + } + Message::InnerBoundsFetched(inner_bounds) => { + self.inner_bounds = inner_bounds; + + Command::none() + } + } + } + + fn view(&self) -> Element { + let view_bounds = |label, bounds| { + text(format!( + "The {label} container is {}", + match bounds { + Some(bounds) => format!("visible at {:?}", bounds), + None => "not visible".to_string(), + } + )) + }; + + column![ + text(format!( + "Mouse position is {}", + match self.mouse_position { + Some(Point { x, y }) => format!("({x}, {y})"), + None => "unknown".to_string(), + } + )), + view_bounds("outer", self.outer_bounds), + view_bounds("inner", self.inner_bounds), + scrollable( + column![ + text("Scroll me!"), + vertical_space(400), + container(text("I am the outer container!")) + .id(OUTER_CONTAINER.clone()) + .padding(40) + .style(theme::Container::Box), + vertical_space(400), + scrollable( + column![ + text("Scroll me!"), + vertical_space(400), + container(text("I am the inner container!")) + .id(INNER_CONTAINER.clone()) + .padding(40) + .style(theme::Container::Box), + vertical_space(400) + ] + .padding(20) + ) + .on_scroll(Message::Scrolled) + .width(Length::Fill) + .height(300), + ] + .padding(20) + ) + .on_scroll(Message::Scrolled) + .width(Length::Fill) + .height(300), + ] + .spacing(10) + .padding(20) + .into() + } + + fn subscription(&self) -> Subscription { + subscription::events_with(|event, _| match event { + Event::Mouse(mouse::Event::CursorMoved { position }) => { + Some(Message::MouseMoved(position)) + } + _ => None, + }) + } + + fn theme(&self) -> Theme { + Theme::Dark + } +} + +use once_cell::sync::Lazy; + +static OUTER_CONTAINER: Lazy = + Lazy::new(|| container::Id::new("outer")); +static INNER_CONTAINER: Lazy = + Lazy::new(|| container::Id::new("inner")); -- cgit From 8961fcd50179d76fa07edfeedee9edc1a0aa93ec Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 27 Jul 2023 01:21:50 +0200 Subject: Highlight container bounds on hover in `visible_bounds` example --- examples/visible_bounds/src/main.rs | 57 ++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 14 deletions(-) (limited to 'examples') diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs index bd7ccdc0..8bc645b7 100644 --- a/examples/visible_bounds/src/main.rs +++ b/examples/visible_bounds/src/main.rs @@ -2,9 +2,12 @@ use iced::executor; use iced::mouse; use iced::subscription::{self, Subscription}; use iced::theme::{self, Theme}; -use iced::widget::{column, container, scrollable, text, vertical_space}; +use iced::widget::{ + column, container, horizontal_space, row, scrollable, text, vertical_space, +}; use iced::{ - Application, Command, Element, Event, Length, Point, Rectangle, Settings, + Alignment, Application, Color, Command, Element, Event, Font, Length, + Point, Rectangle, Settings, }; pub fn main() -> iced::Result { @@ -73,26 +76,52 @@ impl Application for Example { } fn view(&self) -> Element { - let view_bounds = |label, bounds| { - text(format!( - "The {label} container is {}", + let data_row = |label, value, color| { + row![ + text(label), + horizontal_space(Length::Fill), + text(value).font(Font::MONOSPACE).size(14).style(color), + ] + .height(40) + .align_items(Alignment::Center) + }; + + let view_bounds = |label, bounds: Option| { + data_row( + label, match bounds { - Some(bounds) => format!("visible at {:?}", bounds), + Some(bounds) => format!("{:?}", bounds), None => "not visible".to_string(), - } - )) + }, + if bounds + .zip(self.mouse_position) + .map(|(bounds, mouse_position)| { + bounds.contains(mouse_position) + }) + .unwrap_or_default() + { + Color { + g: 1.0, + ..Color::BLACK + } + .into() + } else { + theme::Text::Default + }, + ) }; column![ - text(format!( - "Mouse position is {}", + data_row( + "Mouse position", match self.mouse_position { Some(Point { x, y }) => format!("({x}, {y})"), None => "unknown".to_string(), - } - )), - view_bounds("outer", self.outer_bounds), - view_bounds("inner", self.inner_bounds), + }, + theme::Text::Default, + ), + view_bounds("Outer container", self.outer_bounds), + view_bounds("Inner container", self.inner_bounds), scrollable( column![ text("Scroll me!"), -- cgit From cbb5fcc8829e6fbe60f97cad8597c86ffd4f5b1a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 27 Jul 2023 01:29:20 +0200 Subject: Fetch bounds on window resize in `visible_bounds` example --- examples/visible_bounds/src/main.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs index 8bc645b7..8b684514 100644 --- a/examples/visible_bounds/src/main.rs +++ b/examples/visible_bounds/src/main.rs @@ -5,6 +5,7 @@ use iced::theme::{self, Theme}; use iced::widget::{ column, container, horizontal_space, row, scrollable, text, vertical_space, }; +use iced::window; use iced::{ Alignment, Application, Color, Command, Element, Event, Font, Length, Point, Rectangle, Settings, @@ -23,6 +24,7 @@ struct Example { #[derive(Debug, Clone, Copy)] enum Message { MouseMoved(Point), + WindowResized, Scrolled(scrollable::Viewport), OuterBoundsFetched(Option), InnerBoundsFetched(Option), @@ -56,12 +58,14 @@ impl Application for Example { Command::none() } - Message::Scrolled(_) => Command::batch(vec![ - container::visible_bounds(OUTER_CONTAINER.clone()) - .map(Message::OuterBoundsFetched), - container::visible_bounds(INNER_CONTAINER.clone()) - .map(Message::InnerBoundsFetched), - ]), + Message::Scrolled(_) | Message::WindowResized => { + Command::batch(vec![ + container::visible_bounds(OUTER_CONTAINER.clone()) + .map(Message::OuterBoundsFetched), + container::visible_bounds(INNER_CONTAINER.clone()) + .map(Message::InnerBoundsFetched), + ]) + } Message::OuterBoundsFetched(outer_bounds) => { self.outer_bounds = outer_bounds; @@ -163,6 +167,9 @@ impl Application for Example { Event::Mouse(mouse::Event::CursorMoved { position }) => { Some(Message::MouseMoved(position)) } + Event::Window(window::Event::Resized { .. }) => { + Some(Message::WindowResized) + } _ => None, }) } -- cgit From 398a3f08973f39ad7cb67a236b2e6b44e57d453b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 23 Aug 2023 20:50:31 +0200 Subject: Use `Dark` theme in `stopwatch` example --- examples/stopwatch/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'examples') diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 9581a3ce..842ba3d4 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -134,4 +134,8 @@ impl Application for Stopwatch { .center_y() .into() } + + fn theme(&self) -> Theme { + Theme::Dark + } } -- cgit From 0ae136b5737253e0e74c93e2491ef25f307b73e9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 23 Aug 2023 21:01:15 +0200 Subject: Update vulnerable `env_logger` dependency in examples --- examples/game_of_life/Cargo.toml | 2 +- examples/multitouch/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index f0a794fb..6de45db6 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -10,4 +10,4 @@ iced = { path = "../..", features = ["canvas", "tokio", "debug"] } tokio = { version = "1.0", features = ["sync"] } itertools = "0.9" rustc-hash = "1.1" -env_logger = "0.9" +env_logger = "0.10" diff --git a/examples/multitouch/Cargo.toml b/examples/multitouch/Cargo.toml index f7c8c145..867312f8 100644 --- a/examples/multitouch/Cargo.toml +++ b/examples/multitouch/Cargo.toml @@ -8,5 +8,5 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } tokio = { version = "1.0", features = ["sync"] } -env_logger = "0.9" +env_logger = "0.10" voronator = "0.2" -- cgit From 8830554e4d68289bf037757e5e3e71a50f334512 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 23 Aug 2023 21:01:44 +0200 Subject: Update vulnerable `async-tungstenite` dependency in `websocket` example --- examples/websocket/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml index 03b240c6..12af9658 100644 --- a/examples/websocket/Cargo.toml +++ b/examples/websocket/Cargo.toml @@ -10,7 +10,7 @@ iced = { path = "../..", features = ["tokio", "debug"] } once_cell = "1.15" [dependencies.async-tungstenite] -version = "0.16" +version = "0.23" features = ["tokio-rustls-webpki-roots"] [dependencies.tokio] -- cgit From ed3454301e663a7cb7d73cd56b57b188f4d14a2f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Aug 2023 04:31:21 +0200 Subject: Implement explicit text caching in the widget state tree --- examples/color_palette/src/main.rs | 6 +++--- examples/combo_box/src/main.rs | 1 - examples/custom_quad/src/main.rs | 1 + examples/custom_widget/src/main.rs | 1 + examples/game_of_life/src/main.rs | 2 +- examples/geometry/src/main.rs | 1 + examples/integration/src/main.rs | 13 ++++++------- examples/loading_spinners/src/circular.rs | 1 + examples/loading_spinners/src/linear.rs | 1 + examples/modal/src/main.rs | 11 +++++++++-- examples/toast/src/main.rs | 6 +++++- examples/tour/src/main.rs | 4 ++-- 12 files changed, 31 insertions(+), 17 deletions(-) (limited to 'examples') diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 736a9d53..7dc981d9 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -3,8 +3,8 @@ use iced::mouse; use iced::widget::canvas::{self, Canvas, Frame, Geometry, Path}; use iced::widget::{column, row, text, Slider}; use iced::{ - Color, Element, Length, Point, Rectangle, Renderer, Sandbox, Settings, - Size, Vector, + Color, Element, Length, Pixels, Point, Rectangle, Renderer, Sandbox, + Settings, Size, Vector, }; use palette::{ self, convert::FromColor, rgb::Rgb, Darken, Hsl, Lighten, ShiftHue, @@ -168,7 +168,7 @@ impl Theme { let mut text = canvas::Text { horizontal_alignment: alignment::Horizontal::Center, vertical_alignment: alignment::Vertical::Top, - size: 15.0, + size: Pixels(15.0), ..canvas::Text::default() }; diff --git a/examples/combo_box/src/main.rs b/examples/combo_box/src/main.rs index 2e6f95d5..4f347667 100644 --- a/examples/combo_box/src/main.rs +++ b/examples/combo_box/src/main.rs @@ -40,7 +40,6 @@ impl Sandbox for Example { Message::Selected(language) => { self.selected_language = Some(language); self.text = language.hello().to_string(); - self.languages.unfocus(); } Message::OptionHovered(language) => { self.text = language.hello().to_string(); diff --git a/examples/custom_quad/src/main.rs b/examples/custom_quad/src/main.rs index 4b300116..91401f73 100644 --- a/examples/custom_quad/src/main.rs +++ b/examples/custom_quad/src/main.rs @@ -36,6 +36,7 @@ mod quad { fn layout( &self, + _tree: &widget::Tree, _renderer: &Renderer, _limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index 713bc62d..e0a23541 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -43,6 +43,7 @@ mod circle { fn layout( &self, + _tree: &widget::Tree, _renderer: &Renderer, _limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index e951d734..fa711744 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -591,7 +591,7 @@ mod grid { let text = Text { color: Color::WHITE, - size: 14.0, + size: 14.0.into(), position: Point::new(frame.width(), frame.height()), horizontal_alignment: alignment::Horizontal::Right, vertical_alignment: alignment::Vertical::Bottom, diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 3bc7f46b..a0d505b8 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -26,6 +26,7 @@ mod rainbow { fn layout( &self, + _tree: &widget::Tree, _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 342d4c69..e011a411 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -8,7 +8,7 @@ use iced_wgpu::graphics::Viewport; use iced_wgpu::{wgpu, Backend, Renderer, Settings}; use iced_winit::core::mouse; use iced_winit::core::renderer; -use iced_winit::core::{Color, Size}; +use iced_winit::core::{Color, Font, Pixels, Size}; use iced_winit::runtime::program; use iced_winit::runtime::Debug; use iced_winit::style::Theme; @@ -143,12 +143,11 @@ pub fn main() -> Result<(), Box> { // Initialize iced let mut debug = Debug::new(); - let mut renderer = Renderer::new(Backend::new( - &device, - &queue, - Settings::default(), - format, - )); + let mut renderer = Renderer::new( + Backend::new(&device, &queue, Settings::default(), format), + Font::default(), + Pixels(16.0), + ); let mut state = program::State::new( controls, diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 3898d76e..6bcfd507 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -254,6 +254,7 @@ where fn layout( &self, + _tree: &Tree, _renderer: &iced::Renderer, limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index 20fbe9f3..3addd7bb 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -175,6 +175,7 @@ where fn layout( &self, + _tree: &Tree, _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 8a48f830..5d47f02c 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -285,10 +285,13 @@ mod modal { fn layout( &self, + tree: &widget::Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - self.base.as_widget().layout(renderer, limits) + self.base + .as_widget() + .layout(&tree.children[0], renderer, limits) } fn on_event( @@ -408,7 +411,11 @@ mod modal { .width(Length::Fill) .height(Length::Fill); - let mut child = self.content.as_widget().layout(renderer, &limits); + let mut child = self + .content + .as_widget() + .layout(self.tree, renderer, &limits); + child.align(Alignment::Center, Alignment::Center, limits.max()); let mut node = layout::Node::with_children(self.size, vec![child]); diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 42f6c348..01eea3cc 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -326,10 +326,13 @@ mod toast { fn layout( &self, + tree: &Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - self.content.as_widget().layout(renderer, limits) + self.content + .as_widget() + .layout(&tree.children[0], renderer, limits) } fn tag(&self) -> widget::tree::Tag { @@ -517,6 +520,7 @@ mod toast { 10.0, Alignment::End, self.toasts, + self.state, ) .translate(Vector::new(position.x, position.y)) } diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 13bcd5ff..10de2ae1 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -5,7 +5,7 @@ use iced::widget::{ scrollable, slider, text, text_input, toggler, vertical_space, }; use iced::widget::{Button, Column, Container, Slider}; -use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; +use iced::{Color, Element, Font, Length, Pixels, Renderer, Sandbox, Settings}; pub fn main() -> iced::Result { env_logger::init(); @@ -571,7 +571,7 @@ impl<'a> Step { text_input = text_input.icon(text_input::Icon { font: Font::default(), code_point: '🚀', - size: Some(28.0), + size: Some(Pixels(28.0)), spacing: 10.0, side: text_input::Side::Right, }); -- cgit From a026e917d3364e58fd827995261158d8cb356ce9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 30 Aug 2023 06:36:24 +0200 Subject: Make `widget::Tree` mutable in `Widget::layout` --- examples/custom_quad/src/main.rs | 2 +- examples/custom_widget/src/main.rs | 2 +- examples/geometry/src/main.rs | 2 +- examples/loading_spinners/src/circular.rs | 2 +- examples/loading_spinners/src/linear.rs | 2 +- examples/modal/src/main.rs | 12 +++++++----- examples/toast/src/main.rs | 12 +++++++----- 7 files changed, 19 insertions(+), 15 deletions(-) (limited to 'examples') diff --git a/examples/custom_quad/src/main.rs b/examples/custom_quad/src/main.rs index 91401f73..13b08250 100644 --- a/examples/custom_quad/src/main.rs +++ b/examples/custom_quad/src/main.rs @@ -36,7 +36,7 @@ mod quad { fn layout( &self, - _tree: &widget::Tree, + _tree: &mut widget::Tree, _renderer: &Renderer, _limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index e0a23541..32a14cbe 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -43,7 +43,7 @@ mod circle { fn layout( &self, - _tree: &widget::Tree, + _tree: &mut widget::Tree, _renderer: &Renderer, _limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index a0d505b8..8ab3b493 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -26,7 +26,7 @@ mod rainbow { fn layout( &self, - _tree: &widget::Tree, + _tree: &mut widget::Tree, _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 6bcfd507..bf01c3b4 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -254,7 +254,7 @@ where fn layout( &self, - _tree: &Tree, + _tree: &mut Tree, _renderer: &iced::Renderer, limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index 3addd7bb..c5bb4791 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -175,7 +175,7 @@ where fn layout( &self, - _tree: &Tree, + _tree: &mut Tree, _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 5d47f02c..aa9107d0 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -285,13 +285,15 @@ mod modal { fn layout( &self, - tree: &widget::Tree, + tree: &mut widget::Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - self.base - .as_widget() - .layout(&tree.children[0], renderer, limits) + self.base.as_widget().layout( + &mut tree.children[0], + renderer, + limits, + ) } fn on_event( @@ -402,7 +404,7 @@ mod modal { Message: Clone, { fn layout( - &self, + &mut self, renderer: &Renderer, _bounds: Size, position: Point, diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 01eea3cc..50fa885a 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -326,13 +326,15 @@ mod toast { fn layout( &self, - tree: &Tree, + tree: &mut Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - self.content - .as_widget() - .layout(&tree.children[0], renderer, limits) + self.content.as_widget().layout( + &mut tree.children[0], + renderer, + limits, + ) } fn tag(&self) -> widget::tree::Tag { @@ -503,7 +505,7 @@ mod toast { for Overlay<'a, 'b, Message> { fn layout( - &self, + &mut self, renderer: &Renderer, bounds: Size, position: Point, -- cgit From 6cc354fdc4eec6576e0591cd3a2ce37155cbfa09 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 3 Sep 2023 00:21:04 +0200 Subject: Update `wgpu` to `0.17` --- examples/integration/src/main.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 342d4c69..98b58f16 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -82,7 +82,6 @@ pub fn main() -> Result<(), Box> { futures::futures::executor::block_on(async { let adapter = wgpu::util::initialize_adapter_from_env_or_default( &instance, - backend, Some(&surface), ) .await -- cgit From 3a44ad3c737cf6cff6f04e03a7778d75fcc0c719 Mon Sep 17 00:00:00 2001 From: Akshay Raina <104209297+akshayr-mecha@users.noreply.github.com> Date: Sat, 2 Sep 2023 02:03:39 +0530 Subject: fix(examples-styling): fixed checkbox and toggler getting hidden behind scrollbar Column inside scrollable is having Length::Fill so it is taking entire width thus hiding check box and toggler. Added fixed width to scrollable so Lenth::Fill will be relative to fixed width --- examples/styling/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index f8a4c80a..b457df25 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -108,6 +108,7 @@ impl Sandbox for Styling { column!["Scroll me!", vertical_space(800), "You did it!"] .width(Length::Fill), ) + .width(300) .height(100); let checkbox = checkbox( -- cgit From 20bf01a551b967f6f8be4c38ef01d14a690d8835 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Sun, 3 Sep 2023 08:28:03 +0200 Subject: Use `Length::Fill` instead of fixed length in `styling` example --- examples/styling/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index b457df25..51538ec2 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -108,7 +108,7 @@ impl Sandbox for Styling { column!["Scroll me!", vertical_space(800), "You did it!"] .width(Length::Fill), ) - .width(300) + .width(Length::Fill) .height(100); let checkbox = checkbox( -- cgit From 34495bba1c1ffaa4ea2bab46103b5d66e333c51e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Sep 2023 02:55:09 +0200 Subject: Introduce `keyed::Column` widget --- examples/todos/Cargo.toml | 4 +++- examples/todos/src/main.rs | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 7ad4d558..4a35cfe6 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -7,9 +7,11 @@ publish = false [dependencies] iced = { path = "../..", features = ["async-std", "debug"] } +uuid = { version = "1.0", features = ["v4", "fast-rng", "serde"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -once_cell = "1.15" +once_cell = "1.0" +tracing-subscriber = "0.3" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] async-std = "1.0" diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 6ad7b4fb..1dd8a307 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -5,8 +5,8 @@ use iced::keyboard::{self, KeyCode, Modifiers}; use iced::subscription; use iced::theme::{self, Theme}; use iced::widget::{ - self, button, checkbox, column, container, row, scrollable, text, - text_input, Text, + self, button, checkbox, column, container, keyed_column, row, scrollable, + text, text_input, Text, }; use iced::window; use iced::{Application, Element}; @@ -14,10 +14,13 @@ use iced::{Color, Command, Length, Settings, Subscription}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; +use uuid::Uuid; static INPUT_ID: Lazy = Lazy::new(text_input::Id::unique); pub fn main() -> iced::Result { + tracing_subscriber::fmt::init(); + Todos::run(Settings { window: window::Settings { size: (500, 800), @@ -222,17 +225,19 @@ impl Application for Todos { tasks.iter().filter(|task| filter.matches(task)); let tasks: Element<_> = if filtered_tasks.count() > 0 { - column( + keyed_column( tasks .iter() .enumerate() .filter(|(_, task)| filter.matches(task)) .map(|(i, task)| { - task.view(i).map(move |message| { - Message::TaskMessage(i, message) - }) - }) - .collect(), + ( + task.id, + task.view(i).map(move |message| { + Message::TaskMessage(i, message) + }), + ) + }), ) .spacing(10) .into() @@ -295,6 +300,8 @@ impl Application for Todos { #[derive(Debug, Clone, Serialize, Deserialize)] struct Task { + #[serde(default = "Uuid::new_v4")] + id: Uuid, description: String, completed: bool, @@ -330,6 +337,7 @@ impl Task { fn new(description: String) -> Self { Task { + id: Uuid::new_v4(), description, completed: false, state: TaskState::Idle, -- cgit From 837529bc995a728300c61fc102474cc31f7a6500 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Sep 2023 03:02:46 +0200 Subject: Fix Wasm build of `todos` example --- examples/todos/Cargo.toml | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 4a35cfe6..7292f665 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -18,6 +18,7 @@ async-std = "1.0" directories-next = "2.0" [target.'cfg(target_arch = "wasm32")'.dependencies] +uuid = { version = "1.0", features = ["js"] } web-sys = { version = "0.3", features = ["Window", "Storage"] } wasm-timer = "0.2" -- cgit From f468e25d0c67a01ee79d892f6e8ba9be019f06c7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Sep 2023 12:58:41 +0200 Subject: Use workspace dependencies and package inheritance We are also taking this as a chance to synchronize the versions of all the crates! Because of this, we will skip the `0.11` version. --- examples/arc/Cargo.toml | 3 ++- examples/bezier_tool/Cargo.toml | 3 ++- examples/checkbox/Cargo.toml | 2 +- examples/clock/Cargo.toml | 6 ++++-- examples/color_palette/Cargo.toml | 6 ++++-- examples/combo_box/Cargo.toml | 3 ++- examples/component/Cargo.toml | 3 ++- examples/counter/Cargo.toml | 2 +- examples/custom_quad/Cargo.toml | 3 ++- examples/custom_widget/Cargo.toml | 3 ++- examples/download_progress/Cargo.toml | 3 ++- examples/events/Cargo.toml | 3 ++- examples/exit/Cargo.toml | 2 +- examples/game_of_life/Cargo.toml | 10 ++++++---- examples/game_of_life/src/main.rs | 2 +- examples/geometry/Cargo.toml | 3 ++- examples/integration/Cargo.toml | 13 +++++++------ examples/integration/src/main.rs | 2 +- examples/lazy/Cargo.toml | 3 ++- examples/loading_spinners/Cargo.toml | 8 +++++--- examples/modal/Cargo.toml | 3 ++- examples/multitouch/Cargo.toml | 7 ++++--- examples/multitouch/src/main.rs | 2 +- examples/pane_grid/Cargo.toml | 3 ++- examples/pick_list/Cargo.toml | 3 ++- examples/pokedex/Cargo.toml | 4 +++- examples/progress_bar/Cargo.toml | 2 +- examples/qr_code/Cargo.toml | 3 ++- examples/screenshot/Cargo.toml | 8 +++++--- examples/screenshot/src/main.rs | 2 +- examples/scrollable/Cargo.toml | 6 ++++-- examples/sierpinski_triangle/Cargo.toml | 6 ++++-- examples/slider/Cargo.toml | 2 +- examples/solar_system/Cargo.toml | 6 ++++-- examples/solar_system/src/main.rs | 2 +- examples/stopwatch/Cargo.toml | 3 ++- examples/styling/Cargo.toml | 2 +- examples/svg/Cargo.toml | 3 ++- examples/system_information/Cargo.toml | 6 ++++-- examples/toast/Cargo.toml | 3 ++- examples/todos/Cargo.toml | 12 +++++++----- examples/tooltip/Cargo.toml | 3 ++- examples/tour/Cargo.toml | 6 ++++-- examples/tour/src/main.rs | 2 +- examples/url_handler/Cargo.toml | 2 +- examples/visible_bounds/Cargo.toml | 6 ++++-- examples/websocket/Cargo.toml | 12 ++++++------ 47 files changed, 124 insertions(+), 78 deletions(-) (limited to 'examples') diff --git a/examples/arc/Cargo.toml b/examples/arc/Cargo.toml index e6e74363..5012ff82 100644 --- a/examples/arc/Cargo.toml +++ b/examples/arc/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +iced.workspace = true +iced.features = ["canvas", "tokio", "debug"] diff --git a/examples/bezier_tool/Cargo.toml b/examples/bezier_tool/Cargo.toml index 890e3027..b2547ff1 100644 --- a/examples/bezier_tool/Cargo.toml +++ b/examples/bezier_tool/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas"] } +iced.workspace = true +iced.features = ["canvas"] diff --git a/examples/checkbox/Cargo.toml b/examples/checkbox/Cargo.toml index dde8f910..1e027c4c 100644 --- a/examples/checkbox/Cargo.toml +++ b/examples/checkbox/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml index 5e869eb5..2d3d5908 100644 --- a/examples/clock/Cargo.toml +++ b/examples/clock/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -time = { version = "0.3.5", features = ["local-offset"] } +iced.workspace = true +iced.features = ["canvas", "tokio", "debug"] + +time = { version = "0.3", features = ["local-offset"] } diff --git a/examples/color_palette/Cargo.toml b/examples/color_palette/Cargo.toml index 3be732bb..2da6c6ed 100644 --- a/examples/color_palette/Cargo.toml +++ b/examples/color_palette/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "palette"] } -palette = "0.7.0" +iced.workspace = true +iced.features = ["canvas", "palette"] + +palette.workspace = true diff --git a/examples/combo_box/Cargo.toml b/examples/combo_box/Cargo.toml index be1b5e32..0f5ecf2a 100644 --- a/examples/combo_box/Cargo.toml +++ b/examples/combo_box/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug"] } +iced.workspace = true +iced.features = ["debug"] diff --git a/examples/component/Cargo.toml b/examples/component/Cargo.toml index 9db1e6b4..83b7b8a4 100644 --- a/examples/component/Cargo.toml +++ b/examples/component/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug", "lazy"] } +iced.workspace = true +iced.features = ["debug", "lazy"] diff --git a/examples/counter/Cargo.toml b/examples/counter/Cargo.toml index e31f440f..1e9bba6b 100644 --- a/examples/counter/Cargo.toml +++ b/examples/counter/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/custom_quad/Cargo.toml b/examples/custom_quad/Cargo.toml index f097c2dd..31b5055d 100644 --- a/examples/custom_quad/Cargo.toml +++ b/examples/custom_quad/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["advanced"] } +iced.workspace = true +iced.features = ["advanced"] diff --git a/examples/custom_widget/Cargo.toml b/examples/custom_widget/Cargo.toml index dda0efe8..1e94bc52 100644 --- a/examples/custom_widget/Cargo.toml +++ b/examples/custom_widget/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["advanced"] } +iced.workspace = true +iced.features = ["advanced"] diff --git a/examples/download_progress/Cargo.toml b/examples/download_progress/Cargo.toml index 212832f4..18a49f66 100644 --- a/examples/download_progress/Cargo.toml +++ b/examples/download_progress/Cargo.toml @@ -6,7 +6,8 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["tokio"] } +iced.workspace = true +iced.features = ["tokio"] [dependencies.reqwest] version = "0.11" diff --git a/examples/events/Cargo.toml b/examples/events/Cargo.toml index 15ffc0af..87315a10 100644 --- a/examples/events/Cargo.toml +++ b/examples/events/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug"] } +iced.workspace = true +iced.features = ["debug"] diff --git a/examples/exit/Cargo.toml b/examples/exit/Cargo.toml index 34d0789a..b06fbadc 100644 --- a/examples/exit/Cargo.toml +++ b/examples/exit/Cargo.toml @@ -5,4 +5,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index 6de45db6..f8d21c65 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -6,8 +6,10 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -tokio = { version = "1.0", features = ["sync"] } +iced.workspace = true +iced.features = ["debug", "canvas", "tokio"] + itertools = "0.9" -rustc-hash = "1.1" -env_logger = "0.10" +rustc-hash.workspace = true +tokio = { workspace = true, features = ["sync"] } +tracing-subscriber = "0.3" diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index e951d734..a2038f12 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -18,7 +18,7 @@ use iced::{ use std::time::{Duration, Instant}; pub fn main() -> iced::Result { - env_logger::builder().format_timestamp(None).init(); + tracing_subscriber::fmt::init(); GameOfLife::run(Settings { antialiasing: true, diff --git a/examples/geometry/Cargo.toml b/examples/geometry/Cargo.toml index 6068d651..9606dcb3 100644 --- a/examples/geometry/Cargo.toml +++ b/examples/geometry/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["advanced"] } +iced.workspace = true +iced.features = ["advanced"] diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 22914742..4c55daa7 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -6,18 +6,19 @@ edition = "2021" publish = false [dependencies] -iced_winit = { path = "../../winit" } -iced_wgpu = { path = "../../wgpu" } -iced_widget = { path = "../../widget" } -iced_renderer = { path = "../../renderer", features = ["wgpu"] } -env_logger = "0.10" +iced_winit.workspace = true +iced_wgpu.workspace = true +iced_widget.workspace = true + +tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] console_error_panic_hook = "0.1.7" console_log = "0.2.0" -log = "0.4" +log.workspace = true wasm-bindgen = "0.2" web-sys = { version = "0.3", features = ["Element", "HtmlCanvasElement", "Window", "Document"] } + # This dependency a little bit quirky, it is deep in the tree and without `js` feature it # refuses to work with `wasm32-unknown-unknown target`. Unfortunately, we need this patch # to make it work diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 98b58f16..af48af5f 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -41,7 +41,7 @@ pub fn main() -> Result<(), Box> { }; #[cfg(not(target_arch = "wasm32"))] - env_logger::init(); + tracing_subscriber::fmt::init(); // Initialize winit let event_loop = EventLoop::new(); diff --git a/examples/lazy/Cargo.toml b/examples/lazy/Cargo.toml index e03e89a9..4ccb9584 100644 --- a/examples/lazy/Cargo.toml +++ b/examples/lazy/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug", "lazy"] } +iced.workspace = true +iced.features = ["debug", "lazy"] diff --git a/examples/loading_spinners/Cargo.toml b/examples/loading_spinners/Cargo.toml index ee9a48aa..a32da386 100644 --- a/examples/loading_spinners/Cargo.toml +++ b/examples/loading_spinners/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["advanced", "canvas"] } -lyon_algorithms = "1" -once_cell = "1" +iced.workspace = true +iced.features = ["advanced", "canvas"] + +lyon_algorithms = "1.0" +once_cell.workspace = true \ No newline at end of file diff --git a/examples/modal/Cargo.toml b/examples/modal/Cargo.toml index 3ac61e6a..009d9653 100644 --- a/examples/modal/Cargo.toml +++ b/examples/modal/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["advanced"] } +iced.workspace = true +iced.features = ["advanced"] diff --git a/examples/multitouch/Cargo.toml b/examples/multitouch/Cargo.toml index 867312f8..e0d14f58 100644 --- a/examples/multitouch/Cargo.toml +++ b/examples/multitouch/Cargo.toml @@ -6,7 +6,8 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -tokio = { version = "1.0", features = ["sync"] } -env_logger = "0.10" +iced.workspace = true +iced.features = ["debug", "canvas", "tokio"] + +tracing-subscriber = "0.3" voronator = "0.2" diff --git a/examples/multitouch/src/main.rs b/examples/multitouch/src/main.rs index 2830b78d..ba8df7aa 100644 --- a/examples/multitouch/src/main.rs +++ b/examples/multitouch/src/main.rs @@ -13,7 +13,7 @@ use iced::{ use std::collections::HashMap; pub fn main() -> iced::Result { - env_logger::builder().format_timestamp(None).init(); + tracing_subscriber::fmt::init(); Multitouch::run(Settings { antialiasing: true, diff --git a/examples/pane_grid/Cargo.toml b/examples/pane_grid/Cargo.toml index 4c0bf072..095ecd10 100644 --- a/examples/pane_grid/Cargo.toml +++ b/examples/pane_grid/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug", "lazy"] } +iced.workspace = true +iced.features = ["debug", "lazy"] diff --git a/examples/pick_list/Cargo.toml b/examples/pick_list/Cargo.toml index 4aa4603a..030558e7 100644 --- a/examples/pick_list/Cargo.toml +++ b/examples/pick_list/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug"] } +iced.workspace = true +iced.features = ["debug"] diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index e99fc0c3..de8a5c17 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -6,7 +6,9 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["image", "debug", "tokio"] } +iced.workspace = true +iced.features = ["image", "debug", "tokio"] + serde_json = "1.0" [dependencies.serde] diff --git a/examples/progress_bar/Cargo.toml b/examples/progress_bar/Cargo.toml index 383a9bdd..6624ae15 100644 --- a/examples/progress_bar/Cargo.toml +++ b/examples/progress_bar/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/qr_code/Cargo.toml b/examples/qr_code/Cargo.toml index 2f164df6..8f33ea8c 100644 --- a/examples/qr_code/Cargo.toml +++ b/examples/qr_code/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["qr_code"] } +iced.workspace = true +iced.features = ["qr_code"] diff --git a/examples/screenshot/Cargo.toml b/examples/screenshot/Cargo.toml index b79300b7..dcd77439 100644 --- a/examples/screenshot/Cargo.toml +++ b/examples/screenshot/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug", "image", "advanced"] } -image = { version = "0.24.6", features = ["png"]} -env_logger = "0.10.0" +iced.workspace = true +iced.features = ["debug", "image", "advanced"] + +image = { workspace = true, features = ["png"]} +tracing-subscriber = "0.3" \ No newline at end of file diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 83824535..1d8eaa44 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -13,7 +13,7 @@ use ::image as img; use ::image::ColorType; fn main() -> iced::Result { - env_logger::builder().format_timestamp(None).init(); + tracing_subscriber::fmt::init(); Example::run(iced::Settings::default()) } diff --git a/examples/scrollable/Cargo.toml b/examples/scrollable/Cargo.toml index e6411e26..f8c735c0 100644 --- a/examples/scrollable/Cargo.toml +++ b/examples/scrollable/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug"] } -once_cell = "1.16.0" +iced.workspace = true +iced.features = ["debug"] + +once_cell.workspace = true diff --git a/examples/sierpinski_triangle/Cargo.toml b/examples/sierpinski_triangle/Cargo.toml index 39d45f64..600a9e06 100644 --- a/examples/sierpinski_triangle/Cargo.toml +++ b/examples/sierpinski_triangle/Cargo.toml @@ -6,5 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "debug"] } -rand = "0.8.4" +iced.workspace = true +iced.features = ["debug", "canvas"] + +rand = "0.8" diff --git a/examples/slider/Cargo.toml b/examples/slider/Cargo.toml index 112d7cff..fad8916e 100644 --- a/examples/slider/Cargo.toml +++ b/examples/slider/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/solar_system/Cargo.toml b/examples/solar_system/Cargo.toml index 1a98a87e..ca64da14 100644 --- a/examples/solar_system/Cargo.toml +++ b/examples/solar_system/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -env_logger = "0.10.0" +iced.workspace = true +iced.features = ["debug", "canvas", "tokio"] + rand = "0.8.3" +tracing-subscriber = "0.3" diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 58d06206..8fa8946e 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -23,7 +23,7 @@ use iced::{ use std::time::Instant; pub fn main() -> iced::Result { - env_logger::builder().format_timestamp(None).init(); + tracing_subscriber::fmt::init(); SolarSystem::run(Settings { antialiasing: true, diff --git a/examples/stopwatch/Cargo.toml b/examples/stopwatch/Cargo.toml index f623feb9..6b1419f6 100644 --- a/examples/stopwatch/Cargo.toml +++ b/examples/stopwatch/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["smol"] } +iced.workspace = true +iced.features = ["smol"] diff --git a/examples/styling/Cargo.toml b/examples/styling/Cargo.toml index f771708c..c8a90258 100644 --- a/examples/styling/Cargo.toml +++ b/examples/styling/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/svg/Cargo.toml b/examples/svg/Cargo.toml index f5a6eaa2..78208fb0 100644 --- a/examples/svg/Cargo.toml +++ b/examples/svg/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["svg"] } +iced.workspace = true +iced.features = ["svg"] diff --git a/examples/system_information/Cargo.toml b/examples/system_information/Cargo.toml index 7d1e4b94..41903122 100644 --- a/examples/system_information/Cargo.toml +++ b/examples/system_information/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["system"] } -bytesize = { version = "1.1.0" } +iced.workspace = true +iced.features = ["system"] + +bytesize = "1.1" diff --git a/examples/toast/Cargo.toml b/examples/toast/Cargo.toml index f703572c..113313e2 100644 --- a/examples/toast/Cargo.toml +++ b/examples/toast/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["advanced"] } +iced.workspace = true +iced.features = ["advanced"] diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 7ad4d558..fea20375 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -6,18 +6,20 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["async-std", "debug"] } +iced.workspace = true +iced.features = ["async-std", "debug"] + serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -once_cell = "1.15" +once_cell.workspace = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -async-std = "1.0" +async-std.workspace = true directories-next = "2.0" [target.'cfg(target_arch = "wasm32")'.dependencies] -web-sys = { version = "0.3", features = ["Window", "Storage"] } -wasm-timer = "0.2" +web-sys = { workspace = true, features = ["Window", "Storage"] } +wasm-timer.workspace = true [package.metadata.deb] assets = [ diff --git a/examples/tooltip/Cargo.toml b/examples/tooltip/Cargo.toml index 25840fb4..57bb0dcb 100644 --- a/examples/tooltip/Cargo.toml +++ b/examples/tooltip/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug"] } +iced.workspace = true +iced.features = ["debug"] diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 48471f2d..21206ecb 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["image", "debug"] } -env_logger = "0.10.0" +iced.workspace = true +iced.features = ["image", "debug"] + +tracing-subscriber = "0.3" diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 13bcd5ff..af508206 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -8,7 +8,7 @@ use iced::widget::{Button, Column, Container, Slider}; use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; pub fn main() -> iced::Result { - env_logger::init(); + tracing_subscriber::fmt::init(); Tour::run(Settings::default()) } diff --git a/examples/url_handler/Cargo.toml b/examples/url_handler/Cargo.toml index 4dcff92d..7bb9914b 100644 --- a/examples/url_handler/Cargo.toml +++ b/examples/url_handler/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced.workspace = true diff --git a/examples/visible_bounds/Cargo.toml b/examples/visible_bounds/Cargo.toml index cfa56dd2..37594b84 100644 --- a/examples/visible_bounds/Cargo.toml +++ b/examples/visible_bounds/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug"] } -once_cell = "1" +iced.workspace = true +iced.features = ["debug"] + +once_cell.workspace = true diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml index 12af9658..2756e8e0 100644 --- a/examples/websocket/Cargo.toml +++ b/examples/websocket/Cargo.toml @@ -6,16 +6,16 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["tokio", "debug"] } -once_cell = "1.15" +iced.workspace = true +iced.features = ["debug", "tokio"] + +once_cell.workspace = true +warp = "0.3" [dependencies.async-tungstenite] version = "0.23" features = ["tokio-rustls-webpki-roots"] [dependencies.tokio] -version = "1" +workspace = true features = ["time"] - -[dependencies.warp] -version = "0.3" -- cgit From 08a031cbe5913c249efa7fc82556d5d95f981c4c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 02:45:15 +0200 Subject: Introduce `keyboard::on_key_press` and `on_key_release` Also rename `subscription::events*` to `event::listen*`. --- examples/events/src/main.rs | 5 ++--- examples/modal/src/main.rs | 8 +++++--- examples/pane_grid/src/main.rs | 14 +++---------- examples/screenshot/src/main.rs | 7 +++---- examples/toast/src/main.rs | 8 +++++--- examples/todos/src/main.rs | 40 +++++++++++-------------------------- examples/url_handler/src/main.rs | 13 ++++++------ examples/visible_bounds/src/main.rs | 8 ++++---- 8 files changed, 41 insertions(+), 62 deletions(-) (limited to 'examples') diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 7f3a5e1d..32d0da2c 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -1,9 +1,8 @@ use iced::alignment; +use iced::event::{self, Event}; use iced::executor; -use iced::subscription; use iced::widget::{button, checkbox, container, text, Column}; use iced::window; -use iced::Event; use iced::{ Alignment, Application, Command, Element, Length, Settings, Subscription, Theme, @@ -71,7 +70,7 @@ impl Application for Events { } fn subscription(&self) -> Subscription { - subscription::events().map(Message::EventOccurred) + event::listen().map(Message::EventOccurred) } fn view(&self) -> Element { diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 8a48f830..4aa70886 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -1,12 +1,14 @@ +use iced::event::{self, Event}; use iced::executor; use iced::keyboard; -use iced::subscription::{self, Subscription}; use iced::theme; use iced::widget::{ self, button, column, container, horizontal_space, pick_list, row, text, text_input, }; -use iced::{Alignment, Application, Command, Element, Event, Length, Settings}; +use iced::{ + Alignment, Application, Command, Element, Length, Settings, Subscription, +}; use modal::Modal; use std::fmt; @@ -49,7 +51,7 @@ impl Application for App { } fn subscription(&self) -> Subscription { - subscription::events().map(Message::Event) + event::listen().map(Message::Event) } fn update(&mut self, message: Message) -> Command { diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 04896e20..af87e2c0 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -1,8 +1,6 @@ use iced::alignment::{self, Alignment}; -use iced::event::{self, Event}; use iced::executor; use iced::keyboard; -use iced::subscription; use iced::theme::{self, Theme}; use iced::widget::pane_grid::{self, PaneGrid}; use iced::widget::{ @@ -146,18 +144,12 @@ impl Application for Example { } fn subscription(&self) -> Subscription { - subscription::events_with(|event, status| { - if let event::Status::Captured = status { + keyboard::on_key_press(|key_code, modifiers| { + if !modifiers.command() { return None; } - match event { - Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) if modifiers.command() => handle_hotkey(key_code), - _ => None, - } + handle_hotkey(key_code) }) } diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 83824535..54446724 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -4,9 +4,8 @@ use iced::theme::{Button, Container}; use iced::widget::{button, column, container, image, row, text, text_input}; use iced::window::screenshot::{self, Screenshot}; use iced::{ - event, executor, keyboard, subscription, Alignment, Application, Command, - ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, - Theme, + event, executor, keyboard, Alignment, Application, Command, ContentFit, + Element, Event, Length, Rectangle, Renderer, Subscription, Theme, }; use ::image as img; @@ -254,7 +253,7 @@ impl Application for Example { } fn subscription(&self) -> Subscription { - subscription::events_with(|event, status| { + event::listen_with(|event, status| { if let event::Status::Captured = status { return None; } diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 42f6c348..47b272a9 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -1,10 +1,12 @@ +use iced::event::{self, Event}; use iced::executor; use iced::keyboard; -use iced::subscription::{self, Subscription}; use iced::widget::{ self, button, column, container, pick_list, row, slider, text, text_input, }; -use iced::{Alignment, Application, Command, Element, Event, Length, Settings}; +use iced::{ + Alignment, Application, Command, Element, Length, Settings, Subscription, +}; use toast::{Status, Toast}; @@ -57,7 +59,7 @@ impl Application for App { } fn subscription(&self) -> Subscription { - subscription::events().map(Message::Event) + event::listen().map(Message::Event) } fn update(&mut self, message: Message) -> Command { diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 6ad7b4fb..62c17926 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,8 +1,6 @@ use iced::alignment::{self, Alignment}; -use iced::event::{self, Event}; use iced::font::{self, Font}; -use iced::keyboard::{self, KeyCode, Modifiers}; -use iced::subscription; +use iced::keyboard; use iced::theme::{self, Theme}; use iced::widget::{ self, button, checkbox, column, container, row, scrollable, text, @@ -52,7 +50,7 @@ enum Message { FilterChanged(Filter), TaskMessage(usize, TaskMessage), TabPressed { shift: bool }, - ToggleFullscreen(window::Mode), + ChangeWindowMode(window::Mode), } impl Application for Todos { @@ -163,7 +161,7 @@ impl Application for Todos { widget::focus_next() } } - Message::ToggleFullscreen(mode) => { + Message::ChangeWindowMode(mode) => { window::change_mode(mode) } _ => Command::none(), @@ -262,33 +260,19 @@ impl Application for Todos { } fn subscription(&self) -> Subscription { - subscription::events_with(|event, status| match (event, status) { - ( - Event::Keyboard(keyboard::Event::KeyPressed { - key_code: keyboard::KeyCode::Tab, - modifiers, - .. + keyboard::on_key_press(|key_code, modifiers| { + match (key_code, modifiers) { + (keyboard::KeyCode::Tab, _) => Some(Message::TabPressed { + shift: modifiers.shift(), }), - event::Status::Ignored, - ) => Some(Message::TabPressed { - shift: modifiers.shift(), - }), - ( - Event::Keyboard(keyboard::Event::KeyPressed { - key_code, - modifiers: Modifiers::SHIFT, - }), - event::Status::Ignored, - ) => match key_code { - KeyCode::Up => { - Some(Message::ToggleFullscreen(window::Mode::Fullscreen)) + (keyboard::KeyCode::Up, keyboard::Modifiers::SHIFT) => { + Some(Message::ChangeWindowMode(window::Mode::Fullscreen)) } - KeyCode::Down => { - Some(Message::ToggleFullscreen(window::Mode::Windowed)) + (keyboard::KeyCode::Down, keyboard::Modifiers::SHIFT) => { + Some(Message::ChangeWindowMode(window::Mode::Windowed)) } _ => None, - }, - _ => None, + } }) } } diff --git a/examples/url_handler/src/main.rs b/examples/url_handler/src/main.rs index f63fa06a..bf570123 100644 --- a/examples/url_handler/src/main.rs +++ b/examples/url_handler/src/main.rs @@ -1,6 +1,5 @@ -use iced::event::{Event, MacOS, PlatformSpecific}; +use iced::event::{self, Event}; use iced::executor; -use iced::subscription; use iced::widget::{container, text}; use iced::{ Application, Command, Element, Length, Settings, Subscription, Theme, @@ -37,9 +36,11 @@ impl Application for App { fn update(&mut self, message: Message) -> Command { match message { Message::EventOccurred(event) => { - if let Event::PlatformSpecific(PlatformSpecific::MacOS( - MacOS::ReceivedUrl(url), - )) = event + if let Event::PlatformSpecific( + event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl( + url, + )), + ) = event { self.url = Some(url); } @@ -50,7 +51,7 @@ impl Application for App { } fn subscription(&self) -> Subscription { - subscription::events().map(Message::EventOccurred) + event::listen().map(Message::EventOccurred) } fn view(&self) -> Element { diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs index 8b684514..42dfc24c 100644 --- a/examples/visible_bounds/src/main.rs +++ b/examples/visible_bounds/src/main.rs @@ -1,14 +1,14 @@ +use iced::event::{self, Event}; use iced::executor; use iced::mouse; -use iced::subscription::{self, Subscription}; use iced::theme::{self, Theme}; use iced::widget::{ column, container, horizontal_space, row, scrollable, text, vertical_space, }; use iced::window; use iced::{ - Alignment, Application, Color, Command, Element, Event, Font, Length, - Point, Rectangle, Settings, + Alignment, Application, Color, Command, Element, Font, Length, Point, + Rectangle, Settings, Subscription, }; pub fn main() -> iced::Result { @@ -163,7 +163,7 @@ impl Application for Example { } fn subscription(&self) -> Subscription { - subscription::events_with(|event, _| match event { + event::listen_with(|event, _| match event { Event::Mouse(mouse::Event::CursorMoved { position }) => { Some(Message::MouseMoved(position)) } -- cgit From d21f0698b505d699c44e9414f902dbeca9474e39 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 02:46:19 +0200 Subject: Add hotkey support for `stopwatch` example --- examples/stopwatch/src/main.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 842ba3d4..0b0f0607 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -1,5 +1,6 @@ use iced::alignment; use iced::executor; +use iced::keyboard; use iced::theme::{self, Theme}; use iced::time; use iced::widget::{button, column, container, row, text}; @@ -77,12 +78,25 @@ impl Application for Stopwatch { } fn subscription(&self) -> Subscription { - match self.state { + let tick = match self.state { State::Idle => Subscription::none(), State::Ticking { .. } => { time::every(Duration::from_millis(10)).map(Message::Tick) } + }; + + fn handle_hotkey( + key_code: keyboard::KeyCode, + _modifiers: keyboard::Modifiers, + ) -> Option { + match key_code { + keyboard::KeyCode::Space => Some(Message::Toggle), + keyboard::KeyCode::R => Some(Message::Reset), + _ => None, + } } + + Subscription::batch(vec![tick, keyboard::on_key_press(handle_hotkey)]) } fn view(&self) -> Element { -- cgit From d315e27451d46a815d18ed6037547d178413ede7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 03:15:02 +0200 Subject: Update example dependencies --- examples/game_of_life/Cargo.toml | 2 +- examples/integration/Cargo.toml | 9 ++------- examples/pokedex/Cargo.toml | 7 +++++-- examples/pokedex/src/main.rs | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index f8d21c65..9b291de8 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -9,7 +9,7 @@ publish = false iced.workspace = true iced.features = ["debug", "canvas", "tokio"] -itertools = "0.9" +itertools = "0.11" rustc-hash.workspace = true tokio = { workspace = true, features = ["sync"] } tracing-subscriber = "0.3" diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 4c55daa7..032cc75e 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -13,13 +13,8 @@ iced_widget.workspace = true tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] -console_error_panic_hook = "0.1.7" -console_log = "0.2.0" +console_error_panic_hook = "0.1" +console_log = "1.0" log.workspace = true wasm-bindgen = "0.2" web-sys = { version = "0.3", features = ["Element", "HtmlCanvasElement", "Window", "Document"] } - -# This dependency a little bit quirky, it is deep in the tree and without `js` feature it -# refuses to work with `wasm32-unknown-unknown target`. Unfortunately, we need this patch -# to make it work -getrandom = { version = "0.2", features = ["js"] } diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index de8a5c17..bf7e1e35 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -21,5 +21,8 @@ default-features = false features = ["json", "rustls-tls"] [dependencies.rand] -version = "0.7" -features = ["wasm-bindgen"] +version = "0.8" + +[dependencies.getrandom] +version = "0.2" +features = ["js"] diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 4482814c..8b71a269 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -153,7 +153,7 @@ impl Pokemon { let id = { let mut rng = rand::rngs::OsRng; - rng.gen_range(0, Pokemon::TOTAL) + rng.gen_range(0..Pokemon::TOTAL) }; let fetch_entry = async { -- cgit From 6fd2c1552735639d96d177550e98b314bd6f79a8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 05:05:43 +0200 Subject: Host GIFs and video examples in `iced.rs` RIP Gfycat --- examples/README.md | 20 +++++++------------- examples/bezier_tool/README.md | 4 +--- examples/color_palette/README.md | 6 ++---- examples/counter/README.md | 4 +--- examples/custom_widget/README.md | 4 +--- examples/download_progress/README.md | 4 +--- examples/events/README.md | 6 ------ examples/game_of_life/README.md | 4 +--- examples/geometry/README.md | 4 +--- examples/integration/README.md | 4 +--- examples/loading_spinners/README.md | 6 ------ examples/pane_grid/README.md | 4 +--- examples/pokedex/README.md | 4 +--- examples/progress_bar/README.md | 4 +--- examples/qr_code/README.md | 4 +--- examples/sierpinski_triangle/README.md | 4 +--- examples/solar_system/README.md | 4 +--- examples/stopwatch/README.md | 4 +--- examples/styling/README.md | 4 +--- examples/todos/README.md | 4 ++-- examples/tour/README.md | 4 ++-- 21 files changed, 28 insertions(+), 78 deletions(-) (limited to 'examples') diff --git a/examples/README.md b/examples/README.md index 111e8910..71dad13e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,8 +10,8 @@ A simple UI tour that can run both on native platforms and the web! It showcases The __[`main`](tour/src/main.rs)__ file contains all the code of the example! All the cross-platform GUI is defined in terms of __state__, __messages__, __update logic__ and __view logic__. @@ -33,8 +33,8 @@ A todos tracker inspired by [TodoMVC]. It showcases dynamic layout, text input, The example code is located in the __[`main`](todos/src/main.rs)__ file. @@ -53,9 +53,7 @@ It runs a simulation in a background thread while allowing interaction with a `C The relevant code is located in the __[`main`](game_of_life/src/main.rs)__ file.
- - - +
You can run it with `cargo run`: @@ -72,9 +70,7 @@ An example showcasing custom styling with a light and dark theme. The example code is located in the __[`main`](styling/src/main.rs)__ file.
- - - +
You can run it with `cargo run`: @@ -120,9 +116,7 @@ Since [Iced was born in May 2019], it has been powering the user interfaces in
- - - +
[Iced was born in May 2019]: https://github.com/hecrj/coffee/pull/35 diff --git a/examples/bezier_tool/README.md b/examples/bezier_tool/README.md index ebbb12cc..6dc13785 100644 --- a/examples/bezier_tool/README.md +++ b/examples/bezier_tool/README.md @@ -5,9 +5,7 @@ A Paint-like tool for drawing Bézier curves using the `Canvas` widget. The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/color_palette/README.md b/examples/color_palette/README.md index f90020b1..9c135937 100644 --- a/examples/color_palette/README.md +++ b/examples/color_palette/README.md @@ -3,13 +3,11 @@ A color palette generator, based on a user-defined root color.
- - - +
You can run it with `cargo run`: ``` -cargo run --package pure_color_palette +cargo run --package color_palette ``` diff --git a/examples/counter/README.md b/examples/counter/README.md index 4d9fc5b9..53243c24 100644 --- a/examples/counter/README.md +++ b/examples/counter/README.md @@ -5,9 +5,7 @@ The classic counter example explained in the [`README`](../../README.md). The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/custom_widget/README.md b/examples/custom_widget/README.md index 3d6cf902..b80e898f 100644 --- a/examples/custom_widget/README.md +++ b/examples/custom_widget/README.md @@ -5,9 +5,7 @@ A demonstration of how to build a custom widget that draws a circle. The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/download_progress/README.md b/examples/download_progress/README.md index 7999ce94..19cb2966 100644 --- a/examples/download_progress/README.md +++ b/examples/download_progress/README.md @@ -5,9 +5,7 @@ A basic application that asynchronously downloads multiple dummy files of 100 MB The example implements a custom `Subscription` in the __[`download`](src/download.rs)__ module. This subscription downloads and produces messages that can be used to keep track of its progress.
- - - +
You can run it with `cargo run`: diff --git a/examples/events/README.md b/examples/events/README.md index 3c9a1cab..fd7f9b47 100644 --- a/examples/events/README.md +++ b/examples/events/README.md @@ -4,12 +4,6 @@ A log of native events displayed using a conditional `Subscription`. The __[`main`]__ file contains all the code of the example. - - You can run it with `cargo run`: ``` cargo run --package events diff --git a/examples/game_of_life/README.md b/examples/game_of_life/README.md index aa39201c..60033c1a 100644 --- a/examples/game_of_life/README.md +++ b/examples/game_of_life/README.md @@ -7,9 +7,7 @@ It runs a simulation in a background thread while allowing interaction with a `C The __[`main`]__ file contains the relevant code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/geometry/README.md b/examples/geometry/README.md index 4d5c81cb..16be8d19 100644 --- a/examples/geometry/README.md +++ b/examples/geometry/README.md @@ -5,9 +5,7 @@ A custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [ The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/integration/README.md b/examples/integration/README.md index ece9ba1e..996cdc17 100644 --- a/examples/integration/README.md +++ b/examples/integration/README.md @@ -5,9 +5,7 @@ A demonstration of how to integrate Iced in an existing [`wgpu`] application. The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/loading_spinners/README.md b/examples/loading_spinners/README.md index 3573c6f6..75b88804 100644 --- a/examples/loading_spinners/README.md +++ b/examples/loading_spinners/README.md @@ -2,12 +2,6 @@ Example implementation of animated indeterminate loading spinners. - - You can run it with `cargo run`: ``` cargo run --package loading_spinners diff --git a/examples/pane_grid/README.md b/examples/pane_grid/README.md index a4cfcb7d..65357b23 100644 --- a/examples/pane_grid/README.md +++ b/examples/pane_grid/README.md @@ -15,9 +15,7 @@ This example showcases the `PaneGrid` widget, which features: The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/pokedex/README.md b/examples/pokedex/README.md index 50720f57..8e8562ac 100644 --- a/examples/pokedex/README.md +++ b/examples/pokedex/README.md @@ -4,9 +4,7 @@ An application that loads a random Pokédex entry using the [PokéAPI]. All the example code can be found in the __[`main`](src/main.rs)__ file.
- - - +
You can run it on native platforms with `cargo run`: diff --git a/examples/progress_bar/README.md b/examples/progress_bar/README.md index 1e927b3c..1268ac6b 100644 --- a/examples/progress_bar/README.md +++ b/examples/progress_bar/README.md @@ -5,9 +5,7 @@ A simple progress bar that can be filled by using a slider. The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/qr_code/README.md b/examples/qr_code/README.md index 2dd89c26..0d1abaa7 100644 --- a/examples/qr_code/README.md +++ b/examples/qr_code/README.md @@ -5,9 +5,7 @@ A basic QR code generator that showcases the `QRCode` widget. The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/sierpinski_triangle/README.md b/examples/sierpinski_triangle/README.md index 9fd18257..8b7676d1 100644 --- a/examples/sierpinski_triangle/README.md +++ b/examples/sierpinski_triangle/README.md @@ -5,9 +5,7 @@ A simple [Sierpiński triangle](https://en.wikipedia.org/wiki/Sierpi%C5%84ski_tr Left-click add fixed point, right-click remove fixed point.
- - - +
You can run with cargo: diff --git a/examples/solar_system/README.md b/examples/solar_system/README.md index acfbc466..81ffd3a5 100644 --- a/examples/solar_system/README.md +++ b/examples/solar_system/README.md @@ -5,9 +5,7 @@ An animated solar system drawn using the `Canvas` widget and showcasing how to c The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/stopwatch/README.md b/examples/stopwatch/README.md index 4cf4582e..1cf370bd 100644 --- a/examples/stopwatch/README.md +++ b/examples/stopwatch/README.md @@ -5,9 +5,7 @@ A watch with start/stop and reset buttons showcasing how to listen to time. The __[`main`]__ file contains all the code of the example.
- - - +
You can run it with `cargo run`: diff --git a/examples/styling/README.md b/examples/styling/README.md index 6c198a54..fd12300d 100644 --- a/examples/styling/README.md +++ b/examples/styling/README.md @@ -4,9 +4,7 @@ An example showcasing custom styling with a light and dark theme. All the example code is located in the __[`main`](src/main.rs)__ file.
- - - +
You can run it with `cargo run`: diff --git a/examples/todos/README.md b/examples/todos/README.md index 9c2598b9..852dd88d 100644 --- a/examples/todos/README.md +++ b/examples/todos/README.md @@ -5,8 +5,8 @@ A todos tracker inspired by [TodoMVC]. It showcases dynamic layout, text input, All the example code is located in the __[`main`]__ file. diff --git a/examples/tour/README.md b/examples/tour/README.md index 731e7e66..1c01236b 100644 --- a/examples/tour/README.md +++ b/examples/tour/README.md @@ -5,8 +5,8 @@ A simple UI tour that can run both on native platforms and the web! It showcases The __[`main`]__ file contains all the code of the example! All the cross-platform GUI is defined in terms of __state__, __messages__, __update logic__ and __view logic__. -- cgit From 09965b686ea6bf82e6c13ed5331bbeb059848e4f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 05:51:39 +0200 Subject: Make `scale` methods in `Frame` generic over `f32` and `Vector` --- examples/game_of_life/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index e951d734..1f266c8e 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -550,7 +550,7 @@ mod grid { frame.translate(center); frame.scale(self.scaling); frame.translate(self.translation); - frame.scale(Cell::SIZE as f32); + frame.scale(Cell::SIZE); let region = self.visible_region(frame.size()); @@ -576,7 +576,7 @@ mod grid { frame.translate(center); frame.scale(self.scaling); frame.translate(self.translation); - frame.scale(Cell::SIZE as f32); + frame.scale(Cell::SIZE); frame.fill_rectangle( Point::new(cell.j as f32, cell.i as f32), @@ -630,7 +630,7 @@ mod grid { frame.translate(center); frame.scale(self.scaling); frame.translate(self.translation); - frame.scale(Cell::SIZE as f32); + frame.scale(Cell::SIZE); let region = self.visible_region(frame.size()); let rows = region.rows(); @@ -834,7 +834,7 @@ mod grid { } impl Cell { - const SIZE: usize = 20; + const SIZE: u16 = 20; fn at(position: Point) -> Cell { let i = (position.y / Cell::SIZE as f32).ceil() as isize; -- cgit From 2b746c7b253573c9194844a02afba709839f5910 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Sat, 2 Sep 2023 13:44:45 +0200 Subject: Add gradient picker example --- examples/gradients/Cargo.toml | 8 +++ examples/gradients/src/main.rs | 136 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 examples/gradients/Cargo.toml create mode 100644 examples/gradients/src/main.rs (limited to 'examples') diff --git a/examples/gradients/Cargo.toml b/examples/gradients/Cargo.toml new file mode 100644 index 00000000..1bc65a9c --- /dev/null +++ b/examples/gradients/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "gradients" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } diff --git a/examples/gradients/src/main.rs b/examples/gradients/src/main.rs new file mode 100644 index 00000000..d8b7945e --- /dev/null +++ b/examples/gradients/src/main.rs @@ -0,0 +1,136 @@ +use iced::widget::{column, container, row, slider, text}; +use iced::{ + gradient, Alignment, Background, BorderRadius, Color, Element, Length, + Radians, Sandbox, Settings, +}; + +pub fn main() -> iced::Result { + Gradient::run(Settings::default()) +} + +struct Gradient { + first: Color, + second: Color, + angle: f32, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + FirstChanged(Color), + SecondChanged(Color), + AngleChanged(f32), +} + +impl Sandbox for Gradient { + type Message = Message; + + fn new() -> Self { + let first = Color::new(0.2784314, 0.0627451, 0.4117647, 1.0); + let second = Color::new(0.1882353, 0.772549, 0.8235294, 1.0); + + Self { + first, + second, + angle: 0.0, + } + } + + fn title(&self) -> String { + String::from("Color gradient") + } + + fn update(&mut self, message: Message) { + match message { + Message::FirstChanged(color) => self.first = color, + Message::SecondChanged(color) => self.second = color, + Message::AngleChanged(angle) => self.angle = angle, + } + } + + fn view(&self) -> Element { + let first = self.first; + let second = self.second; + let angle = self.angle; + + let gradient_box = container(text("")) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .style(move |_: &_| { + let gradient = gradient::Linear::new(Radians(angle)) + .add_stop(0.0, first) + .add_stop(1.0, second) + .into(); + + container::Appearance { + text_color: None, + background: Some(Background::Gradient(gradient)), + border_radius: BorderRadius::default(), + border_width: 0.0, + border_color: Color::new(0.0, 0.0, 0.0, 0.0), + } + }); + + let range = 0.0..=1.0; + let l = self.first; + let r = self.second; + + let first_color_picker = row![ + text("First").width(64), + slider(range.clone(), l.r, move |v| { + Message::FirstChanged(Color::new(v, l.g, l.b, l.a)) + }) + .step(0.01), + slider(range.clone(), l.g, move |v| { + Message::FirstChanged(Color::new(l.r, v, l.b, l.a)) + }) + .step(0.01), + slider(range.clone(), l.b, move |v| { + Message::FirstChanged(Color::new(l.r, l.g, v, l.a)) + }) + .step(0.01), + ] + .spacing(8) + .padding(8) + .align_items(Alignment::Center); + + let second_color_picker = row![ + text("Second").width(64), + slider(range.clone(), r.r, move |v| { + Message::SecondChanged(Color::new(v, r.g, r.b, r.a)) + }) + .step(0.01), + slider(range.clone(), r.g, move |v| { + Message::SecondChanged(Color::new(r.r, v, r.b, r.a)) + }) + .step(0.01), + slider(range.clone(), r.b, move |v| { + Message::SecondChanged(Color::new(r.r, r.g, v, r.a)) + }) + .step(0.01), + ] + .spacing(8) + .padding(8) + .align_items(Alignment::Center); + + let angle_picker = row![ + text("Angle").width(64), + slider(0.0..=std::f32::consts::PI * 2.0, self.angle, move |v| { + Message::AngleChanged(v) + }) + .step(0.01) + ] + .spacing(8) + .padding(8) + .align_items(Alignment::Center); + + column![ + first_color_picker, + second_color_picker, + angle_picker, + gradient_box + ] + .into() + } +} -- cgit From adfcd8c727b896589b77322eae01f9710a86f7cc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:37:57 +0200 Subject: Simplify `gradients` example --- examples/gradients/src/main.rs | 99 ++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 62 deletions(-) (limited to 'examples') diff --git a/examples/gradients/src/main.rs b/examples/gradients/src/main.rs index d8b7945e..ad9bfe11 100644 --- a/examples/gradients/src/main.rs +++ b/examples/gradients/src/main.rs @@ -8,16 +8,17 @@ pub fn main() -> iced::Result { Gradient::run(Settings::default()) } +#[derive(Debug, Clone, Copy)] struct Gradient { - first: Color, - second: Color, + start: Color, + end: Color, angle: f32, } #[derive(Debug, Clone, Copy)] enum Message { - FirstChanged(Color), - SecondChanged(Color), + StartChanged(Color), + EndChanged(Color), AngleChanged(f32), } @@ -25,32 +26,30 @@ impl Sandbox for Gradient { type Message = Message; fn new() -> Self { - let first = Color::new(0.2784314, 0.0627451, 0.4117647, 1.0); - let second = Color::new(0.1882353, 0.772549, 0.8235294, 1.0); + let start = Color::new(1.0, 1.0, 1.0, 1.0); + let end = Color::new(0.0, 0.0, 1.0, 1.0); Self { - first, - second, + start, + end, angle: 0.0, } } fn title(&self) -> String { - String::from("Color gradient") + String::from("Gradient") } fn update(&mut self, message: Message) { match message { - Message::FirstChanged(color) => self.first = color, - Message::SecondChanged(color) => self.second = color, + Message::StartChanged(color) => self.start = color, + Message::EndChanged(color) => self.end = color, Message::AngleChanged(angle) => self.angle = angle, } } fn view(&self) -> Element { - let first = self.first; - let second = self.second; - let angle = self.angle; + let Self { start, end, angle } = *self; let gradient_box = container(text("")) .width(Length::Fill) @@ -58,10 +57,12 @@ impl Sandbox for Gradient { .center_x() .center_y() .style(move |_: &_| { - let gradient = gradient::Linear::new(Radians(angle)) - .add_stop(0.0, first) - .add_stop(1.0, second) - .into(); + let gradient = gradient::Linear::new(Radians( + angle + std::f32::consts::PI, + )) + .add_stop(0.0, start) + .add_stop(1.0, end) + .into(); container::Appearance { text_color: None, @@ -72,48 +73,6 @@ impl Sandbox for Gradient { } }); - let range = 0.0..=1.0; - let l = self.first; - let r = self.second; - - let first_color_picker = row![ - text("First").width(64), - slider(range.clone(), l.r, move |v| { - Message::FirstChanged(Color::new(v, l.g, l.b, l.a)) - }) - .step(0.01), - slider(range.clone(), l.g, move |v| { - Message::FirstChanged(Color::new(l.r, v, l.b, l.a)) - }) - .step(0.01), - slider(range.clone(), l.b, move |v| { - Message::FirstChanged(Color::new(l.r, l.g, v, l.a)) - }) - .step(0.01), - ] - .spacing(8) - .padding(8) - .align_items(Alignment::Center); - - let second_color_picker = row![ - text("Second").width(64), - slider(range.clone(), r.r, move |v| { - Message::SecondChanged(Color::new(v, r.g, r.b, r.a)) - }) - .step(0.01), - slider(range.clone(), r.g, move |v| { - Message::SecondChanged(Color::new(r.r, v, r.b, r.a)) - }) - .step(0.01), - slider(range.clone(), r.b, move |v| { - Message::SecondChanged(Color::new(r.r, r.g, v, r.a)) - }) - .step(0.01), - ] - .spacing(8) - .padding(8) - .align_items(Alignment::Center); - let angle_picker = row![ text("Angle").width(64), slider(0.0..=std::f32::consts::PI * 2.0, self.angle, move |v| { @@ -126,11 +85,27 @@ impl Sandbox for Gradient { .align_items(Alignment::Center); column![ - first_color_picker, - second_color_picker, + color_picker("Start", self.start).map(Message::StartChanged), + color_picker("End", self.end).map(Message::EndChanged), angle_picker, gradient_box ] .into() } } + +fn color_picker(label: &str, color: Color) -> Element<'_, Color> { + row![ + text(label).width(64), + slider(0.0..=1.0, color.r, move |r| { Color { r, ..color } }) + .step(0.01), + slider(0.0..=1.0, color.g, move |g| { Color { g, ..color } }) + .step(0.01), + slider(0.0..=1.0, color.b, move |b| { Color { b, ..color } }) + .step(0.01), + ] + .spacing(8) + .padding(8) + .align_items(Alignment::Center) + .into() +} -- cgit From 21259ee745c17d0f257c7a6c356d8a491460f9d6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:38:51 +0200 Subject: Rename `gradients` example to `gradient` --- examples/gradient/Cargo.toml | 8 +++ examples/gradient/src/main.rs | 111 +++++++++++++++++++++++++++++++++++++++++ examples/gradients/Cargo.toml | 8 --- examples/gradients/src/main.rs | 111 ----------------------------------------- 4 files changed, 119 insertions(+), 119 deletions(-) create mode 100644 examples/gradient/Cargo.toml create mode 100644 examples/gradient/src/main.rs delete mode 100644 examples/gradients/Cargo.toml delete mode 100644 examples/gradients/src/main.rs (limited to 'examples') diff --git a/examples/gradient/Cargo.toml b/examples/gradient/Cargo.toml new file mode 100644 index 00000000..1bc65a9c --- /dev/null +++ b/examples/gradient/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "gradients" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs new file mode 100644 index 00000000..ad9bfe11 --- /dev/null +++ b/examples/gradient/src/main.rs @@ -0,0 +1,111 @@ +use iced::widget::{column, container, row, slider, text}; +use iced::{ + gradient, Alignment, Background, BorderRadius, Color, Element, Length, + Radians, Sandbox, Settings, +}; + +pub fn main() -> iced::Result { + Gradient::run(Settings::default()) +} + +#[derive(Debug, Clone, Copy)] +struct Gradient { + start: Color, + end: Color, + angle: f32, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + StartChanged(Color), + EndChanged(Color), + AngleChanged(f32), +} + +impl Sandbox for Gradient { + type Message = Message; + + fn new() -> Self { + let start = Color::new(1.0, 1.0, 1.0, 1.0); + let end = Color::new(0.0, 0.0, 1.0, 1.0); + + Self { + start, + end, + angle: 0.0, + } + } + + fn title(&self) -> String { + String::from("Gradient") + } + + fn update(&mut self, message: Message) { + match message { + Message::StartChanged(color) => self.start = color, + Message::EndChanged(color) => self.end = color, + Message::AngleChanged(angle) => self.angle = angle, + } + } + + fn view(&self) -> Element { + let Self { start, end, angle } = *self; + + let gradient_box = container(text("")) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .style(move |_: &_| { + let gradient = gradient::Linear::new(Radians( + angle + std::f32::consts::PI, + )) + .add_stop(0.0, start) + .add_stop(1.0, end) + .into(); + + container::Appearance { + text_color: None, + background: Some(Background::Gradient(gradient)), + border_radius: BorderRadius::default(), + border_width: 0.0, + border_color: Color::new(0.0, 0.0, 0.0, 0.0), + } + }); + + let angle_picker = row![ + text("Angle").width(64), + slider(0.0..=std::f32::consts::PI * 2.0, self.angle, move |v| { + Message::AngleChanged(v) + }) + .step(0.01) + ] + .spacing(8) + .padding(8) + .align_items(Alignment::Center); + + column![ + color_picker("Start", self.start).map(Message::StartChanged), + color_picker("End", self.end).map(Message::EndChanged), + angle_picker, + gradient_box + ] + .into() + } +} + +fn color_picker(label: &str, color: Color) -> Element<'_, Color> { + row![ + text(label).width(64), + slider(0.0..=1.0, color.r, move |r| { Color { r, ..color } }) + .step(0.01), + slider(0.0..=1.0, color.g, move |g| { Color { g, ..color } }) + .step(0.01), + slider(0.0..=1.0, color.b, move |b| { Color { b, ..color } }) + .step(0.01), + ] + .spacing(8) + .padding(8) + .align_items(Alignment::Center) + .into() +} diff --git a/examples/gradients/Cargo.toml b/examples/gradients/Cargo.toml deleted file mode 100644 index 1bc65a9c..00000000 --- a/examples/gradients/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "gradients" -version = "0.1.0" -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../.." } diff --git a/examples/gradients/src/main.rs b/examples/gradients/src/main.rs deleted file mode 100644 index ad9bfe11..00000000 --- a/examples/gradients/src/main.rs +++ /dev/null @@ -1,111 +0,0 @@ -use iced::widget::{column, container, row, slider, text}; -use iced::{ - gradient, Alignment, Background, BorderRadius, Color, Element, Length, - Radians, Sandbox, Settings, -}; - -pub fn main() -> iced::Result { - Gradient::run(Settings::default()) -} - -#[derive(Debug, Clone, Copy)] -struct Gradient { - start: Color, - end: Color, - angle: f32, -} - -#[derive(Debug, Clone, Copy)] -enum Message { - StartChanged(Color), - EndChanged(Color), - AngleChanged(f32), -} - -impl Sandbox for Gradient { - type Message = Message; - - fn new() -> Self { - let start = Color::new(1.0, 1.0, 1.0, 1.0); - let end = Color::new(0.0, 0.0, 1.0, 1.0); - - Self { - start, - end, - angle: 0.0, - } - } - - fn title(&self) -> String { - String::from("Gradient") - } - - fn update(&mut self, message: Message) { - match message { - Message::StartChanged(color) => self.start = color, - Message::EndChanged(color) => self.end = color, - Message::AngleChanged(angle) => self.angle = angle, - } - } - - fn view(&self) -> Element { - let Self { start, end, angle } = *self; - - let gradient_box = container(text("")) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .style(move |_: &_| { - let gradient = gradient::Linear::new(Radians( - angle + std::f32::consts::PI, - )) - .add_stop(0.0, start) - .add_stop(1.0, end) - .into(); - - container::Appearance { - text_color: None, - background: Some(Background::Gradient(gradient)), - border_radius: BorderRadius::default(), - border_width: 0.0, - border_color: Color::new(0.0, 0.0, 0.0, 0.0), - } - }); - - let angle_picker = row![ - text("Angle").width(64), - slider(0.0..=std::f32::consts::PI * 2.0, self.angle, move |v| { - Message::AngleChanged(v) - }) - .step(0.01) - ] - .spacing(8) - .padding(8) - .align_items(Alignment::Center); - - column![ - color_picker("Start", self.start).map(Message::StartChanged), - color_picker("End", self.end).map(Message::EndChanged), - angle_picker, - gradient_box - ] - .into() - } -} - -fn color_picker(label: &str, color: Color) -> Element<'_, Color> { - row![ - text(label).width(64), - slider(0.0..=1.0, color.r, move |r| { Color { r, ..color } }) - .step(0.01), - slider(0.0..=1.0, color.g, move |g| { Color { g, ..color } }) - .step(0.01), - slider(0.0..=1.0, color.b, move |b| { Color { b, ..color } }) - .step(0.01), - ] - .spacing(8) - .padding(8) - .align_items(Alignment::Center) - .into() -} -- cgit From f83fb9b6cd221ecfc02d468df128dbb8b3751c3f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:40:27 +0200 Subject: Use `Color::WHITE` in `gradient` example --- examples/gradient/src/main.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index ad9bfe11..dd50ebc9 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -26,12 +26,9 @@ impl Sandbox for Gradient { type Message = Message; fn new() -> Self { - let start = Color::new(1.0, 1.0, 1.0, 1.0); - let end = Color::new(0.0, 0.0, 1.0, 1.0); - Self { - start, - end, + start: Color::WHITE, + end: Color::new(0.0, 0.0, 1.0, 1.0), angle: 0.0, } } -- cgit From fc261a539b28dcd5f1898107a7c69c0719f3bbbf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:53:26 +0200 Subject: Fix name of `gradient` example package --- examples/gradient/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/gradient/Cargo.toml b/examples/gradient/Cargo.toml index 1bc65a9c..2dea2c4f 100644 --- a/examples/gradient/Cargo.toml +++ b/examples/gradient/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gradients" +name = "gradient" version = "0.1.0" edition = "2021" publish = false -- cgit From 6ff2e48feb7c0c3f65a3e6d298fc1c73356dc740 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:53:34 +0200 Subject: Use `Default` for `container::Appearance` in `gradient` example --- examples/gradient/src/main.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index dd50ebc9..91a65f1e 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -1,7 +1,7 @@ +use iced::gradient; use iced::widget::{column, container, row, slider, text}; use iced::{ - gradient, Alignment, Background, BorderRadius, Color, Element, Length, - Radians, Sandbox, Settings, + Alignment, Background, Color, Element, Length, Radians, Sandbox, Settings, }; pub fn main() -> iced::Result { @@ -62,11 +62,8 @@ impl Sandbox for Gradient { .into(); container::Appearance { - text_color: None, background: Some(Background::Gradient(gradient)), - border_radius: BorderRadius::default(), - border_width: 0.0, - border_color: Color::new(0.0, 0.0, 0.0, 0.0), + ..Default::default() } }); -- cgit From d6be3ab68246b08ea3f8988833e3b266b524dd1d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:55:09 +0200 Subject: Use `horizontal_space` instead of empty `text` widget in `gradient` example --- examples/gradient/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index 91a65f1e..3ba08a06 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -1,5 +1,5 @@ use iced::gradient; -use iced::widget::{column, container, row, slider, text}; +use iced::widget::{column, container, horizontal_space, row, slider, text}; use iced::{ Alignment, Background, Color, Element, Length, Radians, Sandbox, Settings, }; @@ -48,7 +48,7 @@ impl Sandbox for Gradient { fn view(&self) -> Element { let Self { start, end, angle } = *self; - let gradient_box = container(text("")) + let gradient_box = container(horizontal_space(Length::Fill)) .width(Length::Fill) .height(Length::Fill) .center_x() -- cgit From c1139898c50d22ac3b711801b1058aacef514030 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Sep 2023 07:55:54 +0200 Subject: Remove unnecessary centering in `gradient` example --- examples/gradient/src/main.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'examples') diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index 3ba08a06..0b32e73c 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -51,8 +51,6 @@ impl Sandbox for Gradient { let gradient_box = container(horizontal_space(Length::Fill)) .width(Length::Fill) .height(Length::Fill) - .center_x() - .center_y() .style(move |_: &_| { let gradient = gradient::Linear::new(Radians( angle + std::f32::consts::PI, -- cgit From d2294737c2e28b3b3050d7bf820bbca09896b00e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Sep 2023 01:58:52 +0200 Subject: Use `Radians` as a number directly in `gradient` example --- examples/gradient/src/main.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'examples') diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index 0b32e73c..1bf5822d 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -12,14 +12,14 @@ pub fn main() -> iced::Result { struct Gradient { start: Color, end: Color, - angle: f32, + angle: Radians, } #[derive(Debug, Clone, Copy)] enum Message { StartChanged(Color), EndChanged(Color), - AngleChanged(f32), + AngleChanged(Radians), } impl Sandbox for Gradient { @@ -29,7 +29,7 @@ impl Sandbox for Gradient { Self { start: Color::WHITE, end: Color::new(0.0, 0.0, 1.0, 1.0), - angle: 0.0, + angle: Radians(0.0), } } @@ -52,12 +52,10 @@ impl Sandbox for Gradient { .width(Length::Fill) .height(Length::Fill) .style(move |_: &_| { - let gradient = gradient::Linear::new(Radians( - angle + std::f32::consts::PI, - )) - .add_stop(0.0, start) - .add_stop(1.0, end) - .into(); + let gradient = gradient::Linear::new(angle) + .add_stop(0.0, start) + .add_stop(1.0, end) + .into(); container::Appearance { background: Some(Background::Gradient(gradient)), @@ -67,10 +65,8 @@ impl Sandbox for Gradient { let angle_picker = row![ text("Angle").width(64), - slider(0.0..=std::f32::consts::PI * 2.0, self.angle, move |v| { - Message::AngleChanged(v) - }) - .step(0.01) + slider(Radians::RANGE, self.angle, Message::AngleChanged) + .step(0.01) ] .spacing(8) .padding(8) -- cgit From 9c2ad457d80e8254955396ad034df0dd5f2b59b3 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Sep 2023 13:59:18 +0200 Subject: Add webgl feature for web examples --- examples/counter/Cargo.toml | 4 ++++ examples/integration/Cargo.toml | 3 +++ examples/todos/Cargo.toml | 3 +++ examples/tour/Cargo.toml | 4 ++++ 4 files changed, 14 insertions(+) (limited to 'examples') diff --git a/examples/counter/Cargo.toml b/examples/counter/Cargo.toml index 1e9bba6b..22f86064 100644 --- a/examples/counter/Cargo.toml +++ b/examples/counter/Cargo.toml @@ -7,3 +7,7 @@ publish = false [dependencies] iced.workspace = true + +[target.'cfg(target_arch = "wasm32")'.dependencies] +iced.workspace = true +iced.features = ["webgl"] diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 032cc75e..bf70a619 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -13,6 +13,9 @@ iced_widget.workspace = true tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] +iced_wgpu.workspace = true +iced_wgpu.features = ["webgl"] + console_error_panic_hook = "0.1" console_log = "1.0" log.workspace = true diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index fea20375..3334d84f 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -18,6 +18,9 @@ async-std.workspace = true directories-next = "2.0" [target.'cfg(target_arch = "wasm32")'.dependencies] +iced.workspace = true +iced.features = ["debug", "webgl"] + web-sys = { workspace = true, features = ["Window", "Storage"] } wasm-timer.workspace = true diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 21206ecb..9de017ce 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -10,3 +10,7 @@ iced.workspace = true iced.features = ["image", "debug"] tracing-subscriber = "0.3" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +iced.workspace = true +iced.features = ["image", "debug", "webgl"] -- cgit From e562544807b196dafac9195ea8137958698755e5 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Sep 2023 14:09:48 +0200 Subject: Add logging for web in tour example --- examples/tour/Cargo.toml | 5 +++++ examples/tour/src/main.rs | 7 +++++++ 2 files changed, 12 insertions(+) (limited to 'examples') diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 9de017ce..11920e0d 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -9,8 +9,13 @@ publish = false iced.workspace = true iced.features = ["image", "debug"] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] iced.workspace = true iced.features = ["image", "debug", "webgl"] + +console_error_panic_hook = "0.1" +console_log = "1.0" +log.workspace = true diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index af508206..f0e3c0cd 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -8,6 +8,13 @@ use iced::widget::{Button, Column, Container, Slider}; use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; pub fn main() -> iced::Result { + #[cfg(target_arch = "wasm32")] + { + console_log::init_with_level(log::Level::Debug).expect("Initialize logger"); + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + } + + #[cfg(not(target_arch = "wasm32"))] tracing_subscriber::fmt::init(); Tour::run(Settings::default()) -- cgit From 52da89ac531cdf93b0e74afdd5bf6ad366551f82 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Sep 2023 14:11:40 +0200 Subject: Add instructions to run web examples --- examples/counter/README.md | 8 ++++++++ examples/todos/README.md | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/counter/README.md b/examples/counter/README.md index 53243c24..18761bba 100644 --- a/examples/counter/README.md +++ b/examples/counter/README.md @@ -13,4 +13,12 @@ You can run it with `cargo run`: cargo run --package counter ``` +The web version can be run with [`trunk`]: + +``` +cd examples/counter +trunk serve +``` + [`main`]: src/main.rs +[`trunk`]: https://trunkrs.dev/ diff --git a/examples/todos/README.md b/examples/todos/README.md index 852dd88d..5e42f166 100644 --- a/examples/todos/README.md +++ b/examples/todos/README.md @@ -14,7 +14,14 @@ You can run the native version with `cargo run`: ``` cargo run --package todos ``` -We have not yet implemented a `LocalStorage` version of the auto-save feature. Therefore, it does not work on web _yet_! + +The web version can be run with [`trunk`]: + +``` +cd examples/todos +trunk serve +``` [`main`]: src/main.rs [TodoMVC]: http://todomvc.com/ +[`trunk`]: https://trunkrs.dev/ -- cgit From 052fe0edaeb0d6ce096375751eec02b953faf2a1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Sep 2023 14:15:11 +0200 Subject: Dont use tracing-subscriber dependency on web --- examples/integration/Cargo.toml | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index bf70a619..6b9fcc57 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -10,6 +10,7 @@ iced_winit.workspace = true iced_wgpu.workspace = true iced_widget.workspace = true +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] -- cgit From fbc9ef74c406b2a58bb2a093262323e346faf485 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 9 Sep 2023 22:42:41 +0200 Subject: Change init_with_level to init in examples --- examples/integration/Cargo.toml | 1 - examples/integration/src/main.rs | 2 +- examples/tour/Cargo.toml | 1 - examples/tour/src/main.rs | 2 +- 4 files changed, 2 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 6b9fcc57..a4a961f8 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -19,6 +19,5 @@ iced_wgpu.features = ["webgl"] console_error_panic_hook = "0.1" console_log = "1.0" -log.workspace = true wasm-bindgen = "0.2" web-sys = { version = "0.3", features = ["Element", "HtmlCanvasElement", "Window", "Document"] } diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index af48af5f..d0b2d891 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -29,7 +29,7 @@ use winit::platform::web::WindowBuilderExtWebSys; pub fn main() -> Result<(), Box> { #[cfg(target_arch = "wasm32")] let canvas_element = { - console_log::init_with_level(log::Level::Debug)?; + console_log::init().expect("Initialize logger"); std::panic::set_hook(Box::new(console_error_panic_hook::hook)); diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 11920e0d..9e984ad1 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -18,4 +18,3 @@ iced.features = ["image", "debug", "webgl"] console_error_panic_hook = "0.1" console_log = "1.0" -log.workspace = true diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index f0e3c0cd..3387b481 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -10,7 +10,7 @@ use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; pub fn main() -> iced::Result { #[cfg(target_arch = "wasm32")] { - console_log::init_with_level(log::Level::Debug).expect("Initialize logger"); + console_log::init().expect("Initialize logger"); std::panic::set_hook(Box::new(console_error_panic_hook::hook)); } -- cgit From 8aa7874ba9ba4a0e7cafb9a447a3db92e95aeb87 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 10 Sep 2023 00:43:38 +0200 Subject: Fix Wasm build of `todos` example --- examples/todos/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 5d8936df..501bf67e 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -17,6 +17,7 @@ use uuid::Uuid; static INPUT_ID: Lazy = Lazy::new(text_input::Id::unique); pub fn main() -> iced::Result { + #[cfg(not(target_arch = "wasm32"))] tracing_subscriber::fmt::init(); Todos::run(Settings { -- cgit From 6448429103c9c82b90040ac5a5a097bdded23f82 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Sep 2023 14:51:00 +0200 Subject: Draft `Editor` API and `TextEditor` widget --- examples/editor/Cargo.toml | 10 +++++++++ examples/editor/src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 examples/editor/Cargo.toml create mode 100644 examples/editor/src/main.rs (limited to 'examples') diff --git a/examples/editor/Cargo.toml b/examples/editor/Cargo.toml new file mode 100644 index 00000000..528cf23c --- /dev/null +++ b/examples/editor/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "editor" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced.workspace = true +iced.features = ["debug"] \ No newline at end of file diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs new file mode 100644 index 00000000..50989ac5 --- /dev/null +++ b/examples/editor/src/main.rs @@ -0,0 +1,49 @@ +use iced::widget::{container, text_editor}; +use iced::{Element, Font, Sandbox, Settings}; + +pub fn main() -> iced::Result { + Editor::run(Settings::default()) +} + +struct Editor { + content: text_editor::Content, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Edit(text_editor::Action), +} + +impl Sandbox for Editor { + type Message = Message; + + fn new() -> Self { + Self { + content: text_editor::Content::with(include_str!( + "../../../README.md" + )), + } + } + + fn title(&self) -> String { + String::from("Editor - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::Edit(action) => { + self.content.edit(action); + } + } + } + + fn view(&self) -> Element { + container( + text_editor(&self.content) + .on_edit(Message::Edit) + .font(Font::with_name("Hasklug Nerd Font Mono")), + ) + .padding(20) + .into() + } +} -- cgit From 52b36a9574f45138363a4bfc6394c6da03baa433 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 13 Sep 2023 15:17:04 +0200 Subject: Use `Theme::Dark` in `editor` example --- examples/editor/src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 50989ac5..2a70b34c 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,5 +1,5 @@ use iced::widget::{container, text_editor}; -use iced::{Element, Font, Sandbox, Settings}; +use iced::{Element, Font, Sandbox, Settings, Theme}; pub fn main() -> iced::Result { Editor::run(Settings::default()) @@ -46,4 +46,8 @@ impl Sandbox for Editor { .padding(20) .into() } + + fn theme(&self) -> Theme { + Theme::Dark + } } -- cgit From bebb2b0252cba7af3641e28a12de2c6db46b8946 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Sep 2023 19:37:15 +0200 Subject: Fix styling of horizontal scrollbar in `scrollable` example --- examples/scrollable/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 8c08d993..21e69284 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -389,12 +389,12 @@ impl scrollable::StyleSheet for ScrollbarCustomStyle { background: style .active(&theme::Scrollable::default()) .background, - border_radius: 0.0.into(), + border_radius: 2.0.into(), border_width: 0.0, border_color: Default::default(), scroller: Scroller { color: Color::from_rgb8(250, 85, 134), - border_radius: 0.0.into(), + border_radius: 2.0.into(), border_width: 0.0, border_color: Default::default(), }, -- cgit From d051f21597bb333ac10183aaa3214a292e9aa365 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Sep 2023 15:40:16 +0200 Subject: Implement `Copy` and `Paste` actions for `text::Editor` --- examples/editor/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 2a70b34c..11819c69 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -9,7 +9,7 @@ struct Editor { content: text_editor::Content, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] enum Message { Edit(text_editor::Action), } -- cgit From d3011992a76e83e12f74402c2ade616cdc7f1497 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Sep 2023 19:03:58 +0200 Subject: Implement basic syntax highlighting with `syntect` in `editor` example --- examples/editor/Cargo.toml | 4 +- examples/editor/src/main.rs | 168 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/editor/Cargo.toml b/examples/editor/Cargo.toml index 528cf23c..930ee592 100644 --- a/examples/editor/Cargo.toml +++ b/examples/editor/Cargo.toml @@ -7,4 +7,6 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["debug"] \ No newline at end of file +iced.features = ["advanced", "debug"] + +syntect = "5.1" \ No newline at end of file diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 11819c69..a72feebc 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,6 +1,8 @@ use iced::widget::{container, text_editor}; use iced::{Element, Font, Sandbox, Settings, Theme}; +use highlighter::Highlighter; + pub fn main() -> iced::Result { Editor::run(Settings::default()) } @@ -41,7 +43,10 @@ impl Sandbox for Editor { container( text_editor(&self.content) .on_edit(Message::Edit) - .font(Font::with_name("Hasklug Nerd Font Mono")), + .font(Font::with_name("Hasklug Nerd Font Mono")) + .highlight::(highlighter::Settings { + token: String::from("md"), + }), ) .padding(20) .into() @@ -51,3 +56,164 @@ impl Sandbox for Editor { Theme::Dark } } + +mod highlighter { + use iced::advanced::text::highlighter; + use iced::widget::text_editor; + use iced::{Color, Font, Theme}; + + use std::ops::Range; + use syntect::highlighting; + use syntect::parsing; + + #[derive(Debug, Clone, Hash)] + pub struct Settings { + pub token: String, + } + + pub struct Highlight(highlighting::StyleModifier); + + impl text_editor::Highlight for Highlight { + fn format(&self, _theme: &Theme) -> highlighter::Format { + highlighter::Format { + color: self.0.foreground.map(|color| { + Color::from_rgba8( + color.r, + color.g, + color.b, + color.a as f32 / 255.0, + ) + }), + font: None, + } + } + } + + pub struct Highlighter { + syntaxes: parsing::SyntaxSet, + parser: parsing::ParseState, + stack: parsing::ScopeStack, + theme: highlighting::Theme, + token: String, + current_line: usize, + } + + impl highlighter::Highlighter for Highlighter { + type Settings = Settings; + type Highlight = Highlight; + + type Iterator<'a> = + Box, Self::Highlight)> + 'a>; + + fn new(settings: &Self::Settings) -> Self { + let syntaxes = parsing::SyntaxSet::load_defaults_nonewlines(); + + let syntax = syntaxes + .find_syntax_by_token(&settings.token) + .unwrap_or_else(|| syntaxes.find_syntax_plain_text()); + + let parser = parsing::ParseState::new(&syntax); + let stack = parsing::ScopeStack::new(); + + let theme = highlighting::ThemeSet::load_defaults() + .themes + .remove("base16-mocha.dark") + .unwrap(); + + Highlighter { + syntaxes, + parser, + stack, + theme, + token: settings.token.clone(), + current_line: 0, + } + } + + fn change_line(&mut self, _line: usize) { + // TODO: Caching + let syntax = self + .syntaxes + .find_syntax_by_token(&self.token) + .unwrap_or_else(|| self.syntaxes.find_syntax_plain_text()); + + self.parser = parsing::ParseState::new(&syntax); + self.stack = parsing::ScopeStack::new(); + self.current_line = 0; + } + + fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> { + self.current_line += 1; + + let ops = self + .parser + .parse_line(line, &self.syntaxes) + .unwrap_or_default(); + + Box::new( + ScopeRangeIterator { + ops, + line_length: line.len(), + index: 0, + last_str_index: 0, + } + .filter_map(move |(range, scope)| { + let highlighter = + highlighting::Highlighter::new(&self.theme); + let _ = self.stack.apply(&scope); + + if range.is_empty() { + None + } else { + Some(( + range, + Highlight( + highlighter + .style_mod_for_stack(&self.stack.scopes), + ), + )) + } + }), + ) + } + + fn current_line(&self) -> usize { + self.current_line + } + } + + pub struct ScopeRangeIterator { + ops: Vec<(usize, parsing::ScopeStackOp)>, + line_length: usize, + index: usize, + last_str_index: usize, + } + + impl Iterator for ScopeRangeIterator { + type Item = (std::ops::Range, parsing::ScopeStackOp); + + fn next(&mut self) -> Option { + if self.index > self.ops.len() { + return None; + } + + let next_str_i = if self.index == self.ops.len() { + self.line_length + } else { + self.ops[self.index].0 + }; + + let range = self.last_str_index..next_str_i; + self.last_str_index = next_str_i; + + let op = if self.index == 0 { + parsing::ScopeStackOp::Noop + } else { + self.ops[self.index - 1].1.clone() + }; + + self.index += 1; + Some((range, op)) + } + } +} -- cgit From 790c0dabcf0a50a2466e47daeb4f1e149b2ede5a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Sep 2023 21:45:13 +0200 Subject: Implement syntax highlighting cache in `editor` example --- examples/editor/src/main.rs | 67 ++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 25 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index a72feebc..1235d38b 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -64,7 +64,7 @@ mod highlighter { use std::ops::Range; use syntect::highlighting; - use syntect::parsing; + use syntect::parsing::{self, SyntaxReference}; #[derive(Debug, Clone, Hash)] pub struct Settings { @@ -91,13 +91,14 @@ mod highlighter { pub struct Highlighter { syntaxes: parsing::SyntaxSet, - parser: parsing::ParseState, - stack: parsing::ScopeStack, + syntax: SyntaxReference, + caches: Vec<(parsing::ParseState, parsing::ScopeStack)>, theme: highlighting::Theme, - token: String, current_line: usize, } + const LINES_PER_SNAPSHOT: usize = 50; + impl highlighter::Highlighter for Highlighter { type Settings = Settings; type Highlight = Highlight; @@ -121,34 +122,53 @@ mod highlighter { .unwrap(); Highlighter { + syntax: syntax.clone(), syntaxes, - parser, - stack, + caches: vec![(parser, stack)], theme, - token: settings.token.clone(), current_line: 0, } } - fn change_line(&mut self, _line: usize) { - // TODO: Caching - let syntax = self - .syntaxes - .find_syntax_by_token(&self.token) - .unwrap_or_else(|| self.syntaxes.find_syntax_plain_text()); + fn change_line(&mut self, line: usize) { + let snapshot = line / LINES_PER_SNAPSHOT; + + if snapshot <= self.caches.len() { + self.caches.truncate(snapshot); + self.current_line = snapshot * LINES_PER_SNAPSHOT; + } else { + self.caches.truncate(1); + self.current_line = 0; + } + + let (parser, stack) = + self.caches.last().cloned().unwrap_or_else(|| { + ( + parsing::ParseState::new(&self.syntax), + parsing::ScopeStack::new(), + ) + }); - self.parser = parsing::ParseState::new(&syntax); - self.stack = parsing::ScopeStack::new(); - self.current_line = 0; + self.caches.push((parser, stack)); } fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> { + if self.current_line / LINES_PER_SNAPSHOT >= self.caches.len() { + let (parser, stack) = + self.caches.last().expect("Caches must not be empty"); + + self.caches.push((parser.clone(), stack.clone())); + } + self.current_line += 1; - let ops = self - .parser - .parse_line(line, &self.syntaxes) - .unwrap_or_default(); + let (parser, stack) = + self.caches.last_mut().expect("Caches must not be empty"); + + let ops = + parser.parse_line(line, &self.syntaxes).unwrap_or_default(); + + let highlighter = highlighting::Highlighter::new(&self.theme); Box::new( ScopeRangeIterator { @@ -158,9 +178,7 @@ mod highlighter { last_str_index: 0, } .filter_map(move |(range, scope)| { - let highlighter = - highlighting::Highlighter::new(&self.theme); - let _ = self.stack.apply(&scope); + let _ = stack.apply(&scope); if range.is_empty() { None @@ -168,8 +186,7 @@ mod highlighter { Some(( range, Highlight( - highlighter - .style_mod_for_stack(&self.stack.scopes), + highlighter.style_mod_for_stack(&stack.scopes), ), )) } -- cgit From 8f8528a4ccee049aba779fe86cda786a52afac30 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Sep 2023 23:20:15 +0200 Subject: Fix unnecessary dereference in `editor` example --- examples/editor/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 1235d38b..74649676 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -113,7 +113,7 @@ mod highlighter { .find_syntax_by_token(&settings.token) .unwrap_or_else(|| syntaxes.find_syntax_plain_text()); - let parser = parsing::ParseState::new(&syntax); + let parser = parsing::ParseState::new(syntax); let stack = parsing::ScopeStack::new(); let theme = highlighting::ThemeSet::load_defaults() -- cgit From 8446fe6de52fa68077d23d39f728f79a29b52f00 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Sep 2023 14:38:54 +0200 Subject: Implement theme selector in `editor` example --- examples/editor/src/main.rs | 101 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 19 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 74649676..fa35ba0f 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,5 +1,5 @@ -use iced::widget::{container, text_editor}; -use iced::{Element, Font, Sandbox, Settings, Theme}; +use iced::widget::{column, horizontal_space, pick_list, row, text_editor}; +use iced::{Element, Font, Length, Sandbox, Settings, Theme}; use highlighter::Highlighter; @@ -9,11 +9,13 @@ pub fn main() -> iced::Result { struct Editor { content: text_editor::Content, + theme: highlighter::Theme, } #[derive(Debug, Clone)] enum Message { Edit(text_editor::Action), + ThemeSelected(highlighter::Theme), } impl Sandbox for Editor { @@ -21,9 +23,8 @@ impl Sandbox for Editor { fn new() -> Self { Self { - content: text_editor::Content::with(include_str!( - "../../../README.md" - )), + content: text_editor::Content::with(include_str!("main.rs")), + theme: highlighter::Theme::SolarizedDark, } } @@ -36,18 +37,33 @@ impl Sandbox for Editor { Message::Edit(action) => { self.content.edit(action); } + Message::ThemeSelected(theme) => { + self.theme = theme; + } } } fn view(&self) -> Element { - container( + column![ + row![ + horizontal_space(Length::Fill), + pick_list( + highlighter::Theme::ALL, + Some(self.theme), + Message::ThemeSelected + ) + .padding([5, 10]) + ] + .spacing(10), text_editor(&self.content) .on_edit(Message::Edit) .font(Font::with_name("Hasklug Nerd Font Mono")) .highlight::(highlighter::Settings { - token: String::from("md"), + theme: self.theme, + extension: String::from("rs"), }), - ) + ] + .spacing(10) .padding(20) .into() } @@ -60,21 +76,52 @@ impl Sandbox for Editor { mod highlighter { use iced::advanced::text::highlighter; use iced::widget::text_editor; - use iced::{Color, Font, Theme}; + use iced::{Color, Font}; use std::ops::Range; use syntect::highlighting; use syntect::parsing::{self, SyntaxReference}; - #[derive(Debug, Clone, Hash)] + #[derive(Debug, Clone, PartialEq)] pub struct Settings { - pub token: String, + pub theme: Theme, + pub extension: String, + } + + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub enum Theme { + SolarizedDark, + InspiredGitHub, + Base16Mocha, + } + + impl Theme { + pub const ALL: &[Self] = + &[Self::SolarizedDark, Self::InspiredGitHub, Self::Base16Mocha]; + + fn key(&self) -> &'static str { + match self { + Theme::InspiredGitHub => "InspiredGitHub", + Theme::Base16Mocha => "base16-mocha.dark", + Theme::SolarizedDark => "Solarized (dark)", + } + } + } + + impl std::fmt::Display for Theme { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Theme::InspiredGitHub => write!(f, "Inspired GitHub"), + Theme::Base16Mocha => write!(f, "Mocha"), + Theme::SolarizedDark => write!(f, "Solarized Dark"), + } + } } pub struct Highlight(highlighting::StyleModifier); impl text_editor::Highlight for Highlight { - fn format(&self, _theme: &Theme) -> highlighter::Format { + fn format(&self, _theme: &iced::Theme) -> highlighter::Format { highlighter::Format { color: self.0.foreground.map(|color| { Color::from_rgba8( @@ -92,8 +139,8 @@ mod highlighter { pub struct Highlighter { syntaxes: parsing::SyntaxSet, syntax: SyntaxReference, - caches: Vec<(parsing::ParseState, parsing::ScopeStack)>, theme: highlighting::Theme, + caches: Vec<(parsing::ParseState, parsing::ScopeStack)>, current_line: usize, } @@ -110,26 +157,42 @@ mod highlighter { let syntaxes = parsing::SyntaxSet::load_defaults_nonewlines(); let syntax = syntaxes - .find_syntax_by_token(&settings.token) + .find_syntax_by_token(&settings.extension) .unwrap_or_else(|| syntaxes.find_syntax_plain_text()); - let parser = parsing::ParseState::new(syntax); - let stack = parsing::ScopeStack::new(); - let theme = highlighting::ThemeSet::load_defaults() .themes - .remove("base16-mocha.dark") + .remove(settings.theme.key()) .unwrap(); + let parser = parsing::ParseState::new(syntax); + let stack = parsing::ScopeStack::new(); + Highlighter { syntax: syntax.clone(), syntaxes, - caches: vec![(parser, stack)], theme, + caches: vec![(parser, stack)], current_line: 0, } } + fn update(&mut self, new_settings: &Self::Settings) { + self.syntax = self + .syntaxes + .find_syntax_by_token(&new_settings.extension) + .unwrap_or_else(|| self.syntaxes.find_syntax_plain_text()) + .clone(); + + self.theme = highlighting::ThemeSet::load_defaults() + .themes + .remove(new_settings.theme.key()) + .unwrap(); + + // Restart the highlighter + self.change_line(0); + } + fn change_line(&mut self, line: usize) { let snapshot = line / LINES_PER_SNAPSHOT; -- cgit From e7326f0af6f16cf2ff04fbac93bf296a044923f4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Sep 2023 19:07:41 +0200 Subject: Flesh out the `editor` example a bit more --- examples/editor/Cargo.toml | 8 +- examples/editor/fonts/icons.ttf | Bin 0 -> 6352 bytes examples/editor/src/main.rs | 287 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 269 insertions(+), 26 deletions(-) create mode 100644 examples/editor/fonts/icons.ttf (limited to 'examples') diff --git a/examples/editor/Cargo.toml b/examples/editor/Cargo.toml index 930ee592..eeb34aa1 100644 --- a/examples/editor/Cargo.toml +++ b/examples/editor/Cargo.toml @@ -7,6 +7,10 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["advanced", "debug"] +iced.features = ["advanced", "tokio", "debug"] -syntect = "5.1" \ No newline at end of file +tokio.workspace = true +tokio.features = ["fs"] + +syntect = "5.1" +rfd = "0.12" diff --git a/examples/editor/fonts/icons.ttf b/examples/editor/fonts/icons.ttf new file mode 100644 index 00000000..393c6922 Binary files /dev/null and b/examples/editor/fonts/icons.ttf differ diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index fa35ba0f..09c4b9b5 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,70 +1,218 @@ -use iced::widget::{column, horizontal_space, pick_list, row, text_editor}; -use iced::{Element, Font, Length, Sandbox, Settings, Theme}; +use iced::executor; +use iced::theme::{self, Theme}; +use iced::widget::{ + button, column, container, horizontal_space, pick_list, row, text, + text_editor, tooltip, +}; +use iced::{Application, Command, Element, Font, Length, Settings}; use highlighter::Highlighter; +use std::ffi; +use std::io; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + pub fn main() -> iced::Result { - Editor::run(Settings::default()) + Editor::run(Settings { + fonts: vec![include_bytes!("../fonts/icons.ttf").as_slice().into()], + default_font: Font { + monospaced: true, + ..Font::with_name("Hasklug Nerd Font Mono") + }, + ..Settings::default() + }) } struct Editor { + file: Option, content: text_editor::Content, theme: highlighter::Theme, + is_loading: bool, + is_dirty: bool, } #[derive(Debug, Clone)] enum Message { Edit(text_editor::Action), ThemeSelected(highlighter::Theme), + NewFile, + OpenFile, + FileOpened(Result<(PathBuf, Arc), Error>), + SaveFile, + FileSaved(Result), } -impl Sandbox for Editor { +impl Application for Editor { type Message = Message; - - fn new() -> Self { - Self { - content: text_editor::Content::with(include_str!("main.rs")), - theme: highlighter::Theme::SolarizedDark, - } + type Theme = Theme; + type Executor = executor::Default; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + Self { + file: None, + content: text_editor::Content::new(), + theme: highlighter::Theme::SolarizedDark, + is_loading: true, + is_dirty: false, + }, + Command::perform(load_file(default_file()), Message::FileOpened), + ) } fn title(&self) -> String { String::from("Editor - Iced") } - fn update(&mut self, message: Message) { + fn update(&mut self, message: Message) -> Command { match message { Message::Edit(action) => { + self.is_dirty = self.is_dirty || action.is_edit(); + self.content.edit(action); + + Command::none() } Message::ThemeSelected(theme) => { self.theme = theme; + + Command::none() + } + Message::NewFile => { + if !self.is_loading { + self.file = None; + self.content = text_editor::Content::new(); + } + + Command::none() + } + Message::OpenFile => { + if self.is_loading { + Command::none() + } else { + self.is_loading = true; + + Command::perform(open_file(), Message::FileOpened) + } + } + Message::FileOpened(result) => { + self.is_loading = false; + self.is_dirty = false; + + if let Ok((path, contents)) = result { + self.file = Some(path); + self.content = text_editor::Content::with(&contents); + } + + Command::none() + } + Message::SaveFile => { + if self.is_loading { + Command::none() + } else { + self.is_loading = true; + + let mut contents = self.content.lines().enumerate().fold( + String::new(), + |mut contents, (i, line)| { + if i > 0 { + contents.push_str("\n"); + } + + contents.push_str(&line); + + contents + }, + ); + + if !contents.ends_with("\n") { + contents.push_str("\n"); + } + + Command::perform( + save_file(self.file.clone(), contents), + Message::FileSaved, + ) + } + } + Message::FileSaved(result) => { + self.is_loading = false; + + if let Ok(path) = result { + self.file = Some(path); + self.is_dirty = false; + } + + Command::none() } } } fn view(&self) -> Element { + let controls = row![ + action(new_icon(), "New file", Some(Message::NewFile)), + action( + open_icon(), + "Open file", + (!self.is_loading).then_some(Message::OpenFile) + ), + action( + save_icon(), + "Save file", + self.is_dirty.then_some(Message::SaveFile) + ), + horizontal_space(Length::Fill), + pick_list( + highlighter::Theme::ALL, + Some(self.theme), + Message::ThemeSelected + ) + .text_size(14) + .padding([5, 10]) + ] + .spacing(10); + + let status = row![ + text(if let Some(path) = &self.file { + let path = path.display().to_string(); + + if path.len() > 60 { + format!("...{}", &path[path.len() - 40..]) + } else { + path + } + } else { + String::from("New file") + }), + horizontal_space(Length::Fill), + text({ + let (line, column) = self.content.cursor_position(); + + format!("{}:{}", line + 1, column + 1) + }) + ] + .spacing(10); + column![ - row![ - horizontal_space(Length::Fill), - pick_list( - highlighter::Theme::ALL, - Some(self.theme), - Message::ThemeSelected - ) - .padding([5, 10]) - ] - .spacing(10), + controls, text_editor(&self.content) .on_edit(Message::Edit) - .font(Font::with_name("Hasklug Nerd Font Mono")) .highlight::(highlighter::Settings { theme: self.theme, - extension: String::from("rs"), + extension: self + .file + .as_deref() + .and_then(Path::extension) + .and_then(ffi::OsStr::to_str) + .map(str::to_string) + .unwrap_or(String::from("rs")), }), + status, ] .spacing(10) - .padding(20) + .padding(10) .into() } @@ -73,6 +221,97 @@ impl Sandbox for Editor { } } +#[derive(Debug, Clone)] +pub enum Error { + DialogClosed, + IoError(io::ErrorKind), +} + +fn default_file() -> PathBuf { + PathBuf::from(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))) +} + +async fn open_file() -> Result<(PathBuf, Arc), Error> { + let picked_file = rfd::AsyncFileDialog::new() + .set_title("Open a text file...") + .pick_file() + .await + .ok_or(Error::DialogClosed)?; + + load_file(picked_file.path().to_owned()).await +} + +async fn load_file(path: PathBuf) -> Result<(PathBuf, Arc), Error> { + let contents = tokio::fs::read_to_string(&path) + .await + .map(Arc::new) + .map_err(|error| Error::IoError(error.kind()))?; + + Ok((path, contents)) +} + +async fn save_file( + path: Option, + contents: String, +) -> Result { + let path = if let Some(path) = path { + path + } else { + rfd::AsyncFileDialog::new() + .save_file() + .await + .as_ref() + .map(rfd::FileHandle::path) + .map(Path::to_owned) + .ok_or(Error::DialogClosed)? + }; + + let _ = tokio::fs::write(&path, contents) + .await + .map_err(|error| Error::IoError(error.kind()))?; + + Ok(path) +} + +fn action<'a, Message: Clone + 'a>( + content: impl Into>, + label: &'a str, + on_press: Option, +) -> Element<'a, Message> { + let action = + button(container(content).width(Length::Fill).center_x()).width(40); + + if let Some(on_press) = on_press { + tooltip( + action.on_press(on_press), + label, + tooltip::Position::FollowCursor, + ) + .style(theme::Container::Box) + .into() + } else { + action.style(theme::Button::Secondary).into() + } +} + +fn new_icon<'a, Message>() -> Element<'a, Message> { + icon('\u{0e800}') +} + +fn save_icon<'a, Message>() -> Element<'a, Message> { + icon('\u{0e801}') +} + +fn open_icon<'a, Message>() -> Element<'a, Message> { + icon('\u{0f115}') +} + +fn icon<'a, Message>(codepoint: char) -> Element<'a, Message> { + const ICON_FONT: Font = Font::with_name("editor-icons"); + + text(codepoint).font(ICON_FONT).into() +} + mod highlighter { use iced::advanced::text::highlighter; use iced::widget::text_editor; -- cgit From 161a971d065b3254a2f11cb374d2c94c2d67646b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Sep 2023 19:08:57 +0200 Subject: Fix `clippy` lints --- examples/editor/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 09c4b9b5..785dfb3b 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -118,7 +118,7 @@ impl Application for Editor { String::new(), |mut contents, (i, line)| { if i > 0 { - contents.push_str("\n"); + contents.push('\n'); } contents.push_str(&line); @@ -127,8 +127,8 @@ impl Application for Editor { }, ); - if !contents.ends_with("\n") { - contents.push_str("\n"); + if !contents.ends_with('\n') { + contents.push('\n'); } Command::perform( @@ -266,7 +266,7 @@ async fn save_file( .ok_or(Error::DialogClosed)? }; - let _ = tokio::fs::write(&path, contents) + tokio::fs::write(&path, contents) .await .map_err(|error| Error::IoError(error.kind()))?; -- cgit From 8eec0033dee816bfcc102fc4f511c8bfe08c14ee Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Sep 2023 19:24:09 +0200 Subject: Remove unnecessary `monospaced` flag in `Font` --- examples/editor/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 785dfb3b..5018b3cb 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -16,10 +16,7 @@ use std::sync::Arc; pub fn main() -> iced::Result { Editor::run(Settings { fonts: vec![include_bytes!("../fonts/icons.ttf").as_slice().into()], - default_font: Font { - monospaced: true, - ..Font::with_name("Hasklug Nerd Font Mono") - }, + default_font: Font::with_name("Hasklug Nerd Font Mono"), ..Settings::default() }) } -- cgit From d1d0b3aaee84003278b9db3e86687e776f20b346 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Sep 2023 20:14:38 +0200 Subject: Use `Font::MONOSPACE` in `editor` example --- examples/editor/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 5018b3cb..277eb3e9 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -16,7 +16,7 @@ use std::sync::Arc; pub fn main() -> iced::Result { Editor::run(Settings { fonts: vec![include_bytes!("../fonts/icons.ttf").as_slice().into()], - default_font: Font::with_name("Hasklug Nerd Font Mono"), + default_font: Font::MONOSPACE, ..Settings::default() }) } -- cgit From 3d6b9637c3b1c9f3c654a3ecef7a247cfd6edef3 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 19 Sep 2023 01:31:10 -0400 Subject: Chore: Inline format args for ease of reading A minor cleanup to inline all simple cases of format arguments. Makes the format strings just a bit easier to read. --- examples/game_of_life/src/main.rs | 3 +-- examples/screenshot/src/main.rs | 10 +++++----- examples/system_information/src/main.rs | 4 ++-- examples/todos/src/main.rs | 3 +-- examples/visible_bounds/src/main.rs | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index e451cb06..21a21142 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -610,8 +610,7 @@ mod grid { frame.fill_text(Text { content: format!( - "{} cell{} @ {:?} ({})", - cell_count, + "{cell_count} cell{} @ {:?} ({})", if cell_count == 1 { "" } else { "s" }, self.last_tick_duration, self.last_queued_ticks diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 68c9d031..1c9e187c 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -184,8 +184,8 @@ impl Application for Example { .align_items(Alignment::Center); if let Some(crop_error) = &self.crop_error { - crop_controls = crop_controls - .push(text(format!("Crop error! \n{}", crop_error))); + crop_controls = + crop_controls.push(text(format!("Crop error! \n{crop_error}"))); } let mut controls = column![ @@ -221,9 +221,9 @@ impl Application for Example { if let Some(png_result) = &self.saved_png_path { let msg = match png_result { - Ok(path) => format!("Png saved as: {:?}!", path), + Ok(path) => format!("Png saved as: {path:?}!"), Err(msg) => { - format!("Png could not be saved due to:\n{:?}", msg) + format!("Png could not be saved due to:\n{msg:?}") } }; @@ -281,7 +281,7 @@ async fn save_to_png(screenshot: Screenshot) -> Result { ColorType::Rgba8, ) .map(|_| path) - .map_err(|err| PngError(format!("{:?}", err))) + .map_err(|err| PngError(format!("{err:?}"))) } #[derive(Clone, Debug)] diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 633b6e2b..507431ee 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -105,8 +105,8 @@ impl Application for Example { ByteSize::kb(information.memory_total).to_string(); let memory_total = text(format!( - "Memory (total): {} kb ({})", - information.memory_total, memory_readable + "Memory (total): {} kb ({memory_readable})", + information.memory_total, )); let memory_text = if let Some(memory_used) = diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 501bf67e..3048a668 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -415,8 +415,7 @@ fn view_controls(tasks: &[Task], current_filter: Filter) -> Element { row![ text(format!( - "{} {} left", - tasks_left, + "{tasks_left} {} left", if tasks_left == 1 { "task" } else { "tasks" } )) .width(Length::Fill), diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs index 42dfc24c..697badb4 100644 --- a/examples/visible_bounds/src/main.rs +++ b/examples/visible_bounds/src/main.rs @@ -94,7 +94,7 @@ impl Application for Example { data_row( label, match bounds { - Some(bounds) => format!("{:?}", bounds), + Some(bounds) => format!("{bounds:?}"), None => "not visible".to_string(), }, if bounds -- cgit From c997aad85d7ee6e77085e50e5e599002549d228f Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 19 Sep 2023 01:46:46 -0400 Subject: Chore: Apply clippy map transformations Convert `.map().unwrap_or()` to `.map_or()` and similar transformations. --- examples/integration/src/main.rs | 6 ++++-- examples/screenshot/src/main.rs | 5 +---- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 7945bd20..243297b2 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -200,8 +200,10 @@ pub fn main() -> Result<(), Box> { viewport.scale_factor(), ) }) - .map(mouse::Cursor::Available) - .unwrap_or(mouse::Cursor::Unavailable), + .map_or( + mouse::Cursor::Unavailable, + mouse::Cursor::Available, + ), &mut renderer, &Theme::Dark, &renderer::Style { diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 68c9d031..4dc1b051 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -293,10 +293,7 @@ fn numeric_input( ) -> Element<'_, Option> { text_input( placeholder, - &value - .as_ref() - .map(ToString::to_string) - .unwrap_or_else(String::new), + &value.as_ref().map_or_else(String::new, ToString::to_string), ) .on_input(move |text| { if text.is_empty() { -- cgit From efd0ff6ded4e647e5fad0964555dbed541a075d7 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 19 Sep 2023 01:52:25 -0400 Subject: Chore: Apply some minor clippy fixes * Use `.elapsed()` for duration * Use direct iteration without calling `.iter()` and the like * order fields in the `Text` struct creation as declared --- examples/game_of_life/src/main.rs | 2 +- examples/tour/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index e451cb06..e3089beb 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -793,7 +793,7 @@ mod grid { } } - for (cell, amount) in adjacent_life.iter() { + for (cell, amount) in &adjacent_life { match amount { 2 => {} 3 => { diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 3e3a8ad7..952300bb 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -482,7 +482,7 @@ impl<'a> Step { column( Language::all() .iter() - .cloned() + .copied() .map(|language| { radio( language, -- cgit From 06dc12bfbf75958c6534306b3d1b57ae47bdb37a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 19 Sep 2023 19:35:28 +0200 Subject: Simplify `editor` example --- examples/editor/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 277eb3e9..6def2082 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -4,7 +4,7 @@ use iced::widget::{ button, column, container, horizontal_space, pick_list, row, text, text_editor, tooltip, }; -use iced::{Application, Command, Element, Font, Length, Settings}; +use iced::{Alignment, Application, Command, Element, Font, Length, Settings}; use highlighter::Highlighter; @@ -169,7 +169,8 @@ impl Application for Editor { .text_size(14) .padding([5, 10]) ] - .spacing(10); + .spacing(10) + .align_items(Alignment::Center); let status = row![ text(if let Some(path) = &self.file { @@ -275,8 +276,7 @@ fn action<'a, Message: Clone + 'a>( label: &'a str, on_press: Option, ) -> Element<'a, Message> { - let action = - button(container(content).width(Length::Fill).center_x()).width(40); + let action = button(container(content).width(30).center_x()); if let Some(on_press) = on_press { tooltip( @@ -316,7 +316,7 @@ mod highlighter { use std::ops::Range; use syntect::highlighting; - use syntect::parsing::{self, SyntaxReference}; + use syntect::parsing; #[derive(Debug, Clone, PartialEq)] pub struct Settings { @@ -374,7 +374,7 @@ mod highlighter { pub struct Highlighter { syntaxes: parsing::SyntaxSet, - syntax: SyntaxReference, + syntax: parsing::SyntaxReference, theme: highlighting::Theme, caches: Vec<(parsing::ParseState, parsing::ScopeStack)>, current_line: usize, -- cgit From c0a141ab026f5686d6bd92c8807b174396cb9105 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 19 Sep 2023 19:39:23 +0200 Subject: Save file on `Cmd+S` in `editor` example --- examples/editor/src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 6def2082..36d4287c 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,10 +1,14 @@ use iced::executor; +use iced::keyboard; use iced::theme::{self, Theme}; use iced::widget::{ button, column, container, horizontal_space, pick_list, row, text, text_editor, tooltip, }; -use iced::{Alignment, Application, Command, Element, Font, Length, Settings}; +use iced::{ + Alignment, Application, Command, Element, Font, Length, Settings, + Subscription, +}; use highlighter::Highlighter; @@ -147,6 +151,15 @@ impl Application for Editor { } } + fn subscription(&self) -> Subscription { + keyboard::on_key_press(|key_code, modifiers| match key_code { + keyboard::KeyCode::S if modifiers.command() => { + Some(Message::SaveFile) + } + _ => None, + }) + } + fn view(&self) -> Element { let controls = row![ action(new_icon(), "New file", Some(Message::NewFile)), -- cgit From f806d001e6fb44b5a45029ca257261e6e0d4d4b2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 19 Sep 2023 20:48:50 +0200 Subject: Introduce new `iced_highlighter` subcrate --- examples/editor/Cargo.toml | 2 +- examples/editor/src/main.rs | 251 +++----------------------------------------- 2 files changed, 15 insertions(+), 238 deletions(-) (limited to 'examples') diff --git a/examples/editor/Cargo.toml b/examples/editor/Cargo.toml index eeb34aa1..a77b1e9f 100644 --- a/examples/editor/Cargo.toml +++ b/examples/editor/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["advanced", "tokio", "debug"] +iced.features = ["highlighter", "tokio", "debug"] tokio.workspace = true tokio.features = ["fs"] diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 36d4287c..d513090f 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,4 +1,5 @@ use iced::executor; +use iced::highlighter::{self, Highlighter}; use iced::keyboard; use iced::theme::{self, Theme}; use iced::widget::{ @@ -10,8 +11,6 @@ use iced::{ Subscription, }; -use highlighter::Highlighter; - use std::ffi; use std::io; use std::path::{Path, PathBuf}; @@ -210,16 +209,19 @@ impl Application for Editor { controls, text_editor(&self.content) .on_edit(Message::Edit) - .highlight::(highlighter::Settings { - theme: self.theme, - extension: self - .file - .as_deref() - .and_then(Path::extension) - .and_then(ffi::OsStr::to_str) - .map(str::to_string) - .unwrap_or(String::from("rs")), - }), + .highlight::( + highlighter::Settings { + theme: self.theme, + extension: self + .file + .as_deref() + .and_then(Path::extension) + .and_then(ffi::OsStr::to_str) + .map(str::to_string) + .unwrap_or(String::from("rs")), + }, + |highlight, _theme| highlight.to_format() + ), status, ] .spacing(10) @@ -321,228 +323,3 @@ fn icon<'a, Message>(codepoint: char) -> Element<'a, Message> { text(codepoint).font(ICON_FONT).into() } - -mod highlighter { - use iced::advanced::text::highlighter; - use iced::widget::text_editor; - use iced::{Color, Font}; - - use std::ops::Range; - use syntect::highlighting; - use syntect::parsing; - - #[derive(Debug, Clone, PartialEq)] - pub struct Settings { - pub theme: Theme, - pub extension: String, - } - - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - pub enum Theme { - SolarizedDark, - InspiredGitHub, - Base16Mocha, - } - - impl Theme { - pub const ALL: &[Self] = - &[Self::SolarizedDark, Self::InspiredGitHub, Self::Base16Mocha]; - - fn key(&self) -> &'static str { - match self { - Theme::InspiredGitHub => "InspiredGitHub", - Theme::Base16Mocha => "base16-mocha.dark", - Theme::SolarizedDark => "Solarized (dark)", - } - } - } - - impl std::fmt::Display for Theme { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Theme::InspiredGitHub => write!(f, "Inspired GitHub"), - Theme::Base16Mocha => write!(f, "Mocha"), - Theme::SolarizedDark => write!(f, "Solarized Dark"), - } - } - } - - pub struct Highlight(highlighting::StyleModifier); - - impl text_editor::Highlight for Highlight { - fn format(&self, _theme: &iced::Theme) -> highlighter::Format { - highlighter::Format { - color: self.0.foreground.map(|color| { - Color::from_rgba8( - color.r, - color.g, - color.b, - color.a as f32 / 255.0, - ) - }), - font: None, - } - } - } - - pub struct Highlighter { - syntaxes: parsing::SyntaxSet, - syntax: parsing::SyntaxReference, - theme: highlighting::Theme, - caches: Vec<(parsing::ParseState, parsing::ScopeStack)>, - current_line: usize, - } - - const LINES_PER_SNAPSHOT: usize = 50; - - impl highlighter::Highlighter for Highlighter { - type Settings = Settings; - type Highlight = Highlight; - - type Iterator<'a> = - Box, Self::Highlight)> + 'a>; - - fn new(settings: &Self::Settings) -> Self { - let syntaxes = parsing::SyntaxSet::load_defaults_nonewlines(); - - let syntax = syntaxes - .find_syntax_by_token(&settings.extension) - .unwrap_or_else(|| syntaxes.find_syntax_plain_text()); - - let theme = highlighting::ThemeSet::load_defaults() - .themes - .remove(settings.theme.key()) - .unwrap(); - - let parser = parsing::ParseState::new(syntax); - let stack = parsing::ScopeStack::new(); - - Highlighter { - syntax: syntax.clone(), - syntaxes, - theme, - caches: vec![(parser, stack)], - current_line: 0, - } - } - - fn update(&mut self, new_settings: &Self::Settings) { - self.syntax = self - .syntaxes - .find_syntax_by_token(&new_settings.extension) - .unwrap_or_else(|| self.syntaxes.find_syntax_plain_text()) - .clone(); - - self.theme = highlighting::ThemeSet::load_defaults() - .themes - .remove(new_settings.theme.key()) - .unwrap(); - - // Restart the highlighter - self.change_line(0); - } - - fn change_line(&mut self, line: usize) { - let snapshot = line / LINES_PER_SNAPSHOT; - - if snapshot <= self.caches.len() { - self.caches.truncate(snapshot); - self.current_line = snapshot * LINES_PER_SNAPSHOT; - } else { - self.caches.truncate(1); - self.current_line = 0; - } - - let (parser, stack) = - self.caches.last().cloned().unwrap_or_else(|| { - ( - parsing::ParseState::new(&self.syntax), - parsing::ScopeStack::new(), - ) - }); - - self.caches.push((parser, stack)); - } - - fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> { - if self.current_line / LINES_PER_SNAPSHOT >= self.caches.len() { - let (parser, stack) = - self.caches.last().expect("Caches must not be empty"); - - self.caches.push((parser.clone(), stack.clone())); - } - - self.current_line += 1; - - let (parser, stack) = - self.caches.last_mut().expect("Caches must not be empty"); - - let ops = - parser.parse_line(line, &self.syntaxes).unwrap_or_default(); - - let highlighter = highlighting::Highlighter::new(&self.theme); - - Box::new( - ScopeRangeIterator { - ops, - line_length: line.len(), - index: 0, - last_str_index: 0, - } - .filter_map(move |(range, scope)| { - let _ = stack.apply(&scope); - - if range.is_empty() { - None - } else { - Some(( - range, - Highlight( - highlighter.style_mod_for_stack(&stack.scopes), - ), - )) - } - }), - ) - } - - fn current_line(&self) -> usize { - self.current_line - } - } - - pub struct ScopeRangeIterator { - ops: Vec<(usize, parsing::ScopeStackOp)>, - line_length: usize, - index: usize, - last_str_index: usize, - } - - impl Iterator for ScopeRangeIterator { - type Item = (std::ops::Range, parsing::ScopeStackOp); - - fn next(&mut self) -> Option { - if self.index > self.ops.len() { - return None; - } - - let next_str_i = if self.index == self.ops.len() { - self.line_length - } else { - self.ops[self.index].0 - }; - - let range = self.last_str_index..next_str_i; - self.last_str_index = next_str_i; - - let op = if self.index == 0 { - parsing::ScopeStackOp::Noop - } else { - self.ops[self.index - 1].1.clone() - }; - - self.index += 1; - Some((range, op)) - } - } -} -- cgit From d9fbecf0d80234d63e7e5711f28fc35ee75fa503 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 19 Sep 2023 20:58:15 +0200 Subject: Remove `syntect` dependency from `editor` example --- examples/editor/Cargo.toml | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/Cargo.toml b/examples/editor/Cargo.toml index a77b1e9f..a3f6ea3b 100644 --- a/examples/editor/Cargo.toml +++ b/examples/editor/Cargo.toml @@ -12,5 +12,4 @@ iced.features = ["highlighter", "tokio", "debug"] tokio.workspace = true tokio.features = ["fs"] -syntect = "5.1" rfd = "0.12" -- cgit From ff78e97ad7df4db3b2a97b94e99854f2f9e3021a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 01:21:42 +0200 Subject: Introduce more themes to `iced_highlighter` --- examples/editor/src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index d513090f..f49ca6e8 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -230,7 +230,11 @@ impl Application for Editor { } fn theme(&self) -> Theme { - Theme::Dark + if self.theme.is_dark() { + Theme::Dark + } else { + Theme::Light + } } } -- cgit From 34f07b60273d6cfe13834af54cd0e24d34569387 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 04:11:52 +0200 Subject: Fix `clippy::semicolon_if_nothing_returned` --- examples/bezier_tool/src/main.rs | 2 +- examples/clock/src/main.rs | 2 +- examples/toast/src/main.rs | 2 +- examples/tooltip/src/main.rs | 2 +- examples/tour/src/main.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 310be28f..9e4bc49c 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -81,7 +81,7 @@ mod bezier { } pub fn request_redraw(&mut self) { - self.cache.clear() + self.cache.clear(); } } diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index fae77bc0..eec70dde 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -141,7 +141,7 @@ impl canvas::Program for Clock { frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.second(), 60)); frame.stroke(&long_hand, thin_stroke()); - }) + }); }); vec![clock] diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 8570a38e..20c3dd42 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -639,7 +639,7 @@ mod toast { child .as_widget() .operate(state, layout, renderer, operation); - }) + }); }); } diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index 35b862a8..a904cce0 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -40,7 +40,7 @@ impl Sandbox for Example { Position::Right => Position::FollowCursor, }; - self.position = position + self.position = position; } } } diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 952300bb..d46e40d1 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -285,7 +285,7 @@ impl<'a> Step { is_showing_icon, .. } = self { - *is_showing_icon = toggle + *is_showing_icon = toggle; } } }; -- cgit From 6c386e90a12fd26da12541da3f086dddb7211c0c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 04:33:48 +0200 Subject: Fix `clippy::trivially-copy-pass-by-ref` --- examples/pane_grid/src/main.rs | 29 +++++++++++------------------ examples/todos/src/main.rs | 2 +- 2 files changed, 12 insertions(+), 19 deletions(-) (limited to 'examples') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index af87e2c0..aa3149bb 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -61,11 +61,8 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { Message::Split(axis, pane) => { - let result = self.panes.split( - axis, - &pane, - Pane::new(self.panes_created), - ); + let result = + self.panes.split(axis, pane, Pane::new(self.panes_created)); if let Some((pane, _)) = result { self.focus = Some(pane); @@ -77,7 +74,7 @@ impl Application for Example { if let Some(pane) = self.focus { let result = self.panes.split( axis, - &pane, + pane, Pane::new(self.panes_created), ); @@ -90,8 +87,7 @@ impl Application for Example { } Message::FocusAdjacent(direction) => { if let Some(pane) = self.focus { - if let Some(adjacent) = - self.panes.adjacent(&pane, direction) + if let Some(adjacent) = self.panes.adjacent(pane, direction) { self.focus = Some(adjacent); } @@ -101,37 +97,34 @@ impl Application for Example { self.focus = Some(pane); } Message::Resized(pane_grid::ResizeEvent { split, ratio }) => { - self.panes.resize(&split, ratio); + self.panes.resize(split, ratio); } Message::Dragged(pane_grid::DragEvent::Dropped { pane, target, }) => { - self.panes.drop(&pane, target); + self.panes.drop(pane, target); } Message::Dragged(_) => {} Message::TogglePin(pane) => { - if let Some(Pane { is_pinned, .. }) = self.panes.get_mut(&pane) - { + if let Some(Pane { is_pinned, .. }) = self.panes.get_mut(pane) { *is_pinned = !*is_pinned; } } - Message::Maximize(pane) => self.panes.maximize(&pane), + Message::Maximize(pane) => self.panes.maximize(pane), Message::Restore => { self.panes.restore(); } Message::Close(pane) => { - if let Some((_, sibling)) = self.panes.close(&pane) { + if let Some((_, sibling)) = self.panes.close(pane) { self.focus = Some(sibling); } } Message::CloseFocused => { if let Some(pane) = self.focus { - if let Some(Pane { is_pinned, .. }) = self.panes.get(&pane) - { + if let Some(Pane { is_pinned, .. }) = self.panes.get(pane) { if !is_pinned { - if let Some((_, sibling)) = self.panes.close(&pane) - { + if let Some((_, sibling)) = self.panes.close(pane) { self.focus = Some(sibling); } } diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 3048a668..1ad3aba7 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -443,7 +443,7 @@ pub enum Filter { } impl Filter { - fn matches(&self, task: &Task) -> bool { + fn matches(self, task: &Task) -> bool { match self { Filter::All => true, Filter::Active => !task.completed, -- cgit From 42ed90bc6f92b2085d193e7f143430b8d3847c21 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 04:51:08 +0200 Subject: Fix `clippy::default_trait_access` --- examples/arc/src/main.rs | 2 +- examples/clock/src/main.rs | 2 +- examples/integration/src/controls.rs | 2 +- examples/lazy/src/main.rs | 4 ++-- examples/modal/src/main.rs | 6 ++++-- examples/scrollable/src/main.rs | 4 ++-- examples/solar_system/src/main.rs | 4 ++-- 7 files changed, 13 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index df565859..6a68cca1 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -37,7 +37,7 @@ impl Application for Arc { ( Arc { start: Instant::now(), - cache: Default::default(), + cache: Cache::default(), }, Command::none(), ) diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index eec70dde..920aa0c5 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -35,7 +35,7 @@ impl Application for Clock { Clock { now: time::OffsetDateTime::now_local() .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - clock: Default::default(), + clock: Cache::default(), }, Command::none(), ) diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs index 14e53ede..4714c397 100644 --- a/examples/integration/src/controls.rs +++ b/examples/integration/src/controls.rs @@ -19,7 +19,7 @@ impl Controls { pub fn new() -> Controls { Controls { background_color: Color::BLACK, - text: Default::default(), + text: String::default(), } } diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs index c6baa6a1..9bf17c56 100644 --- a/examples/lazy/src/main.rs +++ b/examples/lazy/src/main.rs @@ -27,7 +27,7 @@ impl Default for App { .into_iter() .map(From::from) .collect(), - input: Default::default(), + input: String::default(), order: Order::Ascending, } } @@ -107,7 +107,7 @@ impl From<&str> for Item { fn from(s: &str) -> Self { Self { name: s.to_owned(), - color: Default::default(), + color: Color::default(), } } } diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index c050d3cc..b0e2c81b 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -228,7 +228,9 @@ mod modal { use iced::alignment::Alignment; use iced::event; use iced::mouse; - use iced::{Color, Element, Event, Length, Point, Rectangle, Size}; + use iced::{ + BorderRadius, Color, Element, Event, Length, Point, Rectangle, Size, + }; /// A widget that centers a modal element over some base element pub struct Modal<'a, Message, Renderer> { @@ -474,7 +476,7 @@ mod modal { renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_radius: Default::default(), + border_radius: BorderRadius::default(), border_width: 0.0, border_color: Color::TRANSPARENT, }, diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 21e69284..d82ea841 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -391,12 +391,12 @@ impl scrollable::StyleSheet for ScrollbarCustomStyle { .background, border_radius: 2.0.into(), border_width: 0.0, - border_color: Default::default(), + border_color: Color::default(), scroller: Scroller { color: Color::from_rgb8(250, 85, 134), border_radius: 2.0.into(), border_width: 0.0, - border_color: Default::default(), + border_color: Color::default(), }, } } else { diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8fa8946e..8295dded 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -117,8 +117,8 @@ impl State { let (width, height) = window::Settings::default().size; State { - space_cache: Default::default(), - system_cache: Default::default(), + space_cache: canvas::Cache::default(), + system_cache: canvas::Cache::default(), start: now, now, stars: Self::generate_stars(width, height), -- cgit From caed50b277495e4375975f3f4e271b8fcbc0c33f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 05:03:25 +0200 Subject: Fix `clippy::match-wildcard-for-single-variants` --- examples/download_progress/src/main.rs | 2 +- examples/game_of_life/src/main.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index 001a1f8f..e52c604c 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -123,7 +123,7 @@ impl Download { | State::Errored { .. } => { self.state = State::Downloading { progress: 0.0 }; } - _ => {} + State::Downloading{ .. } => {} } } diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 437d89d5..c774e769 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -472,7 +472,7 @@ mod grid { * (1.0 / self.scaling), )) } - _ => None, + Interaction::None => None, }; let event_status = match interaction { @@ -676,7 +676,7 @@ mod grid { Interaction::None if cursor.is_over(bounds) => { mouse::Interaction::Crosshair } - _ => mouse::Interaction::default(), + Interaction::None => mouse::Interaction::default(), } } } -- cgit From f8f1a8634402a5eb4275ff0814d03a3104fea65a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 05:30:08 +0200 Subject: Fix `clippy::manual_let_else` --- examples/bezier_tool/src/main.rs | 9 +++------ examples/game_of_life/src/main.rs | 9 +++------ examples/integration/src/main.rs | 2 +- examples/integration/src/scene.rs | 1 - examples/sierpinski_triangle/src/main.rs | 5 +---- examples/websocket/src/echo/server.rs | 5 +---- 6 files changed, 9 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 9e4bc49c..56cb23ba 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -100,12 +100,9 @@ mod bezier { bounds: Rectangle, cursor: mouse::Cursor, ) -> (event::Status, Option) { - let cursor_position = - if let Some(position) = cursor.position_in(bounds) { - position - } else { - return (event::Status::Ignored, None); - }; + let Some(cursor_position) = cursor.position_in(bounds) else { + return (event::Status::Ignored, None); + }; match event { Event::Mouse(mouse_event) => { diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index c774e769..96840143 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -406,12 +406,9 @@ mod grid { *interaction = Interaction::None; } - let cursor_position = - if let Some(position) = cursor.position_in(bounds) { - position - } else { - return (event::Status::Ignored, None); - }; + let Some(cursor_position) = cursor.position_in(bounds) else { + return (event::Status::Ignored, None); + }; let cell = Cell::at(self.project(cursor_position, bounds.size())); let is_populated = self.state.contains(&cell); diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 243297b2..4415fefa 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -256,7 +256,7 @@ pub fn main() -> Result<(), Box> { { // We clear the frame - let mut render_pass = scene.clear( + let mut render_pass = Scene::clear( &view, &mut encoder, program.background_color(), diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs index 90c7efbf..01808f40 100644 --- a/examples/integration/src/scene.rs +++ b/examples/integration/src/scene.rs @@ -16,7 +16,6 @@ impl Scene { } pub fn clear<'a>( - &self, target: &'a wgpu::TextureView, encoder: &'a mut wgpu::CommandEncoder, background_color: Color, diff --git a/examples/sierpinski_triangle/src/main.rs b/examples/sierpinski_triangle/src/main.rs index 885d3c63..ef935c33 100644 --- a/examples/sierpinski_triangle/src/main.rs +++ b/examples/sierpinski_triangle/src/main.rs @@ -108,10 +108,7 @@ impl canvas::Program for SierpinskiGraph { bounds: Rectangle, cursor: mouse::Cursor, ) -> (event::Status, Option) { - let cursor_position = if let Some(position) = cursor.position_in(bounds) - { - position - } else { + let Some(cursor_position) = cursor.position_in(bounds) else { return (event::Status::Ignored, None); }; diff --git a/examples/websocket/src/echo/server.rs b/examples/websocket/src/echo/server.rs index 168a635e..a696a7a4 100644 --- a/examples/websocket/src/echo/server.rs +++ b/examples/websocket/src/echo/server.rs @@ -47,10 +47,7 @@ async fn user_connected(ws: WebSocket) { }); while let Some(result) = user_ws_rx.next().await { - let msg = match result { - Ok(msg) => msg, - Err(_) => break, - }; + let Ok(msg) = result else { break }; let _ = tx.send(msg).await; } -- cgit From 432d9f5f97a7312878f2f86ead13b6742638f7e8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 05:36:11 +0200 Subject: Fix `clippy::unused_async` --- examples/screenshot/Cargo.toml | 8 ++++++-- examples/screenshot/src/main.rs | 23 ++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/screenshot/Cargo.toml b/examples/screenshot/Cargo.toml index dcd77439..77b108bd 100644 --- a/examples/screenshot/Cargo.toml +++ b/examples/screenshot/Cargo.toml @@ -7,7 +7,11 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["debug", "image", "advanced"] +iced.features = ["debug", "image", "advanced", "tokio"] + +image.workspace = true +image.features = ["png"] + +tokio.workspace = true -image = { workspace = true, features = ["png"]} tracing-subscriber = "0.3" \ No newline at end of file diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index d9784dc8..fa06d3e9 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -273,15 +273,20 @@ impl Application for Example { async fn save_to_png(screenshot: Screenshot) -> Result { let path = "screenshot.png".to_string(); - img::save_buffer( - &path, - &screenshot.bytes, - screenshot.size.width, - screenshot.size.height, - ColorType::Rgba8, - ) - .map(|_| path) - .map_err(|err| PngError(format!("{err:?}"))) + + tokio::task::spawn_blocking(move || { + img::save_buffer( + &path, + &screenshot.bytes, + screenshot.size.width, + screenshot.size.height, + ColorType::Rgba8, + ) + .map(|_| path) + .map_err(|err| PngError(format!("{err:?}"))) + }) + .await + .expect("Blocking task to finish") } #[derive(Clone, Debug)] -- cgit From 33d780f691829ecd32f3a218008fcb40e005deb4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 05:37:20 +0200 Subject: Run `cargo fmt` --- examples/download_progress/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index e52c604c..a2fcb275 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -123,7 +123,7 @@ impl Download { | State::Errored { .. } => { self.state = State::Downloading { progress: 0.0 }; } - State::Downloading{ .. } => {} + State::Downloading { .. } => {} } } -- cgit From b27762554627b8e89f2b840b1a8a512e22d4cd87 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Sep 2023 16:26:43 +0200 Subject: Revert "Chore: Apply clippy map transformations" This reverts commit c997aad85d7ee6e77085e50e5e599002549d228f. --- examples/integration/src/main.rs | 6 ++---- examples/screenshot/src/main.rs | 5 ++++- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 4415fefa..c26d52fe 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -200,10 +200,8 @@ pub fn main() -> Result<(), Box> { viewport.scale_factor(), ) }) - .map_or( - mouse::Cursor::Unavailable, - mouse::Cursor::Available, - ), + .map(mouse::Cursor::Available) + .unwrap_or(mouse::Cursor::Unavailable), &mut renderer, &Theme::Dark, &renderer::Style { diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index fa06d3e9..ab0a2ae3 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -298,7 +298,10 @@ fn numeric_input( ) -> Element<'_, Option> { text_input( placeholder, - &value.as_ref().map_or_else(String::new, ToString::to_string), + &value + .as_ref() + .map(ToString::to_string) + .unwrap_or_else(String::new), ) .on_input(move |text| { if text.is_empty() { -- cgit From 8cc19de254c37d3123d5ea1b6513f1f34d35c7c8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Sep 2023 06:00:51 +0200 Subject: Add `text` helper method for `text_editor::Content` --- examples/editor/src/main.rs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index f49ca6e8..a69e1f54 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -114,25 +114,8 @@ impl Application for Editor { } else { self.is_loading = true; - let mut contents = self.content.lines().enumerate().fold( - String::new(), - |mut contents, (i, line)| { - if i > 0 { - contents.push('\n'); - } - - contents.push_str(&line); - - contents - }, - ); - - if !contents.ends_with('\n') { - contents.push('\n'); - } - Command::perform( - save_file(self.file.clone(), contents), + save_file(self.file.clone(), self.content.text()), Message::FileSaved, ) } -- cgit From 54e6d2b5fa1fe29e2e3588b51f6cfff36563cefc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Oct 2023 17:49:19 -0500 Subject: Fix lint in `screenshot` example --- examples/screenshot/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index ab0a2ae3..f781a401 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -298,10 +298,7 @@ fn numeric_input( ) -> Element<'_, Option> { text_input( placeholder, - &value - .as_ref() - .map(ToString::to_string) - .unwrap_or_else(String::new), + &value.as_ref().map(ToString::to_string).unwrap_or_default(), ) .on_input(move |text| { if text.is_empty() { -- cgit From 86b877517feb15b2155c6cfef29246a3f281c8ae Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 27 Oct 2023 03:21:40 +0200 Subject: Update `wgpu` to `0.18` and `cosmic-text` to `0.10` --- examples/integration/src/scene.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs index 01808f40..e29558bf 100644 --- a/examples/integration/src/scene.rs +++ b/examples/integration/src/scene.rs @@ -36,10 +36,12 @@ impl Scene { a: a as f64, } }), - store: true, + store: wgpu::StoreOp::Store, }, })], depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, }) } -- cgit From 625cd745f38215b1cb8f629cdc6d2fa41c9a739a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 27 Oct 2023 05:04:14 +0200 Subject: Write documentation for the new text APIs --- examples/editor/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index a69e1f54..03d1e283 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -34,7 +34,7 @@ struct Editor { #[derive(Debug, Clone)] enum Message { - Edit(text_editor::Action), + ActionPerformed(text_editor::Action), ThemeSelected(highlighter::Theme), NewFile, OpenFile, @@ -68,10 +68,10 @@ impl Application for Editor { fn update(&mut self, message: Message) -> Command { match message { - Message::Edit(action) => { + Message::ActionPerformed(action) => { self.is_dirty = self.is_dirty || action.is_edit(); - self.content.edit(action); + self.content.perform(action); Command::none() } @@ -103,7 +103,7 @@ impl Application for Editor { if let Ok((path, contents)) = result { self.file = Some(path); - self.content = text_editor::Content::with(&contents); + self.content = text_editor::Content::with_text(&contents); } Command::none() @@ -191,7 +191,7 @@ impl Application for Editor { column![ controls, text_editor(&self.content) - .on_edit(Message::Edit) + .on_action(Message::ActionPerformed) .highlight::( highlighter::Settings { theme: self.theme, -- cgit From a5125d6fea824df1191777fe3eb53a2f748208b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 11 Nov 2023 07:02:01 +0100 Subject: Refactor texture image filtering - Support only `Linear` or `Nearest` - Simplify `Layer` groups - Move `FilterMethod` to `Image` and `image::Viewer` --- examples/tour/src/main.rs | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index d46e40d1..7003d8ae 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -1,4 +1,4 @@ -use iced::alignment; +use iced::alignment::{self, Alignment}; use iced::theme; use iced::widget::{ checkbox, column, container, horizontal_space, image, radio, row, @@ -126,7 +126,10 @@ impl Steps { Step::Toggler { can_continue: false, }, - Step::Image { width: 300 }, + Step::Image { + width: 300, + filter_method: image::FilterMethod::Linear, + }, Step::Scrollable, Step::TextInput { value: String::new(), @@ -195,6 +198,7 @@ enum Step { }, Image { width: u16, + filter_method: image::FilterMethod, }, Scrollable, TextInput { @@ -215,6 +219,7 @@ pub enum StepMessage { TextColorChanged(Color), LanguageSelected(Language), ImageWidthChanged(u16), + ImageUseNearestToggled(bool), InputChanged(String), ToggleSecureInput(bool), ToggleTextInputIcon(bool), @@ -265,6 +270,15 @@ impl<'a> Step { *width = new_width; } } + StepMessage::ImageUseNearestToggled(use_nearest) => { + if let Step::Image { filter_method, .. } = self { + *filter_method = if use_nearest { + image::FilterMethod::Nearest + } else { + image::FilterMethod::Linear + }; + } + } StepMessage::InputChanged(new_value) => { if let Step::TextInput { value, .. } = self { *value = new_value; @@ -330,7 +344,10 @@ impl<'a> Step { Step::Toggler { can_continue } => Self::toggler(*can_continue), Step::Slider { value } => Self::slider(*value), Step::Text { size, color } => Self::text(*size, *color), - Step::Image { width } => Self::image(*width), + Step::Image { + width, + filter_method, + } => Self::image(*width, *filter_method), Step::RowsAndColumns { layout, spacing } => { Self::rows_and_columns(*layout, *spacing) } @@ -525,16 +542,25 @@ impl<'a> Step { ) } - fn image(width: u16) -> Column<'a, StepMessage> { + fn image( + width: u16, + filter_method: image::FilterMethod, + ) -> Column<'a, StepMessage> { Self::container("Image") .push("An image that tries to keep its aspect ratio.") - .push(ferris(width)) + .push(ferris(width, filter_method)) .push(slider(100..=500, width, StepMessage::ImageWidthChanged)) .push( text(format!("Width: {width} px")) .width(Length::Fill) .horizontal_alignment(alignment::Horizontal::Center), ) + .push(checkbox( + "Use nearest interpolation", + filter_method == image::FilterMethod::Nearest, + StepMessage::ImageUseNearestToggled, + )) + .align_items(Alignment::Center) } fn scrollable() -> Column<'a, StepMessage> { @@ -555,7 +581,7 @@ impl<'a> Step { .horizontal_alignment(alignment::Horizontal::Center), ) .push(vertical_space(4096)) - .push(ferris(300)) + .push(ferris(300, image::FilterMethod::Linear)) .push( text("You made it!") .width(Length::Fill) @@ -646,7 +672,10 @@ impl<'a> Step { } } -fn ferris<'a>(width: u16) -> Container<'a, StepMessage> { +fn ferris<'a>( + width: u16, + filter_method: image::FilterMethod, +) -> Container<'a, StepMessage> { container( // This should go away once we unify resource loading on native // platforms @@ -655,6 +684,7 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> { } else { image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR"))) } + .filter_method(filter_method) .width(width), ) .width(Length::Fill) -- cgit From f98627a317615151681ca8b324052eb4a170b789 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 12 Nov 2023 03:40:32 +0100 Subject: Add missing `'static` lifetimes to constant slices --- examples/lazy/src/main.rs | 2 +- examples/modal/src/main.rs | 3 ++- examples/toast/src/main.rs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs index 9bf17c56..01560598 100644 --- a/examples/lazy/src/main.rs +++ b/examples/lazy/src/main.rs @@ -46,7 +46,7 @@ enum Color { } impl Color { - const ALL: &[Color] = &[ + const ALL: &'static [Color] = &[ Color::Black, Color::Red, Color::Orange, diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index b0e2c81b..3b69f5e6 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -205,7 +205,8 @@ enum Plan { } impl Plan { - pub const ALL: &[Self] = &[Self::Basic, Self::Pro, Self::Enterprise]; + pub const ALL: &'static [Self] = + &[Self::Basic, Self::Pro, Self::Enterprise]; } impl fmt::Display for Plan { diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 20c3dd42..5b089e8a 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -210,7 +210,7 @@ mod toast { } impl Status { - pub const ALL: &[Self] = + pub const ALL: &'static [Self] = &[Self::Primary, Self::Secondary, Self::Success, Self::Danger]; } -- cgit From 781ef1f94c4859aeeb852f801b72be095b8ff82b Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 14 Sep 2023 13:58:36 -0700 Subject: Added support for custom shader widget for iced_wgpu backend. --- examples/custom_shader/Cargo.toml | 13 + examples/custom_shader/src/camera.rs | 53 ++ examples/custom_shader/src/cubes.rs | 99 ++++ examples/custom_shader/src/main.rs | 174 ++++++ examples/custom_shader/src/pipeline.rs | 600 +++++++++++++++++++++ examples/custom_shader/src/primitive.rs | 95 ++++ examples/custom_shader/src/primitive/buffer.rs | 39 ++ examples/custom_shader/src/primitive/cube.rs | 324 +++++++++++ examples/custom_shader/src/primitive/uniforms.rs | 22 + examples/custom_shader/src/primitive/vertex.rs | 29 + examples/custom_shader/src/shaders/cubes.wgsl | 123 +++++ examples/custom_shader/src/shaders/depth.wgsl | 48 ++ .../src/textures/ice_cube_normal_map.png | Bin 0 -> 1773656 bytes .../custom_shader/src/textures/skybox/neg_x.jpg | Bin 0 -> 7549 bytes .../custom_shader/src/textures/skybox/neg_y.jpg | Bin 0 -> 2722 bytes .../custom_shader/src/textures/skybox/neg_z.jpg | Bin 0 -> 3986 bytes .../custom_shader/src/textures/skybox/pos_x.jpg | Bin 0 -> 5522 bytes .../custom_shader/src/textures/skybox/pos_y.jpg | Bin 0 -> 3382 bytes .../custom_shader/src/textures/skybox/pos_z.jpg | Bin 0 -> 5205 bytes examples/integration/src/main.rs | 1 + 20 files changed, 1620 insertions(+) create mode 100644 examples/custom_shader/Cargo.toml create mode 100644 examples/custom_shader/src/camera.rs create mode 100644 examples/custom_shader/src/cubes.rs create mode 100644 examples/custom_shader/src/main.rs create mode 100644 examples/custom_shader/src/pipeline.rs create mode 100644 examples/custom_shader/src/primitive.rs create mode 100644 examples/custom_shader/src/primitive/buffer.rs create mode 100644 examples/custom_shader/src/primitive/cube.rs create mode 100644 examples/custom_shader/src/primitive/uniforms.rs create mode 100644 examples/custom_shader/src/primitive/vertex.rs create mode 100644 examples/custom_shader/src/shaders/cubes.wgsl create mode 100644 examples/custom_shader/src/shaders/depth.wgsl create mode 100644 examples/custom_shader/src/textures/ice_cube_normal_map.png create mode 100644 examples/custom_shader/src/textures/skybox/neg_x.jpg create mode 100644 examples/custom_shader/src/textures/skybox/neg_y.jpg create mode 100644 examples/custom_shader/src/textures/skybox/neg_z.jpg create mode 100644 examples/custom_shader/src/textures/skybox/pos_x.jpg create mode 100644 examples/custom_shader/src/textures/skybox/pos_y.jpg create mode 100644 examples/custom_shader/src/textures/skybox/pos_z.jpg (limited to 'examples') diff --git a/examples/custom_shader/Cargo.toml b/examples/custom_shader/Cargo.toml new file mode 100644 index 00000000..7a927811 --- /dev/null +++ b/examples/custom_shader/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "custom_shader" +version = "0.1.0" +authors = ["Bingus "] +edition = "2021" + +[dependencies] +iced = { path = "../..", features = ["debug", "advanced"]} +image = { version = "0.24.6"} +wgpu = "0.17" +bytemuck = { version = "1.13.1" } +glam = { version = "0.24.0", features = ["bytemuck"] } +rand = "0.8.5" diff --git a/examples/custom_shader/src/camera.rs b/examples/custom_shader/src/camera.rs new file mode 100644 index 00000000..2a49c102 --- /dev/null +++ b/examples/custom_shader/src/camera.rs @@ -0,0 +1,53 @@ +use glam::{mat4, vec3, vec4}; +use iced::Rectangle; + +#[derive(Copy, Clone)] +pub struct Camera { + eye: glam::Vec3, + target: glam::Vec3, + up: glam::Vec3, + fov_y: f32, + near: f32, + far: f32, +} + +impl Default for Camera { + fn default() -> Self { + Self { + eye: vec3(0.0, 2.0, 3.0), + target: glam::Vec3::ZERO, + up: glam::Vec3::Y, + fov_y: 45.0, + near: 0.1, + far: 100.0, + } + } +} + +pub const OPENGL_TO_WGPU_MATRIX: glam::Mat4 = mat4( + vec4(1.0, 0.0, 0.0, 0.0), + vec4(0.0, 1.0, 0.0, 0.0), + vec4(0.0, 0.0, 0.5, 0.0), + vec4(0.0, 0.0, 0.5, 1.0), +); + +impl Camera { + pub fn build_view_proj_matrix(&self, bounds: Rectangle) -> glam::Mat4 { + //TODO looks distorted without padding; base on surface texture size instead? + let aspect_ratio = bounds.width / (bounds.height + 150.0); + + let view = glam::Mat4::look_at_rh(self.eye, self.target, self.up); + let proj = glam::Mat4::perspective_rh( + self.fov_y, + aspect_ratio, + self.near, + self.far, + ); + + OPENGL_TO_WGPU_MATRIX * proj * view + } + + pub fn position(&self) -> glam::Vec4 { + glam::Vec4::from((self.eye, 0.0)) + } +} diff --git a/examples/custom_shader/src/cubes.rs b/examples/custom_shader/src/cubes.rs new file mode 100644 index 00000000..8dbba4b1 --- /dev/null +++ b/examples/custom_shader/src/cubes.rs @@ -0,0 +1,99 @@ +use crate::camera::Camera; +use crate::primitive; +use crate::primitive::cube::Cube; +use glam::Vec3; +use iced::widget::shader; +use iced::{mouse, Color, Rectangle}; +use rand::Rng; +use std::cmp::Ordering; +use std::iter; +use std::time::Duration; + +pub const MAX: u32 = 500; + +#[derive(Clone)] +pub struct Cubes { + pub size: f32, + pub cubes: Vec, + pub camera: Camera, + pub show_depth_buffer: bool, + pub light_color: Color, +} + +impl Cubes { + pub fn new() -> Self { + let mut cubes = Self { + size: 0.2, + cubes: vec![], + camera: Camera::default(), + show_depth_buffer: false, + light_color: Color::WHITE, + }; + + cubes.adjust_num_cubes(MAX); + + cubes + } + + pub fn update(&mut self, time: Duration) { + for cube in self.cubes.iter_mut() { + cube.update(self.size, time.as_secs_f32()); + } + } + + pub fn adjust_num_cubes(&mut self, num_cubes: u32) { + let curr_cubes = self.cubes.len() as u32; + + match num_cubes.cmp(&curr_cubes) { + Ordering::Greater => { + // spawn + let cubes_2_spawn = (num_cubes - curr_cubes) as usize; + + let mut cubes = 0; + self.cubes.extend(iter::from_fn(|| { + if cubes < cubes_2_spawn { + cubes += 1; + Some(Cube::new(self.size, rnd_origin())) + } else { + None + } + })); + } + Ordering::Less => { + // chop + let cubes_2_cut = curr_cubes - num_cubes; + let new_len = self.cubes.len() - cubes_2_cut as usize; + self.cubes.truncate(new_len); + } + _ => {} + } + } +} + +impl shader::Program for Cubes { + type State = (); + type Primitive = primitive::Primitive; + + fn draw( + &self, + _state: &Self::State, + _cursor: mouse::Cursor, + bounds: Rectangle, + ) -> Self::Primitive { + primitive::Primitive::new( + &self.cubes, + &self.camera, + bounds, + self.show_depth_buffer, + self.light_color, + ) + } +} + +fn rnd_origin() -> Vec3 { + Vec3::new( + rand::thread_rng().gen_range(-4.0..4.0), + rand::thread_rng().gen_range(-4.0..4.0), + rand::thread_rng().gen_range(-4.0..2.0), + ) +} diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs new file mode 100644 index 00000000..76fa1625 --- /dev/null +++ b/examples/custom_shader/src/main.rs @@ -0,0 +1,174 @@ +mod camera; +mod cubes; +mod pipeline; +mod primitive; + +use crate::cubes::Cubes; +use iced::widget::{ + checkbox, column, container, row, slider, text, vertical_space, Shader, +}; +use iced::{ + executor, window, Alignment, Application, Color, Command, Element, Length, + Renderer, Subscription, Theme, +}; +use std::time::Instant; + +fn main() -> iced::Result { + IcedCubes::run(iced::Settings::default()) +} + +struct IcedCubes { + start: Instant, + cubes: Cubes, + num_cubes_slider: u32, +} + +impl Default for IcedCubes { + fn default() -> Self { + Self { + start: Instant::now(), + cubes: Cubes::new(), + num_cubes_slider: cubes::MAX, + } + } +} + +#[derive(Debug, Clone)] +enum Message { + CubeAmountChanged(u32), + CubeSizeChanged(f32), + Tick(Instant), + ShowDepthBuffer(bool), + LightColorChanged(Color), +} + +impl Application for IcedCubes { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + (IcedCubes::default(), Command::none()) + } + + fn title(&self) -> String { + "Iced Cubes".to_string() + } + + fn update(&mut self, message: Self::Message) -> Command { + match message { + Message::CubeAmountChanged(num) => { + self.num_cubes_slider = num; + self.cubes.adjust_num_cubes(num); + } + Message::CubeSizeChanged(size) => { + self.cubes.size = size; + } + Message::Tick(time) => { + self.cubes.update(time - self.start); + } + Message::ShowDepthBuffer(show) => { + self.cubes.show_depth_buffer = show; + } + Message::LightColorChanged(color) => { + self.cubes.light_color = color; + } + } + + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + let top_controls = row![ + control( + "Amount", + slider( + 1..=cubes::MAX, + self.num_cubes_slider, + Message::CubeAmountChanged + ) + .width(100) + ), + control( + "Size", + slider(0.1..=0.25, self.cubes.size, Message::CubeSizeChanged) + .step(0.01) + .width(100), + ), + checkbox( + "Show Depth Buffer", + self.cubes.show_depth_buffer, + Message::ShowDepthBuffer + ), + ] + .spacing(40); + + let bottom_controls = row![ + control( + "R", + slider(0.0..=1.0, self.cubes.light_color.r, move |r| { + Message::LightColorChanged(Color { + r, + ..self.cubes.light_color + }) + }) + .step(0.01) + .width(100) + ), + control( + "G", + slider(0.0..=1.0, self.cubes.light_color.g, move |g| { + Message::LightColorChanged(Color { + g, + ..self.cubes.light_color + }) + }) + .step(0.01) + .width(100) + ), + control( + "B", + slider(0.0..=1.0, self.cubes.light_color.b, move |b| { + Message::LightColorChanged(Color { + b, + ..self.cubes.light_color + }) + }) + .step(0.01) + .width(100) + ) + ] + .spacing(40); + + let controls = column![top_controls, bottom_controls,] + .spacing(10) + .align_items(Alignment::Center); + + let shader = Shader::new(&self.cubes) + .width(Length::Fill) + .height(Length::Fill); + + container( + column![shader, controls, vertical_space(20),] + .spacing(40) + .align_items(Alignment::Center), + ) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } + + fn subscription(&self) -> Subscription { + window::frames().map(Message::Tick) + } +} + +fn control<'a>( + label: &'static str, + control: impl Into>, +) -> Element<'a, Message> { + row![text(label), control.into()].spacing(10).into() +} diff --git a/examples/custom_shader/src/pipeline.rs b/examples/custom_shader/src/pipeline.rs new file mode 100644 index 00000000..9dd154e8 --- /dev/null +++ b/examples/custom_shader/src/pipeline.rs @@ -0,0 +1,600 @@ +use crate::primitive; +use crate::primitive::cube; +use crate::primitive::{Buffer, Uniforms}; +use iced::{Rectangle, Size}; +use wgpu::util::DeviceExt; + +const SKY_TEXTURE_SIZE: u32 = 128; + +pub struct Pipeline { + pipeline: wgpu::RenderPipeline, + vertices: wgpu::Buffer, + cubes: Buffer, + uniforms: wgpu::Buffer, + uniform_bind_group: wgpu::BindGroup, + depth_texture_size: Size, + depth_view: wgpu::TextureView, + depth_pipeline: DepthPipeline, +} + +impl Pipeline { + pub fn new( + device: &wgpu::Device, + queue: &wgpu::Queue, + format: wgpu::TextureFormat, + target_size: Size, + ) -> Self { + //vertices of one cube + let vertices = + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("cubes vertex buffer"), + contents: bytemuck::cast_slice(&cube::Raw::vertices()), + usage: wgpu::BufferUsages::VERTEX, + }); + + //cube instance data + let cubes_buffer = Buffer::new( + device, + "cubes instance buffer", + std::mem::size_of::() as u64, + wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + ); + + //uniforms for all cubes + let uniforms = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("cubes uniform buffer"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + + //depth buffer + let depth_texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("cubes depth texture"), + size: wgpu::Extent3d { + width: target_size.width, + height: target_size.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + + let depth_view = + depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); + + let normal_map_data = load_normal_map_data(); + + //normal map + let normal_texture = device.create_texture_with_data( + queue, + &wgpu::TextureDescriptor { + label: Some("cubes normal map texture"), + size: wgpu::Extent3d { + width: 1024, + height: 1024, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }, + &normal_map_data, + ); + + let normal_view = + normal_texture.create_view(&wgpu::TextureViewDescriptor::default()); + + //skybox texture for reflection/refraction + let skybox_data = load_skybox_data(); + + let skybox_texture = device.create_texture_with_data( + queue, + &wgpu::TextureDescriptor { + label: Some("cubes skybox texture"), + size: wgpu::Extent3d { + width: SKY_TEXTURE_SIZE, + height: SKY_TEXTURE_SIZE, + depth_or_array_layers: 6, //one for each face of the cube + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }, + &skybox_data, + ); + + let sky_view = + skybox_texture.create_view(&wgpu::TextureViewDescriptor { + label: Some("cubes skybox texture view"), + dimension: Some(wgpu::TextureViewDimension::Cube), + ..Default::default() + }); + + let sky_sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("cubes skybox sampler"), + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + address_mode_w: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + mipmap_filter: wgpu::FilterMode::Linear, + ..Default::default() + }); + + let uniform_bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("cubes uniform bind group layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: true, + }, + view_dimension: wgpu::TextureViewDimension::Cube, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 2, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::Filtering, + ), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 3, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: true, + }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + ], + }); + + let uniform_bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("cubes uniform bind group"), + layout: &uniform_bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: uniforms.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(&sky_view), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::Sampler(&sky_sampler), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::TextureView( + &normal_view, + ), + }, + ], + }); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("cubes pipeline layout"), + bind_group_layouts: &[&uniform_bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("cubes shader"), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("shaders/cubes.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("cubes pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[primitive::Vertex::desc(), cube::Raw::desc()], + }, + primitive: wgpu::PrimitiveState::default(), + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + }), + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + format, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::One, + operation: wgpu::BlendOperation::Max, + }, + }), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview: None, + }); + + let depth_pipeline = DepthPipeline::new( + device, + format, + depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), + ); + + Self { + pipeline, + cubes: cubes_buffer, + uniforms, + uniform_bind_group, + vertices, + depth_texture_size: target_size, + depth_view, + depth_pipeline, + } + } + + fn update_depth_texture(&mut self, device: &wgpu::Device, size: Size) { + if self.depth_texture_size.height != size.height + || self.depth_texture_size.width != size.width + { + let text = device.create_texture(&wgpu::TextureDescriptor { + label: Some("cubes depth texture"), + size: wgpu::Extent3d { + width: size.width, + height: size.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + + self.depth_view = + text.create_view(&wgpu::TextureViewDescriptor::default()); + self.depth_texture_size = size; + + self.depth_pipeline.update(device, &text); + } + } + + pub fn update( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + target_size: Size, + uniforms: &Uniforms, + num_cubes: usize, + cubes: &[cube::Raw], + ) { + //recreate depth texture if surface texture size has changed + self.update_depth_texture(device, target_size); + + // update uniforms + queue.write_buffer(&self.uniforms, 0, bytemuck::bytes_of(uniforms)); + + //resize cubes vertex buffer if cubes amount changed + let new_size = num_cubes * std::mem::size_of::(); + self.cubes.resize(device, new_size as u64); + + //always write new cube data since they are constantly rotating + queue.write_buffer(&self.cubes.raw, 0, bytemuck::cast_slice(cubes)); + } + + pub fn render( + &self, + target: &wgpu::TextureView, + encoder: &mut wgpu::CommandEncoder, + bounds: Rectangle, + num_cubes: u32, + show_depth: bool, + ) { + { + let mut pass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("cubes.pipeline.pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: true, + }, + }, + )], + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: true, + }), + stencil_ops: None, + }, + ), + }); + + pass.set_scissor_rect( + bounds.x, + bounds.y, + bounds.width, + bounds.height, + ); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &self.uniform_bind_group, &[]); + pass.set_vertex_buffer(0, self.vertices.slice(..)); + pass.set_vertex_buffer(1, self.cubes.raw.slice(..)); + pass.draw(0..36, 0..num_cubes); + } + + if show_depth { + self.depth_pipeline.render(encoder, target, bounds); + } + } +} + +struct DepthPipeline { + pipeline: wgpu::RenderPipeline, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, + sampler: wgpu::Sampler, + depth_view: wgpu::TextureView, +} + +impl DepthPipeline { + pub fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + depth_texture: wgpu::TextureView, + ) -> Self { + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("cubes.depth_pipeline.sampler"), + ..Default::default() + }); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("cubes.depth_pipeline.bind_group_layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::NonFiltering, + ), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: false, + }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + ], + }); + + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("cubes.depth_pipeline.bind_group"), + layout: &bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Sampler(&sampler), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView( + &depth_texture, + ), + }, + ], + }); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("cubes.depth_pipeline.layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("cubes.depth_pipeline.shader"), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("shaders/depth.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("cubes.depth_pipeline.pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: false, + depth_compare: wgpu::CompareFunction::Less, + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + format, + blend: Some(wgpu::BlendState::REPLACE), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview: None, + }); + + Self { + pipeline, + bind_group_layout, + bind_group, + sampler, + depth_view: depth_texture, + } + } + + pub fn update( + &mut self, + device: &wgpu::Device, + depth_texture: &wgpu::Texture, + ) { + self.depth_view = + depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); + + self.bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("cubes.depth_pipeline.bind_group"), + layout: &self.bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Sampler(&self.sampler), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView( + &self.depth_view, + ), + }, + ], + }); + } + + pub fn render( + &self, + encoder: &mut wgpu::CommandEncoder, + target: &wgpu::TextureView, + bounds: Rectangle, + ) { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("cubes.pipeline.depth_pass"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: true, + }, + })], + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_view, + depth_ops: None, + stencil_ops: None, + }, + ), + }); + + pass.set_scissor_rect(bounds.x, bounds.y, bounds.width, bounds.height); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &self.bind_group, &[]); + pass.draw(0..6, 0..1); + } +} + +fn load_skybox_data() -> Vec { + let pos_x: &[u8] = include_bytes!("textures/skybox/pos_x.jpg"); + let neg_x: &[u8] = include_bytes!("textures/skybox/neg_x.jpg"); + let pos_y: &[u8] = include_bytes!("textures/skybox/pos_y.jpg"); + let neg_y: &[u8] = include_bytes!("textures/skybox/neg_y.jpg"); + let pos_z: &[u8] = include_bytes!("textures/skybox/pos_z.jpg"); + let neg_z: &[u8] = include_bytes!("textures/skybox/neg_z.jpg"); + + let data: [&[u8]; 6] = [pos_x, neg_x, pos_y, neg_y, pos_z, neg_z]; + + data.iter().fold(vec![], |mut acc, bytes| { + let i = image::load_from_memory_with_format( + bytes, + image::ImageFormat::Jpeg, + ) + .unwrap() + .to_rgba8() + .into_raw(); + + acc.extend(i); + acc + }) +} + +fn load_normal_map_data() -> Vec { + let bytes: &[u8] = include_bytes!("textures/ice_cube_normal_map.png"); + + image::load_from_memory_with_format(bytes, image::ImageFormat::Png) + .unwrap() + .to_rgba8() + .into_raw() +} diff --git a/examples/custom_shader/src/primitive.rs b/examples/custom_shader/src/primitive.rs new file mode 100644 index 00000000..2201218f --- /dev/null +++ b/examples/custom_shader/src/primitive.rs @@ -0,0 +1,95 @@ +pub mod cube; +pub mod vertex; + +mod buffer; +mod uniforms; + +use crate::camera::Camera; +use crate::pipeline::Pipeline; +use crate::primitive::cube::Cube; +use iced::advanced::graphics::Transformation; +use iced::widget::shader; +use iced::{Color, Rectangle, Size}; + +pub use crate::primitive::vertex::Vertex; +pub use buffer::Buffer; +pub use uniforms::Uniforms; + +/// A collection of `Cube`s that can be rendered. +#[derive(Debug)] +pub struct Primitive { + cubes: Vec, + uniforms: Uniforms, + show_depth_buffer: bool, +} + +impl Primitive { + pub fn new( + cubes: &[Cube], + camera: &Camera, + bounds: Rectangle, + show_depth_buffer: bool, + light_color: Color, + ) -> Self { + let uniforms = Uniforms::new(camera, bounds, light_color); + + Self { + cubes: cubes + .iter() + .map(cube::Raw::from_cube) + .collect::>(), + uniforms, + show_depth_buffer, + } + } +} + +impl shader::Primitive for Primitive { + fn prepare( + &self, + format: wgpu::TextureFormat, + device: &wgpu::Device, + queue: &wgpu::Queue, + target_size: Size, + _scale_factor: f32, + _transform: Transformation, + storage: &mut shader::Storage, + ) { + if !storage.has::() { + storage.store(Pipeline::new(device, queue, format, target_size)) + } + + let pipeline = storage.get_mut::().unwrap(); + + //upload data to GPU + pipeline.update( + device, + queue, + target_size, + &self.uniforms, + self.cubes.len(), + &self.cubes, + ); + } + + fn render( + &self, + storage: &shader::Storage, + bounds: Rectangle, + target: &wgpu::TextureView, + _target_size: Size, + encoder: &mut wgpu::CommandEncoder, + ) { + //at this point our pipeline should always be initialized + let pipeline = storage.get::().unwrap(); + + //render primitive + pipeline.render( + target, + encoder, + bounds, + self.cubes.len() as u32, + self.show_depth_buffer, + ) + } +} diff --git a/examples/custom_shader/src/primitive/buffer.rs b/examples/custom_shader/src/primitive/buffer.rs new file mode 100644 index 00000000..377ce1bb --- /dev/null +++ b/examples/custom_shader/src/primitive/buffer.rs @@ -0,0 +1,39 @@ +// A custom buffer container for dynamic resizing. +pub struct Buffer { + pub raw: wgpu::Buffer, + label: &'static str, + size: u64, + usage: wgpu::BufferUsages, +} + +impl Buffer { + pub fn new( + device: &wgpu::Device, + label: &'static str, + size: u64, + usage: wgpu::BufferUsages, + ) -> Self { + Self { + raw: device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }), + label, + size, + usage, + } + } + + pub fn resize(&mut self, device: &wgpu::Device, new_size: u64) { + if new_size > self.size { + self.raw = device.create_buffer(&wgpu::BufferDescriptor { + label: Some(self.label), + size: new_size, + usage: self.usage, + mapped_at_creation: false, + }); + } + } +} diff --git a/examples/custom_shader/src/primitive/cube.rs b/examples/custom_shader/src/primitive/cube.rs new file mode 100644 index 00000000..c23f2132 --- /dev/null +++ b/examples/custom_shader/src/primitive/cube.rs @@ -0,0 +1,324 @@ +use crate::primitive::Vertex; +use glam::{vec2, vec3, Vec3}; +use rand::{thread_rng, Rng}; + +/// A single instance of a cube. +#[derive(Debug, Clone)] +pub struct Cube { + pub rotation: glam::Quat, + pub position: Vec3, + pub size: f32, + rotation_dir: f32, + rotation_axis: glam::Vec3, +} + +impl Default for Cube { + fn default() -> Self { + Self { + rotation: glam::Quat::IDENTITY, + position: glam::Vec3::ZERO, + size: 0.1, + rotation_dir: 1.0, + rotation_axis: glam::Vec3::Y, + } + } +} + +impl Cube { + pub fn new(size: f32, origin: Vec3) -> Self { + let rnd = thread_rng().gen_range(0.0..=1.0f32); + + Self { + rotation: glam::Quat::IDENTITY, + position: origin + Vec3::new(0.1, 0.1, 0.1), + size, + rotation_dir: if rnd <= 0.5 { -1.0 } else { 1.0 }, + rotation_axis: if rnd <= 0.33 { + glam::Vec3::Y + } else if rnd <= 0.66 { + glam::Vec3::X + } else { + glam::Vec3::Z + }, + } + } + + pub fn update(&mut self, size: f32, time: f32) { + self.rotation = glam::Quat::from_axis_angle( + self.rotation_axis, + time / 2.0 * self.rotation_dir, + ); + self.size = size; + } +} + +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Debug)] +#[repr(C)] +pub struct Raw { + transformation: glam::Mat4, + normal: glam::Mat3, + _padding: [f32; 3], +} + +impl Raw { + const ATTRIBS: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![ + //cube transformation matrix + 4 => Float32x4, + 5 => Float32x4, + 6 => Float32x4, + 7 => Float32x4, + //normal rotation matrix + 8 => Float32x3, + 9 => Float32x3, + 10 => Float32x3, + ]; + + pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &Self::ATTRIBS, + } + } +} + +impl Raw { + pub fn from_cube(cube: &Cube) -> Raw { + Raw { + transformation: glam::Mat4::from_scale_rotation_translation( + glam::vec3(cube.size, cube.size, cube.size), + cube.rotation, + cube.position, + ), + normal: glam::Mat3::from_quat(cube.rotation), + _padding: [0.0; 3], + } + } + + pub fn vertices() -> [Vertex; 36] { + [ + //face 1 + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + //face 2 + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + //face 3 + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + //face 4 + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + //face 5 + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + //face 6 + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + ] + } +} diff --git a/examples/custom_shader/src/primitive/uniforms.rs b/examples/custom_shader/src/primitive/uniforms.rs new file mode 100644 index 00000000..4fcb413b --- /dev/null +++ b/examples/custom_shader/src/primitive/uniforms.rs @@ -0,0 +1,22 @@ +use crate::camera::Camera; +use iced::{Color, Rectangle}; + +#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +pub struct Uniforms { + camera_proj: glam::Mat4, + camera_pos: glam::Vec4, + light_color: glam::Vec4, +} + +impl Uniforms { + pub fn new(camera: &Camera, bounds: Rectangle, light_color: Color) -> Self { + let camera_proj = camera.build_view_proj_matrix(bounds); + + Self { + camera_proj, + camera_pos: camera.position(), + light_color: glam::Vec4::from(light_color.into_linear()), + } + } +} diff --git a/examples/custom_shader/src/primitive/vertex.rs b/examples/custom_shader/src/primitive/vertex.rs new file mode 100644 index 00000000..6d17aa0f --- /dev/null +++ b/examples/custom_shader/src/primitive/vertex.rs @@ -0,0 +1,29 @@ +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +pub struct Vertex { + pub pos: glam::Vec3, + pub normal: glam::Vec3, + pub tangent: glam::Vec3, + pub uv: glam::Vec2, +} + +impl Vertex { + const ATTRIBS: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![ + //position + 0 => Float32x3, + //normal + 1 => Float32x3, + //tangent + 2 => Float32x3, + //uv + 3 => Float32x2, + ]; + + pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &Self::ATTRIBS, + } + } +} diff --git a/examples/custom_shader/src/shaders/cubes.wgsl b/examples/custom_shader/src/shaders/cubes.wgsl new file mode 100644 index 00000000..cd7f94d8 --- /dev/null +++ b/examples/custom_shader/src/shaders/cubes.wgsl @@ -0,0 +1,123 @@ +struct Uniforms { + projection: mat4x4, + camera_pos: vec4, + light_color: vec4, +} + +const LIGHT_POS: vec3 = vec3(0.0, 3.0, 3.0); + +@group(0) @binding(0) var uniforms: Uniforms; +@group(0) @binding(1) var sky_texture: texture_cube; +@group(0) @binding(2) var tex_sampler: sampler; +@group(0) @binding(3) var normal_texture: texture_2d; + +struct Vertex { + @location(0) position: vec3, + @location(1) normal: vec3, + @location(2) tangent: vec3, + @location(3) uv: vec2, +} + +struct Cube { + @location(4) matrix_0: vec4, + @location(5) matrix_1: vec4, + @location(6) matrix_2: vec4, + @location(7) matrix_3: vec4, + @location(8) normal_matrix_0: vec3, + @location(9) normal_matrix_1: vec3, + @location(10) normal_matrix_2: vec3, +} + +struct Output { + @builtin(position) clip_pos: vec4, + @location(0) uv: vec2, + @location(1) tangent_pos: vec3, + @location(2) tangent_camera_pos: vec3, + @location(3) tangent_light_pos: vec3, +} + +@vertex +fn vs_main(vertex: Vertex, cube: Cube) -> Output { + let cube_matrix = mat4x4( + cube.matrix_0, + cube.matrix_1, + cube.matrix_2, + cube.matrix_3, + ); + + let normal_matrix = mat3x3( + cube.normal_matrix_0, + cube.normal_matrix_1, + cube.normal_matrix_2, + ); + + //convert to tangent space to calculate lighting in same coordinate space as normal map sample + let tangent = normalize(normal_matrix * vertex.tangent); + let normal = normalize(normal_matrix * vertex.normal); + let bitangent = cross(tangent, normal); + + //shift everything into tangent space + let tbn = transpose(mat3x3(tangent, bitangent, normal)); + + let world_pos = cube_matrix * vec4(vertex.position, 1.0); + + var out: Output; + out.clip_pos = uniforms.projection * world_pos; + out.uv = vertex.uv; + out.tangent_pos = tbn * world_pos.xyz; + out.tangent_camera_pos = tbn * uniforms.camera_pos.xyz; + out.tangent_light_pos = tbn * LIGHT_POS; + + return out; +} + +//cube properties +const CUBE_BASE_COLOR: vec4 = vec4(0.294118, 0.462745, 0.611765, 0.6); +const SHINE_DAMPER: f32 = 1.0; +const REFLECTIVITY: f32 = 0.8; +const REFRACTION_INDEX: f32 = 1.31; + +//fog, for the ~* cinematic effect *~ +const FOG_DENSITY: f32 = 0.15; +const FOG_GRADIENT: f32 = 8.0; +const FOG_COLOR: vec4 = vec4(1.0, 1.0, 1.0, 1.0); + +@fragment +fn fs_main(in: Output) -> @location(0) vec4 { + let to_camera = in.tangent_camera_pos - in.tangent_pos; + + //normal sample from texture + var normal = textureSample(normal_texture, tex_sampler, in.uv).xyz; + normal = normal * 2.0 - 1.0; + + //diffuse + let dir_to_light: vec3 = normalize(in.tangent_light_pos - in.tangent_pos); + let brightness = max(dot(normal, dir_to_light), 0.0); + let diffuse: vec3 = brightness * uniforms.light_color.xyz; + + //specular + let dir_to_camera = normalize(to_camera); + let light_dir = -dir_to_light; + let reflected_light_dir = reflect(light_dir, normal); + let specular_factor = max(dot(reflected_light_dir, dir_to_camera), 0.0); + let damped_factor = pow(specular_factor, SHINE_DAMPER); + let specular: vec3 = damped_factor * uniforms.light_color.xyz * REFLECTIVITY; + + //fog + let distance = length(to_camera); + let visibility = clamp(exp(-pow((distance * FOG_DENSITY), FOG_GRADIENT)), 0.0, 1.0); + + //reflection + let reflection_dir = reflect(dir_to_camera, normal); + let reflection_color = textureSample(sky_texture, tex_sampler, reflection_dir); + let refraction_dir = refract(dir_to_camera, normal, REFRACTION_INDEX); + let refraction_color = textureSample(sky_texture, tex_sampler, refraction_dir); + let final_reflect_color = mix(reflection_color, refraction_color, 0.5); + + //mix it all together! + var color = vec4(CUBE_BASE_COLOR.xyz * diffuse + specular, CUBE_BASE_COLOR.w); + color = mix(color, final_reflect_color, 0.8); + color = mix(FOG_COLOR, color, visibility); + + return color; +} diff --git a/examples/custom_shader/src/shaders/depth.wgsl b/examples/custom_shader/src/shaders/depth.wgsl new file mode 100644 index 00000000..a3f7e5ec --- /dev/null +++ b/examples/custom_shader/src/shaders/depth.wgsl @@ -0,0 +1,48 @@ +var positions: array, 6> = array, 6>( + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2(1.0, -1.0), + vec2(-1.0, 1.0), + vec2(1.0, 1.0), + vec2(1.0, -1.0) +); + +var uvs: array, 6> = array, 6>( + vec2(0.0, 0.0), + vec2(0.0, 1.0), + vec2(1.0, 1.0), + vec2(0.0, 0.0), + vec2(1.0, 0.0), + vec2(1.0, 1.0) +); + +@group(0) @binding(0) var depth_sampler: sampler; +@group(0) @binding(1) var depth_texture: texture_2d; + +struct Output { + @builtin(position) position: vec4, + @location(0) uv: vec2, +} + +@vertex +fn vs_main(@builtin(vertex_index) v_index: u32) -> Output { + var out: Output; + + out.position = vec4(positions[v_index], 0.0, 1.0); + out.uv = uvs[v_index]; + + return out; +} + +@fragment +fn fs_main(input: Output) -> @location(0) vec4 { + let depth = textureSample(depth_texture, depth_sampler, input.uv).r; + + if (depth > .9999) { + discard; + } + + let c = 1.0 - depth; + + return vec4(c, c, c, 1.0); +} diff --git a/examples/custom_shader/src/textures/ice_cube_normal_map.png b/examples/custom_shader/src/textures/ice_cube_normal_map.png new file mode 100644 index 00000000..7b4b7228 Binary files /dev/null and b/examples/custom_shader/src/textures/ice_cube_normal_map.png differ diff --git a/examples/custom_shader/src/textures/skybox/neg_x.jpg b/examples/custom_shader/src/textures/skybox/neg_x.jpg new file mode 100644 index 00000000..00cc783d Binary files /dev/null and b/examples/custom_shader/src/textures/skybox/neg_x.jpg differ diff --git a/examples/custom_shader/src/textures/skybox/neg_y.jpg b/examples/custom_shader/src/textures/skybox/neg_y.jpg new file mode 100644 index 00000000..548f6445 Binary files /dev/null and b/examples/custom_shader/src/textures/skybox/neg_y.jpg differ diff --git a/examples/custom_shader/src/textures/skybox/neg_z.jpg b/examples/custom_shader/src/textures/skybox/neg_z.jpg new file mode 100644 index 00000000..5698512e Binary files /dev/null and b/examples/custom_shader/src/textures/skybox/neg_z.jpg differ diff --git a/examples/custom_shader/src/textures/skybox/pos_x.jpg b/examples/custom_shader/src/textures/skybox/pos_x.jpg new file mode 100644 index 00000000..dddecba7 Binary files /dev/null and b/examples/custom_shader/src/textures/skybox/pos_x.jpg differ diff --git a/examples/custom_shader/src/textures/skybox/pos_y.jpg b/examples/custom_shader/src/textures/skybox/pos_y.jpg new file mode 100644 index 00000000..361427fd Binary files /dev/null and b/examples/custom_shader/src/textures/skybox/pos_y.jpg differ diff --git a/examples/custom_shader/src/textures/skybox/pos_z.jpg b/examples/custom_shader/src/textures/skybox/pos_z.jpg new file mode 100644 index 00000000..0085a49e Binary files /dev/null and b/examples/custom_shader/src/textures/skybox/pos_z.jpg differ diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index c26d52fe..0f32fca0 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -271,6 +271,7 @@ pub fn main() -> Result<(), Box> { &queue, &mut encoder, None, + frame.texture.format(), &view, primitive, &viewport, -- cgit From 3e8ed05356dde17a6e31a0dc927a3c19b593b09a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 11:38:16 +0100 Subject: Update `wgpu` in `custom_shader` example --- examples/custom_shader/Cargo.toml | 15 ++++++++++----- examples/custom_shader/src/pipeline.rs | 10 +++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/Cargo.toml b/examples/custom_shader/Cargo.toml index 7a927811..0b8466a9 100644 --- a/examples/custom_shader/Cargo.toml +++ b/examples/custom_shader/Cargo.toml @@ -5,9 +5,14 @@ authors = ["Bingus "] edition = "2021" [dependencies] -iced = { path = "../..", features = ["debug", "advanced"]} -image = { version = "0.24.6"} -wgpu = "0.17" -bytemuck = { version = "1.13.1" } -glam = { version = "0.24.0", features = ["bytemuck"] } +iced.workspace = true +iced.features = ["debug", "advanced"] + +image.workspace = true +wgpu.workspace = true +bytemuck.workspace = true + +glam.workspace = true +glam.features = ["bytemuck"] + rand = "0.8.5" diff --git a/examples/custom_shader/src/pipeline.rs b/examples/custom_shader/src/pipeline.rs index 9dd154e8..eef1081d 100644 --- a/examples/custom_shader/src/pipeline.rs +++ b/examples/custom_shader/src/pipeline.rs @@ -355,7 +355,7 @@ impl Pipeline { resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Load, - store: true, + store: wgpu::StoreOp::Store, }, }, )], @@ -364,11 +364,13 @@ impl Pipeline { view: &self.depth_view, depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), - store: true, + store: wgpu::StoreOp::Store, }), stencil_ops: None, }, ), + timestamp_writes: None, + occlusion_query_set: None, }); pass.set_scissor_rect( @@ -547,7 +549,7 @@ impl DepthPipeline { resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Load, - store: true, + store: wgpu::StoreOp::Store, }, })], depth_stencil_attachment: Some( @@ -557,6 +559,8 @@ impl DepthPipeline { stencil_ops: None, }, ), + timestamp_writes: None, + occlusion_query_set: None, }); pass.set_scissor_rect(bounds.x, bounds.y, bounds.width, bounds.height); -- cgit From 33f626294452aefd1cd04f455fa1d1dfcb7f549e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 11:43:38 +0100 Subject: Fix `clippy` lints :crab: --- examples/custom_shader/src/cubes.rs | 2 +- examples/custom_shader/src/pipeline.rs | 8 ++++---- examples/custom_shader/src/primitive.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/src/cubes.rs b/examples/custom_shader/src/cubes.rs index 8dbba4b1..00e608e3 100644 --- a/examples/custom_shader/src/cubes.rs +++ b/examples/custom_shader/src/cubes.rs @@ -65,7 +65,7 @@ impl Cubes { let new_len = self.cubes.len() - cubes_2_cut as usize; self.cubes.truncate(new_len); } - _ => {} + Ordering::Equal => {} } } } diff --git a/examples/custom_shader/src/pipeline.rs b/examples/custom_shader/src/pipeline.rs index eef1081d..44ad17a2 100644 --- a/examples/custom_shader/src/pipeline.rs +++ b/examples/custom_shader/src/pipeline.rs @@ -479,15 +479,15 @@ impl DepthPipeline { entry_point: "vs_main", buffers: &[], }, - primitive: Default::default(), + primitive: wgpu::PrimitiveState::default(), depth_stencil: Some(wgpu::DepthStencilState { format: wgpu::TextureFormat::Depth32Float, depth_write_enabled: false, depth_compare: wgpu::CompareFunction::Less, - stencil: Default::default(), - bias: Default::default(), + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), }), - multisample: Default::default(), + multisample: wgpu::MultisampleState::default(), fragment: Some(wgpu::FragmentState { module: &shader, entry_point: "fs_main", diff --git a/examples/custom_shader/src/primitive.rs b/examples/custom_shader/src/primitive.rs index 2201218f..f81ce95d 100644 --- a/examples/custom_shader/src/primitive.rs +++ b/examples/custom_shader/src/primitive.rs @@ -56,7 +56,7 @@ impl shader::Primitive for Primitive { storage: &mut shader::Storage, ) { if !storage.has::() { - storage.store(Pipeline::new(device, queue, format, target_size)) + storage.store(Pipeline::new(device, queue, format, target_size)); } let pipeline = storage.get_mut::().unwrap(); @@ -90,6 +90,6 @@ impl shader::Primitive for Primitive { bounds, self.cubes.len() as u32, self.show_depth_buffer, - ) + ); } } -- cgit From 9489e29e6619b14ed9f41a8887c4b34158266f71 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 12:49:49 +0100 Subject: Re-organize `custom` module as `pipeline` module ... and move `Shader` widget to `iced_widget` crate --- examples/custom_shader/src/main.rs | 11 ++++++++--- examples/custom_shader/src/primitive.rs | 15 ++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index 76fa1625..f6370f46 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -3,15 +3,20 @@ mod cubes; mod pipeline; mod primitive; +use crate::camera::Camera; use crate::cubes::Cubes; +use crate::pipeline::Pipeline; + +use iced::executor; +use iced::time::Instant; use iced::widget::{ checkbox, column, container, row, slider, text, vertical_space, Shader, }; +use iced::window; use iced::{ - executor, window, Alignment, Application, Color, Command, Element, Length, - Renderer, Subscription, Theme, + Alignment, Application, Color, Command, Element, Length, Renderer, + Subscription, Theme, }; -use std::time::Instant; fn main() -> iced::Result { IcedCubes::run(iced::Settings::default()) diff --git a/examples/custom_shader/src/primitive.rs b/examples/custom_shader/src/primitive.rs index f81ce95d..520ceb8e 100644 --- a/examples/custom_shader/src/primitive.rs +++ b/examples/custom_shader/src/primitive.rs @@ -4,17 +4,18 @@ pub mod vertex; mod buffer; mod uniforms; -use crate::camera::Camera; -use crate::pipeline::Pipeline; -use crate::primitive::cube::Cube; +pub use buffer::Buffer; +pub use cube::Cube; +pub use uniforms::Uniforms; +pub use vertex::Vertex; + +use crate::Camera; +use crate::Pipeline; + use iced::advanced::graphics::Transformation; use iced::widget::shader; use iced::{Color, Rectangle, Size}; -pub use crate::primitive::vertex::Vertex; -pub use buffer::Buffer; -pub use uniforms::Uniforms; - /// A collection of `Cube`s that can be rendered. #[derive(Debug)] pub struct Primitive { -- cgit From 91d7df52cdedd1b5c431fdb51a6356e827765b60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 13:25:49 +0100 Subject: Create `shader` function helper in `iced_widget` --- examples/custom_shader/src/main.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index f6370f46..e9b6776f 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -10,7 +10,7 @@ use crate::pipeline::Pipeline; use iced::executor; use iced::time::Instant; use iced::widget::{ - checkbox, column, container, row, slider, text, vertical_space, Shader, + checkbox, column, container, row, shader, slider, text, vertical_space, }; use iced::window; use iced::{ @@ -150,9 +150,8 @@ impl Application for IcedCubes { .spacing(10) .align_items(Alignment::Center); - let shader = Shader::new(&self.cubes) - .width(Length::Fill) - .height(Length::Fill); + let shader = + shader(&self.cubes).width(Length::Fill).height(Length::Fill); container( column![shader, controls, vertical_space(20),] -- cgit From 63f36b04638f14af3455ead8b82d581a438a28a3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 14:04:54 +0100 Subject: Export `wgpu` crate in `shader` module in `iced_widget` --- examples/custom_shader/Cargo.toml | 1 - examples/custom_shader/src/main.rs | 1 + examples/custom_shader/src/pipeline.rs | 4 +++- examples/custom_shader/src/primitive.rs | 1 + examples/custom_shader/src/primitive/buffer.rs | 2 ++ examples/custom_shader/src/primitive/cube.rs | 2 ++ examples/custom_shader/src/primitive/vertex.rs | 2 ++ 7 files changed, 11 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/Cargo.toml b/examples/custom_shader/Cargo.toml index 0b8466a9..b602f98d 100644 --- a/examples/custom_shader/Cargo.toml +++ b/examples/custom_shader/Cargo.toml @@ -9,7 +9,6 @@ iced.workspace = true iced.features = ["debug", "advanced"] image.workspace = true -wgpu.workspace = true bytemuck.workspace = true glam.workspace = true diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index e9b6776f..e7b07d78 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -9,6 +9,7 @@ use crate::pipeline::Pipeline; use iced::executor; use iced::time::Instant; +use iced::widget::shader::wgpu; use iced::widget::{ checkbox, column, container, row, shader, slider, text, vertical_space, }; diff --git a/examples/custom_shader/src/pipeline.rs b/examples/custom_shader/src/pipeline.rs index 44ad17a2..9343e5e0 100644 --- a/examples/custom_shader/src/pipeline.rs +++ b/examples/custom_shader/src/pipeline.rs @@ -1,8 +1,10 @@ use crate::primitive; use crate::primitive::cube; use crate::primitive::{Buffer, Uniforms}; +use crate::wgpu; +use crate::wgpu::util::DeviceExt; + use iced::{Rectangle, Size}; -use wgpu::util::DeviceExt; const SKY_TEXTURE_SIZE: u32 = 128; diff --git a/examples/custom_shader/src/primitive.rs b/examples/custom_shader/src/primitive.rs index 520ceb8e..f5862ab3 100644 --- a/examples/custom_shader/src/primitive.rs +++ b/examples/custom_shader/src/primitive.rs @@ -9,6 +9,7 @@ pub use cube::Cube; pub use uniforms::Uniforms; pub use vertex::Vertex; +use crate::wgpu; use crate::Camera; use crate::Pipeline; diff --git a/examples/custom_shader/src/primitive/buffer.rs b/examples/custom_shader/src/primitive/buffer.rs index 377ce1bb..ef4c41c9 100644 --- a/examples/custom_shader/src/primitive/buffer.rs +++ b/examples/custom_shader/src/primitive/buffer.rs @@ -1,3 +1,5 @@ +use crate::wgpu; + // A custom buffer container for dynamic resizing. pub struct Buffer { pub raw: wgpu::Buffer, diff --git a/examples/custom_shader/src/primitive/cube.rs b/examples/custom_shader/src/primitive/cube.rs index c23f2132..7ece685d 100644 --- a/examples/custom_shader/src/primitive/cube.rs +++ b/examples/custom_shader/src/primitive/cube.rs @@ -1,4 +1,6 @@ use crate::primitive::Vertex; +use crate::wgpu; + use glam::{vec2, vec3, Vec3}; use rand::{thread_rng, Rng}; diff --git a/examples/custom_shader/src/primitive/vertex.rs b/examples/custom_shader/src/primitive/vertex.rs index 6d17aa0f..e64cd926 100644 --- a/examples/custom_shader/src/primitive/vertex.rs +++ b/examples/custom_shader/src/primitive/vertex.rs @@ -1,3 +1,5 @@ +use crate::wgpu; + #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[repr(C)] pub struct Vertex { -- cgit From 78a06384b1e6fc5e0c472dc19169fcaf11fe27b2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 14:36:38 +0100 Subject: Use a single source for amount of cubes in `custom_shader` example --- examples/custom_shader/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index e7b07d78..3336e7f5 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -26,7 +26,6 @@ fn main() -> iced::Result { struct IcedCubes { start: Instant, cubes: Cubes, - num_cubes_slider: u32, } impl Default for IcedCubes { @@ -34,7 +33,6 @@ impl Default for IcedCubes { Self { start: Instant::now(), cubes: Cubes::new(), - num_cubes_slider: cubes::MAX, } } } @@ -65,7 +63,6 @@ impl Application for IcedCubes { fn update(&mut self, message: Self::Message) -> Command { match message { Message::CubeAmountChanged(num) => { - self.num_cubes_slider = num; self.cubes.adjust_num_cubes(num); } Message::CubeSizeChanged(size) => { @@ -91,7 +88,7 @@ impl Application for IcedCubes { "Amount", slider( 1..=cubes::MAX, - self.num_cubes_slider, + self.cubes.cubes.len() as u32, Message::CubeAmountChanged ) .width(100) -- cgit From 9ddfaf3ee73cef3e7bd122f4d11893f77647c2c3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 14:41:48 +0100 Subject: Rename `cubes` to `scene` in `custom_shader` example --- examples/custom_shader/src/cubes.rs | 99 ------------------------------------- examples/custom_shader/src/main.rs | 42 ++++++++-------- examples/custom_shader/src/scene.rs | 99 +++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 examples/custom_shader/src/cubes.rs create mode 100644 examples/custom_shader/src/scene.rs (limited to 'examples') diff --git a/examples/custom_shader/src/cubes.rs b/examples/custom_shader/src/cubes.rs deleted file mode 100644 index 00e608e3..00000000 --- a/examples/custom_shader/src/cubes.rs +++ /dev/null @@ -1,99 +0,0 @@ -use crate::camera::Camera; -use crate::primitive; -use crate::primitive::cube::Cube; -use glam::Vec3; -use iced::widget::shader; -use iced::{mouse, Color, Rectangle}; -use rand::Rng; -use std::cmp::Ordering; -use std::iter; -use std::time::Duration; - -pub const MAX: u32 = 500; - -#[derive(Clone)] -pub struct Cubes { - pub size: f32, - pub cubes: Vec, - pub camera: Camera, - pub show_depth_buffer: bool, - pub light_color: Color, -} - -impl Cubes { - pub fn new() -> Self { - let mut cubes = Self { - size: 0.2, - cubes: vec![], - camera: Camera::default(), - show_depth_buffer: false, - light_color: Color::WHITE, - }; - - cubes.adjust_num_cubes(MAX); - - cubes - } - - pub fn update(&mut self, time: Duration) { - for cube in self.cubes.iter_mut() { - cube.update(self.size, time.as_secs_f32()); - } - } - - pub fn adjust_num_cubes(&mut self, num_cubes: u32) { - let curr_cubes = self.cubes.len() as u32; - - match num_cubes.cmp(&curr_cubes) { - Ordering::Greater => { - // spawn - let cubes_2_spawn = (num_cubes - curr_cubes) as usize; - - let mut cubes = 0; - self.cubes.extend(iter::from_fn(|| { - if cubes < cubes_2_spawn { - cubes += 1; - Some(Cube::new(self.size, rnd_origin())) - } else { - None - } - })); - } - Ordering::Less => { - // chop - let cubes_2_cut = curr_cubes - num_cubes; - let new_len = self.cubes.len() - cubes_2_cut as usize; - self.cubes.truncate(new_len); - } - Ordering::Equal => {} - } - } -} - -impl shader::Program for Cubes { - type State = (); - type Primitive = primitive::Primitive; - - fn draw( - &self, - _state: &Self::State, - _cursor: mouse::Cursor, - bounds: Rectangle, - ) -> Self::Primitive { - primitive::Primitive::new( - &self.cubes, - &self.camera, - bounds, - self.show_depth_buffer, - self.light_color, - ) - } -} - -fn rnd_origin() -> Vec3 { - Vec3::new( - rand::thread_rng().gen_range(-4.0..4.0), - rand::thread_rng().gen_range(-4.0..4.0), - rand::thread_rng().gen_range(-4.0..2.0), - ) -} diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index 3336e7f5..f90061d7 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -1,11 +1,11 @@ mod camera; -mod cubes; mod pipeline; mod primitive; +mod scene; use crate::camera::Camera; -use crate::cubes::Cubes; use crate::pipeline::Pipeline; +use crate::scene::Scene; use iced::executor; use iced::time::Instant; @@ -25,14 +25,14 @@ fn main() -> iced::Result { struct IcedCubes { start: Instant, - cubes: Cubes, + scene: Scene, } impl Default for IcedCubes { fn default() -> Self { Self { start: Instant::now(), - cubes: Cubes::new(), + scene: Scene::new(), } } } @@ -62,20 +62,20 @@ impl Application for IcedCubes { fn update(&mut self, message: Self::Message) -> Command { match message { - Message::CubeAmountChanged(num) => { - self.cubes.adjust_num_cubes(num); + Message::CubeAmountChanged(amount) => { + self.scene.change_amount(amount); } Message::CubeSizeChanged(size) => { - self.cubes.size = size; + self.scene.size = size; } Message::Tick(time) => { - self.cubes.update(time - self.start); + self.scene.update(time - self.start); } Message::ShowDepthBuffer(show) => { - self.cubes.show_depth_buffer = show; + self.scene.show_depth_buffer = show; } Message::LightColorChanged(color) => { - self.cubes.light_color = color; + self.scene.light_color = color; } } @@ -87,21 +87,21 @@ impl Application for IcedCubes { control( "Amount", slider( - 1..=cubes::MAX, - self.cubes.cubes.len() as u32, + 1..=scene::MAX, + self.scene.cubes.len() as u32, Message::CubeAmountChanged ) .width(100) ), control( "Size", - slider(0.1..=0.25, self.cubes.size, Message::CubeSizeChanged) + slider(0.1..=0.25, self.scene.size, Message::CubeSizeChanged) .step(0.01) .width(100), ), checkbox( "Show Depth Buffer", - self.cubes.show_depth_buffer, + self.scene.show_depth_buffer, Message::ShowDepthBuffer ), ] @@ -110,10 +110,10 @@ impl Application for IcedCubes { let bottom_controls = row![ control( "R", - slider(0.0..=1.0, self.cubes.light_color.r, move |r| { + slider(0.0..=1.0, self.scene.light_color.r, move |r| { Message::LightColorChanged(Color { r, - ..self.cubes.light_color + ..self.scene.light_color }) }) .step(0.01) @@ -121,10 +121,10 @@ impl Application for IcedCubes { ), control( "G", - slider(0.0..=1.0, self.cubes.light_color.g, move |g| { + slider(0.0..=1.0, self.scene.light_color.g, move |g| { Message::LightColorChanged(Color { g, - ..self.cubes.light_color + ..self.scene.light_color }) }) .step(0.01) @@ -132,10 +132,10 @@ impl Application for IcedCubes { ), control( "B", - slider(0.0..=1.0, self.cubes.light_color.b, move |b| { + slider(0.0..=1.0, self.scene.light_color.b, move |b| { Message::LightColorChanged(Color { b, - ..self.cubes.light_color + ..self.scene.light_color }) }) .step(0.01) @@ -149,7 +149,7 @@ impl Application for IcedCubes { .align_items(Alignment::Center); let shader = - shader(&self.cubes).width(Length::Fill).height(Length::Fill); + shader(&self.scene).width(Length::Fill).height(Length::Fill); container( column![shader, controls, vertical_space(20),] diff --git a/examples/custom_shader/src/scene.rs b/examples/custom_shader/src/scene.rs new file mode 100644 index 00000000..ab923093 --- /dev/null +++ b/examples/custom_shader/src/scene.rs @@ -0,0 +1,99 @@ +use crate::camera::Camera; +use crate::primitive; +use crate::primitive::cube::Cube; +use glam::Vec3; +use iced::widget::shader; +use iced::{mouse, Color, Rectangle}; +use rand::Rng; +use std::cmp::Ordering; +use std::iter; +use std::time::Duration; + +pub const MAX: u32 = 500; + +#[derive(Clone)] +pub struct Scene { + pub size: f32, + pub cubes: Vec, + pub camera: Camera, + pub show_depth_buffer: bool, + pub light_color: Color, +} + +impl Scene { + pub fn new() -> Self { + let mut scene = Self { + size: 0.2, + cubes: vec![], + camera: Camera::default(), + show_depth_buffer: false, + light_color: Color::WHITE, + }; + + scene.change_amount(MAX); + + scene + } + + pub fn update(&mut self, time: Duration) { + for cube in self.cubes.iter_mut() { + cube.update(self.size, time.as_secs_f32()); + } + } + + pub fn change_amount(&mut self, amount: u32) { + let curr_cubes = self.cubes.len() as u32; + + match amount.cmp(&curr_cubes) { + Ordering::Greater => { + // spawn + let cubes_2_spawn = (amount - curr_cubes) as usize; + + let mut cubes = 0; + self.cubes.extend(iter::from_fn(|| { + if cubes < cubes_2_spawn { + cubes += 1; + Some(Cube::new(self.size, rnd_origin())) + } else { + None + } + })); + } + Ordering::Less => { + // chop + let cubes_2_cut = curr_cubes - amount; + let new_len = self.cubes.len() - cubes_2_cut as usize; + self.cubes.truncate(new_len); + } + Ordering::Equal => {} + } + } +} + +impl shader::Program for Scene { + type State = (); + type Primitive = primitive::Primitive; + + fn draw( + &self, + _state: &Self::State, + _cursor: mouse::Cursor, + bounds: Rectangle, + ) -> Self::Primitive { + primitive::Primitive::new( + &self.cubes, + &self.camera, + bounds, + self.show_depth_buffer, + self.light_color, + ) + } +} + +fn rnd_origin() -> Vec3 { + Vec3::new( + rand::thread_rng().gen_range(-4.0..4.0), + rand::thread_rng().gen_range(-4.0..4.0), + rand::thread_rng().gen_range(-4.0..2.0), + ) +} -- cgit From 34b5cb75ef9f97076dd9e306d8afb68058176883 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 14:43:02 +0100 Subject: Remove `Default` implementation in `custom_shader` example --- examples/custom_shader/src/main.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index f90061d7..f4853507 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -28,15 +28,6 @@ struct IcedCubes { scene: Scene, } -impl Default for IcedCubes { - fn default() -> Self { - Self { - start: Instant::now(), - scene: Scene::new(), - } - } -} - #[derive(Debug, Clone)] enum Message { CubeAmountChanged(u32), @@ -53,7 +44,13 @@ impl Application for IcedCubes { type Flags = (); fn new(_flags: Self::Flags) -> (Self, Command) { - (IcedCubes::default(), Command::none()) + ( + Self { + start: Instant::now(), + scene: Scene::new(), + }, + Command::none(), + ) } fn title(&self) -> String { -- cgit From 811aa673e9e832ebe38bf56a087f32fdc7aba59c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 15:48:01 +0100 Subject: Improve module hierarchy of `custom_shader` example --- examples/custom_shader/src/camera.rs | 53 -- examples/custom_shader/src/main.rs | 7 +- examples/custom_shader/src/pipeline.rs | 606 -------------------- examples/custom_shader/src/primitive.rs | 97 ---- examples/custom_shader/src/primitive/buffer.rs | 41 -- examples/custom_shader/src/primitive/cube.rs | 326 ----------- examples/custom_shader/src/primitive/uniforms.rs | 22 - examples/custom_shader/src/primitive/vertex.rs | 31 -- examples/custom_shader/src/scene.rs | 103 +++- examples/custom_shader/src/scene/camera.rs | 53 ++ examples/custom_shader/src/scene/pipeline.rs | 615 +++++++++++++++++++++ .../custom_shader/src/scene/pipeline/buffer.rs | 41 ++ examples/custom_shader/src/scene/pipeline/cube.rs | 326 +++++++++++ .../custom_shader/src/scene/pipeline/uniforms.rs | 23 + .../custom_shader/src/scene/pipeline/vertex.rs | 31 ++ 15 files changed, 1185 insertions(+), 1190 deletions(-) delete mode 100644 examples/custom_shader/src/camera.rs delete mode 100644 examples/custom_shader/src/pipeline.rs delete mode 100644 examples/custom_shader/src/primitive.rs delete mode 100644 examples/custom_shader/src/primitive/buffer.rs delete mode 100644 examples/custom_shader/src/primitive/cube.rs delete mode 100644 examples/custom_shader/src/primitive/uniforms.rs delete mode 100644 examples/custom_shader/src/primitive/vertex.rs create mode 100644 examples/custom_shader/src/scene/camera.rs create mode 100644 examples/custom_shader/src/scene/pipeline.rs create mode 100644 examples/custom_shader/src/scene/pipeline/buffer.rs create mode 100644 examples/custom_shader/src/scene/pipeline/cube.rs create mode 100644 examples/custom_shader/src/scene/pipeline/uniforms.rs create mode 100644 examples/custom_shader/src/scene/pipeline/vertex.rs (limited to 'examples') diff --git a/examples/custom_shader/src/camera.rs b/examples/custom_shader/src/camera.rs deleted file mode 100644 index 2a49c102..00000000 --- a/examples/custom_shader/src/camera.rs +++ /dev/null @@ -1,53 +0,0 @@ -use glam::{mat4, vec3, vec4}; -use iced::Rectangle; - -#[derive(Copy, Clone)] -pub struct Camera { - eye: glam::Vec3, - target: glam::Vec3, - up: glam::Vec3, - fov_y: f32, - near: f32, - far: f32, -} - -impl Default for Camera { - fn default() -> Self { - Self { - eye: vec3(0.0, 2.0, 3.0), - target: glam::Vec3::ZERO, - up: glam::Vec3::Y, - fov_y: 45.0, - near: 0.1, - far: 100.0, - } - } -} - -pub const OPENGL_TO_WGPU_MATRIX: glam::Mat4 = mat4( - vec4(1.0, 0.0, 0.0, 0.0), - vec4(0.0, 1.0, 0.0, 0.0), - vec4(0.0, 0.0, 0.5, 0.0), - vec4(0.0, 0.0, 0.5, 1.0), -); - -impl Camera { - pub fn build_view_proj_matrix(&self, bounds: Rectangle) -> glam::Mat4 { - //TODO looks distorted without padding; base on surface texture size instead? - let aspect_ratio = bounds.width / (bounds.height + 150.0); - - let view = glam::Mat4::look_at_rh(self.eye, self.target, self.up); - let proj = glam::Mat4::perspective_rh( - self.fov_y, - aspect_ratio, - self.near, - self.far, - ); - - OPENGL_TO_WGPU_MATRIX * proj * view - } - - pub fn position(&self) -> glam::Vec4 { - glam::Vec4::from((self.eye, 0.0)) - } -} diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index f4853507..2eb1ac4a 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -1,11 +1,6 @@ -mod camera; -mod pipeline; -mod primitive; mod scene; -use crate::camera::Camera; -use crate::pipeline::Pipeline; -use crate::scene::Scene; +use scene::Scene; use iced::executor; use iced::time::Instant; diff --git a/examples/custom_shader/src/pipeline.rs b/examples/custom_shader/src/pipeline.rs deleted file mode 100644 index 9343e5e0..00000000 --- a/examples/custom_shader/src/pipeline.rs +++ /dev/null @@ -1,606 +0,0 @@ -use crate::primitive; -use crate::primitive::cube; -use crate::primitive::{Buffer, Uniforms}; -use crate::wgpu; -use crate::wgpu::util::DeviceExt; - -use iced::{Rectangle, Size}; - -const SKY_TEXTURE_SIZE: u32 = 128; - -pub struct Pipeline { - pipeline: wgpu::RenderPipeline, - vertices: wgpu::Buffer, - cubes: Buffer, - uniforms: wgpu::Buffer, - uniform_bind_group: wgpu::BindGroup, - depth_texture_size: Size, - depth_view: wgpu::TextureView, - depth_pipeline: DepthPipeline, -} - -impl Pipeline { - pub fn new( - device: &wgpu::Device, - queue: &wgpu::Queue, - format: wgpu::TextureFormat, - target_size: Size, - ) -> Self { - //vertices of one cube - let vertices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("cubes vertex buffer"), - contents: bytemuck::cast_slice(&cube::Raw::vertices()), - usage: wgpu::BufferUsages::VERTEX, - }); - - //cube instance data - let cubes_buffer = Buffer::new( - device, - "cubes instance buffer", - std::mem::size_of::() as u64, - wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, - ); - - //uniforms for all cubes - let uniforms = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("cubes uniform buffer"), - size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); - - //depth buffer - let depth_texture = device.create_texture(&wgpu::TextureDescriptor { - label: Some("cubes depth texture"), - size: wgpu::Extent3d { - width: target_size.width, - height: target_size.height, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Depth32Float, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }); - - let depth_view = - depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); - - let normal_map_data = load_normal_map_data(); - - //normal map - let normal_texture = device.create_texture_with_data( - queue, - &wgpu::TextureDescriptor { - label: Some("cubes normal map texture"), - size: wgpu::Extent3d { - width: 1024, - height: 1024, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }, - &normal_map_data, - ); - - let normal_view = - normal_texture.create_view(&wgpu::TextureViewDescriptor::default()); - - //skybox texture for reflection/refraction - let skybox_data = load_skybox_data(); - - let skybox_texture = device.create_texture_with_data( - queue, - &wgpu::TextureDescriptor { - label: Some("cubes skybox texture"), - size: wgpu::Extent3d { - width: SKY_TEXTURE_SIZE, - height: SKY_TEXTURE_SIZE, - depth_or_array_layers: 6, //one for each face of the cube - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }, - &skybox_data, - ); - - let sky_view = - skybox_texture.create_view(&wgpu::TextureViewDescriptor { - label: Some("cubes skybox texture view"), - dimension: Some(wgpu::TextureViewDimension::Cube), - ..Default::default() - }); - - let sky_sampler = device.create_sampler(&wgpu::SamplerDescriptor { - label: Some("cubes skybox sampler"), - address_mode_u: wgpu::AddressMode::ClampToEdge, - address_mode_v: wgpu::AddressMode::ClampToEdge, - address_mode_w: wgpu::AddressMode::ClampToEdge, - mag_filter: wgpu::FilterMode::Linear, - min_filter: wgpu::FilterMode::Linear, - mipmap_filter: wgpu::FilterMode::Linear, - ..Default::default() - }); - - let uniform_bind_group_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("cubes uniform bind group layout"), - entries: &[ - wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, - ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - count: None, - }, - wgpu::BindGroupLayoutEntry { - binding: 1, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - sample_type: wgpu::TextureSampleType::Float { - filterable: true, - }, - view_dimension: wgpu::TextureViewDimension::Cube, - multisampled: false, - }, - count: None, - }, - wgpu::BindGroupLayoutEntry { - binding: 2, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Sampler( - wgpu::SamplerBindingType::Filtering, - ), - count: None, - }, - wgpu::BindGroupLayoutEntry { - binding: 3, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - sample_type: wgpu::TextureSampleType::Float { - filterable: true, - }, - view_dimension: wgpu::TextureViewDimension::D2, - multisampled: false, - }, - count: None, - }, - ], - }); - - let uniform_bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("cubes uniform bind group"), - layout: &uniform_bind_group_layout, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: uniforms.as_entire_binding(), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView(&sky_view), - }, - wgpu::BindGroupEntry { - binding: 2, - resource: wgpu::BindingResource::Sampler(&sky_sampler), - }, - wgpu::BindGroupEntry { - binding: 3, - resource: wgpu::BindingResource::TextureView( - &normal_view, - ), - }, - ], - }); - - let layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("cubes pipeline layout"), - bind_group_layouts: &[&uniform_bind_group_layout], - push_constant_ranges: &[], - }); - - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("cubes shader"), - source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shaders/cubes.wgsl"), - )), - }); - - let pipeline = - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("cubes pipeline"), - layout: Some(&layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_main", - buffers: &[primitive::Vertex::desc(), cube::Raw::desc()], - }, - primitive: wgpu::PrimitiveState::default(), - depth_stencil: Some(wgpu::DepthStencilState { - format: wgpu::TextureFormat::Depth32Float, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::Less, - stencil: wgpu::StencilState::default(), - bias: wgpu::DepthBiasState::default(), - }), - multisample: wgpu::MultisampleState { - count: 1, - mask: !0, - alpha_to_coverage_enabled: false, - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_main", - targets: &[Some(wgpu::ColorTargetState { - format, - blend: Some(wgpu::BlendState { - color: wgpu::BlendComponent { - src_factor: wgpu::BlendFactor::SrcAlpha, - dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, - operation: wgpu::BlendOperation::Add, - }, - alpha: wgpu::BlendComponent { - src_factor: wgpu::BlendFactor::One, - dst_factor: wgpu::BlendFactor::One, - operation: wgpu::BlendOperation::Max, - }, - }), - write_mask: wgpu::ColorWrites::ALL, - })], - }), - multiview: None, - }); - - let depth_pipeline = DepthPipeline::new( - device, - format, - depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), - ); - - Self { - pipeline, - cubes: cubes_buffer, - uniforms, - uniform_bind_group, - vertices, - depth_texture_size: target_size, - depth_view, - depth_pipeline, - } - } - - fn update_depth_texture(&mut self, device: &wgpu::Device, size: Size) { - if self.depth_texture_size.height != size.height - || self.depth_texture_size.width != size.width - { - let text = device.create_texture(&wgpu::TextureDescriptor { - label: Some("cubes depth texture"), - size: wgpu::Extent3d { - width: size.width, - height: size.height, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Depth32Float, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }); - - self.depth_view = - text.create_view(&wgpu::TextureViewDescriptor::default()); - self.depth_texture_size = size; - - self.depth_pipeline.update(device, &text); - } - } - - pub fn update( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - target_size: Size, - uniforms: &Uniforms, - num_cubes: usize, - cubes: &[cube::Raw], - ) { - //recreate depth texture if surface texture size has changed - self.update_depth_texture(device, target_size); - - // update uniforms - queue.write_buffer(&self.uniforms, 0, bytemuck::bytes_of(uniforms)); - - //resize cubes vertex buffer if cubes amount changed - let new_size = num_cubes * std::mem::size_of::(); - self.cubes.resize(device, new_size as u64); - - //always write new cube data since they are constantly rotating - queue.write_buffer(&self.cubes.raw, 0, bytemuck::cast_slice(cubes)); - } - - pub fn render( - &self, - target: &wgpu::TextureView, - encoder: &mut wgpu::CommandEncoder, - bounds: Rectangle, - num_cubes: u32, - show_depth: bool, - ) { - { - let mut pass = - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("cubes.pipeline.pass"), - color_attachments: &[Some( - wgpu::RenderPassColorAttachment { - view: target, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Load, - store: wgpu::StoreOp::Store, - }, - }, - )], - depth_stencil_attachment: Some( - wgpu::RenderPassDepthStencilAttachment { - view: &self.depth_view, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Clear(1.0), - store: wgpu::StoreOp::Store, - }), - stencil_ops: None, - }, - ), - timestamp_writes: None, - occlusion_query_set: None, - }); - - pass.set_scissor_rect( - bounds.x, - bounds.y, - bounds.width, - bounds.height, - ); - pass.set_pipeline(&self.pipeline); - pass.set_bind_group(0, &self.uniform_bind_group, &[]); - pass.set_vertex_buffer(0, self.vertices.slice(..)); - pass.set_vertex_buffer(1, self.cubes.raw.slice(..)); - pass.draw(0..36, 0..num_cubes); - } - - if show_depth { - self.depth_pipeline.render(encoder, target, bounds); - } - } -} - -struct DepthPipeline { - pipeline: wgpu::RenderPipeline, - bind_group_layout: wgpu::BindGroupLayout, - bind_group: wgpu::BindGroup, - sampler: wgpu::Sampler, - depth_view: wgpu::TextureView, -} - -impl DepthPipeline { - pub fn new( - device: &wgpu::Device, - format: wgpu::TextureFormat, - depth_texture: wgpu::TextureView, - ) -> Self { - let sampler = device.create_sampler(&wgpu::SamplerDescriptor { - label: Some("cubes.depth_pipeline.sampler"), - ..Default::default() - }); - - let bind_group_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("cubes.depth_pipeline.bind_group_layout"), - entries: &[ - wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Sampler( - wgpu::SamplerBindingType::NonFiltering, - ), - count: None, - }, - wgpu::BindGroupLayoutEntry { - binding: 1, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - sample_type: wgpu::TextureSampleType::Float { - filterable: false, - }, - view_dimension: wgpu::TextureViewDimension::D2, - multisampled: false, - }, - count: None, - }, - ], - }); - - let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("cubes.depth_pipeline.bind_group"), - layout: &bind_group_layout, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Sampler(&sampler), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView( - &depth_texture, - ), - }, - ], - }); - - let layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("cubes.depth_pipeline.layout"), - bind_group_layouts: &[&bind_group_layout], - push_constant_ranges: &[], - }); - - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("cubes.depth_pipeline.shader"), - source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shaders/depth.wgsl"), - )), - }); - - let pipeline = - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("cubes.depth_pipeline.pipeline"), - layout: Some(&layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_main", - buffers: &[], - }, - primitive: wgpu::PrimitiveState::default(), - depth_stencil: Some(wgpu::DepthStencilState { - format: wgpu::TextureFormat::Depth32Float, - depth_write_enabled: false, - depth_compare: wgpu::CompareFunction::Less, - stencil: wgpu::StencilState::default(), - bias: wgpu::DepthBiasState::default(), - }), - multisample: wgpu::MultisampleState::default(), - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_main", - targets: &[Some(wgpu::ColorTargetState { - format, - blend: Some(wgpu::BlendState::REPLACE), - write_mask: wgpu::ColorWrites::ALL, - })], - }), - multiview: None, - }); - - Self { - pipeline, - bind_group_layout, - bind_group, - sampler, - depth_view: depth_texture, - } - } - - pub fn update( - &mut self, - device: &wgpu::Device, - depth_texture: &wgpu::Texture, - ) { - self.depth_view = - depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); - - self.bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("cubes.depth_pipeline.bind_group"), - layout: &self.bind_group_layout, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Sampler(&self.sampler), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView( - &self.depth_view, - ), - }, - ], - }); - } - - pub fn render( - &self, - encoder: &mut wgpu::CommandEncoder, - target: &wgpu::TextureView, - bounds: Rectangle, - ) { - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("cubes.pipeline.depth_pass"), - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: target, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Load, - store: wgpu::StoreOp::Store, - }, - })], - depth_stencil_attachment: Some( - wgpu::RenderPassDepthStencilAttachment { - view: &self.depth_view, - depth_ops: None, - stencil_ops: None, - }, - ), - timestamp_writes: None, - occlusion_query_set: None, - }); - - pass.set_scissor_rect(bounds.x, bounds.y, bounds.width, bounds.height); - pass.set_pipeline(&self.pipeline); - pass.set_bind_group(0, &self.bind_group, &[]); - pass.draw(0..6, 0..1); - } -} - -fn load_skybox_data() -> Vec { - let pos_x: &[u8] = include_bytes!("textures/skybox/pos_x.jpg"); - let neg_x: &[u8] = include_bytes!("textures/skybox/neg_x.jpg"); - let pos_y: &[u8] = include_bytes!("textures/skybox/pos_y.jpg"); - let neg_y: &[u8] = include_bytes!("textures/skybox/neg_y.jpg"); - let pos_z: &[u8] = include_bytes!("textures/skybox/pos_z.jpg"); - let neg_z: &[u8] = include_bytes!("textures/skybox/neg_z.jpg"); - - let data: [&[u8]; 6] = [pos_x, neg_x, pos_y, neg_y, pos_z, neg_z]; - - data.iter().fold(vec![], |mut acc, bytes| { - let i = image::load_from_memory_with_format( - bytes, - image::ImageFormat::Jpeg, - ) - .unwrap() - .to_rgba8() - .into_raw(); - - acc.extend(i); - acc - }) -} - -fn load_normal_map_data() -> Vec { - let bytes: &[u8] = include_bytes!("textures/ice_cube_normal_map.png"); - - image::load_from_memory_with_format(bytes, image::ImageFormat::Png) - .unwrap() - .to_rgba8() - .into_raw() -} diff --git a/examples/custom_shader/src/primitive.rs b/examples/custom_shader/src/primitive.rs deleted file mode 100644 index f5862ab3..00000000 --- a/examples/custom_shader/src/primitive.rs +++ /dev/null @@ -1,97 +0,0 @@ -pub mod cube; -pub mod vertex; - -mod buffer; -mod uniforms; - -pub use buffer::Buffer; -pub use cube::Cube; -pub use uniforms::Uniforms; -pub use vertex::Vertex; - -use crate::wgpu; -use crate::Camera; -use crate::Pipeline; - -use iced::advanced::graphics::Transformation; -use iced::widget::shader; -use iced::{Color, Rectangle, Size}; - -/// A collection of `Cube`s that can be rendered. -#[derive(Debug)] -pub struct Primitive { - cubes: Vec, - uniforms: Uniforms, - show_depth_buffer: bool, -} - -impl Primitive { - pub fn new( - cubes: &[Cube], - camera: &Camera, - bounds: Rectangle, - show_depth_buffer: bool, - light_color: Color, - ) -> Self { - let uniforms = Uniforms::new(camera, bounds, light_color); - - Self { - cubes: cubes - .iter() - .map(cube::Raw::from_cube) - .collect::>(), - uniforms, - show_depth_buffer, - } - } -} - -impl shader::Primitive for Primitive { - fn prepare( - &self, - format: wgpu::TextureFormat, - device: &wgpu::Device, - queue: &wgpu::Queue, - target_size: Size, - _scale_factor: f32, - _transform: Transformation, - storage: &mut shader::Storage, - ) { - if !storage.has::() { - storage.store(Pipeline::new(device, queue, format, target_size)); - } - - let pipeline = storage.get_mut::().unwrap(); - - //upload data to GPU - pipeline.update( - device, - queue, - target_size, - &self.uniforms, - self.cubes.len(), - &self.cubes, - ); - } - - fn render( - &self, - storage: &shader::Storage, - bounds: Rectangle, - target: &wgpu::TextureView, - _target_size: Size, - encoder: &mut wgpu::CommandEncoder, - ) { - //at this point our pipeline should always be initialized - let pipeline = storage.get::().unwrap(); - - //render primitive - pipeline.render( - target, - encoder, - bounds, - self.cubes.len() as u32, - self.show_depth_buffer, - ); - } -} diff --git a/examples/custom_shader/src/primitive/buffer.rs b/examples/custom_shader/src/primitive/buffer.rs deleted file mode 100644 index ef4c41c9..00000000 --- a/examples/custom_shader/src/primitive/buffer.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::wgpu; - -// A custom buffer container for dynamic resizing. -pub struct Buffer { - pub raw: wgpu::Buffer, - label: &'static str, - size: u64, - usage: wgpu::BufferUsages, -} - -impl Buffer { - pub fn new( - device: &wgpu::Device, - label: &'static str, - size: u64, - usage: wgpu::BufferUsages, - ) -> Self { - Self { - raw: device.create_buffer(&wgpu::BufferDescriptor { - label: Some(label), - size, - usage, - mapped_at_creation: false, - }), - label, - size, - usage, - } - } - - pub fn resize(&mut self, device: &wgpu::Device, new_size: u64) { - if new_size > self.size { - self.raw = device.create_buffer(&wgpu::BufferDescriptor { - label: Some(self.label), - size: new_size, - usage: self.usage, - mapped_at_creation: false, - }); - } - } -} diff --git a/examples/custom_shader/src/primitive/cube.rs b/examples/custom_shader/src/primitive/cube.rs deleted file mode 100644 index 7ece685d..00000000 --- a/examples/custom_shader/src/primitive/cube.rs +++ /dev/null @@ -1,326 +0,0 @@ -use crate::primitive::Vertex; -use crate::wgpu; - -use glam::{vec2, vec3, Vec3}; -use rand::{thread_rng, Rng}; - -/// A single instance of a cube. -#[derive(Debug, Clone)] -pub struct Cube { - pub rotation: glam::Quat, - pub position: Vec3, - pub size: f32, - rotation_dir: f32, - rotation_axis: glam::Vec3, -} - -impl Default for Cube { - fn default() -> Self { - Self { - rotation: glam::Quat::IDENTITY, - position: glam::Vec3::ZERO, - size: 0.1, - rotation_dir: 1.0, - rotation_axis: glam::Vec3::Y, - } - } -} - -impl Cube { - pub fn new(size: f32, origin: Vec3) -> Self { - let rnd = thread_rng().gen_range(0.0..=1.0f32); - - Self { - rotation: glam::Quat::IDENTITY, - position: origin + Vec3::new(0.1, 0.1, 0.1), - size, - rotation_dir: if rnd <= 0.5 { -1.0 } else { 1.0 }, - rotation_axis: if rnd <= 0.33 { - glam::Vec3::Y - } else if rnd <= 0.66 { - glam::Vec3::X - } else { - glam::Vec3::Z - }, - } - } - - pub fn update(&mut self, size: f32, time: f32) { - self.rotation = glam::Quat::from_axis_angle( - self.rotation_axis, - time / 2.0 * self.rotation_dir, - ); - self.size = size; - } -} - -#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Debug)] -#[repr(C)] -pub struct Raw { - transformation: glam::Mat4, - normal: glam::Mat3, - _padding: [f32; 3], -} - -impl Raw { - const ATTRIBS: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![ - //cube transformation matrix - 4 => Float32x4, - 5 => Float32x4, - 6 => Float32x4, - 7 => Float32x4, - //normal rotation matrix - 8 => Float32x3, - 9 => Float32x3, - 10 => Float32x3, - ]; - - pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { - wgpu::VertexBufferLayout { - array_stride: std::mem::size_of::() as wgpu::BufferAddress, - step_mode: wgpu::VertexStepMode::Instance, - attributes: &Self::ATTRIBS, - } - } -} - -impl Raw { - pub fn from_cube(cube: &Cube) -> Raw { - Raw { - transformation: glam::Mat4::from_scale_rotation_translation( - glam::vec3(cube.size, cube.size, cube.size), - cube.rotation, - cube.position, - ), - normal: glam::Mat3::from_quat(cube.rotation), - _padding: [0.0; 3], - } - } - - pub fn vertices() -> [Vertex; 36] { - [ - //face 1 - Vertex { - pos: vec3(-0.5, -0.5, -0.5), - normal: vec3(0.0, 0.0, -1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - Vertex { - pos: vec3(0.5, -0.5, -0.5), - normal: vec3(0.0, 0.0, -1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 1.0), - }, - Vertex { - pos: vec3(0.5, 0.5, -0.5), - normal: vec3(0.0, 0.0, -1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(0.5, 0.5, -0.5), - normal: vec3(0.0, 0.0, -1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, 0.5, -0.5), - normal: vec3(0.0, 0.0, -1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, -0.5), - normal: vec3(0.0, 0.0, -1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - //face 2 - Vertex { - pos: vec3(-0.5, -0.5, 0.5), - normal: vec3(0.0, 0.0, 1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - Vertex { - pos: vec3(0.5, -0.5, 0.5), - normal: vec3(0.0, 0.0, 1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 1.0), - }, - Vertex { - pos: vec3(0.5, 0.5, 0.5), - normal: vec3(0.0, 0.0, 1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(0.5, 0.5, 0.5), - normal: vec3(0.0, 0.0, 1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, 0.5, 0.5), - normal: vec3(0.0, 0.0, 1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, 0.5), - normal: vec3(0.0, 0.0, 1.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - //face 3 - Vertex { - pos: vec3(-0.5, 0.5, 0.5), - normal: vec3(-1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(0.0, 1.0), - }, - Vertex { - pos: vec3(-0.5, 0.5, -0.5), - normal: vec3(-1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(1.0, 1.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, -0.5), - normal: vec3(-1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, -0.5), - normal: vec3(-1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, 0.5), - normal: vec3(-1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(0.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, 0.5, 0.5), - normal: vec3(-1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(0.0, 1.0), - }, - //face 4 - Vertex { - pos: vec3(0.5, 0.5, 0.5), - normal: vec3(1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(0.0, 1.0), - }, - Vertex { - pos: vec3(0.5, 0.5, -0.5), - normal: vec3(1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(1.0, 1.0), - }, - Vertex { - pos: vec3(0.5, -0.5, -0.5), - normal: vec3(1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(0.5, -0.5, -0.5), - normal: vec3(1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(0.5, -0.5, 0.5), - normal: vec3(1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(0.0, 0.0), - }, - Vertex { - pos: vec3(0.5, 0.5, 0.5), - normal: vec3(1.0, 0.0, 0.0), - tangent: vec3(0.0, 0.0, -1.0), - uv: vec2(0.0, 1.0), - }, - //face 5 - Vertex { - pos: vec3(-0.5, -0.5, -0.5), - normal: vec3(0.0, -1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - Vertex { - pos: vec3(0.5, -0.5, -0.5), - normal: vec3(0.0, -1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 1.0), - }, - Vertex { - pos: vec3(0.5, -0.5, 0.5), - normal: vec3(0.0, -1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(0.5, -0.5, 0.5), - normal: vec3(0.0, -1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, 0.5), - normal: vec3(0.0, -1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, -0.5, -0.5), - normal: vec3(0.0, -1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - //face 6 - Vertex { - pos: vec3(-0.5, 0.5, -0.5), - normal: vec3(0.0, 1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - Vertex { - pos: vec3(0.5, 0.5, -0.5), - normal: vec3(0.0, 1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 1.0), - }, - Vertex { - pos: vec3(0.5, 0.5, 0.5), - normal: vec3(0.0, 1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(0.5, 0.5, 0.5), - normal: vec3(0.0, 1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(1.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, 0.5, 0.5), - normal: vec3(0.0, 1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 0.0), - }, - Vertex { - pos: vec3(-0.5, 0.5, -0.5), - normal: vec3(0.0, 1.0, 0.0), - tangent: vec3(1.0, 0.0, 0.0), - uv: vec2(0.0, 1.0), - }, - ] - } -} diff --git a/examples/custom_shader/src/primitive/uniforms.rs b/examples/custom_shader/src/primitive/uniforms.rs deleted file mode 100644 index 4fcb413b..00000000 --- a/examples/custom_shader/src/primitive/uniforms.rs +++ /dev/null @@ -1,22 +0,0 @@ -use crate::camera::Camera; -use iced::{Color, Rectangle}; - -#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] -#[repr(C)] -pub struct Uniforms { - camera_proj: glam::Mat4, - camera_pos: glam::Vec4, - light_color: glam::Vec4, -} - -impl Uniforms { - pub fn new(camera: &Camera, bounds: Rectangle, light_color: Color) -> Self { - let camera_proj = camera.build_view_proj_matrix(bounds); - - Self { - camera_proj, - camera_pos: camera.position(), - light_color: glam::Vec4::from(light_color.into_linear()), - } - } -} diff --git a/examples/custom_shader/src/primitive/vertex.rs b/examples/custom_shader/src/primitive/vertex.rs deleted file mode 100644 index e64cd926..00000000 --- a/examples/custom_shader/src/primitive/vertex.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::wgpu; - -#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] -#[repr(C)] -pub struct Vertex { - pub pos: glam::Vec3, - pub normal: glam::Vec3, - pub tangent: glam::Vec3, - pub uv: glam::Vec2, -} - -impl Vertex { - const ATTRIBS: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![ - //position - 0 => Float32x3, - //normal - 1 => Float32x3, - //tangent - 2 => Float32x3, - //uv - 3 => Float32x2, - ]; - - pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { - wgpu::VertexBufferLayout { - array_stride: std::mem::size_of::() as wgpu::BufferAddress, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &Self::ATTRIBS, - } - } -} diff --git a/examples/custom_shader/src/scene.rs b/examples/custom_shader/src/scene.rs index ab923093..3b291ce2 100644 --- a/examples/custom_shader/src/scene.rs +++ b/examples/custom_shader/src/scene.rs @@ -1,13 +1,21 @@ -use crate::camera::Camera; -use crate::primitive; -use crate::primitive::cube::Cube; -use glam::Vec3; +mod camera; +mod pipeline; + +use camera::Camera; +use pipeline::Pipeline; + +use crate::wgpu; +use pipeline::cube::{self, Cube}; + +use iced::mouse; +use iced::time::Duration; use iced::widget::shader; -use iced::{mouse, Color, Rectangle}; +use iced::{Color, Rectangle, Size}; + +use glam::Vec3; use rand::Rng; use std::cmp::Ordering; use std::iter; -use std::time::Duration; pub const MAX: u32 = 500; @@ -72,7 +80,7 @@ impl Scene { impl shader::Program for Scene { type State = (); - type Primitive = primitive::Primitive; + type Primitive = Primitive; fn draw( &self, @@ -80,7 +88,7 @@ impl shader::Program for Scene { _cursor: mouse::Cursor, bounds: Rectangle, ) -> Self::Primitive { - primitive::Primitive::new( + Primitive::new( &self.cubes, &self.camera, bounds, @@ -90,6 +98,85 @@ impl shader::Program for Scene { } } +/// A collection of `Cube`s that can be rendered. +#[derive(Debug)] +pub struct Primitive { + cubes: Vec, + uniforms: pipeline::Uniforms, + show_depth_buffer: bool, +} + +impl Primitive { + pub fn new( + cubes: &[Cube], + camera: &Camera, + bounds: Rectangle, + show_depth_buffer: bool, + light_color: Color, + ) -> Self { + let uniforms = pipeline::Uniforms::new(camera, bounds, light_color); + + Self { + cubes: cubes + .iter() + .map(cube::Raw::from_cube) + .collect::>(), + uniforms, + show_depth_buffer, + } + } +} + +impl shader::Primitive for Primitive { + fn prepare( + &self, + format: wgpu::TextureFormat, + device: &wgpu::Device, + queue: &wgpu::Queue, + target_size: Size, + _scale_factor: f32, + _transform: shader::Transformation, + storage: &mut shader::Storage, + ) { + if !storage.has::() { + storage.store(Pipeline::new(device, queue, format, target_size)); + } + + let pipeline = storage.get_mut::().unwrap(); + + //upload data to GPU + pipeline.update( + device, + queue, + target_size, + &self.uniforms, + self.cubes.len(), + &self.cubes, + ); + } + + fn render( + &self, + storage: &shader::Storage, + bounds: Rectangle, + target: &wgpu::TextureView, + _target_size: Size, + encoder: &mut wgpu::CommandEncoder, + ) { + //at this point our pipeline should always be initialized + let pipeline = storage.get::().unwrap(); + + //render primitive + pipeline.render( + target, + encoder, + bounds, + self.cubes.len() as u32, + self.show_depth_buffer, + ); + } +} + fn rnd_origin() -> Vec3 { Vec3::new( rand::thread_rng().gen_range(-4.0..4.0), diff --git a/examples/custom_shader/src/scene/camera.rs b/examples/custom_shader/src/scene/camera.rs new file mode 100644 index 00000000..2a49c102 --- /dev/null +++ b/examples/custom_shader/src/scene/camera.rs @@ -0,0 +1,53 @@ +use glam::{mat4, vec3, vec4}; +use iced::Rectangle; + +#[derive(Copy, Clone)] +pub struct Camera { + eye: glam::Vec3, + target: glam::Vec3, + up: glam::Vec3, + fov_y: f32, + near: f32, + far: f32, +} + +impl Default for Camera { + fn default() -> Self { + Self { + eye: vec3(0.0, 2.0, 3.0), + target: glam::Vec3::ZERO, + up: glam::Vec3::Y, + fov_y: 45.0, + near: 0.1, + far: 100.0, + } + } +} + +pub const OPENGL_TO_WGPU_MATRIX: glam::Mat4 = mat4( + vec4(1.0, 0.0, 0.0, 0.0), + vec4(0.0, 1.0, 0.0, 0.0), + vec4(0.0, 0.0, 0.5, 0.0), + vec4(0.0, 0.0, 0.5, 1.0), +); + +impl Camera { + pub fn build_view_proj_matrix(&self, bounds: Rectangle) -> glam::Mat4 { + //TODO looks distorted without padding; base on surface texture size instead? + let aspect_ratio = bounds.width / (bounds.height + 150.0); + + let view = glam::Mat4::look_at_rh(self.eye, self.target, self.up); + let proj = glam::Mat4::perspective_rh( + self.fov_y, + aspect_ratio, + self.near, + self.far, + ); + + OPENGL_TO_WGPU_MATRIX * proj * view + } + + pub fn position(&self) -> glam::Vec4 { + glam::Vec4::from((self.eye, 0.0)) + } +} diff --git a/examples/custom_shader/src/scene/pipeline.rs b/examples/custom_shader/src/scene/pipeline.rs new file mode 100644 index 00000000..0967e139 --- /dev/null +++ b/examples/custom_shader/src/scene/pipeline.rs @@ -0,0 +1,615 @@ +pub mod cube; + +mod buffer; +mod uniforms; +mod vertex; + +pub use cube::Cube; +pub use uniforms::Uniforms; + +use buffer::Buffer; +use vertex::Vertex; + +use crate::wgpu; +use crate::wgpu::util::DeviceExt; + +use iced::{Rectangle, Size}; + +const SKY_TEXTURE_SIZE: u32 = 128; + +pub struct Pipeline { + pipeline: wgpu::RenderPipeline, + vertices: wgpu::Buffer, + cubes: Buffer, + uniforms: wgpu::Buffer, + uniform_bind_group: wgpu::BindGroup, + depth_texture_size: Size, + depth_view: wgpu::TextureView, + depth_pipeline: DepthPipeline, +} + +impl Pipeline { + pub fn new( + device: &wgpu::Device, + queue: &wgpu::Queue, + format: wgpu::TextureFormat, + target_size: Size, + ) -> Self { + //vertices of one cube + let vertices = + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("cubes vertex buffer"), + contents: bytemuck::cast_slice(&cube::Raw::vertices()), + usage: wgpu::BufferUsages::VERTEX, + }); + + //cube instance data + let cubes_buffer = Buffer::new( + device, + "cubes instance buffer", + std::mem::size_of::() as u64, + wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + ); + + //uniforms for all cubes + let uniforms = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("cubes uniform buffer"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + + //depth buffer + let depth_texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("cubes depth texture"), + size: wgpu::Extent3d { + width: target_size.width, + height: target_size.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + + let depth_view = + depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); + + let normal_map_data = load_normal_map_data(); + + //normal map + let normal_texture = device.create_texture_with_data( + queue, + &wgpu::TextureDescriptor { + label: Some("cubes normal map texture"), + size: wgpu::Extent3d { + width: 1024, + height: 1024, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }, + &normal_map_data, + ); + + let normal_view = + normal_texture.create_view(&wgpu::TextureViewDescriptor::default()); + + //skybox texture for reflection/refraction + let skybox_data = load_skybox_data(); + + let skybox_texture = device.create_texture_with_data( + queue, + &wgpu::TextureDescriptor { + label: Some("cubes skybox texture"), + size: wgpu::Extent3d { + width: SKY_TEXTURE_SIZE, + height: SKY_TEXTURE_SIZE, + depth_or_array_layers: 6, //one for each face of the cube + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }, + &skybox_data, + ); + + let sky_view = + skybox_texture.create_view(&wgpu::TextureViewDescriptor { + label: Some("cubes skybox texture view"), + dimension: Some(wgpu::TextureViewDimension::Cube), + ..Default::default() + }); + + let sky_sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("cubes skybox sampler"), + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + address_mode_w: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + mipmap_filter: wgpu::FilterMode::Linear, + ..Default::default() + }); + + let uniform_bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("cubes uniform bind group layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: true, + }, + view_dimension: wgpu::TextureViewDimension::Cube, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 2, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::Filtering, + ), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 3, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: true, + }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + ], + }); + + let uniform_bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("cubes uniform bind group"), + layout: &uniform_bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: uniforms.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(&sky_view), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::Sampler(&sky_sampler), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::TextureView( + &normal_view, + ), + }, + ], + }); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("cubes pipeline layout"), + bind_group_layouts: &[&uniform_bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("cubes shader"), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("../shaders/cubes.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("cubes pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[Vertex::desc(), cube::Raw::desc()], + }, + primitive: wgpu::PrimitiveState::default(), + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + }), + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + format, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::One, + operation: wgpu::BlendOperation::Max, + }, + }), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview: None, + }); + + let depth_pipeline = DepthPipeline::new( + device, + format, + depth_texture.create_view(&wgpu::TextureViewDescriptor::default()), + ); + + Self { + pipeline, + cubes: cubes_buffer, + uniforms, + uniform_bind_group, + vertices, + depth_texture_size: target_size, + depth_view, + depth_pipeline, + } + } + + fn update_depth_texture(&mut self, device: &wgpu::Device, size: Size) { + if self.depth_texture_size.height != size.height + || self.depth_texture_size.width != size.width + { + let text = device.create_texture(&wgpu::TextureDescriptor { + label: Some("cubes depth texture"), + size: wgpu::Extent3d { + width: size.width, + height: size.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + + self.depth_view = + text.create_view(&wgpu::TextureViewDescriptor::default()); + self.depth_texture_size = size; + + self.depth_pipeline.update(device, &text); + } + } + + pub fn update( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + target_size: Size, + uniforms: &Uniforms, + num_cubes: usize, + cubes: &[cube::Raw], + ) { + //recreate depth texture if surface texture size has changed + self.update_depth_texture(device, target_size); + + // update uniforms + queue.write_buffer(&self.uniforms, 0, bytemuck::bytes_of(uniforms)); + + //resize cubes vertex buffer if cubes amount changed + let new_size = num_cubes * std::mem::size_of::(); + self.cubes.resize(device, new_size as u64); + + //always write new cube data since they are constantly rotating + queue.write_buffer(&self.cubes.raw, 0, bytemuck::cast_slice(cubes)); + } + + pub fn render( + &self, + target: &wgpu::TextureView, + encoder: &mut wgpu::CommandEncoder, + bounds: Rectangle, + num_cubes: u32, + show_depth: bool, + ) { + { + let mut pass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("cubes.pipeline.pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + }, + )], + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }, + ), + timestamp_writes: None, + occlusion_query_set: None, + }); + + pass.set_scissor_rect( + bounds.x, + bounds.y, + bounds.width, + bounds.height, + ); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &self.uniform_bind_group, &[]); + pass.set_vertex_buffer(0, self.vertices.slice(..)); + pass.set_vertex_buffer(1, self.cubes.raw.slice(..)); + pass.draw(0..36, 0..num_cubes); + } + + if show_depth { + self.depth_pipeline.render(encoder, target, bounds); + } + } +} + +struct DepthPipeline { + pipeline: wgpu::RenderPipeline, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, + sampler: wgpu::Sampler, + depth_view: wgpu::TextureView, +} + +impl DepthPipeline { + pub fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + depth_texture: wgpu::TextureView, + ) -> Self { + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("cubes.depth_pipeline.sampler"), + ..Default::default() + }); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("cubes.depth_pipeline.bind_group_layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::NonFiltering, + ), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: false, + }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + ], + }); + + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("cubes.depth_pipeline.bind_group"), + layout: &bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Sampler(&sampler), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView( + &depth_texture, + ), + }, + ], + }); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("cubes.depth_pipeline.layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("cubes.depth_pipeline.shader"), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("../shaders/depth.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("cubes.depth_pipeline.pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[], + }, + primitive: wgpu::PrimitiveState::default(), + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: false, + depth_compare: wgpu::CompareFunction::Less, + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + }), + multisample: wgpu::MultisampleState::default(), + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + format, + blend: Some(wgpu::BlendState::REPLACE), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview: None, + }); + + Self { + pipeline, + bind_group_layout, + bind_group, + sampler, + depth_view: depth_texture, + } + } + + pub fn update( + &mut self, + device: &wgpu::Device, + depth_texture: &wgpu::Texture, + ) { + self.depth_view = + depth_texture.create_view(&wgpu::TextureViewDescriptor::default()); + + self.bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("cubes.depth_pipeline.bind_group"), + layout: &self.bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Sampler(&self.sampler), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView( + &self.depth_view, + ), + }, + ], + }); + } + + pub fn render( + &self, + encoder: &mut wgpu::CommandEncoder, + target: &wgpu::TextureView, + bounds: Rectangle, + ) { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("cubes.pipeline.depth_pass"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_view, + depth_ops: None, + stencil_ops: None, + }, + ), + timestamp_writes: None, + occlusion_query_set: None, + }); + + pass.set_scissor_rect(bounds.x, bounds.y, bounds.width, bounds.height); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &self.bind_group, &[]); + pass.draw(0..6, 0..1); + } +} + +fn load_skybox_data() -> Vec { + let pos_x: &[u8] = include_bytes!("../textures/skybox/pos_x.jpg"); + let neg_x: &[u8] = include_bytes!("../textures/skybox/neg_x.jpg"); + let pos_y: &[u8] = include_bytes!("../textures/skybox/pos_y.jpg"); + let neg_y: &[u8] = include_bytes!("../textures/skybox/neg_y.jpg"); + let pos_z: &[u8] = include_bytes!("../textures/skybox/pos_z.jpg"); + let neg_z: &[u8] = include_bytes!("../textures/skybox/neg_z.jpg"); + + let data: [&[u8]; 6] = [pos_x, neg_x, pos_y, neg_y, pos_z, neg_z]; + + data.iter().fold(vec![], |mut acc, bytes| { + let i = image::load_from_memory_with_format( + bytes, + image::ImageFormat::Jpeg, + ) + .unwrap() + .to_rgba8() + .into_raw(); + + acc.extend(i); + acc + }) +} + +fn load_normal_map_data() -> Vec { + let bytes: &[u8] = include_bytes!("../textures/ice_cube_normal_map.png"); + + image::load_from_memory_with_format(bytes, image::ImageFormat::Png) + .unwrap() + .to_rgba8() + .into_raw() +} diff --git a/examples/custom_shader/src/scene/pipeline/buffer.rs b/examples/custom_shader/src/scene/pipeline/buffer.rs new file mode 100644 index 00000000..ef4c41c9 --- /dev/null +++ b/examples/custom_shader/src/scene/pipeline/buffer.rs @@ -0,0 +1,41 @@ +use crate::wgpu; + +// A custom buffer container for dynamic resizing. +pub struct Buffer { + pub raw: wgpu::Buffer, + label: &'static str, + size: u64, + usage: wgpu::BufferUsages, +} + +impl Buffer { + pub fn new( + device: &wgpu::Device, + label: &'static str, + size: u64, + usage: wgpu::BufferUsages, + ) -> Self { + Self { + raw: device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }), + label, + size, + usage, + } + } + + pub fn resize(&mut self, device: &wgpu::Device, new_size: u64) { + if new_size > self.size { + self.raw = device.create_buffer(&wgpu::BufferDescriptor { + label: Some(self.label), + size: new_size, + usage: self.usage, + mapped_at_creation: false, + }); + } + } +} diff --git a/examples/custom_shader/src/scene/pipeline/cube.rs b/examples/custom_shader/src/scene/pipeline/cube.rs new file mode 100644 index 00000000..de8bad6c --- /dev/null +++ b/examples/custom_shader/src/scene/pipeline/cube.rs @@ -0,0 +1,326 @@ +use crate::scene::pipeline::Vertex; +use crate::wgpu; + +use glam::{vec2, vec3, Vec3}; +use rand::{thread_rng, Rng}; + +/// A single instance of a cube. +#[derive(Debug, Clone)] +pub struct Cube { + pub rotation: glam::Quat, + pub position: Vec3, + pub size: f32, + rotation_dir: f32, + rotation_axis: glam::Vec3, +} + +impl Default for Cube { + fn default() -> Self { + Self { + rotation: glam::Quat::IDENTITY, + position: glam::Vec3::ZERO, + size: 0.1, + rotation_dir: 1.0, + rotation_axis: glam::Vec3::Y, + } + } +} + +impl Cube { + pub fn new(size: f32, origin: Vec3) -> Self { + let rnd = thread_rng().gen_range(0.0..=1.0f32); + + Self { + rotation: glam::Quat::IDENTITY, + position: origin + Vec3::new(0.1, 0.1, 0.1), + size, + rotation_dir: if rnd <= 0.5 { -1.0 } else { 1.0 }, + rotation_axis: if rnd <= 0.33 { + glam::Vec3::Y + } else if rnd <= 0.66 { + glam::Vec3::X + } else { + glam::Vec3::Z + }, + } + } + + pub fn update(&mut self, size: f32, time: f32) { + self.rotation = glam::Quat::from_axis_angle( + self.rotation_axis, + time / 2.0 * self.rotation_dir, + ); + self.size = size; + } +} + +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Debug)] +#[repr(C)] +pub struct Raw { + transformation: glam::Mat4, + normal: glam::Mat3, + _padding: [f32; 3], +} + +impl Raw { + const ATTRIBS: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![ + //cube transformation matrix + 4 => Float32x4, + 5 => Float32x4, + 6 => Float32x4, + 7 => Float32x4, + //normal rotation matrix + 8 => Float32x3, + 9 => Float32x3, + 10 => Float32x3, + ]; + + pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &Self::ATTRIBS, + } + } +} + +impl Raw { + pub fn from_cube(cube: &Cube) -> Raw { + Raw { + transformation: glam::Mat4::from_scale_rotation_translation( + glam::vec3(cube.size, cube.size, cube.size), + cube.rotation, + cube.position, + ), + normal: glam::Mat3::from_quat(cube.rotation), + _padding: [0.0; 3], + } + } + + pub fn vertices() -> [Vertex; 36] { + [ + //face 1 + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, 0.0, -1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + //face 2 + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(0.0, 0.0, 1.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + //face 3 + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(-1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + //face 4 + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(1.0, 0.0, 0.0), + tangent: vec3(0.0, 0.0, -1.0), + uv: vec2(0.0, 1.0), + }, + //face 5 + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, -0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, -0.5, 0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, 0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, -0.5, -0.5), + normal: vec3(0.0, -1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + //face 6 + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, -0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 1.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(0.5, 0.5, 0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(1.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, 0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 0.0), + }, + Vertex { + pos: vec3(-0.5, 0.5, -0.5), + normal: vec3(0.0, 1.0, 0.0), + tangent: vec3(1.0, 0.0, 0.0), + uv: vec2(0.0, 1.0), + }, + ] + } +} diff --git a/examples/custom_shader/src/scene/pipeline/uniforms.rs b/examples/custom_shader/src/scene/pipeline/uniforms.rs new file mode 100644 index 00000000..1eac8292 --- /dev/null +++ b/examples/custom_shader/src/scene/pipeline/uniforms.rs @@ -0,0 +1,23 @@ +use crate::scene::Camera; + +use iced::{Color, Rectangle}; + +#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +pub struct Uniforms { + camera_proj: glam::Mat4, + camera_pos: glam::Vec4, + light_color: glam::Vec4, +} + +impl Uniforms { + pub fn new(camera: &Camera, bounds: Rectangle, light_color: Color) -> Self { + let camera_proj = camera.build_view_proj_matrix(bounds); + + Self { + camera_proj, + camera_pos: camera.position(), + light_color: glam::Vec4::from(light_color.into_linear()), + } + } +} diff --git a/examples/custom_shader/src/scene/pipeline/vertex.rs b/examples/custom_shader/src/scene/pipeline/vertex.rs new file mode 100644 index 00000000..e64cd926 --- /dev/null +++ b/examples/custom_shader/src/scene/pipeline/vertex.rs @@ -0,0 +1,31 @@ +use crate::wgpu; + +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +pub struct Vertex { + pub pos: glam::Vec3, + pub normal: glam::Vec3, + pub tangent: glam::Vec3, + pub uv: glam::Vec2, +} + +impl Vertex { + const ATTRIBS: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![ + //position + 0 => Float32x3, + //normal + 1 => Float32x3, + //tangent + 2 => Float32x3, + //uv + 3 => Float32x2, + ]; + + pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &Self::ATTRIBS, + } + } +} -- cgit From 77dfa60c9640236df8b56084a6b1c58826b51e7e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 15:49:09 +0100 Subject: Move `textures` directory outside of `src` in `custom_shader` example --- examples/custom_shader/src/scene/pipeline.rs | 14 +++++++------- .../src/textures/ice_cube_normal_map.png | Bin 1773656 -> 0 bytes examples/custom_shader/src/textures/skybox/neg_x.jpg | Bin 7549 -> 0 bytes examples/custom_shader/src/textures/skybox/neg_y.jpg | Bin 2722 -> 0 bytes examples/custom_shader/src/textures/skybox/neg_z.jpg | Bin 3986 -> 0 bytes examples/custom_shader/src/textures/skybox/pos_x.jpg | Bin 5522 -> 0 bytes examples/custom_shader/src/textures/skybox/pos_y.jpg | Bin 3382 -> 0 bytes examples/custom_shader/src/textures/skybox/pos_z.jpg | Bin 5205 -> 0 bytes .../custom_shader/textures/ice_cube_normal_map.png | Bin 0 -> 1773656 bytes examples/custom_shader/textures/skybox/neg_x.jpg | Bin 0 -> 7549 bytes examples/custom_shader/textures/skybox/neg_y.jpg | Bin 0 -> 2722 bytes examples/custom_shader/textures/skybox/neg_z.jpg | Bin 0 -> 3986 bytes examples/custom_shader/textures/skybox/pos_x.jpg | Bin 0 -> 5522 bytes examples/custom_shader/textures/skybox/pos_y.jpg | Bin 0 -> 3382 bytes examples/custom_shader/textures/skybox/pos_z.jpg | Bin 0 -> 5205 bytes 15 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 examples/custom_shader/src/textures/ice_cube_normal_map.png delete mode 100644 examples/custom_shader/src/textures/skybox/neg_x.jpg delete mode 100644 examples/custom_shader/src/textures/skybox/neg_y.jpg delete mode 100644 examples/custom_shader/src/textures/skybox/neg_z.jpg delete mode 100644 examples/custom_shader/src/textures/skybox/pos_x.jpg delete mode 100644 examples/custom_shader/src/textures/skybox/pos_y.jpg delete mode 100644 examples/custom_shader/src/textures/skybox/pos_z.jpg create mode 100644 examples/custom_shader/textures/ice_cube_normal_map.png create mode 100644 examples/custom_shader/textures/skybox/neg_x.jpg create mode 100644 examples/custom_shader/textures/skybox/neg_y.jpg create mode 100644 examples/custom_shader/textures/skybox/neg_z.jpg create mode 100644 examples/custom_shader/textures/skybox/pos_x.jpg create mode 100644 examples/custom_shader/textures/skybox/pos_y.jpg create mode 100644 examples/custom_shader/textures/skybox/pos_z.jpg (limited to 'examples') diff --git a/examples/custom_shader/src/scene/pipeline.rs b/examples/custom_shader/src/scene/pipeline.rs index 0967e139..3956c12e 100644 --- a/examples/custom_shader/src/scene/pipeline.rs +++ b/examples/custom_shader/src/scene/pipeline.rs @@ -582,12 +582,12 @@ impl DepthPipeline { } fn load_skybox_data() -> Vec { - let pos_x: &[u8] = include_bytes!("../textures/skybox/pos_x.jpg"); - let neg_x: &[u8] = include_bytes!("../textures/skybox/neg_x.jpg"); - let pos_y: &[u8] = include_bytes!("../textures/skybox/pos_y.jpg"); - let neg_y: &[u8] = include_bytes!("../textures/skybox/neg_y.jpg"); - let pos_z: &[u8] = include_bytes!("../textures/skybox/pos_z.jpg"); - let neg_z: &[u8] = include_bytes!("../textures/skybox/neg_z.jpg"); + let pos_x: &[u8] = include_bytes!("../../textures/skybox/pos_x.jpg"); + let neg_x: &[u8] = include_bytes!("../../textures/skybox/neg_x.jpg"); + let pos_y: &[u8] = include_bytes!("../../textures/skybox/pos_y.jpg"); + let neg_y: &[u8] = include_bytes!("../../textures/skybox/neg_y.jpg"); + let pos_z: &[u8] = include_bytes!("../../textures/skybox/pos_z.jpg"); + let neg_z: &[u8] = include_bytes!("../../textures/skybox/neg_z.jpg"); let data: [&[u8]; 6] = [pos_x, neg_x, pos_y, neg_y, pos_z, neg_z]; @@ -606,7 +606,7 @@ fn load_skybox_data() -> Vec { } fn load_normal_map_data() -> Vec { - let bytes: &[u8] = include_bytes!("../textures/ice_cube_normal_map.png"); + let bytes: &[u8] = include_bytes!("../../textures/ice_cube_normal_map.png"); image::load_from_memory_with_format(bytes, image::ImageFormat::Png) .unwrap() diff --git a/examples/custom_shader/src/textures/ice_cube_normal_map.png b/examples/custom_shader/src/textures/ice_cube_normal_map.png deleted file mode 100644 index 7b4b7228..00000000 Binary files a/examples/custom_shader/src/textures/ice_cube_normal_map.png and /dev/null differ diff --git a/examples/custom_shader/src/textures/skybox/neg_x.jpg b/examples/custom_shader/src/textures/skybox/neg_x.jpg deleted file mode 100644 index 00cc783d..00000000 Binary files a/examples/custom_shader/src/textures/skybox/neg_x.jpg and /dev/null differ diff --git a/examples/custom_shader/src/textures/skybox/neg_y.jpg b/examples/custom_shader/src/textures/skybox/neg_y.jpg deleted file mode 100644 index 548f6445..00000000 Binary files a/examples/custom_shader/src/textures/skybox/neg_y.jpg and /dev/null differ diff --git a/examples/custom_shader/src/textures/skybox/neg_z.jpg b/examples/custom_shader/src/textures/skybox/neg_z.jpg deleted file mode 100644 index 5698512e..00000000 Binary files a/examples/custom_shader/src/textures/skybox/neg_z.jpg and /dev/null differ diff --git a/examples/custom_shader/src/textures/skybox/pos_x.jpg b/examples/custom_shader/src/textures/skybox/pos_x.jpg deleted file mode 100644 index dddecba7..00000000 Binary files a/examples/custom_shader/src/textures/skybox/pos_x.jpg and /dev/null differ diff --git a/examples/custom_shader/src/textures/skybox/pos_y.jpg b/examples/custom_shader/src/textures/skybox/pos_y.jpg deleted file mode 100644 index 361427fd..00000000 Binary files a/examples/custom_shader/src/textures/skybox/pos_y.jpg and /dev/null differ diff --git a/examples/custom_shader/src/textures/skybox/pos_z.jpg b/examples/custom_shader/src/textures/skybox/pos_z.jpg deleted file mode 100644 index 0085a49e..00000000 Binary files a/examples/custom_shader/src/textures/skybox/pos_z.jpg and /dev/null differ diff --git a/examples/custom_shader/textures/ice_cube_normal_map.png b/examples/custom_shader/textures/ice_cube_normal_map.png new file mode 100644 index 00000000..7b4b7228 Binary files /dev/null and b/examples/custom_shader/textures/ice_cube_normal_map.png differ diff --git a/examples/custom_shader/textures/skybox/neg_x.jpg b/examples/custom_shader/textures/skybox/neg_x.jpg new file mode 100644 index 00000000..00cc783d Binary files /dev/null and b/examples/custom_shader/textures/skybox/neg_x.jpg differ diff --git a/examples/custom_shader/textures/skybox/neg_y.jpg b/examples/custom_shader/textures/skybox/neg_y.jpg new file mode 100644 index 00000000..548f6445 Binary files /dev/null and b/examples/custom_shader/textures/skybox/neg_y.jpg differ diff --git a/examples/custom_shader/textures/skybox/neg_z.jpg b/examples/custom_shader/textures/skybox/neg_z.jpg new file mode 100644 index 00000000..5698512e Binary files /dev/null and b/examples/custom_shader/textures/skybox/neg_z.jpg differ diff --git a/examples/custom_shader/textures/skybox/pos_x.jpg b/examples/custom_shader/textures/skybox/pos_x.jpg new file mode 100644 index 00000000..dddecba7 Binary files /dev/null and b/examples/custom_shader/textures/skybox/pos_x.jpg differ diff --git a/examples/custom_shader/textures/skybox/pos_y.jpg b/examples/custom_shader/textures/skybox/pos_y.jpg new file mode 100644 index 00000000..361427fd Binary files /dev/null and b/examples/custom_shader/textures/skybox/pos_y.jpg differ diff --git a/examples/custom_shader/textures/skybox/pos_z.jpg b/examples/custom_shader/textures/skybox/pos_z.jpg new file mode 100644 index 00000000..0085a49e Binary files /dev/null and b/examples/custom_shader/textures/skybox/pos_z.jpg differ -- cgit From 0968c5b64a528ff92a5a93f6586eef557546da25 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Nov 2023 15:58:32 +0100 Subject: Remove unused import in `custom_shader` example --- examples/custom_shader/src/scene/pipeline.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/custom_shader/src/scene/pipeline.rs b/examples/custom_shader/src/scene/pipeline.rs index 3956c12e..94c6c562 100644 --- a/examples/custom_shader/src/scene/pipeline.rs +++ b/examples/custom_shader/src/scene/pipeline.rs @@ -4,7 +4,6 @@ mod buffer; mod uniforms; mod vertex; -pub use cube::Cube; pub use uniforms::Uniforms; use buffer::Buffer; -- cgit From 7dd32f3be43c72e11dac5e07918e9ad6d36b6555 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 15 Nov 2023 10:27:26 +0100 Subject: Update `itertools` dependency for `game_of_life` example --- examples/game_of_life/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index 9b291de8..7596844c 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -9,7 +9,7 @@ publish = false iced.workspace = true iced.features = ["debug", "canvas", "tokio"] -itertools = "0.11" +itertools = "0.12" rustc-hash.workspace = true tokio = { workspace = true, features = ["sync"] } tracing-subscriber = "0.3" -- cgit From 921ddec1285027a6a2feb88b0a5c29ec8f942f8b Mon Sep 17 00:00:00 2001 From: arslee07 Date: Wed, 22 Nov 2023 00:32:01 +0900 Subject: Use the correct GIF for the progress bar example --- examples/progress_bar/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/progress_bar/README.md b/examples/progress_bar/README.md index 1268ac6b..a87829c6 100644 --- a/examples/progress_bar/README.md +++ b/examples/progress_bar/README.md @@ -5,7 +5,7 @@ A simple progress bar that can be filled by using a slider. The __[`main`]__ file contains all the code of the example.
- +
You can run it with `cargo run`: -- cgit From 89e3de7c08dc07eefbcc2617f0da63282aa1c8ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 21 Nov 2023 18:55:53 +0100 Subject: Fix `modal` and `toast` examples --- examples/modal/src/main.rs | 2 ++ examples/toast/src/main.rs | 1 + 2 files changed, 3 insertions(+) (limited to 'examples') diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 3b69f5e6..acb14372 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -231,6 +231,7 @@ mod modal { use iced::mouse; use iced::{ BorderRadius, Color, Element, Event, Length, Point, Rectangle, Size, + Vector, }; /// A widget that centers a modal element over some base element @@ -413,6 +414,7 @@ mod modal { renderer: &Renderer, _bounds: Size, position: Point, + _translation: Vector, ) -> layout::Node { let limits = layout::Limits::new(Size::ZERO, self.size) .width(Length::Fill) diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 5b089e8a..934049d5 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -511,6 +511,7 @@ mod toast { renderer: &Renderer, bounds: Size, position: Point, + _translation: Vector, ) -> layout::Node { let limits = layout::Limits::new(Size::ZERO, bounds) .width(Length::Fill) -- cgit From ab7dae554cac801aeed5d9aa4d3850d50be86263 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Nov 2023 23:13:38 +0100 Subject: Provide actual bounds to `Shader` primitives ... and allow for proper translation and scissoring. --- examples/custom_shader/src/main.rs | 21 ++++++++------------- examples/custom_shader/src/scene.rs | 6 +++--- examples/custom_shader/src/scene/pipeline.rs | 21 +++++++++++++-------- 3 files changed, 24 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index 2eb1ac4a..3bfa3a43 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -5,9 +5,7 @@ use scene::Scene; use iced::executor; use iced::time::Instant; use iced::widget::shader::wgpu; -use iced::widget::{ - checkbox, column, container, row, shader, slider, text, vertical_space, -}; +use iced::widget::{checkbox, column, container, row, shader, slider, text}; use iced::window; use iced::{ Alignment, Application, Color, Command, Element, Length, Renderer, @@ -138,21 +136,18 @@ impl Application for IcedCubes { let controls = column![top_controls, bottom_controls,] .spacing(10) + .padding(20) .align_items(Alignment::Center); let shader = shader(&self.scene).width(Length::Fill).height(Length::Fill); - container( - column![shader, controls, vertical_space(20),] - .spacing(40) - .align_items(Alignment::Center), - ) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into() + container(column![shader, controls].align_items(Alignment::Center)) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() } fn subscription(&self) -> Subscription { diff --git a/examples/custom_shader/src/scene.rs b/examples/custom_shader/src/scene.rs index 3b291ce2..a35efdd9 100644 --- a/examples/custom_shader/src/scene.rs +++ b/examples/custom_shader/src/scene.rs @@ -133,9 +133,9 @@ impl shader::Primitive for Primitive { format: wgpu::TextureFormat, device: &wgpu::Device, queue: &wgpu::Queue, + _bounds: Rectangle, target_size: Size, _scale_factor: f32, - _transform: shader::Transformation, storage: &mut shader::Storage, ) { if !storage.has::() { @@ -158,9 +158,9 @@ impl shader::Primitive for Primitive { fn render( &self, storage: &shader::Storage, - bounds: Rectangle, target: &wgpu::TextureView, _target_size: Size, + viewport: Rectangle, encoder: &mut wgpu::CommandEncoder, ) { //at this point our pipeline should always be initialized @@ -170,7 +170,7 @@ impl shader::Primitive for Primitive { pipeline.render( target, encoder, - bounds, + viewport, self.cubes.len() as u32, self.show_depth_buffer, ); diff --git a/examples/custom_shader/src/scene/pipeline.rs b/examples/custom_shader/src/scene/pipeline.rs index 94c6c562..124b421f 100644 --- a/examples/custom_shader/src/scene/pipeline.rs +++ b/examples/custom_shader/src/scene/pipeline.rs @@ -351,7 +351,7 @@ impl Pipeline { &self, target: &wgpu::TextureView, encoder: &mut wgpu::CommandEncoder, - bounds: Rectangle, + viewport: Rectangle, num_cubes: u32, show_depth: bool, ) { @@ -384,10 +384,10 @@ impl Pipeline { }); pass.set_scissor_rect( - bounds.x, - bounds.y, - bounds.width, - bounds.height, + viewport.x, + viewport.y, + viewport.width, + viewport.height, ); pass.set_pipeline(&self.pipeline); pass.set_bind_group(0, &self.uniform_bind_group, &[]); @@ -397,7 +397,7 @@ impl Pipeline { } if show_depth { - self.depth_pipeline.render(encoder, target, bounds); + self.depth_pipeline.render(encoder, target, viewport); } } } @@ -550,7 +550,7 @@ impl DepthPipeline { &self, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, - bounds: Rectangle, + viewport: Rectangle, ) { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("cubes.pipeline.depth_pass"), @@ -573,7 +573,12 @@ impl DepthPipeline { occlusion_query_set: None, }); - pass.set_scissor_rect(bounds.x, bounds.y, bounds.width, bounds.height); + pass.set_scissor_rect( + viewport.x, + viewport.y, + viewport.width, + viewport.height, + ); pass.set_pipeline(&self.pipeline); pass.set_bind_group(0, &self.bind_group, &[]); pass.draw(0..6, 0..1); -- cgit