Implement keyboard and blur/focus events

This commit is contained in:
Héctor Ramón Jiménez 2019-06-25 21:01:13 +02:00
parent d5368d7979
commit 8ad078b964
7 changed files with 303 additions and 74 deletions

View file

@ -7,9 +7,8 @@ pub use self::proxy::Proxy;
pub use self::window_target::WindowTarget;
use super::{backend, device, monitor, window};
use crate::event::{DeviceId, ElementState, Event, KeyboardInput, WindowEvent};
use crate::event::Event;
use crate::event_loop as root;
use crate::window::WindowId;
use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque};
use std::marker::PhantomData;
@ -45,54 +44,10 @@ impl<T> EventLoop<T> {
_marker: PhantomData,
};
let runner = self.elw.p.run(Box::new(move |event, flow| {
self.elw.p.run(Box::new(move |event, flow| {
event_handler(event, &target, flow)
}));
backend::Document::on_blur(|| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::Focused(false),
});
});
backend::Document::on_focus(|| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::Focused(true),
});
});
backend::Document::on_key_down(|scancode, virtual_keycode, modifiers| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::KeyboardInput {
device_id: DeviceId(unsafe { device::Id::dummy() }),
input: KeyboardInput {
scancode,
state: ElementState::Pressed,
virtual_keycode,
modifiers,
},
},
});
});
backend::Document::on_key_up(|scancode, virtual_keycode, modifiers| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::KeyboardInput {
device_id: DeviceId(unsafe { device::Id::dummy() }),
input: KeyboardInput {
scancode,
state: ElementState::Released,
virtual_keycode,
modifiers,
},
},
});
});
// Throw an exception to break out of Rust exceution and use unreachable to tell the
// compiler this function won't return, giving it a return type of '!'
backend::throw(

View file

@ -1,5 +1,5 @@
use super::{backend, device, proxy::Proxy, runner, window};
use crate::event::{DeviceId, ElementState, Event, TouchPhase, WindowEvent};
use crate::event::{DeviceId, ElementState, Event, KeyboardInput, TouchPhase, WindowEvent};
use crate::event_loop::ControlFlow;
use crate::window::WindowId;
use std::clone::Clone;
@ -27,15 +27,59 @@ impl<T> WindowTarget<T> {
Proxy::new(self.runner.clone())
}
pub fn run(
&self,
event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>,
) -> &runner::Shared<T> {
pub fn run(&self, event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>) {
self.runner.set_listener(event_handler);
&self.runner
}
pub fn register(&self, canvas: &mut backend::Canvas) {
let runner = self.runner.clone();
canvas.on_blur(move || {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::Focused(false),
});
});
let runner = self.runner.clone();
canvas.on_focus(move || {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::Focused(true),
});
});
let runner = self.runner.clone();
canvas.on_key_down(move |scancode, virtual_keycode, modifiers| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::KeyboardInput {
device_id: DeviceId(unsafe { device::Id::dummy() }),
input: KeyboardInput {
scancode,
state: ElementState::Pressed,
virtual_keycode,
modifiers,
},
},
});
});
let runner = self.runner.clone();
canvas.on_key_up(move |scancode, virtual_keycode, modifiers| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::KeyboardInput {
device_id: DeviceId(unsafe { device::Id::dummy() }),
input: KeyboardInput {
scancode,
state: ElementState::Released,
virtual_keycode,
modifiers,
},
},
});
});
let runner = self.runner.clone();
canvas.on_mouse_out(move |pointer_id| {
runner.send_event(Event::WindowEvent {