Skip to content

fix(keys): yield to menus only where their shortcuts apply - #462

Merged
houko merged 2 commits into
nsgo-dev:mainfrom
houko:fix/menu-yield-follows-text-focus
Nov 17, 2026
Merged

houko merged 2 commits into
nsgo-dev:mainfrom
houko:fix/menu-yield-follows-text-focus

Conversation

@houko

@houko houko commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

A MenuNotEditing item with Command/Control+R removed a yielding reload binding even while a text field had focus and the menu declined the shortcut, so the key did nothing. Keep yielding bindings active while the menu leaves their key to an input or web view, and refresh the mounted keymap as focus moves, blurs, or its view is removed.

Track desktop WebView focus through NSWindow first-responder observation (including embedded views moving between host windows), a GTK subtree focus controller, and WebView2 focus events. Route these reports through core's shared focus handling, so a focused page also counts for confined bindings, HoldsFocus and focus-within. Update the keyboard behavior documentation in English and Chinese.

Closes #447.

Verification:

  • The new core regression fails without the fix (mounted keymap empty while focused) and passes with it. It checks the actual mounted keymap and callback dispatch across macOS, Windows and Linux, inputs and WebViews, menu modes, submenus, reordered focus/blur reports and focused-view removal.
  • go build ./..., cross vets of core for windows/amd64, linux/amd64, android/arm64 and js/wasm, vets of the Windows, Linux and macOS backends, and go tool nsgo check -target all passed.
  • go tool nsgo test ./core and go tool nsgo test -run 'Layer|Skills|Doc' . passed.
  • Windows 11 (real session): the new native TestMenuYieldFollowsNativeFocus presses Control+R through the window's shortcut path outside any field (the menu item runs), in a field (the binding runs), after blur (the item again), in a focused WebView2 page (the binding) and after the page gives the focus back (the item). It fails without the WebView2 focus reporting. The existing Windows menu, web and key tests still pass.
  • Linux (Ubuntu 24.04 GNOME VM, X11): TestMenuYieldFollowsNativeTextFocus and TestMenuYieldFollowsNativeWebFocus pass; the web test fails without the GTK focus controller. On that VM WebKit's bubblewrap sandbox cannot start (bwrap: setting up uid map: Permission denied), so the web test ran with WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1.
  • macOS (this Mac, real key presses through System Events): with a MenuNotEditing Command+R item and a yielding binding, Command+R outside any field ran the item, and with a WKWebView page or the text field focused ran the binding. Built without this change, the same presses in the page and the field did nothing. TestWebFocusFollowsTheHostWindow and the macOS menu tests pass.

Known limit, not changed here: on Windows a WebView2 page with the keyboard focus keeps every key, so neither app bindings nor menu shortcuts see Control+R there (the page reloads instead); the keymap state above is still correct for when such keys are routed.

@github-actions github-actions Bot added size/M bug Something isn't working documentation Improvements or additions to documentation macOS Windows Linux core and removed bug Something isn't working labels Nov 16, 2026
@houko
houko force-pushed the fix/menu-yield-follows-text-focus branch from 0da7355 to a89e7c2 Compare November 17, 2026 00:15
@github-actions github-actions Bot added size/L bug Something isn't working and removed size/M labels Nov 17, 2026
@houko
houko force-pushed the fix/menu-yield-follows-text-focus branch from a89e7c2 to 7f58b5a Compare November 17, 2026 01:05
Add a native Windows regression that presses Control+R through the window's shortcut path with a MenuNotEditing item and a yielding binding, outside any field, in a field and in a WebView2 page, and unwrap the comments the fix added.
@houko
houko force-pushed the fix/menu-yield-follows-text-focus branch from 7f58b5a to ddb8966 Compare November 17, 2026 01:20
@houko

houko commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed. Read the full diff against main and the Acceptance of #447, and rebased the branch onto current main (no conflicts, CHANGELOG.md untouched, no hard-wrapped lines or attribution).

Code review: menuTakes now skips NotInText items only while an Input or WebView has the core's focus; the keymap is resent on focus moves whenever a yielding binding exists, and web views' focus and blur now go through the shared focus handling instead of dispatchWeb. Checked the side effects: the browser backend never reports focus for a WebView (its iframe is a child of the view's element, so viewOf does not match it), Android, iOS and HarmonyOS do not read the focus bit of the event mask, the macOS first-responder observer is removed in the surface's stops (which View.Close runs before releasing the view) and when an embedded surface moves windows, and the Linux focus controller's handlers are registered through connect, so unhost disconnects them.

Fail-without-fix checks:

  • Removing the NotInText && editing case makes TestMenuYieldFollowsTextFocus fail (mounted keymap empty while focused).
  • Reverting only the focus-driven keymap refresh and the web focus routing in core also makes it fail on all three OSes, for inputs and for web views.
  • Linux: without the GTK focus controller, TestMenuYieldFollowsNativeWebFocus fails ("keymap did not follow web focus true").

Builds and tests (after each rebase): go build ./... at both commits; GOOS=windows/linux/android/js builds; vets of core for windows amd64/386/arm64, linux, android and js/wasm, and of the Windows and Linux backends; go tool nsgo test ./core (635 passed) and go tool nsgo test -run 'Layer|Skills|Doc' . (35 passed).

Devices:

  • Linux, Ubuntu 24.04 GNOME VM (X11), on the final rebased head: TestMenuYieldFollowsNativeTextFocus, TestMenuYieldFollowsNativeWebFocus, TestApplicationMenuBuildsAndAnswersItsShortcut and TestRadioMenuItemsActivate pass. WebKit's bubblewrap sandbox cannot set up its uid map on that VM, so the web test ran with the sandbox disabled; this is a VM limit, not this change.
  • Windows 11, interactive session: TestMenuYieldFollowsNativeFocus and TestMenuLeavesOutMacRolesAndDrawsRadios pass.
  • macOS (Apple Silicon): TestWebFocusFollowsTheHostWindow, the macOS menu tests and TestWebMessagesComeOnlyFromTheSourcesPage pass. A probe app with a MenuNotEditing Command+R item and a yielding binding, driven by real Command+R key presses: outside any field the menu item ran; with the native field focused the binding ran (four of four presses); with the WKWebView page focused (AX focus on the web area) the binding ran (three of three).

Minor, not blocking: since main now keeps every GTK test window on one thread, the reason given for running TestMenuYieldFollowsNativeWebFocus in a subprocess is weaker than its comment says, but the isolation is harmless.

@houko
houko marked this pull request as ready for review November 17, 2026 01:20
@houko
houko merged commit fe35dd1 into nsgo-dev:main Nov 17, 2026
16 checks passed
@houko
houko deleted the fix/menu-yield-follows-text-focus branch November 18, 2026 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core documentation Improvements or additions to documentation Linux macOS size/L Windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core: YieldsToMenu ignores MenuEntry.NotInText, so a MenuNotEditing mod+r item leaves the key dead while a text input has focus

1 participant