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