From 2caaabbc8fc62edf611b0869f1380e9db4c67d2e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 12:47:02 -0800 Subject: [PATCH] Shortcuts: a chord before a key with a hold shortcut keeps its order in a frame When a key with a hold shortcut is pressed and released in the same frame after a chord with Cmd, Ctrl or Alt, the key no longer waits: it goes back to the frame, which handles it as it does with Hold Shortcuts off. Its tap no longer runs before the chord, so Cmd+Z followed by X in one frame no longer undoes X's change instead of the earlier edit. --- crates/ui-egui/src/shortcuts/hold_keys.rs | 31 +++++++++++++++---- .../ui-egui/src/shortcuts/hold_keys/tests.rs | 24 ++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/shortcuts/hold_keys.rs b/crates/ui-egui/src/shortcuts/hold_keys.rs index fd8f8842fd..8bec32c8bd 100644 --- a/crates/ui-egui/src/shortcuts/hold_keys.rs +++ b/crates/ui-egui/src/shortcuts/hold_keys.rs @@ -208,6 +208,15 @@ fn tool_takes(app: &mut VectorcraftApp, key: Key, text: &str) -> bool { tool_key.is_some_and(|k| app.session.tool_claims_key(k, view)) } +/// Is there a press with Cmd, Ctrl or Alt before the event at `start` that no wait took? +fn chord_before(events: &[egui::Event], start: usize, taken: &[usize]) -> bool { + events.iter().enumerate().take(start).any(|(k, e)| { + !taken.contains(&k) + && matches!(e, egui::Event::Key { key, pressed: true, modifiers: m, .. } + if !super::is_modifier(*key) && (m.command || m.ctrl || m.alt || m.mac_cmd)) + }) +} + /// Runs the single-key shortcuts of the texts typed before the event at `start` that no wait took, /// in order, as the frame's shortcuts would run them, and takes them and their presses from the /// frame. A text typed with Cmd, Ctrl or Alt, by a key the active tool takes, or with no shortcut, @@ -259,7 +268,8 @@ fn run_typed_before( /// went down; and a new press of a key with a hold shortcut starts a wait, taking the press and /// its text from the frame, with the key's repeats that follow in the frame and their text. When a /// wait that started in this frame also ends in it, the keys typed before its key run first -/// ([`run_typed_before`]), so they act in the order they went down. Then a wait +/// ([`run_typed_before`]), so they act in the order they went down; after a chord with Cmd, Ctrl +/// or Alt before its key, the key goes back to the frame instead ([`chord_before`]). Then a wait /// whose delay has passed with the key down runs the hold shortcut. True when a result opened a /// dialog or the palette: the rest of the frame's keys are dropped and no other shortcut runs. pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { @@ -299,16 +309,25 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { } }; if let Some((hold, down)) = ended { - // A key typed before it that opened a dialog or the palette ends the wait without - // running it. - if let Some(start) = started.take() + 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 + && 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 && 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 + // without running it. cancel(app, ctx); stop_at = Some(opened); break; - } - if settle(app, ctx, hold, down) { + } else if settle(app, ctx, hold, down) { stop_at = Some(j); break; } diff --git a/crates/ui-egui/src/shortcuts/hold_keys/tests.rs b/crates/ui-egui/src/shortcuts/hold_keys/tests.rs index 1413e80e97..13255e273c 100644 --- a/crates/ui-egui/src/shortcuts/hold_keys/tests.rs +++ b/crates/ui-egui/src/shortcuts/hold_keys/tests.rs @@ -395,6 +395,30 @@ fn a_wait_ended_in_its_frame_runs_after_the_keys_before_it() { assert_eq!(selected(&app), 0, "then the chord"); } +/// A command chord (Undo) pressed before a wait that starts and ends in one frame runs first: the +/// key's tap does not run before it, and the frame acts as it does with Hold Shortcuts off. +#[test] +fn a_chord_before_a_wait_ended_in_its_frame_runs_first() { + assert_eq!(crate::shortcut_editor::default_of("edit.undo"), Some("Cmd+Z")); + let frame = |app: &mut VectorcraftApp| { + app.run("paint.setFill", json!({"color": "#00ff00"})).unwrap(); + 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.push(release(Key::X)); + Keyboard::new().frame(app, 0.0, events); + (fill(app), selected(app)) + }; + let mut off = swap_on_x(); + off.run("prefs.set", json!({"key": "holdShortcuts", "value": false})).unwrap(); + let off = frame(&mut off); + let mut app = swap_on_x(); + let on = frame(&mut app); + assert_eq!(on.0, "#ff0000", "Undo restores the red fill and does not undo X's swap"); + assert_eq!(on, off); + assert!(app.hold_keys.pending.is_none()); +} + /// The repeats of a key in the frame that starts its wait, and the text they type, stay out of the /// frame. #[test]