2022-11-14 21:59:18 +00:00
|
|
|
use crate::{Window, WindowCloseRequested, Windows};
|
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::*;
|
|
|
|
use bevy_input::{keyboard::KeyCode, Input};
|
2020-07-09 21:18:35 -07: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.
|
|
|
|
/// To disable this behaviour, 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: Res<Windows>) {
|
|
|
|
if windows.iter().count() == 0 {
|
2020-07-09 21:18:35 -07:00
|
|
|
app_exit_events.send(AppExit);
|
|
|
|
}
|
2020-04-24 18:55:15 -07: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.
|
|
|
|
/// To disable this behaviour, 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 windows: ResMut<Windows>,
|
|
|
|
mut closed: EventReader<WindowCloseRequested>,
|
|
|
|
) {
|
|
|
|
for event in closed.iter() {
|
|
|
|
windows.get_mut(event.id).map(Window::close);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
|
|
|
|
///
|
|
|
|
/// This is useful for examples or prototyping.
|
2022-11-14 21:59:18 +00:00
|
|
|
pub fn close_on_esc(mut windows: ResMut<Windows>, input: Res<Input<KeyCode>>) {
|
|
|
|
if input.just_pressed(KeyCode::Escape) {
|
|
|
|
if let Some(window) = windows.get_focused_mut() {
|
|
|
|
window.close();
|
2022-05-05 13:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|