From d9475fba2bf54f615d143cfd0f4919f1f96fed74 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 12:16:39 +0000 Subject: [PATCH] craft-ui: a root keeps the key focus it holds for a member at step 8 The stop of an arena member (a radio in its group, a theme chip) has its root's egui widget as its widget. Step 8 registers that widget with the root's own and its parts' senses, and the members' senses join once the body has run (widen_root). egui drops the focus of a widget registered without a focus sense, so Tab, an AccessKit Focus or a press that focused a member lost it in the same pass. While the focus service says the root holds focus for another component's stop (focus::held_for_member), the step-8 sense includes focus. --- crates/ui-contract/src/primitives/sensing.rs | 14 +++++++------- crates/ui-contract/src/runtime/focus.rs | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index aac6880d1..ab1bc0b6e 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -630,8 +630,8 @@ pub(crate) struct RootResult { pub activation: bool, /// The member a click activated. pub activated_member: Option, - /// The sense the root's egui widget carries at step 8: its own and its parts'. Its members' senses join it once - /// its body has run (`widen_root`). + /// The sense the root's egui widget carries at step 8: its own and its parts', and focus while it holds key focus + /// for a member. Its members' senses join it once its body has run (`widen_root`). pub sense: Sense, } @@ -644,11 +644,11 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { let read = PassRead::of(ctx); let (clip, layer) = (ui.clip_rect(), ui.layer_id()); // A disabled root only hovers, whatever its parts and members sense; it keeps the focus stop its own sense or a part's grants. - let sense = if spec.enabled { - spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense) - } else { - Sense::HOVER | spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense).intersection(Sense::FOCUS) - }; + let own = spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense); + let sense = if spec.enabled { own } else { Sense::HOVER | own.intersection(Sense::FOCUS) }; + // A root that holds key focus for a member's stop senses focus now: egui drops the focus of a widget registered + // without it, before the members, which read it, run and join their senses (`widen_root`). + let sense = if crate::runtime::focus::held_for_member(ctx, spec.id) { sense | Sense::FOCUS } else { sense }; // The response rect is the root's whole rect and egui's hit test uses its part inside the clip (`interact_rect`), // as for any egui widget: a root the clip cuts reports and reveals where it is, not where it shows. let response = response::widget(ui, spec.id, spec.rect, sense); diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 59e11f4b4..3a30b889c 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -648,6 +648,12 @@ pub(crate) fn request_now(ctx: &Context, target: FocusTarget) { apply(ctx, acts); } +/// Whether egui key focus is on the widget `root` for another component's stop: an arena member, whose stop's widget +/// is its root's (`Stop::root`). +pub(crate) fn held_for_member(ctx: &Context, root: Id) -> bool { + egui_focus::focused(ctx) == Some(root) && focused_stop(ctx).is_some_and(|(id, _)| id != root) +} + /// Whether `at` (screen points) lies in a popup layer opened above the layer of the widget that holds egui focus: a /// press or a wheel there is the popup's, not the focused widget's. The layer under `at` is the layer test's; the /// focused widget's layer is egui's last-pass record of it, since a stop records no layer of its own.