Support ReceivedCharacter event

This commit is contained in:
Héctor Ramón Jiménez 2019-06-25 21:18:11 +02:00
parent 8ad078b964
commit 8f66d96915
3 changed files with 23 additions and 8 deletions

View file

@ -80,6 +80,14 @@ impl<T> WindowTarget<T> {
}); });
}); });
let runner = self.runner.clone();
canvas.on_key_press(move |char_code| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(window::Id),
event: WindowEvent::ReceivedCharacter(char_code),
});
});
let runner = self.runner.clone(); let runner = self.runner.clone();
canvas.on_mouse_out(move |pointer_id| { canvas.on_mouse_out(move |pointer_id| {
runner.send_event(Event::WindowEvent { runner.send_event(Event::WindowEvent {

View file

@ -14,6 +14,7 @@ pub struct Canvas {
on_blur: Option<Closure<dyn FnMut(FocusEvent)>>, on_blur: Option<Closure<dyn FnMut(FocusEvent)>>,
on_key_up: Option<Closure<dyn FnMut(KeyboardEvent)>>, on_key_up: Option<Closure<dyn FnMut(KeyboardEvent)>>,
on_key_down: Option<Closure<dyn FnMut(KeyboardEvent)>>, on_key_down: Option<Closure<dyn FnMut(KeyboardEvent)>>,
on_key_press: Option<Closure<dyn FnMut(KeyboardEvent)>>,
on_mouse_out: Option<Closure<dyn FnMut(PointerEvent)>>, on_mouse_out: Option<Closure<dyn FnMut(PointerEvent)>>,
on_mouse_over: Option<Closure<dyn FnMut(PointerEvent)>>, on_mouse_over: Option<Closure<dyn FnMut(PointerEvent)>>,
on_mouse_up: Option<Closure<dyn FnMut(PointerEvent)>>, on_mouse_up: Option<Closure<dyn FnMut(PointerEvent)>>,
@ -59,6 +60,7 @@ impl Canvas {
on_focus: None, on_focus: None,
on_key_up: None, on_key_up: None,
on_key_down: None, on_key_down: None,
on_key_press: None,
on_mouse_out: None, on_mouse_out: None,
on_mouse_over: None, on_mouse_over: None,
on_mouse_up: None, on_mouse_up: None,
@ -148,6 +150,15 @@ impl Canvas {
})); }));
} }
pub fn on_key_press<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(char),
{
self.on_key_press = Some(self.add_event("keypress", move |event: KeyboardEvent| {
handler(event::codepoint(&event));
}));
}
pub fn on_mouse_out<F>(&mut self, mut handler: F) pub fn on_mouse_out<F>(&mut self, mut handler: F)
where where
F: 'static + FnMut(i32), F: 'static + FnMut(i32),
@ -183,14 +194,7 @@ impl Canvas {
where where
F: 'static + FnMut(i32, MouseButton, ModifiersState), F: 'static + FnMut(i32, MouseButton, ModifiersState),
{ {
let canvas = self.raw.clone();
self.on_mouse_down = Some(self.add_event("pointerdown", move |event: PointerEvent| { self.on_mouse_down = Some(self.add_event("pointerdown", move |event: PointerEvent| {
// We focus the canvas manually when the user clicks on it.
// This is necessary because we are preventing the default event behavior
// in `add_event`
canvas.focus().expect("Failed to focus canvas");
handler( handler(
event.pointer_id(), event.pointer_id(),
event::mouse_button(&event), event::mouse_button(&event),
@ -231,7 +235,6 @@ impl Canvas {
let closure = Closure::wrap(Box::new(move |event: E| { let closure = Closure::wrap(Box::new(move |event: E| {
{ {
let event_ref = event.as_ref(); let event_ref = event.as_ref();
event_ref.prevent_default();
event_ref.stop_propagation(); event_ref.stop_propagation();
event_ref.cancel_bubble(); event_ref.cancel_bubble();
} }

View file

@ -218,3 +218,7 @@ pub fn keyboard_modifiers(event: &KeyboardEvent) -> ModifiersState {
logo: event.meta_key(), logo: event.meta_key(),
} }
} }
pub fn codepoint(event: &KeyboardEvent) -> char {
event.key().chars().next().unwrap()
}