Skip to content

Commit 308e092

Browse files
committed
Add Windows::get_focused(_mut) (#6571)
Add a method to get the focused window. Use this instead of `WindowFocused` events in `close_on_esc`. Seems that the OS/window manager might not always send focused events on application startup. Sadly, not a fix for #5646. Co-authored-by: devil-ira <justthecooldude@gmail.com>
1 parent 5f12611 commit 308e092

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

crates/bevy_window/src/system.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Window, WindowCloseRequested, WindowFocused, WindowId, Windows};
1+
use crate::{Window, WindowCloseRequested, Windows};
22

33
use bevy_app::AppExit;
44
use bevy_ecs::prelude::*;
@@ -36,22 +36,10 @@ pub fn close_when_requested(
3636
/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
3737
///
3838
/// This is useful for examples or prototyping.
39-
pub fn close_on_esc(
40-
mut focused: Local<Option<WindowId>>,
41-
mut focused_events: EventReader<WindowFocused>,
42-
mut windows: ResMut<Windows>,
43-
input: Res<Input<KeyCode>>,
44-
) {
45-
// TODO: Track this in e.g. a resource to ensure consistent behaviour across similar systems
46-
for event in focused_events.iter() {
47-
*focused = event.focused.then_some(event.id);
48-
}
49-
50-
if let Some(focused) = &*focused {
51-
if input.just_pressed(KeyCode::Escape) {
52-
if let Some(window) = windows.get_mut(*focused) {
53-
window.close();
54-
}
39+
pub fn close_on_esc(mut windows: ResMut<Windows>, input: Res<Input<KeyCode>>) {
40+
if input.just_pressed(KeyCode::Escape) {
41+
if let Some(window) = windows.get_focused_mut() {
42+
window.close();
5543
}
5644
}
5745
}

crates/bevy_window/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl Window {
432432
cursor_icon: CursorIcon::Default,
433433
physical_cursor_position: None,
434434
raw_handle,
435-
focused: true,
435+
focused: false,
436436
mode: window_descriptor.mode,
437437
canvas: window_descriptor.canvas.clone(),
438438
fit_canvas_to_parent: window_descriptor.fit_canvas_to_parent,

crates/bevy_window/src/windows.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ impl Windows {
5353
.expect("Primary window does not exist")
5454
}
5555

56+
/// Get a reference to the focused [`Window`].
57+
pub fn get_focused(&self) -> Option<&Window> {
58+
self.windows.values().find(|window| window.is_focused())
59+
}
60+
61+
/// Get a mutable reference to the focused [`Window`].
62+
pub fn get_focused_mut(&mut self) -> Option<&mut Window> {
63+
self.windows.values_mut().find(|window| window.is_focused())
64+
}
65+
5666
/// Returns the scale factor for the [`Window`] of `id`, or `1.0` if the window does not exist.
5767
pub fn scale_factor(&self, id: WindowId) -> f64 {
5868
if let Some(window) = self.get(id) {

0 commit comments

Comments
 (0)