1use std::rc::Rc;
  2
  3use crate::{
  4    Action, AsKeystroke, DummyKeyboardMapper, InvalidKeystrokeError, KeyBindingContextPredicate,
  5    KeybindingKeystroke, Keystroke, PlatformKeyboardMapper, SharedString,
  6};
  7use smallvec::SmallVec;
  8
  9/// A keybinding and its associated metadata, from the keymap.
 10pub struct KeyBinding {
 11    pub(crate) action: Box<dyn Action>,
 12    pub(crate) keystrokes: SmallVec<[KeybindingKeystroke; 2]>,
 13    pub(crate) context_predicate: Option<Rc<KeyBindingContextPredicate>>,
 14    pub(crate) meta: Option<KeyBindingMetaIndex>,
 15    /// The json input string used when building the keybinding, if any
 16    pub(crate) action_input: Option<SharedString>,
 17}
 18
 19impl Clone for KeyBinding {
 20    fn clone(&self) -> Self {
 21        KeyBinding {
 22            action: self.action.boxed_clone(),
 23            keystrokes: self.keystrokes.clone(),
 24            context_predicate: self.context_predicate.clone(),
 25            meta: self.meta,
 26            action_input: self.action_input.clone(),
 27        }
 28    }
 29}
 30
 31impl KeyBinding {
 32    /// Construct a new keybinding from the given data. Panics on parse error.
 33    pub fn new<A: Action>(keystrokes: &str, action: A, context: Option<&str>) -> Self {
 34        let context_predicate =
 35            context.map(|context| KeyBindingContextPredicate::parse(context).unwrap().into());
 36        Self::load(
 37            keystrokes,
 38            Box::new(action),
 39            context_predicate,
 40            false,
 41            None,
 42            &DummyKeyboardMapper,
 43        )
 44        .unwrap()
 45    }
 46
 47    /// Load a keybinding from the given raw data.
 48    pub fn load(
 49        keystrokes: &str,
 50        action: Box<dyn Action>,
 51        context_predicate: Option<Rc<KeyBindingContextPredicate>>,
 52        use_key_equivalents: bool,
 53        action_input: Option<SharedString>,
 54        keyboard_mapper: &dyn PlatformKeyboardMapper,
 55    ) -> std::result::Result<Self, InvalidKeystrokeError> {
 56        let keystrokes: SmallVec<[KeybindingKeystroke; 2]> = keystrokes
 57            .split_whitespace()
 58            .map(|source| {
 59                let keystroke = Keystroke::parse(source)?;
 60                Ok(KeybindingKeystroke::new_with_mapper(
 61                    keystroke,
 62                    use_key_equivalents,
 63                    keyboard_mapper,
 64                ))
 65            })
 66            .collect::<std::result::Result<_, _>>()?;
 67
 68        Ok(Self {
 69            keystrokes,
 70            action,
 71            context_predicate,
 72            meta: None,
 73            action_input,
 74        })
 75    }
 76
 77    /// Set the metadata for this binding.
 78    pub const fn with_meta(mut self, meta: KeyBindingMetaIndex) -> Self {
 79        self.meta = Some(meta);
 80        self
 81    }
 82
 83    /// Set the metadata for this binding.
 84    pub const fn set_meta(&mut self, meta: KeyBindingMetaIndex) {
 85        self.meta = Some(meta);
 86    }
 87
 88    /// Check if the given keystrokes match this binding.
 89    pub fn match_keystrokes(&self, typed: &[impl AsKeystroke]) -> Option<bool> {
 90        if self.keystrokes.len() < typed.len() {
 91            return None;
 92        }
 93
 94        for (target, typed) in self.keystrokes.iter().zip(typed.iter()) {
 95            if !typed.as_keystroke().should_match(target) {
 96                return None;
 97            }
 98        }
 99
100        Some(self.keystrokes.len() > typed.len())
101    }
102
103    /// Get the keystrokes associated with this binding
104    pub fn keystrokes(&self) -> &[KeybindingKeystroke] {
105        self.keystrokes.as_slice()
106    }
107
108    /// Get the action associated with this binding
109    pub fn action(&self) -> &dyn Action {
110        self.action.as_ref()
111    }
112
113    /// Get the predicate used to match this binding
114    pub fn predicate(&self) -> Option<Rc<KeyBindingContextPredicate>> {
115        self.context_predicate.as_ref().map(|rc| rc.clone())
116    }
117
118    /// Get the metadata for this binding
119    pub const fn meta(&self) -> Option<KeyBindingMetaIndex> {
120        self.meta
121    }
122
123    /// Get the action input associated with the action for this binding
124    pub fn action_input(&self) -> Option<SharedString> {
125        self.action_input.clone()
126    }
127}
128
129impl std::fmt::Debug for KeyBinding {
130    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131        f.debug_struct("KeyBinding")
132            .field("keystrokes", &self.keystrokes)
133            .field("context_predicate", &self.context_predicate)
134            .field("action", &self.action.name())
135            .finish()
136    }
137}
138
139/// A unique identifier for retrieval of metadata associated with a key binding.
140/// Intended to be used as an index or key into a user-defined store of metadata
141/// associated with the binding, such as the source of the binding.
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
143pub struct KeyBindingMetaIndex(pub u32);