From a25b1af45690bdd8e1cbb20ee3a5b1c4342de455 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Jun 2024 01:47:39 +0200 Subject: Replace `Command` with a new `Task` API with chain support --- examples/multi_window/src/main.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'examples/multi_window/src') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index eb74c94a..e15f8759 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -6,7 +6,7 @@ use iced::widget::{ }; use iced::window; use iced::{ - Alignment, Command, Element, Length, Point, Settings, Subscription, Theme, + Alignment, Element, Length, Point, Settings, Subscription, Task, Theme, Vector, }; @@ -48,13 +48,13 @@ impl multi_window::Application for Example { type Theme = Theme; type Flags = (); - fn new(_flags: ()) -> (Self, Command) { + fn new(_flags: ()) -> (Self, Task) { ( Example { windows: HashMap::from([(window::Id::MAIN, Window::new(1))]), next_window_pos: window::Position::Default, }, - Command::none(), + Task::none(), ) } @@ -65,14 +65,14 @@ impl multi_window::Application for Example { .unwrap_or("Example".to_string()) } - fn update(&mut self, message: Message) -> Command { + fn update(&mut self, message: Message) -> Task { match message { Message::ScaleInputChanged(id, scale) => { let window = self.windows.get_mut(&id).expect("Window not found!"); window.scale_input = scale; - Command::none() + Task::none() } Message::ScaleChanged(id, scale) => { let window = @@ -83,7 +83,7 @@ impl multi_window::Application for Example { .unwrap_or(window.current_scale) .clamp(0.5, 5.0); - Command::none() + Task::none() } Message::TitleChanged(id, title) => { let window = @@ -91,12 +91,12 @@ impl multi_window::Application for Example { window.title = title; - Command::none() + Task::none() } Message::CloseWindow(id) => window::close(id), Message::WindowClosed(id) => { self.windows.remove(&id); - Command::none() + Task::none() } Message::WindowOpened(id, position) => { if let Some(position) = position { @@ -108,13 +108,13 @@ impl multi_window::Application for Example { if let Some(window) = self.windows.get(&id) { text_input::focus(window.input_id.clone()) } else { - Command::none() + Task::none() } } Message::NewWindow => { let count = self.windows.len() + 1; - let (id, spawn_window) = window::spawn(window::Settings { + let (id, spawn_window) = window::open(window::Settings { position: self.next_window_pos, exit_on_close_request: count % 2 == 0, ..Default::default() -- cgit From 88b938440285fdb44c9e5bd572fda5c0f94996ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Jun 2024 03:04:51 +0200 Subject: Use `Task` chaining to simplify `multi_window` example --- examples/multi_window/src/main.rs | 155 ++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 83 deletions(-) (limited to 'examples/multi_window/src') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index e15f8759..fa9adb87 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,16 +1,15 @@ -use iced::event; use iced::executor; use iced::multi_window::{self, Application}; use iced::widget::{ - button, center, column, container, scrollable, text, text_input, + button, center, column, container, horizontal_space, scrollable, text, + text_input, }; use iced::window; use iced::{ - Alignment, Element, Length, Point, Settings, Subscription, Task, Theme, - Vector, + Alignment, Element, Length, Settings, Subscription, Task, Theme, Vector, }; -use std::collections::HashMap; +use std::collections::BTreeMap; fn main() -> iced::Result { Example::run(Settings::default()) @@ -18,8 +17,7 @@ fn main() -> iced::Result { #[derive(Default)] struct Example { - windows: HashMap, - next_window_pos: window::Position, + windows: BTreeMap, } #[derive(Debug)] @@ -33,13 +31,12 @@ struct Window { #[derive(Debug, Clone)] enum Message { + OpenWindow, + WindowOpened(window::Id), + WindowClosed(window::Id), ScaleInputChanged(window::Id, String), ScaleChanged(window::Id, String), TitleChanged(window::Id, String), - CloseWindow(window::Id), - WindowOpened(window::Id, Option), - WindowClosed(window::Id), - NewWindow, } impl multi_window::Application for Example { @@ -51,8 +48,7 @@ impl multi_window::Application for Example { fn new(_flags: ()) -> (Self, Task) { ( Example { - windows: HashMap::from([(window::Id::MAIN, Window::new(1))]), - next_window_pos: window::Position::Default, + windows: BTreeMap::from([(window::Id::MAIN, Window::new(1))]), }, Task::none(), ) @@ -62,79 +58,89 @@ impl multi_window::Application for Example { self.windows .get(&window) .map(|window| window.title.clone()) - .unwrap_or("Example".to_string()) + .unwrap_or_default() } fn update(&mut self, message: Message) -> Task { match message { - Message::ScaleInputChanged(id, scale) => { - let window = - self.windows.get_mut(&id).expect("Window not found!"); - window.scale_input = scale; - - Task::none() - } - Message::ScaleChanged(id, scale) => { - let window = - self.windows.get_mut(&id).expect("Window not found!"); - - window.current_scale = scale - .parse::() - .unwrap_or(window.current_scale) - .clamp(0.5, 5.0); - - Task::none() + Message::OpenWindow => { + let Some(last_window) = self.windows.keys().last() else { + return Task::none(); + }; + + window::fetch_position(*last_window) + .then(|last_position| { + let position = last_position.map_or( + window::Position::Default, + |last_position| { + window::Position::Specific( + last_position + Vector::new(20.0, 20.0), + ) + }, + ); + + window::open(window::Settings { + position, + ..window::Settings::default() + }) + }) + .map(Message::WindowOpened) } - Message::TitleChanged(id, title) => { - let window = - self.windows.get_mut(&id).expect("Window not found."); - - window.title = title; + Message::WindowOpened(id) => { + self.windows.insert(id, Window::new(self.windows.len() + 1)); - Task::none() + if let Some(window) = self.windows.get(&id) { + text_input::focus(window.input_id.clone()) + } else { + Task::none() + } } - Message::CloseWindow(id) => window::close(id), Message::WindowClosed(id) => { self.windows.remove(&id); + Task::none() } - Message::WindowOpened(id, position) => { - if let Some(position) = position { - self.next_window_pos = window::Position::Specific( - position + Vector::new(20.0, 20.0), - ); + Message::ScaleInputChanged(id, scale) => { + if let Some(window) = self.windows.get_mut(&id) { + window.scale_input = scale; } - if let Some(window) = self.windows.get(&id) { - text_input::focus(window.input_id.clone()) - } else { - Task::none() - } + Task::none() } - Message::NewWindow => { - let count = self.windows.len() + 1; - - let (id, spawn_window) = window::open(window::Settings { - position: self.next_window_pos, - exit_on_close_request: count % 2 == 0, - ..Default::default() - }); + Message::ScaleChanged(id, scale) => { + if let Some(window) = self.windows.get_mut(&id) { + window.current_scale = scale + .parse::() + .unwrap_or(window.current_scale) + .clamp(0.5, 5.0); + } - self.windows.insert(id, Window::new(count)); + Task::none() + } + Message::TitleChanged(id, title) => { + if let Some(window) = self.windows.get_mut(&id) { + window.title = title; + } - spawn_window + Task::none() } } } - fn view(&self, window: window::Id) -> Element { - let content = self.windows.get(&window).unwrap().view(window); - - center(content).into() + fn view(&self, window_id: window::Id) -> Element { + if let Some(window) = self.windows.get(&window_id) { + center(window.view(window_id)).into() + } else { + horizontal_space().into() + } } - fn theme(&self, window: window::Id) -> Self::Theme { - self.windows.get(&window).unwrap().theme.clone() + fn theme(&self, window: window::Id) -> Theme { + if let Some(window) = self.windows.get(&window) { + window.theme.clone() + } else { + Theme::default() + } } fn scale_factor(&self, window: window::Id) -> f64 { @@ -145,24 +151,7 @@ impl multi_window::Application for Example { } fn subscription(&self) -> Subscription { - event::listen_with(|event, _, window| { - if let iced::Event::Window(window_event) = event { - match window_event { - window::Event::CloseRequested => { - Some(Message::CloseWindow(window)) - } - window::Event::Opened { position, .. } => { - Some(Message::WindowOpened(window, position)) - } - window::Event::Closed => { - Some(Message::WindowClosed(window)) - } - _ => None, - } - } else { - None - } - }) + window::closings().map(Message::WindowClosed) } } @@ -200,7 +189,7 @@ impl Window { ]; let new_window_button = - button(text("New Window")).on_press(Message::NewWindow); + button(text("New Window")).on_press(Message::OpenWindow); let content = scrollable( column![scale_input, title_input, new_window_button] -- cgit From 20945e3f9013c663deeb71096c749bc7b90d462c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Jun 2024 03:11:07 +0200 Subject: Simplify `WindowOpened` message handler in `multi_window` example --- examples/multi_window/src/main.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'examples/multi_window/src') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index fa9adb87..14e4e56b 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -87,13 +87,12 @@ impl multi_window::Application for Example { .map(Message::WindowOpened) } Message::WindowOpened(id) => { - self.windows.insert(id, Window::new(self.windows.len() + 1)); + let window = Window::new(self.windows.len() + 1); + let focus_input = text_input::focus(window.input_id.clone()); - if let Some(window) = self.windows.get(&id) { - text_input::focus(window.input_id.clone()) - } else { - Task::none() - } + self.windows.insert(id, window); + + focus_input } Message::WindowClosed(id) => { self.windows.remove(&id); -- cgit From 7f13fab0582fe681b8546126245512bdc2338ead Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Jun 2024 03:15:14 +0200 Subject: Use all themes in `multi_window` example --- examples/multi_window/src/main.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'examples/multi_window/src') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 14e4e56b..ba764654 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -160,11 +160,7 @@ impl Window { title: format!("Window_{}", count), scale_input: "1.0".to_string(), current_scale: 1.0, - theme: if count % 2 == 0 { - Theme::Light - } else { - Theme::Dark - }, + theme: Theme::ALL[count % Theme::ALL.len()].clone(), input_id: text_input::Id::unique(), } } -- cgit From b5c5a016c4f2b608a740b37c494186557a064f48 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 16 Jun 2024 20:15:55 +0200 Subject: Rename `window::closings` to `window::close_events` --- examples/multi_window/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/multi_window/src') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index ba764654..b82ad1f3 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -150,7 +150,7 @@ impl multi_window::Application for Example { } fn subscription(&self) -> Subscription { - window::closings().map(Message::WindowClosed) + window::close_events().map(Message::WindowClosed) } } -- cgit