Skip to content

Commit 0b00bc8

Browse files
authored
Expose WindowDestroyed events (#9016)
# 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.
1 parent 40f5cf1 commit 0b00bc8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use bevy_utils::{
4141
use bevy_window::{
4242
exit_on_all_closed, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime,
4343
ReceivedCharacter, RequestRedraw, Window, WindowBackendScaleFactorChanged,
44-
WindowCloseRequested, WindowCreated, WindowFocused, WindowMoved, WindowResized,
45-
WindowScaleFactorChanged, WindowThemeChanged,
44+
WindowCloseRequested, WindowCreated, WindowDestroyed, WindowFocused, WindowMoved,
45+
WindowResized, WindowScaleFactorChanged, WindowThemeChanged,
4646
};
4747

4848
#[cfg(target_os = "android")]
@@ -231,6 +231,7 @@ struct WindowEvents<'w> {
231231
window_focused: EventWriter<'w, WindowFocused>,
232232
window_moved: EventWriter<'w, WindowMoved>,
233233
window_theme_changed: EventWriter<'w, WindowThemeChanged>,
234+
window_destroyed: EventWriter<'w, WindowDestroyed>,
234235
}
235236

236237
#[derive(SystemParam)]
@@ -638,6 +639,11 @@ pub fn winit_runner(mut app: App) {
638639
theme: convert_winit_theme(theme),
639640
});
640641
}
642+
WindowEvent::Destroyed => {
643+
window_events.window_destroyed.send(WindowDestroyed {
644+
window: window_entity,
645+
});
646+
}
641647
_ => {}
642648
}
643649

0 commit comments

Comments
 (0)