on_key!() { /* proc-macro */ }
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 and event::KeyCode::* are brought into scope
  • States need to be cloned before being moved into the key handler
  • The event is immediately matched

In addition to the above, this macro also:

  • implicitly introduces the |event| closure parameter
  • adds the catch-all _ => () case to the match 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.