Skip to content

rich-editor: Let Copy with nothing selected copy the selection around the editor - #3071

Merged
huacnlee merged 1 commit into
longbridge:mainfrom
AkaraChen:eta/richeditor-copy-around
Nov 24, 2026
Merged

huacnlee merged 1 commit into
longbridge:mainfrom
AkaraChen:eta/richeditor-copy-around

Conversation

@AkaraChen

Copy link
Copy Markdown
Contributor

What

On macOS, a focused RichEditor takes Cmd+C itself (keys::key_table binds it with When::Always) and copies only its own selection (Rv::copy returns early when it is empty). So text selected around the editor cannot be copied while the editor keeps the keyboard focus. That is the usual chat shape: a reply selected above a composer that still has the caret.

A plain Input already handles this (copies_selection_around in nsh-platform-macos/src/text_input.rs). This PR does the same for the RichEditor. While nothing is selected in the editor, its Copy binding is registered with pass_through. The key still reaches the editor and its plugins, then goes on to the platform's Copy, which copies the selection around it. Once the editor has a selection, the binding consumes the key as before.

  • keys.rs: KeySpec::copy marks the platform's Copy keystroke (Cmd+C on Apple keyboards, Ctrl+C elsewhere).
  • controller.rs: the Copy keystroke is bound in its own effect, re-bound as the selection empties or fills (sel_rev), with pass_through: empty.
  • examples/gallery/tests/copy-selection.test.tsx: the fixture gets a RichEditor composer, and the two "selection around it" cases run for it too.

How it was found

eta (an app on nsh) received a report that Cmd+C copies nothing in rich text on macOS. Measured on a GitHub macOS 15 runner, AppKit host, nsh nightly f577c51, with real key events (CGEvent) and pbpaste:

with a caret in a RichEditor, text selected in a TextView Cmd+C Edit › Copy (menu)
nsh nightly clipboard unchanged copies the selection

Second finding (not fixed here)

A view with focusable takes the keyboard focus when pressed (eta's transcript region). With it focused, after selecting text in a TextView inside it, both Cmd+C and an enabled Edit › Copy write nothing to the pasteboard. Accessibility reports the focused element as that AXGroup. I have not traced this one yet.

Status: draft, not verified at runtime

  • cargo check -p nsh-base passes. rustfmt was run on the changed files.
  • I could not check the behavior on macOS. I built nsh-cli from source (debug) on the runner, and examples/gallery/tests/copy-selection.test.tsx fails 7 of 7 on main there too: the Inputs never show (no pressable view matches id:composer). The new RichEditor cases fail on this branch in the same harness, which says nothing either way.
  • eta works around both findings on its side for now.

Please treat this as a pointer to the cause rather than a ready change. Not for merging as is.

🤖 Generated with Claude Code

… the editor

A focused RichEditor bound Cmd+C / Ctrl+C itself and copied only its own
selection, so text selected around it (a chat's messages above its composer,
which keeps the focus while they are selected) could not be copied on macOS.
With nothing selected in the editor the key now goes on to the platform's
Copy as well, as an Input's editor already does; plugins still hear it.

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

@huacnlee huacnlee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 The fix targets the right cause and mirrors Input, but it is unverified at runtime and the author marks it not for merging yet.

Reviewed head: a5cbc9d

  • Diagnosis: correct. key_table binds Cmd/Ctrl+C with When::Always and Rv::copy returns early on an empty selection, so the key never reaches the platform Copy. Passing it through only while the editor's selection is empty matches copies_selection_around in text_input.rs.
  • Implementation: small and contained. No public API change. KeySpec::copy is crate-private, and the Copy binding is re-registered on sel_rev inside the same focus-scoped effect pattern the other keys already use. Plugins still see the key.
  • Tests: cargo test -p nsh-base rich_editor passes locally (Linux): 310 + 248 passed.
  • Gap: no macOS run with real key events yet. The gallery copy-selection test also fails on main in the author's harness, so it proves nothing either way. One check on a Mac (caret in the RichEditor, text selected in a TextView above, Cmd+C → pbpaste) is what this needs before merging.
  • The second finding (a focusable container swallowing Copy) is out of scope here. It deserves its own issue.

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

@huacnlee
huacnlee marked this pull request as ready for review November 24, 2026 08:39
@huacnlee
huacnlee merged commit 8eace47 into longbridge:main Nov 24, 2026
36 checks passed
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.

2 participants