-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to bevy_window for closing windows
Make the rest of bevy work with multiple windows Remove exit_on_esc Add suggested docs
- Loading branch information
Showing
16 changed files
with
148 additions
and
51 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
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,11 +1,55 @@ | ||
use crate::WindowCloseRequested; | ||
use crate::{Window, WindowCloseRequested, WindowFocused, WindowId, Windows}; | ||
use bevy_app::{AppExit, 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() { | ||
/// Whether to exit the application when there are no open windows. | ||
/// | ||
/// By default, this system is added by the [`crate::WindowPlugin`]. | ||
/// To disable this behaviour, set `close_when_requested` (on the [`crate::WindowPlugin`]) to `false`. | ||
/// Please ensure that you read the caveats documented on that field. | ||
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); | ||
} | ||
} | ||
|
||
/// Whether to close windows when they are requested to be closed (i.e. when the close button is pressed). | ||
/// Not adding this system (without replacement) will lead to the close button having no effect. | ||
/// | ||
/// By default, this system is added by the [`crate::WindowPlugin`]. | ||
/// To disable this behaviour, set `close_when_requested` (on the [`crate::WindowPlugin`]) to `false` | ||
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); | ||
} | ||
} | ||
|
||
// TODO: Consider using the kbd tag here for escape: <kbd>esc</kbd> | ||
// Currently, it isn't rendered by vscode's hover markdown provider (and the contents are lost) | ||
/// Close the focused window whenever the escape key is pressed | ||
/// | ||
/// This is useful for examples | ||
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
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.