Skip to content

feat(native): selectable text element, used by command output - #72

Merged
rynfar merged 3 commits into
nativefrom
nw/69-selectable-text
Nov 2, 2026
Merged

rynfar merged 3 commits into
nativefrom
nw/69-selectable-text

Conversation

@rynfar

@rynfar rynfar commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Problem

Command output in the native transcript was drawn as styled text you could not select or copy. On the web, CommandOutputPanel.tsx gets browser text selection for free (drag, double/triple-click, shift-click, copy), so copying a failing test line or a path from command output only worked there. Only the markdown viewer was selectable natively.

Approach

  • crates/core/src/text_selection.rs (pure, tested): the selection model. A press places a caret or takes the word (double-click) or line (triple-click) under the pointer, and a drag or shift-click grows it from that unit at the same granularity. It also covers select all, copy extraction, and carrying a selection across a text change. Word ranges walk graphemes, so marks and emoji modifiers stay with their base character. The selection moves with its text by the number of bytes that left the front, comparing the selected bytes once, and it is dropped once that text was dropped or redrawn.
  • crates/core/src/terminal_output.rs: terminal_output_display, the join of terminalOutputSpans into one text plus styled ranges. It moved out of the app's build_display so copying across styled spans can be tested. It also adds native-only TerminalOutputState::origin: where text starts in everything folded, so a selection follows its line as the 64K tail drops its front, including among identical lines and mid-line cuts. It flows through CommandOutput::origin.
  • crates/app/src/selectable_text.rs: SelectableText::new(id, text).highlights(ranges), a reusable element. It takes its font, size and colour from the parent, so any row can wrap its text in it without changes to the element.
    • Hit-testing maps a point to the nearest caret and to the character under the pointer, row by row across soft wraps. Selection fills run start-to-edge, whole rows, then edge-to-end, like the markdown viewer and editors; at a soft wrap the start takes the next row so no sliver is left after the previous one. The colour is the theme's selection token, the same one the markdown viewer uses.
    • Input: cmd-c/cmd-a are bound in its SelectableText key context to the shared Copy/SelectAll actions, so the Edit menu reaches it too. With nothing selected, Copy propagates so the window's markdown selection still copies. Right-click uses the app's existing gpui-component context menu (as the terminal does): Copy (disabled when nothing is selected) and Select all. A click anywhere else clears the selection, except the click that uses or dismisses that menu.
    • Mouse handling: a press inside the text stops propagation, so the row behind does not take it as a click. A drag that ends on the row header no longer collapses the row, and the window's markdown selection layer does not also start a selection. The drag follows the pointer in the capture phase.
    • Performance: nothing runs when idle. During a drag it notifies only when the selected range actually changes. Painting is at most three quads. Span background fills are lifted under the selection so it stays visible over coloured output; the fonts in the runs don't change, so GPUI's line layout cache still hits. A drag held past the output scroller's edge scrolls it (.autoscroll(&ScrollHandle)), with a 33ms step timer only during that drag. The existing 2,000-span and 64K tail caps still apply. If a caller draws the text truncated, the element stays unselectable rather than selecting wrong offsets.
  • crates/app/src/transcript/command_output.rs: the output uses the element. Nothing else changes: idle renders of the old and new builds are byte-identical.
  • crates/app/src/snapshot.rs: the offscreen renderer gains --double-click, --triple-click, --shift-click, --right-click, --drag x1,y1,x2,y2 and --keys. They replay in command-line order with --click and feat(native): timeline minimap #71's --hover and --scroll. This was needed for the evidence below because events posted to a real window are dropped without accessibility trust. Note: it runs on the real platform, so --keys cmd-c would reach the system clipboard.

Not in this PR: highlighting the command line (ShellCommandBlock), adopting the element in other transcript rows, and TR-CITE.

Ledger

