Module text_selection
Expand description
Shared text-selection primitives for TextInput, NotesApp, and any
future text editor (terminal scrollback selector, code editor, …).
This module owns:
Selection— anchor+cursor pair with helpers for the visible range.- Boundary walkers —
prev_word_boundary/next_word_boundary/word_boundaries/line_boundaries/prev_char_boundary/next_char_boundary. All return byte indices and stay on UTF-8 char boundaries by construction. - Clipboard helpers —
clipboard_copy/clipboard_pasteand the primary-selection pair, resolved against the clipboard of the currentcrate::host::HostServices(seecrate::clipboard): the compositor’s mirror for in-process chrome, the desktop clipboard for a native app, or an in-process buffer.
Word operations follow Unicode Standard Annex #29, so apostrophes, combining marks, non-Latin scripts, and Unicode numeric forms behave as coherent words instead of being reduced to an ASCII-style predicate.
Structs§
- Selection
- Anchor + cursor pair.
anchor == cursormeans “no live selection”. - Text
Position - A document byte offset plus the visual side of an ambiguous boundary.
Enums§
- Text
Affinity - Which shaped run owns a caret at a shared logical byte boundary.
Functions§
- clipboard_
copy - Push
textonto the system clipboard — the Ctrl+C buffer. - clipboard_
copy_ image - Push an RGBA8 image (
width * height * 4bytes, row-major, no padding) onto the system clipboard as image data, so it can be pasted into an image editor or chat. Returnsfalsewhen the buffer size is wrong or no backend can carry an image. Used by the PDF reader to copy a selected page region. - clipboard_
copy_ primary - Push
textonto the primary selection — the mouse-driven “select then middle-click paste” buffer, distinct from the Ctrl+C clipboard. Terminals and editors set this whenever a range is selected so a middle-click paste in any app receives it. Goes through the same clipboard asclipboard_copy. - clipboard_
paste - Pull the current clipboard contents as a UTF-8 string. Returns
Nonewhen the clipboard is empty / unavailable / non-text. - clipboard_
paste_ image - The image on the clipboard, when another app copied one (a screenshot,
an image from a viewer or a browser).
Nonewhen it holds no image. - clipboard_
paste_ primary - Read the primary selection as a UTF-8 string — what a middle-click
paste should insert.
Nonewhen empty or unavailable. - extend_
span_ drag - Word/line-granularity drag extension — the shared rule behind
double-click-then-drag in
TextInputand the reader. - grapheme_
boundary_ at_ or_ before - The nearest extended-grapheme boundary at or before
byte_idx. - line_
boundaries - Byte range
(start, end)of the line containingbyte_idx, where lines are linefeed-delimited (\n). The end is the index of the trailing\n, ortext.len()for the final line. Triple-click line selection uses this. - next_
char_ boundary - Step one user-perceived character to the right of
byte_idx. Seeprev_char_boundaryfor the compatibility note. - next_
word_ boundary - Mirror of
prev_word_boundaryfor forward movement: the next UAX #29 word start, or the end of the string. - next_
word_ boundary_ chars - Char index of the next word boundary at or after
char_idx— the char-indexed mirror ofnext_word_boundary. Used to extend a reader selection rightward by a whole word (Shift+Ctrl+Right). - prev_
char_ boundary - Step one user-perceived character to the left of
byte_idx. Extended grapheme clusters keep combining marks, emoji modifiers, flags, and ZWJ sequences together. The legacy function name is retained for API compatibility. - prev_
word_ boundary - Walk back to the previous UAX #29 word start. If the cursor is inside a word this returns that word’s start; at its start it returns the preceding word’s start. The result is always a UTF-8 boundary.
- prev_
word_ boundary_ chars - Char index of the previous word boundary at or before
char_idx— the char-indexed mirror ofprev_word_boundary. Used to extend a reader selection leftward by a whole word (Shift+Ctrl+Left). - try_
clipboard_ copy - Copy text only if a system clipboard backend accepts it.
- word_
boundaries - Byte range
(start, end)of the UAX #29 word containingbyte_idx, or the word immediately on the left when the index lands in following punctuation or whitespace. Used by double-click word selection. - word_
boundaries_ chars - Char range
(start, end)of the word containingchar_idx— the char-indexed mirror ofword_boundaries. Drives double-click word selection in a reflowable reader.