From ae722739ab984d9daa1f0f4c6cea1a6ea82f5cc2 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 03:28:05 +0000 Subject: [PATCH] ui-contract: presses the focused component holds leave egui's input again, and Tab stays navigation with nothing focused The merge of next (b138b533fe) passed whether a stop was focused where withdraw_taken takes whether the app's own widget reads its keys from egui, so no held press left egui's input while anything had focus. With nothing focused the document canvas's held claims leave out Tab and Shift+Tab: a tool that claims the plain keys no longer keeps keyboard navigation from moving into the window. The unfocused-canvas trial releases its keys, since a press of a key already down is a repeat. --- crates/ui-contract/src/runtime/keys/input.rs | 11 ++++++++--- crates/ui-contract/src/runtime/keys/route.rs | 4 +++- crates/ui-contract/src/testing/key_trials.rs | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index de0b670c2..b0ca7a5fc 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -541,12 +541,17 @@ enum PressUse { /// The chords the focused component of `record` claims: its stop's, the state-dependent ones (`keys::claim`) of the /// component, its arena root and its menu level, and a bare End where it declares `EndKey::EndMode`. With nothing -/// focused and no modal layer open, the window's document canvas's state-dependent claims hold instead. +/// focused and no modal layer open, the window's document canvas's state-dependent claims hold instead, except Tab and +/// Shift+Tab. pub(super) fn focused_claims(record: &focus::FocusedRecord, stage: &Stage) -> Vec { let mut claims = record.claims.clone(); let canvas = record.canvas.filter(|_| !record.foreign); - let holds = |owner: Id| [record.focused, record.root, record.menu, record.menu_bar, canvas].contains(&Some(owner)); - claims.extend(stage.claims.iter().filter(|(owner, _)| holds(*owner)).map(|(_, chord)| *chord)); + let holds = |owner: Id| [record.focused, record.root, record.menu, record.menu_bar].contains(&Some(owner)); + // The unfocused canvas's tool never takes Tab or Shift+Tab: with nothing focused they move focus into the window, + // so a tool that claims the plain keys cannot trap keyboard navigation. + let canvas_holds = + |owner: Id, chord: &Chord| Some(owner) == canvas && !(chord.key == Key::Tab && (chord.mods == Mods::NONE || chord.mods == Mods::SHIFT)); + claims.extend(stage.claims.iter().filter(|(owner, chord)| holds(*owner) || canvas_holds(*owner, chord)).map(|(_, chord)| *chord)); // A bare End belongs to a stop that declares `EndKey::EndMode`: it reads the key as a claimed chord and toggles its // mode, so no collection navigation or scroll owner takes it as a move to the end. if record.keys.is_some_and(|keys| keys.end == EndKey::EndMode) { diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index f88b186d9..a889e9d4d 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -185,7 +185,9 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { } let over: Vec = batch.presses.iter().map(held_press).collect(); let (shortcuts, held) = owned_shortcuts(ctx, target.as_ref(), &batch, &consumed, &taken_texts, &over); - withdraw_taken(ctx, target.is_some(), &batch, &held, &withdrawn_texts); + // The app's own widget, focused or the window's canvas, reads its keys from egui. + let host = target.as_ref().or(canvas.as_ref()).is_some_and(|rec| rec.stop.host); + withdraw_taken(ctx, host, &batch, &held, &withdrawn_texts); let pasted = batch.paste.clone().zip(focused).map(|(content, id)| (id, content)); // PLAN: a repeat Tab of a held-over key is not gated; `take_nav` reads `batch.tabs` before this hook runs (owner: focus). // One Escape press cancels once: its repeats cancel nothing more, in the layer it closed or in what that uncovered. diff --git a/crates/ui-contract/src/testing/key_trials.rs b/crates/ui-contract/src/testing/key_trials.rs index acc91136a..1efd74fce 100644 --- a/crates/ui-contract/src/testing/key_trials.rs +++ b/crates/ui-contract/src/testing/key_trials.rs @@ -258,7 +258,8 @@ fn an_unfocused_press_the_window_canvas_takes_reaches_no_app_shortcut() { take_output(&frames.ctx); let fired = |frames: &mut Frames, show: &mut dyn FnMut(&mut Ui)| { frames.frame(vec![key_press(Key::PageDown, Modifiers::NONE), key_press(Key::ArrowRight, Modifiers::NONE)], &mut *show); - frames.frame(Vec::new(), &mut *show); + // Released, so the next round's presses are fresh presses, not repeats of held keys. + frames.frame(vec![key_release(Key::PageDown, Modifiers::NONE), key_release(Key::ArrowRight, Modifiers::NONE)], &mut *show); take_output(&frames.ctx).fired.len() }; assert_eq!(fired(&mut frames, &mut show), 0, "neither press reaches the app's commands");