TR-CMDOUT stays partial. The Feature text now reads "live output stream; the output is selectable and copyable, the command line is not syntax-highlighted yet". Native adds crates/core/src/text_selection.rs and crates/app/src/selectable_text.rs, and Issue is #64, #69. The rest of the table is only re-padded.

Evidence

Tests:

  • t3-core text_selection::tests: word and line boundaries (spaces, punctuation, past line end, empty line, Unicode), drags both ways, word and line growth, shift-click extension, select all and char boundaries. Copy across styled spans uses the real span pipeline. Remap covers appended and redrawn-below output (kept), a redrawn selection (dropped), and a real TerminalOutputState tail past its cap (shifted, or dropped once the selected lines scroll out).
  • t3-native selectable_text::tests: hit-testing against constructed GPUI line layouts: nearest boundary vs character under the pointer, row edges, above/below the text, newline offsets, and wrapped rows. Selection fills cover one row, across wrapped rows, start or end at a soft wrap, across lines, and a selected newline alone.

Screenshots (GPUI offscreen renderer via --snapshot against the smoke server, "Native client showcase", vp test run expanded, the same --drag replayed on both builds; full windows are g-*.png, crops gc-*.png), in /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots69/:

  • Before (base 63cd8da): gc-before-dark.png, gc-before-light.png. The drag selects nothing.
  • After: gc-after-dark.png, gc-after-light.png. The drag selects across three lines.
  • After, other gestures: gc-dbl.png (double-click "math"), gc-triple.png (triple-click a line), gc-shift.png (click then shift-click), gc-all.png (cmd-a), gc-dragout.png (drag ending on the row header: the row stays open), gc-away.png (click elsewhere clears), gc-menu.png / gc-after-menu-light.png (right-click: Copy ⌘C enabled, Select all ⌘A).

Copy with cmd-c was not run against the system clipboard, to avoid overwriting the developer's clipboard. It is covered by the core copy test plus the menu showing Copy enabled for the live selection.

A snapshot run that ends with a gpui-component context menu open exits non-zero at teardown ("leaked PopupMenu handle"). The PNG is written first. This is pre-existing: it reproduces with this PR's menu hook removed, and removing the window first doesn't help.

Review findings and their fixes are in the PR conversation.

Gates (from apps/native): cargo clippy --workspace --all-targets -- -D warnings (also with --features snapshot), cargo test --workspace, node scripts/gen-theme.ts --check, node --test parity/sync.test.ts, and, from packages/client-runtime, node ../../apps/native/conformance/generate.ts --check all pass.

Closes #69

@rynfar rynfar mentioned this pull request Nov 2, 2026
Command output can now be selected and copied: drag (across wrapped lines),
double-click a word, triple-click a line, shift-click to extend, Cmd+C,
Cmd+A, and a context menu with Copy. A click elsewhere clears it, and the
selection holds while output streams in below it.

The selection rules (word and line boundaries, drag growth, copy, carrying a
selection across appended or tail-truncated text) live in
t3_core::text_selection; crates/app/src/selectable_text.rs maps the pointer
to offsets and paints. The offscreen snapshot gains double/triple/shift/right
clicks, drags, scrolls and keystrokes.
- Remap compares the selected bytes once instead of trying every line
  boundary; the terminal fold now reports where its text starts
  (origin), so a selection follows its own line when the tail drops its
  front, including among identical lines and mid-line cuts.
- Word selection walks graphemes: marks and emoji modifiers stay with
  their base, and a caret never splits one.
- ANSI span fills are lifted under the selection so it stays visible.
- A drag held past the output scroller's edge scrolls it.
@rynfar

rynfar commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Review findings: all five fixed

