|
|
@@ -45,14 +45,12 @@ impl Arg { |
| 45 |
45 |
} |
| 46 |
46 |
} |
| 47 |
47 |
|
| 48 |
|
-// Individual key press in a sequence |
| 49 |
48 |
#[derive(Clone, Debug)] |
| 50 |
49 |
pub struct KeyPress { |
| 51 |
50 |
pub(crate) modifiers: Vec<KeyButMask>, |
| 52 |
51 |
pub(crate) key: Keycode, |
| 53 |
52 |
} |
| 54 |
53 |
|
| 55 |
|
-// Keybinding that can be a single key or a chord (sequence of keys) |
| 56 |
54 |
#[derive(Clone)] |
| 57 |
55 |
pub struct KeyBinding { |
| 58 |
56 |
pub(crate) keys: Vec<KeyPress>, |
|
|
@@ -65,7 +63,6 @@ impl KeyBinding { |
| 65 |
63 |
Self { keys, func, arg } |
| 66 |
64 |
} |
| 67 |
65 |
|
| 68 |
|
- // Helper for backwards compatibility with single-key bindings |
| 69 |
66 |
pub fn single_key(modifiers: Vec<KeyButMask>, key: Keycode, func: KeyAction, arg: Arg) -> Self { |
| 70 |
67 |
Self { |
| 71 |
68 |
keys: vec![KeyPress { modifiers, key }], |
|
|
@@ -75,16 +72,13 @@ impl KeyBinding { |
| 75 |
72 |
} |
| 76 |
73 |
} |
| 77 |
74 |
|
| 78 |
|
-// For backwards compatibility during migration |
| 79 |
75 |
pub type Key = KeyBinding; |
| 80 |
76 |
|
| 81 |
77 |
#[derive(Debug, Clone)] |
| 82 |
78 |
pub enum KeychordState { |
| 83 |
79 |
Idle, |
| 84 |
80 |
InProgress { |
| 85 |
|
- // Indices of keybindings that could still match |
| 86 |
81 |
candidates: Vec<usize>, |
| 87 |
|
- // How many keys have been pressed in the sequence |
| 88 |
82 |
keys_pressed: usize, |
| 89 |
83 |
}, |
| 90 |
84 |
} |
|
|
@@ -155,19 +149,15 @@ pub fn handle_key_press( |
| 155 |
149 |
} |
| 156 |
150 |
|
| 157 |
151 |
match keychord_state { |
| 158 |
|
- KeychordState::Idle => { |
| 159 |
|
- handle_first_key(event, keybindings) |
| 160 |
|
- } |
| 161 |
|
- KeychordState::InProgress { candidates, keys_pressed } => { |
| 162 |
|
- handle_next_key(event, keybindings, candidates, *keys_pressed) |
| 163 |
|
- } |
|
152 |
+ KeychordState::Idle => handle_first_key(event, keybindings), |
|
153 |
+ KeychordState::InProgress { |
|
154 |
+ candidates, |
|
155 |
+ keys_pressed, |
|
156 |
+ } => handle_next_key(event, keybindings, candidates, *keys_pressed), |
| 164 |
157 |
} |
| 165 |
158 |
} |
| 166 |
159 |
|
| 167 |
|
-fn handle_first_key( |
| 168 |
|
- event: KeyPressEvent, |
| 169 |
|
- keybindings: &[KeyBinding], |
| 170 |
|
-) -> KeychordResult { |
|
160 |
+fn handle_first_key(event: KeyPressEvent, keybindings: &[KeyBinding]) -> KeychordResult { |
| 171 |
161 |
let mut candidates = Vec::new(); |
| 172 |
162 |
|
| 173 |
163 |
for (keybinding_index, keybinding) in keybindings.iter().enumerate() { |
|
|
@@ -180,10 +170,7 @@ fn handle_first_key( |
| 180 |
170 |
|
| 181 |
171 |
if event.detail == first_key.key && event.state == modifier_mask.into() { |
| 182 |
172 |
if keybinding.keys.len() == 1 { |
| 183 |
|
- return KeychordResult::Completed( |
| 184 |
|
- keybinding.func, |
| 185 |
|
- keybinding.arg.clone(), |
| 186 |
|
- ); |
|
173 |
+ return KeychordResult::Completed(keybinding.func, keybinding.arg.clone()); |
| 187 |
174 |
} else { |
| 188 |
175 |
candidates.push(keybinding_index); |
| 189 |
176 |
} |
|
|
@@ -224,10 +211,7 @@ fn handle_next_key( |
| 224 |
211 |
|
| 225 |
212 |
if event.detail == next_key.key && modifiers_match { |
| 226 |
213 |
if keys_pressed + 1 == keybinding.keys.len() { |
| 227 |
|
- return KeychordResult::Completed( |
| 228 |
|
- keybinding.func, |
| 229 |
|
- keybinding.arg.clone(), |
| 230 |
|
- ); |
|
214 |
+ return KeychordResult::Completed(keybinding.func, keybinding.arg.clone()); |
| 231 |
215 |
} else { |
| 232 |
216 |
new_candidates.push(candidate_index); |
| 233 |
217 |
} |