use crate::arena::SmallVec;
use crate::pod::{KeyCode, KeyModifiers};
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
pub struct ActionId(pub u32);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct KeyStroke {
pub key: KeyCode,
pub modifiers: KeyModifiers,
}
impl KeyStroke {
#[inline(always)]
pub const fn new(key: KeyCode, modifiers: KeyModifiers) -> Self {
Self { key, modifiers }
}
#[inline(always)]
pub const fn plain(key: KeyCode) -> Self {
Self::new(key, KeyModifiers::NONE)
}
#[inline(always)]
pub const fn ctrl(ch: char) -> Self {
Self::new(KeyCode::Char(ch), KeyModifiers::CTRL)
}
#[inline(always)]
pub const fn ctrl_shift(ch: char) -> Self {
Self::new(
KeyCode::Char(ch),
KeyModifiers(KeyModifiers::CTRL.0 | KeyModifiers::SHIFT.0),
)
}
#[inline(always)]
pub const fn alt(ch: char) -> Self {
Self::new(KeyCode::Char(ch), KeyModifiers::ALT)
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct KeyChord {
pub first: KeyStroke,
pub second: Option<KeyStroke>,
}
impl KeyChord {
#[inline(always)]
pub const fn single(stroke: KeyStroke) -> Self {
Self {
first: stroke,
second: None,
}
}
#[inline(always)]
pub const fn pair(first: KeyStroke, second: KeyStroke) -> Self {
Self {
first,
second: Some(second),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct KeyBinding {
pub context: Option<&'static str>,
pub chord: KeyChord,
pub action: ActionId,
}
impl KeyBinding {
#[inline(always)]
pub const fn global(chord: KeyChord, action: ActionId) -> Self {
Self {
context: None,
chord,
action,
}
}
#[inline(always)]
pub const fn scoped(context: &'static str, chord: KeyChord, action: ActionId) -> Self {
Self {
context: Some(context),
chord,
action,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum KeymapMatch {
Matched(ActionId),
PendingChord,
NoMatch,
}
#[derive(Clone, Debug)]
pub struct Keymap<const CAP: usize = 64> {
bindings: SmallVec<KeyBinding, CAP>,
pending_first: Option<KeyStroke>,
pending_since_ms: u32,
chord_timeout_ms: u32,
}
impl<const CAP: usize> Default for Keymap<CAP> {
fn default() -> Self {
Self::new()
}
}
impl<const CAP: usize> Keymap<CAP> {
#[inline]
pub const fn new() -> Self {
Self {
bindings: SmallVec::new(),
pending_first: None,
pending_since_ms: 0,
chord_timeout_ms: 1500,
}
}
#[inline(always)]
pub fn set_chord_timeout_ms(&mut self, timeout_ms: u32) {
self.chord_timeout_ms = timeout_ms.max(100);
}
#[inline]
pub fn bind(&mut self, binding: KeyBinding) -> bool {
self.bindings.push(binding)
}
#[inline]
pub fn clear(&mut self) {
self.bindings.clear();
self.pending_first = None;
}
#[inline(always)]
pub fn reset_pending(&mut self) {
self.pending_first = None;
}
#[inline(always)]
pub const fn has_pending_chord(&self) -> bool {
self.pending_first.is_some()
}
#[inline]
fn normalize_stroke(mut stroke: KeyStroke) -> KeyStroke {
if let KeyCode::Char(c) = stroke.key {
if c.is_ascii_uppercase() {
stroke.key = KeyCode::Char(c.to_ascii_lowercase());
}
}
stroke
}
fn find_in_context(
&self,
target_ctx: Option<&str>,
first: KeyStroke,
second: Option<KeyStroke>,
) -> Option<ActionId> {
let nf = Self::normalize_stroke(first);
let ns = second.map(Self::normalize_stroke);
for b in self.bindings.as_slice().iter().rev() {
if b.context != target_ctx {
continue;
}
let bf = Self::normalize_stroke(b.chord.first);
let bs = b.chord.second.map(Self::normalize_stroke);
if bf == nf && bs == ns {
return Some(b.action);
}
}
None
}
fn has_chord_prefix_in_context(&self, target_ctx: Option<&str>, first: KeyStroke) -> bool {
let nf = Self::normalize_stroke(first);
for b in self.bindings.as_slice() {
if b.context == target_ctx
&& b.chord.second.is_some()
&& Self::normalize_stroke(b.chord.first) == nf
{
return true;
}
}
false
}
pub fn resolve(
&mut self,
context_chain: &[&str],
stroke: KeyStroke,
now_ms: u32,
) -> KeymapMatch {
if let Some(prev) = self.pending_first {
if now_ms.wrapping_sub(self.pending_since_ms) > self.chord_timeout_ms {
self.pending_first = None;
} else {
self.pending_first = None;
for &ctx in context_chain {
if let Some(act) = self.find_in_context(Some(ctx), prev, Some(stroke)) {
return KeymapMatch::Matched(act);
}
}
if let Some(act) = self.find_in_context(None, prev, Some(stroke)) {
return KeymapMatch::Matched(act);
}
}
}
for &ctx in context_chain {
if self.has_chord_prefix_in_context(Some(ctx), stroke) {
self.pending_first = Some(stroke);
self.pending_since_ms = now_ms;
return KeymapMatch::PendingChord;
}
if let Some(act) = self.find_in_context(Some(ctx), stroke, None) {
return KeymapMatch::Matched(act);
}
}
if self.has_chord_prefix_in_context(None, stroke) {
self.pending_first = Some(stroke);
self.pending_since_ms = now_ms;
return KeymapMatch::PendingChord;
}
if let Some(act) = self.find_in_context(None, stroke, None) {
return KeymapMatch::Matched(act);
}
KeymapMatch::NoMatch
}
}