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/visible_bounds') 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/visible_bounds') 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/visible_bounds') 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 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/visible_bounds/Cargo.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'examples/visible_bounds') 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 -- 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/visible_bounds/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples/visible_bounds') 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 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/visible_bounds/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/visible_bounds') 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