Skip to content

Commit 7b50184

Browse files
committed
Update
1 parent 03f46c4 commit 7b50184

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

editor/src/messages/input_preprocessor/input_preprocessor_message_handler.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct InputPreprocessorMessageHandler {
1818
pub keyboard: KeyStates,
1919
pub mouse: MouseState,
2020
pub last_key_down: Option<(Key, u64)>, // (Key, timestamp)
21+
pub double_tap_key: Option<Key>,
2122
}
2223

2324
#[message_handler_data]
@@ -48,14 +49,12 @@ impl<'a> MessageHandler<InputPreprocessorMessage, InputPreprocessorMessageContex
4849
self.keyboard.set(key as usize);
4950

5051
if !key_repeat {
51-
let is_double_tap = if let Some((last_key, last_time)) = self.last_key_down {
52-
last_key == key && self.time.saturating_sub(last_time) < DOUBLE_CLICK_MILLISECONDS
53-
} else {
54-
false
55-
};
52+
let is_double_tap = self
53+
.last_key_down
54+
.is_some_and(|(last_key, last_time)| last_key == key && self.time.saturating_sub(last_time) < DOUBLE_CLICK_MILLISECONDS);
5655

5756
if is_double_tap {
58-
responses.add(InputMapperMessage::DoubleTap(key));
57+
self.double_tap_key = Some(key);
5958
self.last_key_down = None;
6059
} else {
6160
self.last_key_down = Some((key, self.time));
@@ -71,6 +70,10 @@ impl<'a> MessageHandler<InputPreprocessorMessage, InputPreprocessorMessageContex
7170
if !key_repeat {
7271
responses.add(InputMapperMessage::KeyUpNoRepeat(key));
7372
}
73+
if self.double_tap_key == Some(key) {
74+
responses.add(InputMapperMessage::DoubleTap(key));
75+
self.double_tap_key = None;
76+
}
7477
responses.add(InputMapperMessage::KeyUp(key));
7578
}
7679
InputPreprocessorMessage::PointerDown { editor_mouse_state, modifier_keys } => {
@@ -324,23 +327,46 @@ mod test {
324327
);
325328
}
326329

330+
fn key_up(input_preprocessor: &mut InputPreprocessorMessageHandler, key: Key, responses: &mut VecDeque<Message>) {
331+
input_preprocessor.process_message(
332+
InputPreprocessorMessage::KeyUp {
333+
key,
334+
key_repeat: false,
335+
modifier_keys: ModifierKeys::empty(),
336+
},
337+
responses,
338+
InputPreprocessorMessageContext {
339+
viewport: &ViewportMessageHandler::default(),
340+
},
341+
);
342+
}
343+
327344
#[test]
328345
fn process_double_tap_within_threshold() {
329346
let mut input_preprocessor = InputPreprocessorMessageHandler::default();
330347
let mut responses = VecDeque::new();
331348

332349
// First tap at time 0
333350
key_down(&mut input_preprocessor, Key::Space, &mut responses);
351+
key_up(&mut input_preprocessor, Key::Space, &mut responses);
334352
responses.clear();
335353

336354
// Second tap within threshold
337355
input_preprocessor.time = 50;
338356
key_down(&mut input_preprocessor, Key::Space, &mut responses);
339357

340-
assert!(responses.contains(&InputMapperMessage::DoubleTap(Key::Space).into()));
358+
assert!(!responses.contains(&InputMapperMessage::DoubleTap(Key::Space).into()));
341359
assert!(responses.contains(&InputMapperMessage::KeyDown(Key::Space).into()));
342360
assert!(responses.contains(&InputMapperMessage::KeyDownNoRepeat(Key::Space).into()));
343361
assert!(input_preprocessor.last_key_down.is_none());
362+
assert_eq!(input_preprocessor.double_tap_key, Some(Key::Space));
363+
364+
responses.clear();
365+
key_up(&mut input_preprocessor, Key::Space, &mut responses);
366+
367+
assert!(responses.contains(&InputMapperMessage::DoubleTap(Key::Space).into()));
368+
assert!(input_preprocessor.last_key_down.is_none());
369+
assert!(input_preprocessor.double_tap_key.is_none());
344370
}
345371

346372
#[test]
@@ -350,6 +376,7 @@ mod test {
350376

351377
// First tap at time 0
352378
key_down(&mut input_preprocessor, Key::Space, &mut responses);
379+
key_up(&mut input_preprocessor, Key::Space, &mut responses);
353380
responses.clear();
354381

355382
// Second tap outside threshold
@@ -360,5 +387,11 @@ mod test {
360387
assert!(responses.contains(&InputMapperMessage::KeyDown(Key::Space).into()));
361388
assert!(responses.contains(&InputMapperMessage::KeyDownNoRepeat(Key::Space).into()));
362389
assert_eq!(input_preprocessor.last_key_down, Some((Key::Space, DOUBLE_CLICK_MILLISECONDS + 1)));
390+
assert!(input_preprocessor.double_tap_key.is_none());
391+
392+
responses.clear();
393+
key_up(&mut input_preprocessor, Key::Space, &mut responses);
394+
395+
assert!(!responses.contains(&InputMapperMessage::DoubleTap(Key::Space).into()));
363396
}
364397
}

0 commit comments

Comments
 (0)