on_key!() { /* proc-macro */ }
Expand description
Helper macro for creating key handlers.
Details
This macro is used to simplify a common pattern constructing a event::KeyHandler
where:
event
,event::KeyEvent
,event::KeyCode
::*
, andevent::handler::Propagate
are brought into scopestate::State
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 - returns
event::handler::Propagate::Stop
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.mutate(|text| text.push(c)),
KeyEvent { code: Char(c), .. } => text.mutate(|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.mutate(|text| text.push(c)),
KeyEvent { code: Char(c), .. } => text.mutate(|text| text.pop()),
_ => (),
};
};
};
Helper macro for creating key handlers.
See the documentation in the intuitive
crate for details.