Rebased on native (after #71). The ordered snapshot gestures now include #71's --hover, and its --scroll (which waits for hover before the wheel). Gestures replay in command-line order.

  1. Remap was quadratic. Fixed. Selection::remap(old, new, dropped) no longer searches. It compares the selected bytes once at their moved position, so the cost is O(selection) whatever repeats around them. Covered by a_redraw_under_a_selection_among_many_repeated_lines_drops_it (64 KiB of a\n, final X becomes Y).
  2. Identity under the bounded tail. Fixed the way you suggested. The terminal fold now reports TerminalOutputState::origin: where text starts in everything folded, in plain UTF-8 bytes.
    • Lines count as they settle, and front trims of the window's first line count too, so mid-line cuts are covered. The text ends where the window ends, so origin = window start + window length − plain text length.
    • It flows through CommandOutput::origin into SelectableText::origin, and the selection shifts by the origin delta. A replace frame resets the fold; an origin that falls back is compared in place.
    • Tests: a_selection_follows_its_line_among_identical_ones (your tick case, now 0..4), a_selection_follows_a_line_cut_mid_way (newline-free output over the cap) and origin_counts_the_plain_bytes_the_tail_dropped (escapes and multibyte text across 20 truncating folds).
  3. Graphemes. Fixed. Word ranges walk graphemes via unicode-segmentation (already in the lockfile through GPUI), each classified by its base character, and a Char caret floors to a grapheme start. Covered by a_double_click_keeps_marks_and_modifiers_with_their_base (decomposed café and 👍🏽, including clicks on the mark and the modifier themselves).
  4. ANSI fills hid the selection. Fixed. Span background fills are lifted from the runs inside the selected range, the way ::selection overrides them. Fills outside the range and span colours are unchanged, and the runs' fonts don't change, so shaping stays cached. Covered by a_selection_lifts_span_fills_only_where_it_runs. There is no capture: nothing on the smoke server prints background-coloured output.
  5. No auto-scroll. Fixed. SelectableText::autoscroll(&ScrollHandle) is given the output scroller. A drag past its top or bottom edge scrolls it by the overshoot, at most 48px per step. It keeps stepping every 33ms while the pointer is held there, and stops on release, on return inside, or at the end. The timer exists only during such a drag. Covered by a_drag_past_the_scrollers_edge_scrolls_toward_it. There is no capture: every output on the smoke server fits inside 320px.

Verified against the old logic: with 653ff6f's remap and word_range_at grafted back in, the three new behavior tests fail (…identical_ones, …cut_mid_way, …marks_and_modifiers…). They pass now.

Gates are green: cargo clippy --workspace --all-targets -- -D warnings (also with --features snapshot), cargo test --workspace, gen-theme --check, parity/sync.test.ts, and conformance/generate.ts --check, which replays the seeded fold corpus through the new origin bookkeeping.

@rynfar
rynfar force-pushed the nw/69-selectable-text branch from 653ff6f to dfb3045 Compare November 2, 2026 21:13
- A line trimmed below another counts its lost start toward the tail's
  origin, so a selection in it survives the line above scrolling out.
- Drag steps hit-test the painted layout offset by any scroll since that
  paint, and the release settles the endpoint.
@rynfar

rynfar commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Follow-up findings: both fixed (b35ee39)

  1. Long line below another. The fold now counts a line's lost start toward window_start whatever row it is on, not only row 0. A line long enough to be trimmed fills the tail by itself, so nothing above it is drawn while that holds, and offsets from the trimmed line onward stay consistent. With your repro, origin is now 11, not 7. Tests: a_selection_follows_a_long_line_cut_below_another and a new case in origin_counts_the_plain_bytes_the_tail_dropped. Both fail on dfb3045 (origin 7 vs 11).
  2. Auto-scroll endpoint lag. The state records the scroll offset its layout was painted at. Drag steps and auto-scroll ticks hit-test that layout shifted by the scroll since then (Lines::hit_scrolled). The release recomputes the endpoint before ending the drag. Test: a_drag_hits_text_where_a_scroll_since_the_last_paint_moved_it. On dfb3045 the same point hit the first row instead of the third.

Gates green: clippy (also with --features snapshot), cargo test --workspace, gen-theme, parity sync, conformance.

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