| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use crate::{ |
| 4 | Action, AsKeystroke, DummyKeyboardMapper, InvalidKeystrokeError, KeyBindingContextPredicate, |
| 5 | KeybindingKeystroke, Keystroke, PlatformKeyboardMapper, SharedString, |
| 6 | }; |
| 7 | use smallvec::SmallVec; |
| 8 | |
| 9 | |
| 10 | pub 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 | |
| 16 | pub(crate) action_input: Option<SharedString>, |
| 17 | } |
| 18 | |
| 19 | impl 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 | |
| 31 | impl KeyBinding { |
| 32 | |
| 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 | |
| 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 | |
| 78 | pub fn with_meta(mut self, meta: KeyBindingMetaIndex) -> Self { |
| 79 | self.meta = Some(meta); |
| 80 | self |
| 81 | } |
| 82 | |
| 83 | |
| 84 | pub fn set_meta(&mut self, meta: KeyBindingMetaIndex) { |
| 85 | self.meta = Some(meta); |
| 86 | } |
| 87 | |
| 88 | |
| 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 | |
| 104 | pub fn keystrokes(&self) -> &[KeybindingKeystroke] { |
| 105 | self.keystrokes.as_slice() |
| 106 | } |
| 107 | |
| 108 | |
| 109 | pub fn action(&self) -> &dyn Action { |
| 110 | self.action.as_ref() |
| 111 | } |
| 112 | |
| 113 | |
| 114 | pub fn predicate(&self) -> Option<Rc<KeyBindingContextPredicate>> { |
| 115 | self.context_predicate.as_ref().map(|rc| rc.clone()) |
| 116 | } |
| 117 | |
| 118 | |
| 119 | pub fn meta(&self) -> Option<KeyBindingMetaIndex> { |
| 120 | self.meta |
| 121 | } |
| 122 | |
| 123 | |
| 124 | pub fn action_input(&self) -> Option<SharedString> { |
| 125 | self.action_input.clone() |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl 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 | |
| 140 | |
| 141 | |
| 142 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 143 | pub struct KeyBindingMetaIndex(pub u32); |