2023-01-19 00:38:28 +00:00
|
|
|
use crate::{PrimaryWindow, Window, WindowCloseRequested};
|
2022-05-05 13:35:43 +00:00
|
|
|
|
2022-03-01 19:33:56 +00:00
|
|
|
use bevy_app::AppExit;
|
2022-05-05 13:35:43 +00:00
|
|
|
use bevy_ecs::prelude::*;
|
2020-07-10 04:18:35 +00:00
|
|
|
|
2022-05-05 13:35:43 +00:00
|
|
|
/// Exit the application when there are no open windows.
|
|
|
|
///
|
|
|
|
/// This system is added by the [`WindowPlugin`] in the default configuration.
|
2023-04-08 16:22:46 +00:00
|
|
|
/// To disable this behavior, set `close_when_requested` (on the [`WindowPlugin`]) to `false`.
|
2022-05-05 13:35:43 +00:00
|
|
|
/// Ensure that you read the caveats documented on that field if doing so.
|
|
|
|
///
|
|
|
|
/// [`WindowPlugin`]: crate::WindowPlugin
|
2023-01-19 00:38:28 +00:00
|
|
|
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");
|
2024-04-22 16:48:18 +00:00
|
|
|
app_exit_events.send(AppExit::Success);
|
2023-01-19 00:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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() {
|
2023-08-31 07:20:53 +00:00
|
|
|
bevy_utils::tracing::info!("Primary window was closed, exiting");
|
2024-04-22 16:48:18 +00:00
|
|
|
app_exit_events.send(AppExit::Success);
|
2020-07-10 04:18:35 +00:00
|
|
|
}
|
2020-04-25 01:55:15 +00:00
|
|
|
}
|
2022-05-05 13:35:43 +00:00
|
|
|
|
|
|
|
/// 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.
|
2023-04-08 16:22:46 +00:00
|
|
|
/// To disable this behavior, set `close_when_requested` (on the [`WindowPlugin`]) to `false`.
|
2022-05-05 13:35:43 +00:00
|
|
|
/// Ensure that you read the caveats documented on that field if doing so.
|
|
|
|
///
|
|
|
|
/// [`WindowPlugin`]: crate::WindowPlugin
|
2023-01-19 00:38:28 +00:00
|
|
|
pub fn close_when_requested(mut commands: Commands, mut closed: EventReader<WindowCloseRequested>) {
|
2023-08-30 14:20:03 +00:00
|
|
|
for event in closed.read() {
|
2023-01-19 00:38:28 +00:00
|
|
|
commands.entity(event.window).despawn();
|
2022-05-05 13:35:43 +00:00
|
|
|
}
|
|
|
|
}
|