Skip to content

root: Make Tab work before anything is focused - #4017

Open
HappyFox001 wants to merge 1 commit into
longbridge:mainfrom
HappyFox001:root-tab-without-focus
Open

HappyFox001 wants to merge 1 commit into
longbridge:mainfrom
HappyFox001:root-tab-without-focus

Conversation

@HappyFox001

@HappyFox001 HappyFox001 commented Nov 17, 2026 •

Copy link
Copy Markdown

Closes #4004.

Description

In a window opened with gpui_kit::open_window, Tab and Shift-Tab do nothing until something has focus. If an app doesn't focus a control at startup, a keyboard user can't get into it. The same thing happens again after the focused control is removed or blurred.

Cause. Root binds Tab, Shift-Tab and copy in its Root key context, which is set on div#root. With nothing focused, GPUI dispatches a keystroke from the window's root node (focus_node_id_in_rendered_frame falls back to root_node_id()). That node is the Root view's own element, one level above div#root, so the context isn't on the dispatch path and no binding matches.

Fix. Root keeps a focus handle and takes focus whenever nothing else has it:

  • when the window opens. This is deferred, so content that focuses itself on open (the story gallery does) still gets focus first;
  • from on_focus_lost, when the focused element is blurred or leaves the tree.

The handle isn't a tab stop, so Tab from it goes to the first stop and Shift-Tab to the last, the same as from no focus.

The handle sits on an empty, zero-sized child of div#root rather than on div#root itself. A focusable div takes focus on mouse down, so on the surface it would pull focus out of a focused input every time the background is clicked. The child has no role, so screen readers still announce the window, as they do today.

control_steps.rs had a test that focused the first stop by hand to get around this. It now just presses Tab.

No public API changes.

This change was written with AI assistance (Claude Code).

How to Test

New tests in crates/kit/tests/integration/root_focus.rs:

  • tab_in_a_window_with_nothing_focused_reaches_the_first_control
  • shift_tab_in_a_window_with_nothing_focused_reaches_the_last_control
  • tab_reaches_the_controls_again_after_the_focused_one_goes_away
  • content_that_focuses_itself_when_the_window_opens_keeps_that_focus
  • pressing_the_background_leaves_the_focused_control_focused

On main the first three fail. The last two pass with and without the fix; they check that Root doesn't take focus it shouldn't.

Run on macOS (Apple Silicon):

cargo test -p gpui-base -p gpui-component -p gpui-kit -p gpui-component-story --features gpui-component-story/test-support --locked
cargo test -p gpui-kit --features gpui-fast,test-support --locked
cargo clippy -p gpui-base -p gpui-kit -p gpui-component --locked -- --deny warnings

These use GPUI test-window events. I haven't checked native keyboard input or a screen reader.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
  • Passed cargo run for story tests related to the changes. (Not run. The story gallery focuses its own root when it opens, so it doesn't show this bug; the tests above cover it.)
  • Tested macOS, Windows and Linux platforms performance (if the change is platform-specific) (Not platform-specific; tested on macOS only.)

🤖 Generated with Claude Code

With nothing focused, GPUI dispatches keystrokes from the window's root
node, above `div#root` where the `Root` key context lives, so Tab,
Shift-Tab and copy matched no binding until a control had been clicked.

`Root` now holds a focus handle whenever nothing else has focus: when the
window opens (deferred, so content that focuses itself on open comes
first) and from `on_focus_lost`. The handle is tracked on an empty child
of the surface rather than the surface itself, so pressing the background
does not take focus away from a focused control.

Closes longbridge#4004.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

Copy link
Copy Markdown
Member

✅ LGTM: small Root-level fix so Tab/Shift-Tab work with nothing focused; no public API change.

  • Cause analysis matches GPUI: with no focus, dispatch starts at the window root node, above the Root key context on div#root, so the bindings never matched.
  • The fallback handle is a zero-sized, non-tab-stop child, so background clicks don't steal focus from inputs, and on_focus_lost only fires when the window ends up with no focus, so dialogs/popovers that restore focus are unaffected.
  • The initial focus is deferred, so content that focuses itself on open keeps priority (covered by a test).

Ran at 5552c4d on Linux: cargo test -p gpui-kit --features test-support --test integration -- root_focus control_steps → 8 passed. Native keyboard / screen reader not checked.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

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.

Root: Tab does nothing until something in the window has focus

2 participants