@@ -148,7 +148,6 @@ impl FromStr for Mods {
148
148
}
149
149
}
150
150
151
- // TODO: Rename this to `KeyEvent`
152
151
#[ derive( Clone , PartialEq , Eq , Debug ) ]
153
152
pub struct KeyInput {
154
153
key : Key ,
@@ -257,7 +256,7 @@ impl<A> KeyBinds<A> {
257
256
#[ derive( Default ) ]
258
257
pub struct KeyBindMatcher < A > {
259
258
binds : KeyBinds < A > ,
260
- current : Vec < KeyInput > ,
259
+ ongoing : Vec < KeyInput > ,
261
260
last_input : Option < Instant > ,
262
261
timeout : Duration ,
263
262
}
@@ -266,7 +265,7 @@ impl<A> KeyBindMatcher<A> {
266
265
pub fn new ( binds : KeyBinds < A > ) -> Self {
267
266
Self {
268
267
binds,
269
- current : vec ! [ ] ,
268
+ ongoing : vec ! [ ] ,
270
269
last_input : None ,
271
270
timeout : Duration :: from_secs ( 1 ) ,
272
271
}
@@ -278,31 +277,31 @@ impl<A> KeyBindMatcher<A> {
278
277
}
279
278
280
279
pub fn reset ( & mut self ) {
280
+ self . ongoing . clear ( ) ;
281
281
self . last_input = None ;
282
- self . current . clear ( ) ;
283
282
}
284
283
285
284
fn handle_timeout ( & mut self ) {
286
285
let now = Instant :: now ( ) ;
287
- let timeout = self
286
+ let is_timeout = self
288
287
. last_input
289
288
. 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 ( ) ;
294
291
}
292
+ self . last_input = Some ( now) ;
295
293
}
296
294
297
295
pub fn trigger < I : Into < KeyInput > > ( & mut self , input : I ) -> Option < & A > {
298
296
self . handle_timeout ( ) ;
299
- self . current . push ( input. into ( ) ) ;
297
+ self . ongoing . push ( input. into ( ) ) ;
300
298
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 ) ?;
302
301
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 ( ) ;
304
304
self . last_input = None ;
305
- self . current . clear ( ) ;
306
305
Some ( action)
307
306
}
308
307
}
0 commit comments