use std::hash::Hash;
use smallvec::SmallVec;
use uzor::input::core::keymap::{KeymapLevel, KeymapRegistry};
use uzor::input::KeyboardShortcut;
use uzor::WidgetId;
use crate::types::command::{Binding, KeymapCmd, KeymapScope};
use crate::types::ids::Revision;
use crate::types::ops::{KeymapEffect, KeymapOp};
pub type KeymapEffects = SmallVec<[KeymapEffect; 2]>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Entry<A> {
action: A,
through_modal: bool,
}
#[derive(Clone, Debug)]
pub struct KeymapEngine<O, A: Clone> {
table: KeymapRegistry<Entry<A>, O>,
rev: Revision,
}
impl<O, A: Clone> Default for KeymapEngine<O, A> {
fn default() -> Self {
Self {
table: KeymapRegistry::default(),
rev: Revision::ZERO,
}
}
}
impl<O: Copy + Eq + Hash, A: Copy + Eq> KeymapEngine<O, A> {
pub fn new() -> Self {
Self::default()
}
pub fn apply(&mut self, op: KeymapOp<O, A>) -> KeymapEffects {
let changed = match op {
KeymapOp::Cmd(cmd) => self.cmd(cmd),
};
if changed {
self.rev.bump();
}
KeymapEffects::new()
}
pub fn resolve(
&self,
chord: &KeyboardShortcut,
focused: Option<&WidgetId>,
top_overlay: Option<&O>,
modal_open: bool,
) -> Option<A> {
match self.table.resolve_with_level(chord, focused, top_overlay)? {
(KeymapLevel::Global, e) if modal_open && !e.through_modal => None,
(_, e) => Some(e.action),
}
}
pub fn revision(&self) -> Revision {
self.rev
}
#[allow(dead_code)]
pub fn view(&self) -> KeymapEngineView<'_, O, A> {
KeymapEngineView { e: self }
}
fn cmd(&mut self, cmd: KeymapCmd<O, A>) -> bool {
match cmd {
KeymapCmd::Bind { scope, binding } => {
let Binding {
chord,
action,
through_modal,
} = binding;
let entry = Entry {
action,
through_modal,
};
match scope {
KeymapScope::Global => self.table.bind_global(chord, entry),
KeymapScope::Overlay(o) => self.table.bind_overlay(o, chord, entry),
KeymapScope::Focused(id) => self.table.bind_widget(id, chord, entry),
}
true
}
KeymapCmd::Unbind { scope, chord } => match scope {
KeymapScope::Global => self.table.unbind_global(&chord),
KeymapScope::Overlay(o) => self.table.unbind_overlay(&o, &chord),
KeymapScope::Focused(id) => self.table.unbind_widget(&id, &chord),
},
KeymapCmd::Clear(scope) => match scope {
KeymapScope::Global => self.table.clear_global(),
KeymapScope::Overlay(o) => self.table.clear_overlay(&o),
KeymapScope::Focused(id) => self.table.clear_widget(&id),
},
}
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct KeymapEngineView<'a, O, A: Clone> {
e: &'a KeymapEngine<O, A>,
}
impl<O, A: Clone> Clone for KeymapEngineView<'_, O, A> {
fn clone(&self) -> Self {
*self
}
}
impl<O, A: Clone> Copy for KeymapEngineView<'_, O, A> {}
#[allow(dead_code)]
impl<O: Copy + Eq + Hash, A: Copy + Eq> KeymapEngineView<'_, O, A> {
pub fn len(&self) -> usize {
self.e.table.len()
}
pub fn is_empty(&self) -> bool {
self.e.table.is_empty()
}
pub fn resolve(
&self,
chord: &KeyboardShortcut,
focused: Option<&WidgetId>,
top_overlay: Option<&O>,
modal_open: bool,
) -> Option<A> {
self.e.resolve(chord, focused, top_overlay, modal_open)
}
}
#[cfg(test)]
mod tests {
use super::*;
use uzor::input::KeyCode;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Ov {
Settings,
Palette,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Act {
Save,
CloseSettings,
SubmitField,
Quit,
Other,
}
type Engine = KeymapEngine<Ov, Act>;
fn ctrl_s() -> KeyboardShortcut {
KeyboardShortcut::command(KeyCode::S)
}
fn bind(e: &mut Engine, scope: KeymapScope<Ov>, binding: Binding<Act>) {
e.apply(KeymapOp::Cmd(KeymapCmd::Bind { scope, binding }));
}
fn field() -> WidgetId {
WidgetId::from("settings/name")
}
#[test]
fn precedence_focused_then_overlay_then_global() {
let mut e = Engine::new();
bind(
&mut e,
KeymapScope::Global,
Binding::new(ctrl_s(), Act::Save),
);
bind(
&mut e,
KeymapScope::Overlay(Ov::Settings),
Binding::new(ctrl_s(), Act::CloseSettings),
);
bind(
&mut e,
KeymapScope::Focused(field()),
Binding::new(ctrl_s(), Act::SubmitField),
);
let f = field();
assert_eq!(
e.resolve(&ctrl_s(), Some(&f), Some(&Ov::Settings), true),
Some(Act::SubmitField)
);
assert_eq!(
e.resolve(&ctrl_s(), None, Some(&Ov::Settings), true),
Some(Act::CloseSettings)
);
assert_eq!(e.resolve(&ctrl_s(), None, None, false), Some(Act::Save));
assert_eq!(
e.resolve(&ctrl_s(), None, Some(&Ov::Palette), false),
Some(Act::Save)
);
assert_eq!(e.resolve(&ctrl_s(), None, None, false), Some(Act::Save));
assert_eq!(
e.resolve(&ctrl_s(), None, Some(&Ov::Settings), true),
Some(Act::CloseSettings)
);
assert_eq!(e.view().len(), 3);
}
#[test]
fn through_modal_opt_out() {
let mut e = Engine::new();
let ctrl_q = KeyboardShortcut::command(KeyCode::Q);
bind(
&mut e,
KeymapScope::Global,
Binding::new(ctrl_s(), Act::Save),
);
bind(
&mut e,
KeymapScope::Global,
Binding {
chord: ctrl_q.clone(),
action: Act::Quit,
through_modal: true,
},
);
assert_eq!(e.resolve(&ctrl_s(), None, Some(&Ov::Settings), true), None);
assert_eq!(
e.resolve(&ctrl_q, None, Some(&Ov::Settings), true),
Some(Act::Quit)
);
assert_eq!(e.resolve(&ctrl_s(), None, None, false), Some(Act::Save));
assert_eq!(e.resolve(&ctrl_q, None, None, false), Some(Act::Quit));
bind(
&mut e,
KeymapScope::Global,
Binding::new(ctrl_q.clone(), Act::Other),
);
assert_eq!(e.resolve(&ctrl_q, None, None, true), None);
assert_eq!(e.resolve(&ctrl_q, None, None, false), Some(Act::Other));
}
#[test]
fn last_binding_wins_and_revision_moves_only_on_change() {
let mut e = Engine::new();
assert_eq!(e.revision(), Revision::ZERO);
assert!(e.view().is_empty());
bind(
&mut e,
KeymapScope::Global,
Binding::new(ctrl_s(), Act::Save),
);
bind(
&mut e,
KeymapScope::Global,
Binding::new(ctrl_s(), Act::Other),
);
assert_eq!(e.revision(), Revision(2));
assert_eq!(e.resolve(&ctrl_s(), None, None, false), Some(Act::Other));
assert_eq!(
e.resolve(&KeyboardShortcut::key(KeyCode::S), None, None, false),
None
);
e.apply(KeymapOp::Cmd(KeymapCmd::Unbind {
scope: KeymapScope::Overlay(Ov::Palette),
chord: ctrl_s(),
}));
e.apply(KeymapOp::Cmd(KeymapCmd::Clear(KeymapScope::Focused(
field(),
))));
assert_eq!(e.revision(), Revision(2));
e.apply(KeymapOp::Cmd(KeymapCmd::Unbind {
scope: KeymapScope::Global,
chord: ctrl_s(),
}));
assert_eq!(e.revision(), Revision(3));
assert_eq!(e.resolve(&ctrl_s(), None, None, false), None);
bind(
&mut e,
KeymapScope::Overlay(Ov::Settings),
Binding::new(ctrl_s(), Act::CloseSettings),
);
bind(
&mut e,
KeymapScope::Global,
Binding::new(ctrl_s(), Act::Save),
);
e.apply(KeymapOp::Cmd(KeymapCmd::Clear(KeymapScope::Overlay(
Ov::Settings,
))));
assert_eq!(e.revision(), Revision(6));
assert_eq!(
e.resolve(&ctrl_s(), None, Some(&Ov::Settings), false),
Some(Act::Save)
);
assert_eq!(e.view().len(), 1);
}
}