Skip to content

menu: Focus a context menu before its first frame - #3422

Merged
madcodelife merged 1 commit into
mainfrom
context-menu-focus-on-open
Oct 9, 2026
Merged

madcodelife merged 1 commit into
mainfrom
context-menu-focus-on-open

Conversation

@madcodelife

@madcodelife madcodelife commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Closes #3364

Description

With accessibility active, opening a ContextMenu aborts a debug build with set_focus called more than once in a single frame.

GPUI registers the focused element as the frame's accessibility focus while prepainting it, and allows one per frame. ContextMenu moved focus to its menu inside the menu's own prepaint. The menu is deferred and prepaints last, so the element that held focus before (for example the right-clicked input) had already registered, and the menu registered a second time.

  • ContextMenu now focuses the menu in the deferred callback that builds it, before its first frame is drawn, after the input selection ownership from input: Preserve selection, focus ring and context menu actions #3382 is resolved. DeferredMenu::build_menu no longer touches focus.
  • The old prepaint focus ran on every frame and pulled focus back whenever it left the menu. That also covered a case it was never written for: entering a submenu with the keyboard and then pointing at another parent item closes the submenu while it still holds focus, leaving Escape and the arrow keys with nowhere to go. PopupMenu now takes focus back itself when a hover closes a focused submenu, which also applies to DropdownMenu and AppMenuBar.

Behavior change: while a context menu is open, focus that the application moves elsewhere is no longer pulled back to the menu on the next frame.

How to Test

cargo test -p gpui-component --lib menu::
cargo test -p gpui-kit --features test-support --test menu
  • menu_takes_focus_before_its_first_frame checks that focus has left the previous element when the frame that draws the menu begins rendering. It fails on main.
  • hovering_away_from_a_keyboard_entered_submenu_refocuses_its_parent enters a submenu with the keyboard, hovers another parent item, and checks that Escape still dismisses the menu. It passes on main, fails with only the ContextMenu change, and passes with this PR.

The headless test platform cannot activate the accessibility tree, so the tests cover the focus ordering that causes the panic rather than the panic itself. This has not been run against a screen reader on a native debug build.

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.
  • Tested macOS, Windows and Linux platforms performance (if the change is platform-specific)

@madcodelife
madcodelife merged commit 9c4b175 into main Oct 9, 2026
15 checks passed
@madcodelife
madcodelife deleted the context-menu-focus-on-open branch October 9, 2026 06:54
linruohan pushed a commit to linruohan/gpui-component that referenced this pull request Oct 9, 2026
Closes longbridge#3364

## Description

With accessibility active, opening a `ContextMenu` aborts a debug build
with `set_focus called more than once in a single frame`.

GPUI registers the focused element as the frame's accessibility focus
while prepainting it, and allows one per frame. `ContextMenu` moved
focus to its menu inside the menu's own prepaint. The menu is deferred
and prepaints last, so the element that held focus before (for example
the right-clicked input) had already registered, and the menu registered
a second time.

- `ContextMenu` now focuses the menu in the deferred callback that
builds it, before its first frame is drawn, after the input selection
ownership from longbridge#3382 is resolved. `DeferredMenu::build_menu` no longer
touches focus.
- The old prepaint focus ran on every frame and pulled focus back
whenever it left the menu. That also covered a case it was never written
for: entering a submenu with the keyboard and then pointing at another
parent item closes the submenu while it still holds focus, leaving
Escape and the arrow keys with nowhere to go. `PopupMenu` now takes
focus back itself when a hover closes a focused submenu, which also
applies to `DropdownMenu` and `AppMenuBar`.

Behavior change: while a context menu is open, focus that the
application moves elsewhere is no longer pulled back to the menu on the
next frame.

## How to Test

```bash
cargo test -p gpui-component --lib menu::
cargo test -p gpui-kit --features test-support --test menu
```

- `menu_takes_focus_before_its_first_frame` checks that focus has left
the previous element when the frame that draws the menu begins
rendering. It fails on `main`.
- `hovering_away_from_a_keyboard_entered_submenu_refocuses_its_parent`
enters a submenu with the keyboard, hovers another parent item, and
checks that Escape still dismisses the menu. It passes on `main`, fails
with only the `ContextMenu` change, and passes with this PR.

The headless test platform cannot activate the accessibility tree, so
the tests cover the focus ordering that causes the panic rather than the
panic itself. This has not been run against a screen reader on a native
debug build.

## Checklist

- [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and
followed the guidelines.
- [x] Reviewed the changes in this PR and confirmed AI generated code
(If any) is accurate.
- [x] Passed `cargo run` for story tests related to the changes.
- [ ] Tested macOS, Windows and Linux platforms performance (if the
change is platform-specific)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

ContextMenu: accessibility focus assertion aborts native debug builds during prepaint

1 participant