Shortcuts: a tool key before a hold key in one frame reaches its tool first

When a key the active tool takes (E with the Extend Path tool) went
down before a key with a hold shortcut that was pressed and let go in
the same frame, the hold key's tap ran first. A tap that chose another
tool then sent the earlier key to the new tool. The hold key now goes
back to the frame in that case, as it does after a chord with Cmd, Ctrl
or Alt, so both keys act in order as with Hold Shortcuts off.
This commit is contained in:
dotCooCoo
2026-11-17 07:19:14 -08:00
parent e6bf84050a
commit c886ab1698
2 changed files with 44 additions and 12 deletions
+23 -12
View File
@@ -208,12 +208,22 @@ 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))
/// Is there a press before the event at `start` that no wait took and that the frame handles: one
/// with Cmd, Ctrl or Alt, or one that typed a text of a key the active tool takes ([`tool_takes`])?
fn chord_before(app: &mut VectorcraftApp, events: &[egui::Event], typed_by: &[Option<usize>], start: usize, taken: &[usize]) -> bool {
let mut before = events.iter().enumerate().take(start).filter(|(k, _)| !taken.contains(k));
before.any(|(k, e)| match e {
egui::Event::Key { key, pressed: true, modifiers: m, .. } if !super::is_modifier(*key) => {
m.command
|| m.ctrl
|| m.alt
|| m.mac_cmd
|| typed_by
.iter()
.enumerate()
.any(|(t, by)| *by == Some(k) && matches!(events.get(t), Some(egui::Event::Text(text)) if tool_takes(app, *key, text)))
}
_ => false,
})
}
@@ -269,7 +279,8 @@ fn run_typed_before(
/// 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; after a chord with Cmd, Ctrl
/// or Alt before its key, the key goes back to the frame instead ([`chord_before`]). Then a wait
/// or Alt, or a key the active tool takes, 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 {
@@ -311,12 +322,12 @@ pub(super) fn intake(app: &mut VectorcraftApp, ctx: &egui::Context) -> bool {
if let Some((hold, down)) = ended {
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. Its press,
// its repeats and their texts stay in the frame, and its later repeats reach later
// frames.
// run, or a key the active tool takes, 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 that key. 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)
&& chord_before(app, &events, &typed_by, start, &taken)
{
app.hold_keys.pending = None;
taken.truncate(before);
@@ -424,6 +424,27 @@ fn a_chord_before_a_wait_ended_in_its_frame_runs_first() {
assert!(app.hold_keys.pending.is_none());
}
/// A key the active tool takes (E with the Extend Path tool), pressed before a wait that starts and
/// ends in one frame, reaches the tool first: the key's tap, which chooses another tool, does not
/// run before it, and the frame acts as it does with Hold Shortcuts off.
#[test]
fn a_tool_key_before_a_wait_ended_in_its_frame_reaches_the_tool_first() {
let frame = |app: &mut VectorcraftApp| {
app.select_tool(vectorcraft_tools::addons::extend_path::ID);
let mut events = press(Key::E, "e", Modifiers::NONE);
events.extend(press(Key::X, "x", Modifiers::NONE));
events.push(release(Key::X));
Keyboard::new().frame(app, 0.0, events);
(app.session.tool_id().to_string(), fill(app))
};
let off = frame(&mut erolka_x(false));
let mut app = erolka_x(true);
let on = frame(&mut app);
assert_eq!(on.0, "eyedropper", "X's tap chooses the Eyedropper after E reached the Extend Path tool");
assert_eq!(on, off);
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.