From f8292ccf7ef99b254e936329de9dd7e079760e55 Mon Sep 17 00:00:00 2001 From: "Hans W. Uhlig" Date: Fri, 12 Feb 2021 21:32:32 -0800 Subject: [PATCH] Simple Implementation to address #1327 by adding a focused field to the window and related system (#1386) * Simple Implementation to address #1327 by adding a focused field to the window and related system * Changing Window update function from bevy_window to bevy_winit. * Removing unused imports. --- crates/bevy_window/src/window.rs | 13 +++++++++++++ crates/bevy_winit/src/lib.rs | 1 + 2 files changed, 14 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index fc9d800cda..777137348e 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -64,6 +64,7 @@ pub struct Window { cursor_visible: bool, cursor_locked: bool, cursor_position: Option, + focused: bool, mode: WindowMode, #[cfg(target_arch = "wasm32")] pub canvas: Option, @@ -152,6 +153,7 @@ impl Window { cursor_visible: window_descriptor.cursor_visible, cursor_locked: window_descriptor.cursor_locked, cursor_position: None, + focused: true, mode: window_descriptor.mode, #[cfg(target_arch = "wasm32")] canvas: window_descriptor.canvas.clone(), @@ -395,6 +397,12 @@ impl Window { .push(WindowCommand::SetCursorPosition { position }); } + #[allow(missing_docs)] + #[inline] + pub fn update_focused_status_from_backend(&mut self, focused: bool) { + self.focused = focused; + } + #[allow(missing_docs)] #[inline] pub fn update_cursor_position_from_backend(&mut self, cursor_position: Option) { @@ -418,6 +426,11 @@ impl Window { pub fn drain_commands(&mut self) -> impl Iterator + '_ { self.command_queue.drain(..) } + + #[inline] + pub fn is_focused(&self) -> bool { + self.focused + } } #[derive(Debug, Clone)] diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 57b4e0d264..02cf35c05c 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -407,6 +407,7 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { ); } WindowEvent::Focused(focused) => { + window.update_focused_status_from_backend(focused); let mut focused_events = app.resources.get_mut::>().unwrap(); focused_events.send(WindowFocused {