Skip to content

menus: driver.MenuShortcut gives keys to items the Linux and Windows menus never choose while editing, so YieldsToMenu bindings lose them #710

Closed

Description

@houko

Context

driver.MenuShortcut (internal/driver/keys.go:143-176) is the core's and the headless model's rule for which application menu item takes a key. Two cases while a text input or web view has the focus (editing) do not match what the Windows and Linux backends do.

1. Unmodified keys on Linux. The GTK backend never offers an unmodified key to the menu while editing (internal/platform/linux/input.go:852-854):

if editing && state&(gdkControl|gdkAlt|gdkSuper|gdkMeta) == 0 {
	return false // a field's or a page's own keys
}

MenuShortcut has no such rule, so menuHasKey (core/keys.go:288) reports that an F5 item takes F5 while a field is focused. A binding of F5 made with YieldsToMenu is then left out of the keymap, and on Linux the menu does not take F5 either: the key reaches neither.

2. A NotInText item followed by another item with the same shortcut. While editing, MenuShortcut skips a NotInText item and lets a later item with the same shortcut take the key (case e.NotInText && editing: at keys.go:171; covered by the "another item takes the key" case in core/keys_test.go). The Windows and Linux backends keep only the first item per key (menuKeys, internal/platform/windows/chrome.go:812-826 and internal/platform/linux/view.go:185-198), and their menuShortcut returns false for it while editing (windows chrome.go:1020-1029, linux input.go:959-964), so the later item never runs. On macOS, validateMenu (internal/platform/macos/desktop.go:978) disables the NotInText item during the key press and AppKit stops at the first matching key equivalent, as MenuShortcut itself assumes for disabled items on a Mac.

Related: #447 (YieldsToMenu ignoring NotInText), which fixed the single-item case.

Why it matters

In both cases the core decides that the menu takes the key, so a YieldsToMenu binding stands aside, while the platform's menu does nothing with it. The key is lost, and headless tests (which use MenuShortcut) pass.

Minimal reproduction

Case 2: SetApplicationMenu with [{Shortcut: "mod+r", When: MenuNotEditing, OnSelect: a}, {Shortcut: "mod+r", OnSelect: b}], a focused text input, and cx.OnKeyWith("mod+r", KeyOptions{YieldsToMenu: true}, c). Press Ctrl+R (Cmd+R on macOS): headless runs b; on Windows, Linux and macOS none of a, b or c runs.

Case 1: on Linux, a menu item with Shortcut: "f5", a focused text input, and cx.OnKeyWith("f5", KeyOptions{YieldsToMenu: true}, c). Press F5: nothing runs.

Proposal

Make MenuShortcut model what the menus do: while editing, ignore unmodified shortcuts on Linux (pass the OS rather than only mac), and treat a NotInText item as ending the search for its key (as a disabled item does on a Mac) on every platform, since all three stop at the first item with the key. Alternatively change the backends to look past a NotInText item, but macOS cannot do that with key equivalents.

Acceptance

  • In both reproductions the YieldsToMenu binding runs on every desktop platform, and headless agrees.
  • Tests cover both cases in core/keys_test.go and the driver.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions