Skip to content

Commit

Permalink
Expose WindowDestroyed events (#9016)
Browse files Browse the repository at this point in the history
# Objective

I'm creating an iOS game and had to find a way to persist game state
when the application is terminated. This required listening to the
[`applicationWillTerminate()`
method](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate),
but I cannot do so myself anymore since `winit` already set up a
delegate to listen for it, and there can be only one delegate.

So I had to move up the stack and try to respond to one of the events
from `winit` instead. It appears `winit` fires two events that could
serve my purpose: `WindowEvent::Destroyed` and `Event::LoopDestroyed`.
It seemed to me the former might be slightly more generally useful, and
I also found a past discussion that suggested it would be appropriate
for Bevy to have a `WindowDestroyed` event:
bevyengine/bevy#5589 (comment)

## Solution

- I've added the `WindowDestroyed` event, which fires when `winit` fires
`WindowEvent::Destroyed`.

---

## Changelog

### Added

- Introduced a new `WindowDestroyed` event type. It is used to indicate
a window has been destroyed by the windowing system.
  • Loading branch information
arendjr authored Jul 4, 2023
1 parent 40f5cf1 commit 0b00bc8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use bevy_utils::{
use bevy_window::{
exit_on_all_closed, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime,
ReceivedCharacter, RequestRedraw, Window, WindowBackendScaleFactorChanged,
WindowCloseRequested, WindowCreated, WindowFocused, WindowMoved, WindowResized,
WindowScaleFactorChanged, WindowThemeChanged,
WindowCloseRequested, WindowCreated, WindowDestroyed, WindowFocused, WindowMoved,
WindowResized, WindowScaleFactorChanged, WindowThemeChanged,
};

#[cfg(target_os = "android")]
Expand Down Expand Up @@ -231,6 +231,7 @@ struct WindowEvents<'w> {
window_focused: EventWriter<'w, WindowFocused>,
window_moved: EventWriter<'w, WindowMoved>,
window_theme_changed: EventWriter<'w, WindowThemeChanged>,
window_destroyed: EventWriter<'w, WindowDestroyed>,
}

#[derive(SystemParam)]
Expand Down Expand Up @@ -638,6 +639,11 @@ pub fn winit_runner(mut app: App) {
theme: convert_winit_theme(theme),
});
}
WindowEvent::Destroyed => {
window_events.window_destroyed.send(WindowDestroyed {
window: window_entity,
});
}
_ => {}
}

Expand Down

0 comments on commit 0b00bc8

Please sign in to comment.