-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - Allow closing windows at runtime #3575
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb9fb17
Add support to bevy_window for closing windows
DJMcNab 72eaf1f
Update docs
DJMcNab fb42b8b
Update docs some more
DJMcNab bbce82b
Don't update RequestRedraw docs
DJMcNab 2510dea
Remove surplus comment
DJMcNab c9deedb
Remove explicit drop
DJMcNab aaaf85c
Fix resizing compilation
DJMcNab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>) { | ||
DJMcNab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this field should exist. I agree with you that it's unintuitive behavior to set this to false; when would app developers want to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, if the user has unsaved changes, it's traditional to give an 'unsaved changes' warning window.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, got it. In that case, this should be exposed as a resource: that behavior needs to be able to be changed dynamically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really? If a developer wants this functionality, they can easily enough add it themselves. This is merely making sure the default behaviour is correct, and we give an escape hatch for when people need more complicated logic.
That is, I don't really want to encourage disabling this, but I want to make the option available.