Commit a71794d
committed
text: drag-select and Ctrl+C copy
Closes #36.
<!-- drag-drop the multi-paragraph selection screenshot here -->
Drag-select on `text(...)` and `rich_text![...]` widgets, plus a `selectable_group` to coordinate selection across siblings (which is how `markdown::view_with` ends up with multi-paragraph selection). Off by default; existing apps see no behavior change.
## Usage
```rust
text("hello").selectable(true)
rich_text![span("foo")].selectable(true)
markdown::view_with(
&items,
Settings { selectable: true, group_selection: true, ..settings },
&viewer,
)
```
## What's in here
- Selection on `Rich` and `Text`: per-widget drag, `Ctrl+C` copies the rendered text. Multibyte chars at the selection edges are snapped to UTF-8 boundaries.
- New `selectable_group(content)` widget that walks its subtree, finds the `Rich` widgets, and coordinates a single drag across them. When the drag crosses into a sibling, the originator's selection extends to its end, intermediate widgets get fully selected, and the destination is selected from its start to the cursor. `Ctrl+C` joins them with a newline.
- `markdown::Settings::{selectable, group_selection}` to opt in for markdown surfaces.
- Selection rectangles use the same glyph-walking algorithm as `text_editor`'s `highlight_line`, so wrapping and `align_x` / `align_y` work without extra code.
- New `Paragraph::selection_bounds(start, end) -> Vec<Rectangle>` trait method with a default empty impl, so non-glyph renderers (e.g. the null one) are unaffected.
- `text::Style::selection: Color` for the highlight, defaulting to `palette.primary.weak.color` to match `text_input` / `text_editor`.
- Mouse cursor switches to `Interaction::Text` over selectable widgets.
## Cross-widget coordination
When `selectable_group` is in the tree, it sets an `externally_managed` flag on every `Rich::State` it walks into, so individual widgets skip their own drag-select / `Ctrl+C` handling and just render whatever selection the group writes in. The group owns drag origin + focus tracking and recomputes per-widget byte ranges on each `CursorMoved`.
`Rich::State` is exposed as a `pub struct` with a minimal accessor set (`selection`, `set_selection`, `paragraph`, `text_len`, `selection_text`, `set_externally_managed`). I couldn't find a clean way to downcast `tree::State` to a `&mut dyn Trait` so the group ends up concretely aware of `Rich`. Open to suggestions if there's a more elegant pattern.
The within-widget coordination (clicking outside a `Rich`'s text drops focus, so siblings self-clear on the same event) is the same trick `text_input` uses — every widget sees every press and reacts to its own bounds.
## Out of scope
- Plain `text(...)` inside `selectable_group` — only `rich_text` is coordinated. Plain text widgets still select on their own.
- RTL / bidi — same level as the existing `rich_text` rendering.
- Programmatic access to a group's selection through Operations — could be a follow-up if there's a use case.
- Screen reader narration — not regressed, not improved.
## Migration
- `text::Style` gained a `selection` field. Three internal callsites in `checkbox` / `radio` / `toggler`, plus the `color_maybe` helpers on `Rich` / `Text`, were updated to spread `..Style::default()`.
- `markdown::Settings` gained `selectable` and `group_selection`. Most consumers build it via `with_text_size` / `with_style` and won't notice; explicit struct literals need the new fields.
- `core::widget::text::State<P>` keeps its existing `paragraph::Plain<P>` alias so label-style widgets (`checkbox`, `radio`, `toggler`) compile unchanged. The `Text` widget itself uses a private `Internal<P>` that wraps `Plain<P>` with selection state.
## Note on markdown helpers
`unordered_list` and `ordered_list` were calling `view_with` recursively for nested bullet content. With the new wrapping behavior that would create a nested `selectable_group` per bullet, breaking cross-list selection. Switched them to call `items()` directly, which produces just the column without wrapping. `quote`, `table`, and `code_block` already used `item()` / `items()` and didn't need changes.
## Tested against
I've been running this in [oryxis](https://github.com/wilsonglasser/oryxis), my SSH client. The screenshot above is its AI chat panel rendering markdown via `view_with`. Selection works across paragraphs, headings, code blocks, numbered lists, nested bullets, and tables.1 parent 4255f61 commit a71794d
13 files changed
Lines changed: 1676 additions & 50 deletions
File tree
- core/src
- text
- widget
- operation
- graphics/src/text
- widget/src
- text
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
76 | 97 | | |
77 | 98 | | |
78 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
49 | 60 | | |
50 | 61 | | |
51 | 62 | | |
| |||
90 | 101 | | |
91 | 102 | | |
92 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
93 | 113 | | |
94 | 114 | | |
95 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
0 commit comments