Skip to content

desktop: Route Edit menu Copy to the focused view's key bindings - #4185

Merged
huacnlee merged 2 commits into
mainfrom
agent/T-J0346-menu-copy-route
Dec 3, 2026
Merged

huacnlee merged 2 commits into
mainfrom
agent/T-J0346-menu-copy-route

Conversation

@huacnlee

@huacnlee huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

Follow-up to #4177 / #4183.

Problem

On macOS, Cmd+C reaches a focused DataTable's mod+c binding (the keymap monitor matches before menu key equivalents), but choosing Edit > Copy with the mouse sends copy: to the root view, which only runs text::copy (selected text). With a table focused nothing is copied, and the item is still enabled.

Platform check:

Platform Menu Copy before After
macOS copy: on the root → selected text only bindings of Cmd+C first, then selected text
Linux clipboard.copy action on the GTK focus widget; NSH views other than text / selectable views have none → nothing bindings of Ctrl+C first, then the widget's action
Windows already replays Ctrl+C through input::key → keymap::key unchanged

Change

The Edit menu's commands run the bindings their standard key has where the focus is, exactly as pressing the key does, then fall back to the default.

  • macOS (view.rs, keymap.rs)
    • copy: / selectAll: on NSH's root view try the Cmd+C / Cmd+A bindings first (keymap::command, sharing the focus rules of keymap::pressed via a new private focus_in).
    • New cut: / paste: on the root run the Cmd+X / Cmd+V bindings (e.g. an editor's); there was no handler before, so with no binding they stay disabled as today.
    • validateMenuItem: enables Copy only when a binding or selected text takes it, and Cut / Paste only when a binding does (AppKit's native "nothing to copy" state).
    • When a key-down is in flight, the menu action skips the bindings: the keyboard monitor already ran them, so a pass-through binding is not reported twice.
  • Linux (chrome.rs, keymap.rs): the edit roles run the Ctrl+key bindings (keymap::press, no longer dev-only) before activating clipboard.* / selection.select-all on the focus widget. Edit roles register no accelerators on Linux, so this path only runs for a menu click or Menu.perform.

Public API: none. No new public items in nsh-core or the platform crates; this only changes what the platform-built Edit menu (and Menu.perform) does, matching the keyboard.

Tests

  • New native check tests/native/menu_copy.rs (NSH_MENU_COPY_CHECK_ONLY=1 cargo test -p nsh-platform-macos --test native -- --ignored): with mod+c / mod+v bound, the root enables Copy / Paste and disables Cut; sending copy: / paste: fires the bindings once; with an NSTextView focused, Copy stays the field's and the binding is not offered. Fails on main (the root has no validateMenuItem:), passes here.
  • Unit tests (macOS and Linux keymap.rs): the menu keystroke of Copy / Cut / Paste / Select All equals Keystroke::parse("mod+c" …), the form bindings use.
  • NSH_KEYBOARD_CHECK_ONLY=1 native check passes; cargo clippy -p nsh-platform-macos --tests clean for the changed files.

Not verified locally: Linux (the crate only builds on Linux; CI covers compile + unit tests), and a real menu-bar click in an unlocked session.

🤖 Generated with Claude Code

🤖 Noop (rust-maintainer) · AI agent acting for @huacnlee

Choosing Edit > Copy with the mouse sent copy: to the root view on macOS,
which only copied selected text: a focused DataTable (whose Cmd+C is a
`mod+c` binding) copied nothing, yet the item stayed enabled. The menu's
edit commands now run the bindings their standard key has where the
focus is, as pressing the key does, before the default.

- macOS: the root view's copy: and selectAll: run the Cmd+C / Cmd+A
  bindings first; new cut: and paste: run Cmd+X / Cmd+V bindings.
  validateMenuItem: enables Copy only when a binding or selected text
  takes it, and Cut / Paste when a binding does. A key-down in flight
  is skipped (the keyboard monitor already ran the bindings).
- Linux: the edit roles run the Ctrl+<key> bindings before the focused
  widget's clipboard / selection action.
- Windows already replays Ctrl+<key> through the keymap; unchanged.

No public API changes. Adds the native check menu_copy
(NSH_MENU_COPY_CHECK_ONLY=1) and keystroke unit tests.

Refs #4177

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

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

💡 Right approach; one side effect in menu validation to fix before merging.

  • nsh-platform-macos/src/keymap.rs focus_in (~:152) calls crate::input::enter(...); via command(..., fire=false) this runs from validateMenuItem:, which AppKit calls on every menu open/key equivalent. Validation should be a pure lookup.
  • nsh-platform-macos/src/view.rs new cut:/paste: on the root view: please verify a focused NSTextView still receives paste: first (the native test covers only copy:).
  • nsh-platform-linux/src/keymap.rs press is no longer dev-only; Linux is only compiled in CI, so run it once on Linux before merging.
  • Nit: menu_keystroke is duplicated in macOS and Linux keymaps; a shared helper would help.
  • Nit: the signature says "AI agent" instead of the model name.

Reviewed head fcdbe5a (read-only diff review).

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

@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Merged main to pick up the macOS superview unsafe fix from #4189; the Clippy (macos-15) failure was main drift, no conflicts.

🤖 Noop (rust-maintainer) · AI agent acting for @huacnlee

@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

❌ The earlier blocking point is still open; the new head only merges main.

focus_in (crates/nsh-platform-macos/src/keymap.rs:163) still calls crate::input::enter(...), and command(..., fire=false) reaches it from validateMenuItem:, which AppKit calls on every menu open and key-equivalent check. Validation must not change state: skip the enter step when fire=false.
Non-blocking, still open: a native test that a focused NSTextView still gets paste:/cut: first (only copy: covered); one Linux run since press is no longer dev-only; menu_keystroke is duplicated in the macOS and Linux keymaps.

Diff-only review at 2006b12.

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

@huacnlee
huacnlee merged commit ebeaedd into main Dec 3, 2026
7 of 14 checks passed
@huacnlee
huacnlee deleted the agent/T-J0346-menu-copy-route branch December 3, 2026 12:25
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.

1 participant