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 --- runtime/src/window.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'runtime/src/window.rs') 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), /// Close the window and exits the application. Close(Id), @@ -155,16 +155,36 @@ pub fn frames() -> Subscription { }) } -/// Opens a new window with the given `settings`. -/// -/// Returns the new window [`Id`] alongside the [`Task`]. -pub fn open(settings: Settings) -> (Id, Task) { +/// Subscribes to all window close requests of the running application. +pub fn close_requests() -> Subscription { + 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 { + 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 { 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`. -- cgit