Skip to content

Commit ee82eec

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: #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 8aa84ba commit ee82eec

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

crates/bevy_window/src/event.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ pub struct WindowClosed {
9191
/// by the time this event is received.
9292
pub window: Entity,
9393
}
94+
95+
/// An event that is sent whenever a window is destroyed by the underlying window system.
96+
///
97+
/// Note that if your application only has a single window, this event may be your last chance to
98+
/// persist state before the application terminates.
99+
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
100+
#[reflect(Debug, PartialEq)]
101+
#[cfg_attr(
102+
feature = "serialize",
103+
derive(serde::Serialize, serde::Deserialize),
104+
reflect(Serialize, Deserialize)
105+
)]
106+
pub struct WindowDestroyed {
107+
/// Window that has been destroyed.
108+
///
109+
/// Note that this entity probably no longer exists
110+
/// by the time this event is received.
111+
pub window: Entity,
112+
}
113+
94114
/// An event reporting that the mouse cursor has moved inside a window.
95115
///
96116
/// The event is sent only if the cursor is over one of the application's windows.

crates/bevy_window/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl Plugin for WindowPlugin {
7474
.add_event::<WindowCreated>()
7575
.add_event::<WindowClosed>()
7676
.add_event::<WindowCloseRequested>()
77+
.add_event::<WindowDestroyed>()
7778
.add_event::<RequestRedraw>()
7879
.add_event::<CursorMoved>()
7980
.add_event::<CursorEntered>()

crates/bevy_winit/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)