bevy_window/system.rs
1use crate::{ClosingWindow, PrimaryWindow, Window, WindowCloseRequested};
2
3use bevy_app::AppExit;
4use bevy_ecs::prelude::*;
5
6/// A [`SystemSet`] for the system that exits the application.
7/// Which can be either [`exit_on_all_closed`] or [`exit_on_primary_closed`].
8#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
9pub struct ExitSystems;
10
11/// Exit the application when there are no open windows.
12///
13/// This system is added by the [`WindowPlugin`] in the default configuration.
14/// To disable this behavior, set `close_when_requested` (on the [`WindowPlugin`]) to `false`.
15/// Ensure that you read the caveats documented on that field if doing so.
16///
17/// [`WindowPlugin`]: crate::WindowPlugin
18pub fn exit_on_all_closed(
19 mut app_exit_writer: MessageWriter<AppExit>,
20 windows: Query<(), With<Window>>,
21) {
22 if windows.is_empty() {
23 log::info!("No windows are open, exiting");
24 app_exit_writer.write(AppExit::Success);
25 }
26}
27
28/// Exit the application when the primary window has been closed
29///
30/// This system is added by the [`WindowPlugin`]
31///
32/// [`WindowPlugin`]: crate::WindowPlugin
33pub fn exit_on_primary_closed(
34 mut app_exit_writer: MessageWriter<AppExit>,
35 windows: Query<(), (With<Window>, With<PrimaryWindow>)>,
36) {
37 if windows.is_empty() {
38 log::info!("Primary window was closed, exiting");
39 app_exit_writer.write(AppExit::Success);
40 }
41}
42
43/// Close windows in response to [`WindowCloseRequested`] (e.g. when the close button is pressed).
44///
45/// This system is added by the [`WindowPlugin`] in the default configuration.
46/// To disable this behavior, set `close_when_requested` (on the [`WindowPlugin`]) to `false`.
47/// Ensure that you read the caveats documented on that field if doing so.
48///
49/// [`WindowPlugin`]: crate::WindowPlugin
50pub fn close_when_requested(
51 mut commands: Commands,
52 mut closed: MessageReader<WindowCloseRequested>,
53 closing: Query<Entity, With<ClosingWindow>>,
54) {
55 // This was inserted by us on the last frame so now we can despawn the window
56 for window in closing.iter() {
57 commands.entity(window).despawn();
58 }
59 // Mark the window as closing so we can despawn it on the next frame
60 for event in closed.read() {
61 // When spamming the window close button on windows (other platforms too probably)
62 // we may receive a `WindowCloseRequested` for a window we've just despawned in the above
63 // loop.
64 commands.entity(event.window).try_insert(ClosingWindow);
65 }
66}