pub use uievents_code;
pub use wasm_keyboard_macros as macros;
use uievents_code::KeyboardEventCode;
#[cfg(any(doc, feature = "keypress"))]
macro_rules! key_handler_ty {
($key_id:ident, $state:ident, $f1:ident, $f2:ident, $f3:ident) => {
KeyHandler<$key_id, $state, $f1, $f2, $f3>
};
}
#[cfg(not(any(doc, feature = "keypress")))]
macro_rules! key_handler_ty {
($key_id:ident, $state:ident, $f1:ident, $f2:ident, $f3:ident) => {
KeyHandler<$key_id, $state, $f1, $f2>
};
}
pub trait KeyHandler {
const CODE: KeyboardEventCode;
fn handle_keydown(&self);
fn handle_keyup(&self);
#[cfg(any(doc, feature = "keypress"))]
fn handle_keypress(&self);
}
pub mod implementors {
use uievents_code::KeyboardEventCode;
pub struct KeyHandler<
const KEY_ID: u8,
State,
F1: Fn(&State),
F2: Fn(&State),
#[cfg(any(doc, feature = "keypress"))] F3: Fn(&State),
> {
handle_keydown_impl: F1,
handle_keyup_impl: F2,
#[cfg(any(doc, feature = "keypress"))]
handle_keypress_impl: F3,
state: State,
}
impl<
const KEY_ID: u8,
State,
F1,
F2,
#[cfg(any(doc, feature = "keypress"))] F3: Fn(&State),
> key_handler_ty!(KEY_ID, State, F1, F2, F3)
where
F1: Fn(&State),
F2: Fn(&State),
{
pub fn new(
state: State,
handle_keydown_impl: F1,
handle_keyup_impl: F2,
#[cfg(any(doc, feature = "keypress"))] handle_keypress_impl: F3,
) -> Self {
Self {
state,
handle_keydown_impl,
handle_keyup_impl,
#[cfg(any(doc, feature = "keypress"))]
handle_keypress_impl,
}
}
}
impl<
const KEY_ID: u8,
State,
F1: Fn(&State),
F2: Fn(&State),
#[cfg(any(doc, feature = "keypress"))] F3: Fn(&State),
> super::KeyHandler for key_handler_ty!(KEY_ID, State, F1, F2, F3)
{
const CODE: KeyboardEventCode = match KeyboardEventCode::from_repr(KEY_ID) {
Some(code) => code,
None => panic!("Invalid key code."),
};
fn handle_keydown(&self) {
let &Self {
ref handle_keydown_impl,
ref state,
..
} = self;
(handle_keydown_impl)(state)
}
fn handle_keyup(&self) {
let &Self {
ref handle_keyup_impl,
ref state,
..
} = self;
(handle_keyup_impl)(state)
}
#[cfg(any(doc, feature = "keypress"))]
fn handle_keypress(&self) {
let &Self {
ref handle_keypress_impl,
ref state,
..
} = self;
(handle_keypress_impl)(state)
}
}
}
pub trait KeyboardHandler {
fn handle_keydown(&self, event: &::web_sys::KeyboardEvent);
fn handle_keyup(&self, event: &::web_sys::KeyboardEvent);
#[cfg(feature = "keypress")]
fn handle_keypress(&self, event: &::web_sys::KeyboardEvent);
}