Make window::close return and introduce Task::discard

This commit is contained in:
Héctor Ramón Jiménez 2024-08-12 05:12:42 +02:00
parent 7740c35a2a
commit 01aa84e41a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 24 additions and 7 deletions

View file

@ -37,7 +37,7 @@ impl Events {
} }
Message::EventOccurred(event) => { Message::EventOccurred(event) => {
if let Event::Window(window::Event::CloseRequested) = 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 { } else {
Task::none() Task::none()
} }
@ -47,7 +47,9 @@ impl Events {
Task::none() Task::none()
} }
Message::Exit => window::get_latest().and_then(window::close), Message::Exit => {
window::get_latest().and_then(window::close).discard()
}
} }
} }

View file

@ -20,7 +20,9 @@ enum Message {
impl Exit { impl Exit {
fn update(&mut self, message: Message) -> Task<Message> { fn update(&mut self, message: Message) -> Task<Message> {
match message { match message {
Message::Confirm => window::get_latest().and_then(window::close), Message::Confirm => {
window::get_latest().and_then(window::close).discard()
}
Message::Exit => { Message::Exit => {
self.show_confirm = true; self.show_confirm = true;

View file

@ -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`]. /// Creates a new [`Task`] that can be aborted with the returned [`Handle`].
pub fn abortable(self) -> (Self, Handle) pub fn abortable(self) -> (Self, Handle)
where where

View file

@ -24,7 +24,7 @@ pub enum Action {
Open(Id, Settings, oneshot::Sender<Id>), Open(Id, Settings, oneshot::Sender<Id>),
/// Close the window and exits the application. /// Close the window and exits the application.
Close(Id), Close(Id, oneshot::Sender<Id>),
/// Gets the [`Id`] of the oldest window. /// Gets the [`Id`] of the oldest window.
GetOldest(oneshot::Sender<Option<Id>>), GetOldest(oneshot::Sender<Option<Id>>),
@ -230,8 +230,8 @@ pub fn open(settings: Settings) -> (Id, Task<Id>) {
} }
/// Closes the window with `id`. /// Closes the window with `id`.
pub fn close<T>(id: Id) -> Task<T> { pub fn close(id: Id) -> Task<Id> {
task::effect(crate::Action::Window(Action::Close(id))) task::oneshot(|channel| crate::Action::Window(Action::Close(id, channel)))
} }
/// Gets the window [`Id`] of the oldest window. /// Gets the window [`Id`] of the oldest window.

View file

@ -1209,9 +1209,11 @@ fn run_action<P, C>(
*is_window_opening = true; *is_window_opening = true;
} }
window::Action::Close(id) => { window::Action::Close(id, channel) => {
let _ = window_manager.remove(id); let _ = window_manager.remove(id);
let _ = ui_caches.remove(&id); let _ = ui_caches.remove(&id);
let _ = channel.send(id);
} }
window::Action::GetOldest(channel) => { window::Action::GetOldest(channel) => {
let id = let id =