Implemented on_keydown/up

This commit is contained in:
Samuel Guerra 2019-08-27 00:13:07 -03:00
parent d0096796d3
commit 511f84d929
3 changed files with 88 additions and 2 deletions

View File

@ -1,4 +1,4 @@
#![windows_subsystem = "windows"]
//#![windows_subsystem = "windows"]
pub mod app;
pub mod ui;
@ -23,6 +23,8 @@ fn main() {
.map(|l| text(c, l, rgb(0, 0, 0), "Arial", 14).background_color(rgb(255, 255, 255)))
.collect::<Vec<_>>(),
)
.on_keydown(|k, _| println!("on_keydown: {}", k))
.on_keyup(|k, _| println!("on_keyup: {}", k))
})
.window("window2", rgbaf(0.3, 0.2, 0.1, 1.0), |_| {
center(h_stack(vec![

82
src/ui/event.rs Normal file
View File

@ -0,0 +1,82 @@
use super::{ElementState, KeyboardInput, ModifiersState, NextUpdate, Ui, VirtualKeyCode};
use std::fmt;
#[derive(Debug)]
pub struct KeyInput {
pub key: VirtualKeyCode,
pub modifiers: ModifiersState,
}
impl fmt::Display for KeyInput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.key {
VirtualKeyCode::LControl
| VirtualKeyCode::RControl
| VirtualKeyCode::LShift
| VirtualKeyCode::RShift
| VirtualKeyCode::LAlt
| VirtualKeyCode::RAlt => write!(f, "{:?}", self.key),
_ => {
if self.modifiers.ctrl {
write!(f, "Ctrl + ")?;
}
if self.modifiers.alt {
write!(f, "Alt + ")?;
}
if self.modifiers.shift {
write!(f, "Shift + ")?;
}
if self.modifiers.logo {
write!(f, "Logo + ")?;
}
write!(f, "{:?}", self.key)
}
}
}
}
macro_rules! on_key {
($state: ident, $name: ident, $ext_name: ident, $ext_fn: ident) => {
pub struct $name<T: Ui, F: FnMut(KeyInput, &mut NextUpdate)> {
child: T,
handler: F,
}
impl<T: Ui, F: FnMut(KeyInput, &mut NextUpdate)> $name<T, F> {
pub fn new(child: T, handler: F) -> Self {
$name { child, handler }
}
}
impl<T: Ui, F: FnMut(KeyInput, &mut NextUpdate)> Ui for $name<T, F> {
type Child = T;
fn for_each_child(&mut self, mut action: impl FnMut(&mut Self::Child)) {
action(&mut self.child)
}
fn keyboard_input(&mut self, input: &KeyboardInput, update: &mut NextUpdate) {
self.child.keyboard_input(input, update);
if let (ElementState::$state, Some(key)) = (input.state, input.virtual_keycode) {
let input = KeyInput {
key,
modifiers: input.modifiers,
};
(self.handler)(input, update);
}
}
}
pub trait $ext_name: Ui + Sized {
fn $ext_fn<F: FnMut(KeyInput, &mut NextUpdate)>(self, handler: F) -> $name<Self, F> {
$name::new(self, handler)
}
}
impl<T: Ui + Sized> $ext_name for T {}
};
}
on_key!(Pressed, OnKeyDown, OnKeyDownExt, on_keydown);
on_key!(Released, OnKeyUp, OnKeyUpExt, on_keyup);

View File

@ -1,17 +1,19 @@
mod color;
mod event;
mod layout;
mod stack;
mod text;
pub use crate::window::NextUpdate;
pub use color::*;
pub use event::*;
pub use layout::*;
pub use stack::*;
pub use text::*;
use app_units::Au;
use font_loader::system_fonts;
pub use glutin::event::KeyboardInput;
pub use glutin::event::{ElementState, KeyboardInput, ModifiersState, ScanCode, VirtualKeyCode};
use std::collections::HashMap;
use webrender::api::*;
pub use webrender::api::{LayoutPoint, LayoutRect, LayoutSize};