Skip to main content

Module text_selection

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_paste and the primary-selection pair, resolved against the clipboard of the current crate::host::HostServices (see crate::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 == cursor means “no live selection”.
TextPosition
A document byte offset plus the visual side of an ambiguous boundary.

Enums§

TextAffinity
Which shaped run owns a caret at a shared logical byte boundary.

Functions§

clipboard_copy
Push text onto the system clipboard — the Ctrl+C buffer.
clipboard_copy_image
Push an RGBA8 image (width * height * 4 bytes, row-major, no padding) onto the system clipboard as image data, so it can be pasted into an image editor or chat. Returns false when 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 text onto 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 as clipboard_copy.
clipboard_paste
Pull the current clipboard contents as a UTF-8 string. Returns None when 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). None when it holds no image.
clipboard_paste_primary
Read the primary selection as a UTF-8 string — what a middle-click paste should insert. None when empty or unavailable.
extend_span_drag
Word/line-granularity drag extension — the shared rule behind double-click-then-drag in TextInput and 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 containing byte_idx, where lines are linefeed-delimited (\n). The end is the index of the trailing \n, or text.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. See prev_char_boundary for the compatibility note.
next_word_boundary
Mirror of prev_word_boundary for 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 of next_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 of prev_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 containing byte_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 containing char_idx — the char-indexed mirror of word_boundaries. Drives double-click word selection in a reflowable reader.