From deb9ad9f07297649cbed562fdce3b32921f5e1dc Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 17:28:05 +0700 Subject: [PATCH] ui-contract: Tab toggles the panels from read-only text a press focused (a selection part, a press-only stop), as from any pressed control; only a text field keeps Tab for the next field --- crates/ui-contract/src/runtime/keys/input.rs | 11 +++++----- crates/ui-contract/src/testing/key_trials.rs | 22 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 4d648e177..17e7b1d10 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -549,17 +549,18 @@ pub(super) fn focused_claims(record: &focus::FocusedRecord, stage: &Stage) -> Ve /// /// 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, the canvas is, or a press focused a component other than a text field, as clicking a panel's row -/// and pressing Tab hides the panels in Photoshop. Tab moves focus from a text field however it got the focus (Tab +/// and pressing Tab hides the panels in Photoshop; read-only text a press focused to select from (a stop only a press +/// reaches, a label's selection part) is no text field. Tab moves focus from a text field however it got the focus (Tab /// commits a typed value and goes to the next field) and 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 { +fn tab_toggles_chrome(enabled: bool, canvas: bool, layer: overlay::LayerKeys, focused: Option<&focus::Stop>, 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); - let pressed_control = pressed && focused.is_some_and(|keys| keys.owner != OwnerClass::Text); + let on_canvas = focused.is_some_and(|stop| stop.keys.chrome_key); + let pressed_control = pressed && focused.is_some_and(|stop| stop.keys.owner != OwnerClass::Text || stop.press_only); if canvas { focused.is_none() || on_canvas || pressed_control } else { on_canvas } } @@ -572,7 +573,7 @@ pub(super) fn tab_toggles_now(ctx: &Context) -> bool { 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) + tab_toggles_chrome(enabled, canvas, layer, focused.as_ref().map(|rec| &rec.stop), pressed) } /// Whether `chord` is the key of a chrome command (`ChromeCommand`), as the window's keymap binds it. diff --git a/crates/ui-contract/src/testing/key_trials.rs b/crates/ui-contract/src/testing/key_trials.rs index fecf7bb17..b7c3edfaa 100644 --- a/crates/ui-contract/src/testing/key_trials.rs +++ b/crates/ui-contract/src/testing/key_trials.rs @@ -140,6 +140,28 @@ fn tab_moves_focus_from_a_pressed_text_field() { assert_ne!(egui_focused(&frames.ctx), Some(id("field")), "focus left the field"); } +/// Read-only text a press focused to select from (a label's selection part, a stop only a press reaches) is no text +/// field: Tab toggles the panels from it, as from any pressed control. Guards against a toggle that read every stop of +/// the text class as a field that keeps Tab for the next field. +#[trial] +fn tab_toggles_from_pressed_read_only_text() { + 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("label")); + register_stop(ui.ctx(), Stop::new(id("label"), KeyUse::of(Input::Text)).press_only()); + press_shape(ui, rect, Sense::CLICK | Sense::TAKE_FOCUS, id("label")); + seen.set(rect); + 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("label")), "the press focused the text"); + assert_eq!(press(&mut frames, &mut show, Key::Tab, Modifiers::NONE), [(ViewportId::ROOT, ChromeToggle::Panels)], "pressed read-only text"); + assert_eq!(egui_focused(&frames.ctx), Some(id("label")), "the toggle moved no focus"); +} + #[trial] fn tab_in_a_window_without_a_canvas_moves_focus() { let mut show = without_canvas;