diff options
author | 2024-06-14 03:04:51 +0200 | |
---|---|---|
committer | 2024-06-14 03:05:58 +0200 | |
commit | 88b938440285fdb44c9e5bd572fda5c0f94996ca (patch) | |
tree | 76c645af8cf454a8611fe66f75f0a4931dd99383 /runtime | |
parent | b21e4567dc32250c90d2ea9c78080cd8bcb66368 (diff) | |
download | iced-88b938440285fdb44c9e5bd572fda5c0f94996ca.tar.gz iced-88b938440285fdb44c9e5bd572fda5c0f94996ca.tar.bz2 iced-88b938440285fdb44c9e5bd572fda5c0f94996ca.zip |
Use `Task` chaining to simplify `multi_window` example
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/src/window.rs | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 59f285fd..2ba3e796 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -21,7 +21,7 @@ use raw_window_handle::WindowHandle; #[allow(missing_debug_implementations)] pub enum Action { /// Opens a new window with some [`Settings`]. - Open(Id, Settings), + Open(Id, Settings, oneshot::Sender<Id>), /// Close the window and exits the application. Close(Id), @@ -155,16 +155,36 @@ pub fn frames() -> Subscription<Instant> { }) } -/// Opens a new window with the given `settings`. -/// -/// Returns the new window [`Id`] alongside the [`Task`]. -pub fn open<T>(settings: Settings) -> (Id, Task<T>) { +/// Subscribes to all window close requests of the running application. +pub fn close_requests() -> Subscription<Id> { + event::listen_with(|event, _status, id| { + if let crate::core::Event::Window(Event::CloseRequested) = event { + Some(id) + } else { + None + } + }) +} + +/// Subscribes to all window closings of the running application. +pub fn closings() -> Subscription<Id> { + event::listen_with(|event, _status, id| { + if let crate::core::Event::Window(Event::Closed) = event { + Some(id) + } else { + None + } + }) +} + +/// Opens a new window with the given [`Settings`]; producing the [`Id`] +/// of the new window on completion. +pub fn open(settings: Settings) -> Task<Id> { let id = Id::unique(); - ( - id, - Task::effect(crate::Action::Window(Action::Open(id, settings))), - ) + Task::oneshot(|channel| { + crate::Action::Window(Action::Open(id, settings, channel)) + }) } /// Closes the window with `id`. |