Skip to content
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

Added FocusedWindow event and raw winit WindowEvents #956

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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