Skip to content

focus_on_open and focus_now on every focusable owned chain (#326, #395) - #420

Merged
samitbasu merged 4 commits into
mainfrom
focus-326
Nov 19, 2026
Merged

samitbasu merged 4 commits into
mainfrom
focus-326

Conversation

@samitbasu

Copy link
Copy Markdown
Owner

Part of #326 (its item 2, focus_on_open on the owned chains). Closes #395.

What changes

  • Every focusable owned chain gains .focus_on_open() and .focus_now(..):
    • button, toggle, toggle item, checkbox, switch, radio group, segmented, select, slider, stepper, text field, splitter, and the dropdown menu (through its trigger).
  • .focus_on_open() marks the node holding the chain's focusable widget, as NodeHandle::focus_on_open does. The overlay the control is in opens with focus on it, instead of on the overlay's first focusable.
  • .focus_now(..) (Focus: a one-shot app focus request for a field, new or existing #395) focuses the control on every frame it's set, whether the control is new that frame or already built. The app sets it on the frame it wants and drops it after, e.g. .when(open_now, |c| c.focus_now(Select::All)).
    • On the text chains (text field, stepper) it is .focus_now(Select), and it selects as Select says: with Select::All, typing replaces the text.
    • On every other chain it is .focus_now(), with no argument (the user's pick).
  • One crate-internal Autofocus holds both:
    • It gains OnOverlayOpen and Now(Select), beside Never, OnAppear and OnChange.
    • So "focus on appear and on open" or "on open and now" can't be combined: the last call wins (the user's pick).
    • Each chain holds one autofocus field. At build it marks its focus node with autofocus.open_focus(), and at draw it runs autofocus.apply(..) on that widget's response. Each public method is one line that sets the field.
    • When One options layer for owned controls: Options, Configure/Interactive traits, refused(reason), when/when_some #315's Focus trait lands, those methods move into the trait.
  • NodeHandle::focus_on_open (behaviour change): a marked node now gives the overlay's focus to the first focusable widget registered inside it: the node itself, or a widget within it.
    • Before, a marked node that wasn't itself the focusable widget was passed over, and the overlay's first focusable took focus.
    • This matters for the stepper and for a text field with an icon: their text edit is an egui widget inside the marked leaf, under its own id.
    • Nothing else changes for a node that is itself focusable, since its own widget is the first one it registers.

Decided by default, for review

The user picked focus_now(Select) for the text chains, focus_now() elsewhere, and no focus_now on menu items. These were left to my defaults:

  • The radio group and the segmented control focus the chosen item, which is the group's Tab stop. If none is enabled and chosen, they focus the first enabled item.
  • The select marks its trigger. The list is a separate overlay that keeps focus on the trigger.
  • The slider marks its thumb. The readout field keeps its own focus-on-appear.
  • The toggle and the toggle item get the methods through their ButtonChain. The dropdown menu passes its setting to its trigger button.
  • Left out:
    • custom, whose focusable is the caller's own content, so NodeHandle::focus_on_open already serves it;
    • table, whose rows aren't Tab stops;
    • menu items, which keep their existing focus_on_open.
  • focus_now inside an overlay that is opening: on the frame the overlay opens, its opening focus is requested after the draw, so it wins, as with focus_on_appear today. focus_on_open is the way to choose that focus.

Wording for you to check: NodeHandle::focus_on_open's doc now says "the node, or a widget inside it, must take focus", and opening_focus's doc follows it.

Tests

New in tests/it/focus_options.rs. Each test runs every chain through the real build, layout and draw, using kittest's focus and key events:

  • without_the_option_a_popover_opens_on_its_first_focusable, a precondition: the button placed before the control would take focus by default.
  • a_popover_opens_on_the_control_marked_focus_on_open: for each of the 13 chains, focus lands on its focus node (the chosen circle, the pressed cell, the thumb, the stepper's field, and so on), with no reports.
  • of_two_controls_marked_focus_on_open_the_first_takes_focus_and_it_is_reported
  • focus_on_open_on_the_page_takes_no_focus
  • focus_now_focuses_a_control_already_built
  • focus_now_focuses_a_control_built_that_frame
  • once_the_app_drops_focus_now_focus_is_left_where_the_user_puts_it
  • focus_now_selecting_all_lets_typing_replace_a_field_s_text, focus_now_selecting_nothing_types_beside_the_text and focus_now_selecting_all_lets_typing_replace_a_stepper_s_number

Every existing test passes unchanged, and no golden changed.

Reversion check: on main the new suite doesn't compile, because the API is new. Each mutation below fails tests, and the unmutated branch passes all 159 focus tests:

  • focus_on_open marks no node: 2 fail;
  • focus_now requests no focus: 6 fail;
  • focus_now ignores its Select: 2 fail (field and stepper);
  • the radio and segmented target skips the chosen item: 4 fail;
  • a marked node's opening focus only the node itself, the old rule: 1 fails (the stepper);
  • the button never applies its setting: 3 fail;
  • the dropdown drops its setting on the way to its trigger: 4 fail.

Checks

  • clippy (workspace, all targets, all features) and fmt are clean.
  • cargo test -p eggshell-ui passes: lib 316, it 1631, doctests 92 and 15.
  • No UI change, so there is nothing to try by hand. The new options take effect only when an app calls them.

TODO(prose)

Every new public method (focus_on_open and focus_now on each chain) has a one-line draft for you to rewrite.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LesfK54UWq1haPSEr9X22o

samitbasu and others added 4 commits November 19, 2026 10:22
An owned control couldn't be the one an overlay opens on, and an app
couldn't focus a field once at a moment of its choosing whether the field
was new that frame or already built.

The crate-internal Autofocus gains OnOverlayOpen and Now(Select), so the
modes stay one value and can't be combined. Each focusable chain holds
one autofocus field: it marks its focus node at build
(Autofocus::open_focus) and applies it to that widget's response at draw.
The public methods are one line each: focus_on_open(), and focus_now(),
which is focus_now(Select) on the text field and the stepper. The radio
group and the segmented control target their chosen item, else the first
enabled one.

Behaviour change: a node marked focus_on_open now gives the overlay's
focus to the first focusable widget registered inside it, the node itself
or one within. Before, a marked node that wasn't itself the widget was
passed over. The stepper's and an icon text field's edit sits inside
their marked leaf under its own id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LesfK54UWq1haPSEr9X22o
@samitbasu

Copy link
Copy Markdown
Owner Author

Approved by the user on its tests (2026-11-19). GitHub's required checks pass on the branch merged with current main. Merging.

@samitbasu
samitbasu merged commit 345fdd9 into main Nov 19, 2026
4 checks passed
@samitbasu
samitbasu deleted the focus-326 branch November 19, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Focus: a one-shot app focus request for a field, new or existing

1 participant