
# Objective Fixes two issues related to #13208. First, we ensure render resources for a window are always dropped first to ensure that the `winit::Window` always drops on the main thread when it is removed from `WinitWindows`. Previously, changes in #12978 caused the window to drop in the render world, causing issues. We accomplish this by delaying despawning the window by a frame by inserting a marker component `ClosingWindow` that indicates the window has been requested to close and is in the process of closing. The render world now responds to the equivalent `WindowClosing` event rather than `WindowCloseed` which now fires after the render resources are guarunteed to be cleaned up. Secondly, fixing the above caused (revealed?) that additional events were being delivered to the the event loop handler after exit had already been requested: in my testing `RedrawRequested` and `LoopExiting`. This caused errors to be reported try to send an exit event on the close channel. There are two options here: - Guard the handler so no additional events are delivered once the app is exiting. I ~considered this but worried it might be confusing or bug prone if in the future someone wants to handle `LoopExiting` or some other event to clean-up while exiting.~ We are now taking this approach. - Only send an exit signal if we are not already exiting. ~It doesn't appear to cause any problems to handle the extra events so this seems safer.~ Fixing this also appears to have fixed #13231. Fixes #10260. ## Testing Tested on mac only. --- ## Changelog ### Added - A `WindowClosing` event has been added that indicates the window will be despawned on the next frame. ### Changed - Windows now close a frame after their exit has been requested. ## Migration Guide - Ensure custom exit logic does not rely on the app exiting the same frame as a window is closed.
56 lines
2.0 KiB
Rust
56 lines
2.0 KiB
Rust
use crate::{ClosingWindow, PrimaryWindow, Window, WindowCloseRequested};
|
|
|
|
use bevy_app::AppExit;
|
|
use bevy_ecs::prelude::*;
|
|
|
|
/// Exit the application when there are no open windows.
|
|
///
|
|
/// This system is added by the [`WindowPlugin`] in the default configuration.
|
|
/// To disable this behavior, set `close_when_requested` (on the [`WindowPlugin`]) to `false`.
|
|
/// Ensure that you read the caveats documented on that field if doing so.
|
|
///
|
|
/// [`WindowPlugin`]: crate::WindowPlugin
|
|
pub fn exit_on_all_closed(mut app_exit_events: EventWriter<AppExit>, windows: Query<&Window>) {
|
|
if windows.is_empty() {
|
|
bevy_utils::tracing::info!("No windows are open, exiting");
|
|
app_exit_events.send(AppExit::Success);
|
|
}
|
|
}
|
|
|
|
/// Exit the application when the primary window has been closed
|
|
///
|
|
/// This system is added by the [`WindowPlugin`]
|
|
///
|
|
/// [`WindowPlugin`]: crate::WindowPlugin
|
|
pub fn exit_on_primary_closed(
|
|
mut app_exit_events: EventWriter<AppExit>,
|
|
windows: Query<(), (With<Window>, With<PrimaryWindow>)>,
|
|
) {
|
|
if windows.is_empty() {
|
|
bevy_utils::tracing::info!("Primary window was closed, exiting");
|
|
app_exit_events.send(AppExit::Success);
|
|
}
|
|
}
|
|
|
|
/// Close windows in response to [`WindowCloseRequested`] (e.g. when the close button is pressed).
|
|
///
|
|
/// This system is added by the [`WindowPlugin`] in the default configuration.
|
|
/// To disable this behavior, set `close_when_requested` (on the [`WindowPlugin`]) to `false`.
|
|
/// Ensure that you read the caveats documented on that field if doing so.
|
|
///
|
|
/// [`WindowPlugin`]: crate::WindowPlugin
|
|
pub fn close_when_requested(
|
|
mut commands: Commands,
|
|
mut closed: EventReader<WindowCloseRequested>,
|
|
closing: Query<Entity, With<ClosingWindow>>,
|
|
) {
|
|
// This was inserted by us on the last frame so now we can despawn the window
|
|
for window in closing.iter() {
|
|
commands.entity(window).despawn();
|
|
}
|
|
// Mark the window as closing so we can despawn it on the next frame
|
|
for event in closed.read() {
|
|
commands.entity(event.window).insert(ClosingWindow);
|
|
}
|
|
}
|