1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::{PrimaryWindow, Window, WindowCloseRequested};
use bevy_app::AppExit;
use bevy_ecs::prelude::*;
use bevy_input::{keyboard::KeyCode, Input};
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);
}
}
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 windows was closed, exiting");
app_exit_events.send(AppExit);
}
}
pub fn close_when_requested(mut commands: Commands, mut closed: EventReader<WindowCloseRequested>) {
for event in closed.iter() {
commands.entity(event.window).despawn();
}
}
pub fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<Input<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}