diff --git a/crates/ui-egui/src/shortcuts/hold_keys.rs b/crates/ui-egui/src/shortcuts/hold_keys.rs index b494050bd2..fd8f8842fd 100644 --- a/crates/ui-egui/src/shortcuts/hold_keys.rs +++ b/crates/ui-egui/src/shortcuts/hold_keys.rs @@ -211,7 +211,8 @@ fn tool_takes(app: &mut VectorcraftApp, key: Key, text: &str) -> bool { /// 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, -/// is left to the frame. True when one opened a dialog or the palette. +/// is left to the frame. When one opened a dialog or the palette, the position of its first event +/// in the frame: its press, or its text where the text came first (the web build). fn run_typed_before( app: &mut VectorcraftApp, ctx: &egui::Context, @@ -219,7 +220,7 @@ fn run_typed_before( typed_by: &[Option], start: usize, taken: &mut Vec, -) -> bool { +) -> Option { for (k, e) in events.iter().enumerate().take(start) { let egui::Event::Text(text) = e else { continue }; let by = typed_by.get(k).copied().flatten(); @@ -246,10 +247,10 @@ fn run_typed_before( taken.push(k); taken.extend(by); if super::run_single_key(app, ctx, &chords, key, repeat) { - return true; + return Some(by.map_or(k, |b| b.min(k))); } } - false + None } /// Before the frame's shortcuts run: goes through the frame's events in order. A release of the @@ -301,10 +302,10 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { // A key typed before it that opened a dialog or the palette ends the wait without // running it. if let Some(start) = started.take() - && run_typed_before(app, ctx, &events, &typed_by, start, &mut taken) + && let Some(opened) = run_typed_before(app, ctx, &events, &typed_by, start, &mut taken) { cancel(app, ctx); - stop_at = Some(j); + stop_at = Some(opened); break; } if settle(app, ctx, hold, down) { @@ -351,7 +352,8 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool { app.hold_keys.pending = Some(Pending { entry, chords, key: *key, physical: *physical_key, mods: *m, since: now, delay, viewport }); } // Take the waiting presses and their texts, and after a result that opened a dialog, the key - // event that ended the wait and every later key event and text of the frame, with the text a + // event that ended the wait (or the typed key that opened the dialog, when that came earlier) + // and every later key event and text of the frame, with the text a // press typed before it (the web build sends a press's text first): none of them reaches the // dialog. ctx.input_mut(|i| { diff --git a/crates/ui-egui/src/shortcuts/hold_keys/tests.rs b/crates/ui-egui/src/shortcuts/hold_keys/tests.rs index 4e745510fe..1413e80e97 100644 --- a/crates/ui-egui/src/shortcuts/hold_keys/tests.rs +++ b/crates/ui-egui/src/shortcuts/hold_keys/tests.rs @@ -587,6 +587,24 @@ fn the_key_that_ends_a_wait_does_not_reach_the_dialog_it_opened() { assert_eq!(fill(&app), "#ff0000"); } +/// A key typed before a wait that starts and ends in one frame, and that opens a dialog, keeps the +/// frame's later keys from the dialog, the ones before the waiting key included. +#[test] +fn a_key_typed_before_a_wait_does_not_let_later_keys_reach_its_dialog() { + let mut app = app_with(true); + app.run("shortcuts.set", json!({"id": "edit.preferences", "shortcut": "D", "force": true})).unwrap(); + app.run("shortcuts.set", json!({"id": "paint.swap", "hold": "X"})).unwrap(); + let keys = Keyboard::new(); + let mut events = press(Key::D, "d", Modifiers::NONE); + events.push(egui::Event::Key { key: Key::Enter, physical_key: Some(Key::Enter), pressed: true, repeat: false, modifiers: Modifiers::NONE }); + events.extend(press(Key::X, "x", Modifiers::NONE)); + events.push(release(Key::X)); + let left = keys.frame_left(&mut app, 0.0, events); + assert!(app.ui.dialog.is_some(), "D opened Preferences"); + assert!(!left.iter().any(|e| matches!(e, egui::Event::Key { key: Key::Enter, .. })), "{left:?}"); + assert_eq!(fill(&app), "#ff0000"); +} + /// On the web build a press's text comes before it: the text of the key that ends such a wait /// does not reach the dialog either. #[test]