From 059d9b17d64f71e7398a488ab61f3deae293a2a1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 08:03:41 +0000 Subject: [PATCH 1/2] Tab after a press on a component toggles the panels in a canvas window: the focused stop keeps how focus reached it; focus_holds names a Tab that moves focus; typing lets only command chords and function keys fire --- crates/keymap/src/keymap/command.rs | 5 ++- crates/ui-contract/src/runtime/focus.rs | 10 ++++- crates/ui-contract/src/runtime/keys/input.rs | 34 +++++++++++----- crates/ui-contract/src/runtime/keys/route.rs | 9 +++-- crates/ui-contract/src/testing/key_trials.rs | 42 +++++++++++++++++--- 5 files changed, 80 insertions(+), 20 deletions(-) diff --git a/crates/keymap/src/keymap/command.rs b/crates/keymap/src/keymap/command.rs index 47019ebe6..e20a87fbd 100644 --- a/crates/keymap/src/keymap/command.rs +++ b/crates/keymap/src/keymap/command.rs @@ -170,9 +170,10 @@ impl CommandDef { #[non_exhaustive] pub enum TypingRule { /// Plain chords (no cmd, ctrl or alt) go to the text; function keys still fire. - #[default] PlainKeysWait, - /// Only chords with the command modifier and function keys fire. + /// Only chords with the command modifier and function keys fire: Alt and Shift chords type characters on some + /// layouts (Option on the Mac), so they stay with the text, as in Photoshop. + #[default] CommandKeysOnly, /// Nothing fires. AllWait, diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index b2c78d9f5..8448fdb84 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -156,7 +156,8 @@ pub(crate) struct State { pub(crate) press: press::PressState, /// The frame whose begin hook ran last; a move in the end hook belongs to the next frame. pub(super) frame: u64, - /// The stop focus moved onto and the frame whose passes see it. + /// The stop focus last moved onto, the frame whose passes see the move, and what moved it there. It stays until focus + /// moves again: while the stop keeps the focus, it is how the stop got it (`origin`). pub(super) arrived: Option<(u64, StopKey, FocusOrigin)>, /// The running pass's begin hook left a Tab to egui (`NavKeys::tab_to_egui`); its end hook takes it. pub(super) tab_to_egui: bool, @@ -671,6 +672,13 @@ pub(crate) fn facts(ctx: &Context, id: Id) -> Facts { } } +/// What moved focus onto the focused stop `rec`, for as long as it keeps the focus; `None` when the focus service did +/// not move it there (an app's own widget took egui focus). +pub(crate) fn origin(ctx: &Context, rec: &StopRec) -> Option { + let key = rec.stop.key(); + with(ctx, |state| state.arrived.filter(|(_, arrived, _)| *arrived == key).map(|(.., origin)| origin)) +} + /// `is_focused` and focus arrived by keyboard, so the ring shows. pub(crate) fn focus_visible(ctx: &Context, id: Id) -> bool { is_focused(ctx, id) && with(ctx, |state| state.visible) diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index c08780c68..ec63de0ec 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -364,16 +364,36 @@ pub(super) fn focused_claims(record: &focus::FocusedRecord, stage: &Stage) -> Ve /// canvas (a stop with `KeyUse::chrome_key`) in its last pass; `layer` is what the layer stack says about the focus /// and `focused` is the `KeyUse` of the focused component, `None` when nothing is focused. /// +/// `pressed` is whether a pointer press moved focus onto the focused component (`focus::origin`). +/// /// A modal or dialog layer never toggles: Tab moves focus inside it. In a window with a canvas the toggle is on when -/// nothing is focused or the canvas is; elsewhere it is on only when the canvas is focused. A component that keeps -/// Tab never reaches this function: the press is its own. -fn tab_toggles_chrome(enabled: bool, canvas: bool, layer: overlay::LayerKeys, focused: Option<&KeyUse>) -> bool { +/// nothing is focused, the canvas is, or a press focused the component, as clicking a panel's row and pressing Tab hides +/// the panels in Photoshop; Tab moves focus from a component the keyboard or a request focused. Elsewhere it is on only +/// when the canvas is focused. A component that keeps Tab never reaches this function: the press is its own. +fn tab_toggles_chrome(enabled: bool, canvas: bool, layer: overlay::LayerKeys, focused: Option<&KeyUse>, pressed: bool) -> bool { let closed = matches!(layer, overlay::LayerKeys::Blocked | overlay::LayerKeys::Class(OwnerClass::Dialog)); if !enabled || closed { return false; } let on_canvas = focused.is_some_and(|keys| keys.chrome_key); - if canvas { focused.is_none() || on_canvas } else { on_canvas } + if canvas { focused.is_none() || on_canvas || pressed } else { on_canvas } +} + +/// Whether a bare Tab pressed now in the running viewport would toggle its panels rather than move focus, by +/// [`tab_toggles_chrome`] over the viewport's root env, canvas, layers and focus. +pub(super) fn tab_toggles_now(ctx: &Context) -> bool { + let viewport = crate::runtime::egui::pass::current_viewport(ctx); + let enabled = Env::root_for(ctx, viewport).is_some_and(|env| env.tab_toggles_chrome); + let focused = focus::focused_rec(ctx); + let canvas = focus::canvas_keys(ctx, viewport).is_some(); + let layer = overlay::key_layer(ctx, &focused.as_ref().map(|rec| focus::scope_chain(ctx, rec)).unwrap_or_default(), None); + let pressed = focused.as_ref().is_some_and(|rec| focus::origin(ctx, rec) == Some(focus::FocusOrigin::Press)); + tab_toggles_chrome(enabled, canvas, layer, focused.as_ref().map(|rec| &rec.stop.keys), pressed) +} + +/// Whether `chord` is the key of a chrome command (`ChromeCommand`), as the window's keymap binds it. +pub(super) fn is_chrome_chord(ctx: &Context, chord: &Chord) -> bool { + commands::window_shortcuts(ctx).iter().any(|(key, bound)| matches!(key, WindowShortcut::Chrome(_)) && bound == chord) } /// The toggle of the chrome command whose bound chord `input` presses, when that command is enabled. @@ -516,11 +536,7 @@ pub(crate) fn take_nav(ctx: &Context) -> NavKeys { return nav; } let viewport = crate::runtime::egui::pass::current_viewport(ctx); - let enabled = Env::root_for(ctx, viewport).is_some_and(|env| env.tab_toggles_chrome); - let focused = focus::focused_rec(ctx); - let canvas = focus::canvas_keys(ctx, viewport).is_some(); - let layer = overlay::key_layer(ctx, &focused.as_ref().map(|rec| focus::scope_chain(ctx, rec)).unwrap_or_default(), None); - let gate = tab_toggles_chrome(enabled, canvas, layer, focused.as_ref().map(|rec| &rec.stop.keys)); + let gate = tab_toggles_now(ctx); for press in tabs { match gate.then(|| chrome_toggle_of(ctx, &press.input)).flatten() { Some(toggle) if !press.repeat => request_chrome_toggle(ctx, viewport, toggle), diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index afd8cc49d..2c435f877 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -297,8 +297,9 @@ fn text_of(chord: Chord) -> Option<&'static str> { /// Whether the focused component of the running viewport holds a press of `chord`, as routing decides for the press: /// it or its menu claims the chord, its routing takes the press or the text the press types (a list's type-ahead), or a -/// menu or recorder has the keys. No app command bound to the chord runs while the component has the focus, and the press -/// reaches egui's input only for the app's own widget (`Stop::host`), which reads its keys there. +/// menu or recorder has the keys, or the chord is a bare Tab that moves focus from it rather than toggling the panels +/// (`tab_toggles_now`). No app command bound to the chord runs while the component has the focus, and the press reaches +/// egui's input only for the app's own widget (`Stop::host`), which reads its keys there. pub fn focus_holds(ctx: &Context, chord: Chord) -> bool { let Some(rec) = focus::focused_rec(ctx) else { return false }; let viewport = crate::runtime::egui::pass::current_viewport(ctx); @@ -312,7 +313,9 @@ pub fn focus_holds(ctx: &Context, chord: Chord) -> bool { let taken = !matches!(route, PressRoute::Free); let typed = text_of(chord).is_some_and(|text| !text_route(&rec, driven, text).is_empty()); let owner = owner_at(ctx, Some(&rec), &focus::scope_chain(ctx, &rec), None); - holds_press(owner, Some(&rec), &press, taken, typed) + let bare_tab = chord.key == Key::Tab && !chord.mods.cmd && !chord.mods.ctrl && !chord.mods.alt; + let traverses = bare_tab && !(super::input::tab_toggles_now(ctx) && super::input::is_chrome_chord(ctx, &chord)); + holds_press(owner, Some(&rec), &press, taken, typed) || traverses } /// Takes what the framework handed to one taker out of the running pass's egui input, where the app and egui's text diff --git a/crates/ui-contract/src/testing/key_trials.rs b/crates/ui-contract/src/testing/key_trials.rs index c8acfb1d7..1c4882152 100644 --- a/crates/ui-contract/src/testing/key_trials.rs +++ b/crates/ui-contract/src/testing/key_trials.rs @@ -4,13 +4,16 @@ use crate::runtime::commands::{ChromeCommand, CommandInput, CommandSet as _}; use crate::runtime::env::RootEnv; use crate::runtime::focus::{FocusTarget, KeyUse, request}; use crate::runtime::frame::{ChromeToggle, take_output}; -use crate::runtime::keys::{intents_of, take_shortcuts}; -use crate::testing::focus::{Stop, egui_focused, named_id, register_stop, widget}; +use crate::runtime::keys::{focus_holds, intents_of, take_shortcuts}; +use crate::testing::focus::{Stop, egui_focused, named_id, press_shape, register_stop, widget}; use crate::testing::input::{key_press, key_repeat}; +use crate::testing::pointer::click; use crate::testing::{Frames, trial}; -use craft_behavior::{Input, Intent}; -use craft_keymap::Overrides; -use egui::{Id, Key, Modifiers, Ui, ViewportId}; +use craft_behavior::{Input, Intent, Sense}; +use craft_keymap::{Chord, Overrides}; +use egui::{Id, Key, Modifiers, Rect, Ui, ViewportId}; +use std::cell::Cell; +use std::rc::Rc; use std::sync::Arc; fn id(name: &str) -> Id { @@ -84,6 +87,35 @@ fn tab_in_a_window_with_a_canvas_moves_focus_off_a_control() { assert!(press(&mut frames, &mut show, Key::Tab, Modifiers::NONE).is_empty(), "a text field never loses Tab"); } +/// In a window with a canvas, a component a press focused leaves Tab to the panels' toggle, as clicking a panel's row and +/// pressing Tab hides the panels in Photoshop; one the keyboard or a request focused moves focus on Tab. `focus_holds` +/// names the Tab as the component's exactly when it moves focus. +#[trial] +fn tab_toggles_from_a_pressed_component_and_moves_focus_from_a_requested_one() { + let at = Rc::new(Cell::new(Rect::NOTHING)); + let seen = Rc::clone(&at); + let mut show = move |ui: &mut Ui| { + let rect = widget(ui, id("button")); + register_stop(ui.ctx(), Stop::new(id("button"), KeyUse::of(Input::Pointer))); + press_shape(ui, rect, Sense::CLICK | Sense::TAKE_FOCUS, id("button")); + seen.set(rect); + stop(ui, "other", KeyUse::of(Input::Pointer)); + stop(ui, "canvas", KeyUse::CANVAS); + }; + let mut frames = shown(true, &mut show); + click(&mut frames, at.get().center(), &mut show); + take_output(&frames.ctx); + assert_eq!(egui_focused(&frames.ctx), Some(id("button")), "the press focused the button"); + let tab = Chord::plain(craft_keymap::Key::Tab); + assert!(!focus_holds(&frames.ctx, tab), "a pressed component leaves Tab to the toggle"); + assert_eq!(press(&mut frames, &mut show, Key::Tab, Modifiers::NONE), [(ViewportId::ROOT, ChromeToggle::Panels)], "a pressed component"); + assert_eq!(egui_focused(&frames.ctx), Some(id("button")), "the toggle moved no focus"); + focus_on(&mut frames, &mut show, "other"); + assert!(focus_holds(&frames.ctx, tab), "a requested component's Tab moves focus"); + assert!(press(&mut frames, &mut show, Key::Tab, Modifiers::NONE).is_empty(), "a requested component"); + assert_ne!(egui_focused(&frames.ctx), Some(id("other")), "focus left the component"); +} + #[trial] fn tab_in_a_window_without_a_canvas_moves_focus() { let mut show = without_canvas; From 86fce21a01e9f12f89fb67adedd3073c635c1ab6 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 08:07:14 +0000 Subject: [PATCH 2/2] cargo fmt --- crates/ui-contract/src/runtime/commands/board.rs | 3 ++- crates/ui-contract/src/runtime/keys/input.rs | 8 +++++++- crates/ui-shell/tests/it/commands/app_catalog.rs | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 8f6672ed2..ba0e0ea62 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -1205,7 +1205,8 @@ pub(crate) fn begin(ctx: &Context) { } } _ => { - let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, shortcuts.typing && !shortcut.canvas, Some((&mut matcher, time))) else { + let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, shortcuts.typing && !shortcut.canvas, Some((&mut matcher, time))) + else { continue; }; let (app, instance) = with(ctx, |board| board.target_of(ctx, &hit.command, &board.here)); diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index ec63de0ec..dc057863b 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -300,7 +300,13 @@ impl Strip<'_> { PressUse::Press => { batch.presses.push(Press { key, mods, repeat, in_egui: None, release: None, text: None, canvas: false }); if !repeat { - batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: false, at }); + batch.shortcuts.push(Shortcut { + input: *input, + phase: FirePhase::Press, + press: Some(batch.presses.len() - 1), + canvas: false, + at, + }); } true } diff --git a/crates/ui-shell/tests/it/commands/app_catalog.rs b/crates/ui-shell/tests/it/commands/app_catalog.rs index c726f77cc..ef6ecaff4 100644 --- a/crates/ui-shell/tests/it/commands/app_catalog.rs +++ b/crates/ui-shell/tests/it/commands/app_catalog.rs @@ -109,7 +109,8 @@ fn a_keys_press_and_release_fire_with_the_times_they_arrived_at() { let mut frames = Frames::new(); frames.commands(catalog(1)); frames.idle(3, &mut show); - let times = |frames: &Frames| take_output(&frames.ctx).fired.into_iter().map(|fired| (fired.command.id, fired.phase, fired.time)).collect::>(); + let times = + |frames: &Frames| take_output(&frames.ctx).fired.into_iter().map(|fired| (fired.command.id, fired.phase, fired.time)).collect::>(); let now = |frames: &Frames| frames.ctx.input(|input| input.time); frames.frame(vec![key_press(Key::J, Modifiers::NONE)], &mut show); let pressed = now(&frames);