Skip to content

Commit

Permalink
Added WindowFocused event (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebluefish authored Dec 7, 2020
1 parent e1b995f commit 08b6aa5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
7 changes: 7 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
1 change: 1 addition & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Plugin for WindowPlugin {
.add_event::<CursorEntered>()
.add_event::<CursorLeft>()
.add_event::<ReceivedCharacter>()
.add_event::<WindowFocused>()
.init_resource::<Windows>();

if self.add_primary_window {
Expand Down
18 changes: 12 additions & 6 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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::<winit::event::WindowEvent>()
.init_resource::<WinitWindows>()
app.init_resource::<WinitWindows>()
.set_runner(winit_runner)
.add_system(change_window);
}
Expand Down Expand Up @@ -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::<Events<WindowFocused>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
focused_events.send(WindowFocused {
id: window_id,
focused,
});
}
_ => {}
},
event::Event::DeviceEvent {
Expand Down

0 comments on commit 08b6aa5

Please sign in to comment.