diff --git a/crates/ui-egui/src/shortcuts/hold_keys.rs b/crates/ui-egui/src/shortcuts/hold_keys.rs index 7cb360baf8..b504be57d8 100644 --- a/crates/ui-egui/src/shortcuts/hold_keys.rs +++ b/crates/ui-egui/src/shortcuts/hold_keys.rs @@ -283,9 +283,9 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { let text_of = |k: usize| typed_by.iter().position(|by| *by == Some(k)).and_then(|j| events.get(j).map(|e| (j, e))); let mut taken: Vec = vec![]; let mut stop_at = None; - // The press of a wait that started in this frame: when the wait also ends in this frame, the - // keys typed before it run first. - let mut started: Option = None; + // The press of a wait that started in this frame, and how many events were taken before it: + // when the wait also ends in this frame, the keys typed before it run first. + let mut started: Option<(usize, usize)> = None; for (j, e) in events.iter().enumerate() { if let Some(p) = app.hold_keys.pending.as_ref().filter(|p| p.viewport == viewport) { let overdue = now - p.since >= p.delay; @@ -312,14 +312,15 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { let start = started.take(); // After a chord with Cmd, Ctrl or Alt pressed before it, which the frame's shortcuts // run, the key goes back to the frame without waiting, so the frame handles it as it - // does with Hold Shortcuts off and its tap does not run before the chord. - if let Some(start) = start + // does with Hold Shortcuts off and its tap does not run before the chord. Its press, + // its repeats and their texts stay in the frame, and its later repeats reach later + // frames. + if let Some((start, before)) = start && chord_before(&events, start, &taken) { - cancel(app, ctx); - let text = text_of(start).map(|(t, _)| t); - taken.retain(|&k| k != start && Some(k) != text); - } else if let Some(start) = start + app.hold_keys.pending = None; + taken.truncate(before); + } else if let Some((start, _)) = start && let Some(opened) = run_typed_before(app, ctx, &events, &typed_by, start, &mut taken) { // A key typed before it that opened a dialog or the palette ends the wait @@ -349,7 +350,7 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { if tool_takes(app, *key, text) { continue; } - started = Some(j); + started = Some((j, taken.len())); taken.extend([j, t]); // The key's repeats in this frame, and the text they type, stay out of the frame too. for (r, later) in events.iter().enumerate().skip(j + 1) { diff --git a/crates/ui-egui/src/shortcuts/hold_keys/tests.rs b/crates/ui-egui/src/shortcuts/hold_keys/tests.rs index e8838fcfec..09d4b41689 100644 --- a/crates/ui-egui/src/shortcuts/hold_keys/tests.rs +++ b/crates/ui-egui/src/shortcuts/hold_keys/tests.rs @@ -419,6 +419,36 @@ fn a_chord_before_a_wait_ended_in_its_frame_runs_first() { assert!(app.hold_keys.pending.is_none()); } +/// After a command chord before a wait that starts and ends in one frame, the key's repeats in +/// that frame and in later ones, and the text they type, reach the frame as with Hold Shortcuts +/// off. +#[test] +fn a_chord_before_a_wait_leaves_the_key_repeats_to_the_frame() { + let run = |app: &mut VectorcraftApp| { + app.run("paint.setFill", json!({"color": "#00ff00"})).unwrap(); + let n = steps(app, 0).len(); + let keys = Keyboard::new(); + let mut events = + vec![egui::Event::Key { key: Key::Z, physical_key: Some(Key::Z), pressed: true, repeat: false, modifiers: Modifiers::COMMAND }]; + events.extend(press(Key::X, "x", Modifiers::NONE)); + events.extend(press(Key::X, "x", Modifiers::NONE)); + events.extend(press(Key::D, "d", Modifiers::NONE)); + let first = keys.frame_left(app, 0.0, events); + let mut events = press(Key::X, "x", Modifiers::NONE); + events.extend([release(Key::X), release(Key::D)]); + let second = keys.frame_left(app, 0.05, events); + (steps(app, n), first, second, fill(app)) + }; + let mut off = swap_on_x(); + off.run("prefs.set", json!({"key": "holdShortcuts", "value": false})).unwrap(); + let off = run(&mut off); + let mut app = swap_on_x(); + let on = run(&mut app); + assert_eq!(on, off); + assert!(app.hold_keys.pending.is_none()); + assert!(app.hold_keys.swallowed.is_empty()); +} + /// The repeats of a key in the frame that starts its wait, and the text they type, stay out of the /// frame. #[test]