Skip to content

Commit

Permalink
Make window::close return and introduce Task::discard
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Aug 12, 2024
1 parent 7740c35 commit 01aa84e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
6 changes: 4 additions & 2 deletions examples/events/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion examples/exit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions runtime/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>),
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 01aa84e

Please sign in to comment.