diff options
author | 2024-08-12 05:12:42 +0200 | |
---|---|---|
committer | 2024-08-12 05:12:42 +0200 | |
commit | 01aa84e41afa556fd4e82ef11f2f55cf443ef1aa (patch) | |
tree | d794bc53ecd2f8405ec6128f7d730042179e1635 | |
parent | 7740c35a2a0f162b04f78075afa5a8e2448a782c (diff) | |
download | iced-01aa84e41afa556fd4e82ef11f2f55cf443ef1aa.tar.gz iced-01aa84e41afa556fd4e82ef11f2f55cf443ef1aa.tar.bz2 iced-01aa84e41afa556fd4e82ef11f2f55cf443ef1aa.zip |
Make `window::close` return and introduce `Task::discard`
-rw-r--r-- | examples/events/src/main.rs | 6 | ||||
-rw-r--r-- | examples/exit/src/main.rs | 4 | ||||
-rw-r--r-- | runtime/src/task.rs | 11 | ||||
-rw-r--r-- | runtime/src/window.rs | 6 | ||||
-rw-r--r-- | winit/src/program.rs | 4 |
5 files changed, 24 insertions, 7 deletions
diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 5bada9b5..e432eb14 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -37,7 +37,7 @@ impl Events { } Message::EventOccurred(event) => { if let Event::Window(window::Event::CloseRequested) = event { - window::get_latest().and_then(window::close) + window::get_latest().and_then(window::close).discard() } else { Task::none() } @@ -47,7 +47,9 @@ impl Events { Task::none() } - Message::Exit => window::get_latest().and_then(window::close), + Message::Exit => { + window::get_latest().and_then(window::close).discard() + } } } diff --git a/examples/exit/src/main.rs b/examples/exit/src/main.rs index 48b0864c..d8334bcc 100644 --- a/examples/exit/src/main.rs +++ b/examples/exit/src/main.rs @@ -20,7 +20,9 @@ enum Message { impl Exit { fn update(&mut self, message: Message) -> Task<Message> { match message { - Message::Confirm => window::get_latest().and_then(window::close), + Message::Confirm => { + window::get_latest().and_then(window::close).discard() + } Message::Exit => { self.show_confirm = true; diff --git a/runtime/src/task.rs b/runtime/src/task.rs index 4d75ddaa..ec8d7cc7 100644 --- a/runtime/src/task.rs +++ b/runtime/src/task.rs @@ -159,6 +159,17 @@ impl<T> Task<T> { } } + /// Creates a new [`Task`] that discards the result of the current one. + /// + /// Useful if you only care about the side effects of a [`Task`]. + pub fn discard<O>(self) -> Task<O> + where + T: MaybeSend + 'static, + O: MaybeSend + 'static, + { + self.then(|_| Task::none()) + } + /// Creates a new [`Task`] that can be aborted with the returned [`Handle`]. pub fn abortable(self) -> (Self, Handle) where diff --git a/runtime/src/window.rs b/runtime/src/window.rs index cd27cdfe..0d280f1f 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -24,7 +24,7 @@ pub enum Action { Open(Id, Settings, oneshot::Sender<Id>), /// Close the window and exits the application. - Close(Id), + Close(Id, oneshot::Sender<Id>), /// Gets the [`Id`] of the oldest window. GetOldest(oneshot::Sender<Option<Id>>), @@ -230,8 +230,8 @@ pub fn open(settings: Settings) -> (Id, Task<Id>) { } /// Closes the window with `id`. -pub fn close<T>(id: Id) -> Task<T> { - task::effect(crate::Action::Window(Action::Close(id))) +pub fn close(id: Id) -> Task<Id> { + task::oneshot(|channel| crate::Action::Window(Action::Close(id, channel))) } /// Gets the window [`Id`] of the oldest window. diff --git a/winit/src/program.rs b/winit/src/program.rs index 139b2b8f..a51f4fd7 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -1209,9 +1209,11 @@ fn run_action<P, C>( *is_window_opening = true; } - window::Action::Close(id) => { + window::Action::Close(id, channel) => { let _ = window_manager.remove(id); let _ = ui_caches.remove(&id); + + let _ = channel.send(id); } window::Action::GetOldest(channel) => { let id = |