macro_rules! on_key {
([ $($capture:ident),* ] $($children:tt)*) => { ... };
($($children:tt)*) => { ... };
}
Expand description
Helper macro for creating key handlers.
Details
This macro is used to simplify a common pattern of key handlers where:
event
,event::KeyEvent
andevent::KeyCode::*
are brought into scopeState
s need to be cloned before being moved into the key handler- The event is immediately
match
ed
In addition to the above, this macro also:
- implicitly introduces the
|event|
closure parameter - adds the catch-all
_ => ()
case to thematch
expression
Usage
An example usage looks like the following:
let text = use_state(String::new);
let on_key = on_key! { [text]
KeyEvent { code: Char(c), .. } => text.update(|text| text.push(c)),
KeyEvent { code: Char(c), .. } => text.update(|text| text.pop()),
};
and expands to the following:
let text = use_state(String::new);
let on_key = {
let text = text.clone();
move |event| {
use intuitive::event::{self, KeyEvent, KeyCode::*};
match event {
KeyEvent { code: Char(c), .. } => text.update(|text| text.push(c)),
KeyEvent { code: Char(c), .. } => text.update(|text| text.pop()),
_ => (),
};
};
};
Notice that a trailing comma is required in this macro, as _ => ()
is
always added as the last arm of the match expression.