diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index bc0dc9d872d7d..2a5250ab75359 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -91,6 +91,26 @@ pub struct WindowClosed { /// by the time this event is received. pub window: Entity, } + +/// An event that is sent whenever a window is destroyed by the underlying window system. +/// +/// Note that if your application only has a single window, this event may be your last chance to +/// persist state before the application terminates. +#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] +pub struct WindowDestroyed { + /// Window that has been destroyed. + /// + /// Note that this entity probably no longer exists + /// by the time this event is received. + pub window: Entity, +} + /// An event reporting that the mouse cursor has moved inside a window. /// /// The event is sent only if the cursor is over one of the application's windows. diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index 8850aa7f8de83..3ae1c083c8f2e 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -74,6 +74,7 @@ impl Plugin for WindowPlugin { .add_event::() .add_event::() .add_event::() + .add_event::() .add_event::() .add_event::() .add_event::() diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index ac4db6d29fb74..46b3e3e195f59 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -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")] @@ -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)] @@ -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, + }); + } _ => {} }