forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow closing windows at runtime (bevyengine#3575)
# Objective Fixes bevyengine#3180, builds from bevyengine#2898 ## Solution Support requesting a window to be closed and closing a window in `bevy_window`, and handle this in `bevy_winit`. This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually. ## Changelog ### Added - `Window::close` to allow closing windows. - `WindowClosed` to allow reacting to windows being closed. ### Changed Replaced `bevy::system::exit_on_esc_system` with `bevy::window::close_on_esc`. ## Fixed The app no longer exits when any window is closed. This difference is only observable when there are multiple windows. ## Migration Guide `bevy::input::system::exit_on_esc_system` has been removed. Use `bevy::window::close_on_esc` instead. `CloseWindow` has been removed. Use `Window::close` instead. The `Close` variant has been added to `WindowCommand`. Handle this by closing the relevant window.
- Loading branch information
Showing
16 changed files
with
178 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,57 @@ | ||
use crate::WindowCloseRequested; | ||
use crate::{Window, WindowCloseRequested, WindowFocused, WindowId, Windows}; | ||
|
||
use bevy_app::AppExit; | ||
use bevy_ecs::event::{EventReader, EventWriter}; | ||
use bevy_ecs::prelude::*; | ||
use bevy_input::{keyboard::KeyCode, Input}; | ||
|
||
pub fn exit_on_window_close_system( | ||
mut app_exit_events: EventWriter<AppExit>, | ||
mut window_close_requested_events: EventReader<WindowCloseRequested>, | ||
) { | ||
if window_close_requested_events.iter().next().is_some() { | ||
/// 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 { | ||
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 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. | ||
pub fn close_on_esc( | ||
mut focused: Local<Option<WindowId>>, | ||
mut focused_events: EventReader<WindowFocused>, | ||
mut windows: ResMut<Windows>, | ||
input: Res<Input<KeyCode>>, | ||
) { | ||
// TODO: Track this in e.g. a resource to ensure consistent behaviour across similar systems | ||
for event in focused_events.iter() { | ||
*focused = event.focused.then(|| event.id); | ||
} | ||
|
||
if let Some(focused) = &*focused { | ||
if input.just_pressed(KeyCode::Escape) { | ||
if let Some(window) = windows.get_mut(*focused) { | ||
window.close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.