From 1385f0ff3d290e15bb3aa80cf3dd4ce97fe61d47 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 11:21:26 +0700 Subject: [PATCH] ui-contract: a held Escape cancels once: its repeats raise no Cancel, focused or not --- crates/ui-contract/src/runtime/keys/route.rs | 3 ++- crates/ui-panels/tests/it/focus/keys.rs | 26 ++++++++++++++++++++ docs/craft-framework.md | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 332d0cd1ca..6fa1b36212 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -136,7 +136,8 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { withdraw_taken(ctx, target.as_ref(), &batch, &held, &withdrawn_texts); let pasted = batch.paste.as_deref().zip(focused).map(|(text, id)| (id, Arc::::from(text))); // PLAN: a repeat Tab of a held-over key is not gated; `take_nav` reads `batch.tabs` before this hook runs (owner: focus). - let kept: Vec<&EscapePress> = batch.escapes.iter().filter(|escape| !(escape.repeat && held_over.contains(&Key::Escape))).collect(); + // One Escape press cancels once: its repeats cancel nothing more, in the layer it closed or in what that uncovered. + let kept: Vec<&EscapePress> = batch.escapes.iter().filter(|escape| !escape.repeat).collect(); let escapes = craft_behavior::focus::route_escapes(kept.len(), press_captured); let mut cancel_mods = None; // With a captured press the first kept Escape is the press's; the others are the components' cancels. diff --git a/crates/ui-panels/tests/it/focus/keys.rs b/crates/ui-panels/tests/it/focus/keys.rs index e751535fbd..40180901fe 100644 --- a/crates/ui-panels/tests/it/focus/keys.rs +++ b/crates/ui-panels/tests/it/focus/keys.rs @@ -36,6 +36,32 @@ fn the_first_arrow_and_escape_after_a_focus_change_act_as_intents() { assert_eq!(focused(&frames.ctx), Some(id("a")), "Escape cleared no egui focus"); } +/// A held Escape cancels once, focused or not: its repeats raise no `Cancel`, and a fresh press after its release does. +#[trial] +fn a_held_escape_cancels_once() { + use craft_ui::testing::focus::key_release; + for target in [Some(id("a")), None] { + let mut frames = Frames::new(); + let cancels = Cell::new(0); + let mut show = |ui: &mut egui::Ui| { + control(ui, "a"); + cancels.set(cancels.get() + usize::from(intents_of(ui.ctx(), target, false).take(Intent::Cancel))); + }; + frames.idle(2, &mut show); + if target.is_some() { + focus(&mut frames, "a", &mut show); + } + frames.frame(vec![key_press(Key::Escape, Modifiers::NONE)], &mut show); + for _ in 0..3 { + frames.frame(vec![key_repeat(Key::Escape, Modifiers::NONE)], &mut show); + } + assert_eq!(cancels.get(), 1, "{target:?}: the press cancels and its repeats do not"); + frames.frame(vec![key_release(Key::Escape, Modifiers::NONE)], &mut show); + frames.frame(vec![key_press(Key::Escape, Modifiers::NONE)], &mut show); + assert_eq!(cancels.get(), 2, "{target:?}: a fresh press cancels again"); + } +} + /// Static claims hold in the frame after focus arrives; the chord is withheld from egui and read once through /// `take_claimed`, and a claimed Escape is no `Cancel`. #[trial] diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 4443a80b27..19276c95c0 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -153,7 +153,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | --- | --- | | Tab | Bare Tab moves framework focus and never reaches egui, unless the focused framework stop keeps Tab or an app's own widget has the focus (below), or nothing has the focus in a window that registered no focus stop: egui then moves its own focus into the app's widgets. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus framework controls the platform's rule skips. With `RootEnv::tab_toggles_chrome`, Tab toggles the chrome in a window that shows a document canvas, with nothing focused or the canvas focused, and arrives in `FrameOutput::chrome_toggles`. The canvas is a framework canvas, or the app's own egui canvas declared each pass with `ui::runtime::focus::host_canvas`. With nothing focused, the canvas is the key target: a canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) gets the press in egui's input, as when it is focused, and nothing toggles. A focused canvas whose editor part keeps Tab reads Shift+Tab as `Intent::BackTab` (`EditorInput::BackTab`); no other stop gets it as an intent, and a text field keeps Shift+Tab as a focus move. | | F6 | F6 and Shift+F6 move between panes and never reach egui. They are the defaults of the commands `window.nextPane` and `window.previousPane` (`NavCommand`): a default chord that another command binds is that command's, and the overrides move or unbind them (`CommandInput::overrides`). An app whose own shortcut table keeps F6 unbinds them there. | -| Escape | Plain Escape (Alt and Shift may be held) goes to the focused framework stop and its scopes; with no framework stop focused, it goes to the window as `Intent::Cancel`. It reaches egui only while an app's own widget has the focus (below). An Escape the runtime takes outside a captured framework press also ends egui's own drag in that pass, as egui's Escape does. | +| Escape | Plain Escape (Alt and Shift may be held) goes to the focused framework stop and its scopes; with no framework stop focused, it goes to the window as `Intent::Cancel`. A held Escape cancels once: its repeats raise no `Cancel`. It reaches egui only while an app's own widget has the focus (below). An Escape the runtime takes outside a captured framework press also ends egui's own drag in that pass, as egui's Escape does. | | F10, Alt+letter | Taken while a framework menu bar is registered: F10 (`window.focusMenuBar`, bound and yielded as F6 is) focuses the bar, Alt+letter opens a menu (not on macOS) unless the keymap binds it to an enabled command, which runs instead. | | Edit chords | Taken while a framework editor is focused. With nothing focused, the edit chords and egui's copy, cut and paste events of the commands in the window canvas's edit set (`KeyUse::edits`, `host_canvas`) stay in egui's input for it, and no command fires with them. Otherwise egui's copy, cut and paste events become the ⌘C/X/V chord for the app's commands, unless an app's own widget has the focus (below). While egui holds a label text selection, copy and cut stay egui's, and egui copies the selection. | | IME | Taken while a framework editor owns the IME: its events and `PlatformOutput.ime` are the runtime's. Otherwise they stay egui's, for the app's own `TextEdit` or IME code. |