Skip to content

Commit 6d098a1

Browse files
committed
fix ongoing key sequence is not properly cleared on timeout
1 parent 5ec0769 commit 6d098a1

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/lib.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl FromStr for Mods {
148148
}
149149
}
150150

151-
// TODO: Rename this to `KeyEvent`
152151
#[derive(Clone, PartialEq, Eq, Debug)]
153152
pub struct KeyInput {
154153
key: Key,
@@ -257,7 +256,7 @@ impl<A> KeyBinds<A> {
257256
#[derive(Default)]
258257
pub struct KeyBindMatcher<A> {
259258
binds: KeyBinds<A>,
260-
current: Vec<KeyInput>,
259+
ongoing: Vec<KeyInput>,
261260
last_input: Option<Instant>,
262261
timeout: Duration,
263262
}
@@ -266,7 +265,7 @@ impl<A> KeyBindMatcher<A> {
266265
pub fn new(binds: KeyBinds<A>) -> Self {
267266
Self {
268267
binds,
269-
current: vec![],
268+
ongoing: vec![],
270269
last_input: None,
271270
timeout: Duration::from_secs(1),
272271
}
@@ -278,31 +277,31 @@ impl<A> KeyBindMatcher<A> {
278277
}
279278

280279
pub fn reset(&mut self) {
280+
self.ongoing.clear();
281281
self.last_input = None;
282-
self.current.clear();
283282
}
284283

285284
fn handle_timeout(&mut self) {
286285
let now = Instant::now();
287-
let timeout = self
286+
let is_timeout = self
288287
.last_input
289288
.is_some_and(|t| now.duration_since(t) > self.timeout);
290-
if timeout {
291-
self.reset();
292-
} else {
293-
self.last_input = Some(now);
289+
if is_timeout {
290+
self.ongoing.clear();
294291
}
292+
self.last_input = Some(now);
295293
}
296294

297295
pub fn trigger<I: Into<KeyInput>>(&mut self, input: I) -> Option<&A> {
298296
self.handle_timeout();
299-
self.current.push(input.into());
297+
self.ongoing.push(input.into());
300298

301-
let action = self.binds.find(&self.current).map(|b| &b.action)?;
299+
// TODO: When no keybind is prefix-matched, call `self.reset()`
300+
let action = self.binds.find(&self.ongoing).map(|b| &b.action)?;
302301

303-
// `self.reset()` cannot be called here because the borrow checker depends on field splitting.
302+
// `self.reset` cannot be called because the borrow checker needs to split field lifetimes.
303+
self.ongoing.clear();
304304
self.last_input = None;
305-
self.current.clear();
306305
Some(action)
307306
}
308307
}

0 commit comments

Comments
 (0)