From 08b6aa59f82ba30f0f3c8e0901cc521e6f7bf3a8 Mon Sep 17 00:00:00 2001 From: thebluefish Date: Mon, 7 Dec 2020 13:24:25 -0800 Subject: [PATCH] Added WindowFocused event (#956) --- crates/bevy_window/src/event.rs | 7 +++++++ crates/bevy_window/src/lib.rs | 1 + crates/bevy_winit/src/lib.rs | 18 ++++++++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index 658daa30feec9..287177d58b322 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -57,3 +57,10 @@ pub struct ReceivedCharacter { pub id: WindowId, pub char: char, } + +/// An event that indicates a window has received or lost focus. +#[derive(Debug, Clone)] +pub struct WindowFocused { + pub id: WindowId, + pub focused: bool, +} diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index fb4257d818c2b..c5b70009601df 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -42,6 +42,7 @@ impl Plugin for WindowPlugin { .add_event::() .add_event::() .add_event::() + .add_event::() .init_resource::(); if self.add_primary_window { diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 0b6940b01680c..faaacf626806a 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -15,7 +15,7 @@ use bevy_math::Vec2; use bevy_utils::tracing::{error, trace}; use bevy_window::{ CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window, - WindowCloseRequested, WindowCreated, WindowResized, Windows, + WindowCloseRequested, WindowCreated, WindowFocused, WindowResized, Windows, }; use winit::{ event::{self, DeviceEvent, Event, WindowEvent}, @@ -27,11 +27,7 @@ pub struct WinitPlugin; impl Plugin for WinitPlugin { fn build(&self, app: &mut AppBuilder) { - app - // TODO: It would be great to provide a raw winit WindowEvent here, but the lifetime on it is - // stopping us. there are plans to remove the lifetime: https://github.com/rust-windowing/winit/pull/1456 - // .add_event::() - .init_resource::() + app.init_resource::() .set_runner(winit_runner) .add_system(change_window); } @@ -340,6 +336,16 @@ pub fn winit_runner(mut app: App) { window.update_scale_factor_from_backend(scale_factor); window.update_resolution_from_backend(size.width, size.height); } + WindowEvent::Focused(focused) => { + let mut focused_events = + app.resources.get_mut::>().unwrap(); + let winit_windows = app.resources.get_mut::().unwrap(); + let window_id = winit_windows.get_window_id(winit_window_id).unwrap(); + focused_events.send(WindowFocused { + id: window_id, + focused, + }); + } _ => {} }, event::Event::DeviceEvent {