
# Objective Fix https://github.com/bevyengine/bevy/issues/4530 - Make it easier to open/close/modify windows by setting them up as `Entity`s with a `Window` component. - Make multiple windows very simple to set up. (just add a `Window` component to an entity and it should open) ## Solution - Move all properties of window descriptor to ~components~ a component. - Replace `WindowId` with `Entity`. - ~Use change detection for components to update backend rather than events/commands. (The `CursorMoved`/`WindowResized`/... events are kept for user convenience.~ Check each field individually to see what we need to update, events are still kept for user convenience. --- ## Changelog - `WindowDescriptor` renamed to `Window`. - Width/height consolidated into a `WindowResolution` component. - Requesting maximization/minimization is done on the [`Window::state`] field. - `WindowId` is now `Entity`. ## Migration Guide - Replace `WindowDescriptor` with `Window`. - Change `width` and `height` fields in a `WindowResolution`, either by doing ```rust WindowResolution::new(width, height) // Explicitly // or using From<_> for tuples for convenience (1920., 1080.).into() ``` - Replace any `WindowCommand` code to just modify the `Window`'s fields directly and creating/closing windows is now by spawning/despawning an entity with a `Window` component like so: ```rust let window = commands.spawn(Window { ... }).id(); // open window commands.entity(window).despawn(); // close window ``` ## Unresolved - ~How do we tell when a window is minimized by a user?~ ~Currently using the `Resize(0, 0)` as an indicator of minimization.~ No longer attempting to tell given how finnicky this was across platforms, now the user can only request that a window be maximized/minimized. ## Future work - Move `exit_on_close` functionality out from windowing and into app(?) - https://github.com/bevyengine/bevy/issues/5621 - https://github.com/bevyengine/bevy/issues/7099 - https://github.com/bevyengine/bevy/issues/7098 Co-authored-by: Carter Anderson <mcanders1@gmail.com>
67 lines
2.2 KiB
Rust
67 lines
2.2 KiB
Rust
use crate::{PrimaryWindow, Window, WindowCloseRequested};
|
|
|
|
use bevy_app::AppExit;
|
|
use bevy_ecs::prelude::*;
|
|
use bevy_input::{keyboard::KeyCode, Input};
|
|
|
|
/// 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: Query<&Window>) {
|
|
if windows.is_empty() {
|
|
bevy_utils::tracing::info!("No windows are open, exiting");
|
|
app_exit_events.send(AppExit);
|
|
}
|
|
}
|
|
|
|
/// 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 windows was closed, exiting");
|
|
app_exit_events.send(AppExit);
|
|
}
|
|
}
|
|
|
|
/// 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 commands: Commands, mut closed: EventReader<WindowCloseRequested>) {
|
|
for event in closed.iter() {
|
|
commands.entity(event.window).despawn();
|
|
}
|
|
}
|
|
|
|
/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
|
|
///
|
|
/// This is useful for examples or prototyping.
|
|
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();
|
|
}
|
|
}
|
|
}
|