use keymap_core::{Key, KeyInput, Keymap, Modifiers, resolve_layered};
#[derive(Clone, Debug, PartialEq)]
enum Action {
Save,
SplitPanel,
Quit,
}
enum Context {
Editor,
Panel,
}
fn ctrl(c: char) -> KeyInput {
KeyInput::new(Key::Char(c), Modifiers::CTRL)
}
fn main() {
let mut base = Keymap::new();
base.bind(ctrl('s'), Action::Save);
base.bind(ctrl('q'), Action::Quit);
let mut panel = Keymap::new();
panel.bind(ctrl('s'), Action::SplitPanel);
let layers_for = |ctx: &Context| -> Vec<&Keymap<Action>> {
match ctx {
Context::Editor => vec![&base],
Context::Panel => vec![&panel, &base],
}
};
for ctx in [Context::Editor, Context::Panel] {
let name = match ctx {
Context::Editor => "editor",
Context::Panel => "panel",
};
let layers = layers_for(&ctx);
for key in [ctrl('s'), ctrl('q')] {
match resolve_layered(layers.iter().copied(), &key) {
Some(action) => println!("[{name:>6}] {key:>6} -> {action:?}"),
None => println!("[{name:>6}] {key:>6} -> pass through"),
}
}
}
}