pub struct TextSelection { /* private fields */ }Expand description
A caret (collapsed) or a selected range within a Paragraph.
Positions are byte indices into the paragraph’s text, on character (grapheme cluster)
boundaries. A selection belongs to the paragraph it was made from: indices read from it
(range, anchor, focus) are
meaningless for another text, and after Paragraph::reflow (or to carry positions to a
new paragraph) call refreshed. Everything here is allocation-free.
Equality compares anchor and focus positions with their affinities; the sticky
horizontal position used by Up/Down and the word/line granularity of a drag are not
compared.
Implementations§
Source§impl TextSelection
impl TextSelection
Sourcepub fn caret_at(paragraph: &Paragraph, index: usize) -> Self
pub fn caret_at(paragraph: &Paragraph, index: usize) -> Self
A caret before the character at byte index (clamped to the text).
Sourcepub fn caret_with(
paragraph: &Paragraph,
index: usize,
affinity: Affinity,
) -> Self
pub fn caret_with( paragraph: &Paragraph, index: usize, affinity: Affinity, ) -> Self
A caret at byte index (clamped to the text) with the given affinity.
Sourcepub fn from_range(paragraph: &Paragraph, anchor: usize, focus: usize) -> Self
pub fn from_range(paragraph: &Paragraph, anchor: usize, focus: usize) -> Self
The range from anchor to focus (byte indices, clamped to the text; focus may
come first): a programmatic selection, such as a search result or an undo restore.
Sourcepub fn select_all(paragraph: &Paragraph) -> Self
pub fn select_all(paragraph: &Paragraph) -> Self
The whole text, focus at the end.
Sourcepub fn at_point(paragraph: &Paragraph, point: Point) -> Self
pub fn at_point(paragraph: &Paragraph, point: Point) -> Self
A caret at the text position nearest point (relative to the paragraph’s top-left
corner): what a click places.
Sourcepub fn word_at_point(paragraph: &Paragraph, point: Point) -> Self
pub fn word_at_point(paragraph: &Paragraph, point: Point) -> Self
The word under point (a double click).
Sourcepub fn line_at_point(paragraph: &Paragraph, point: Point) -> Self
pub fn line_at_point(paragraph: &Paragraph, point: Point) -> Self
The visual line under point (a triple click).
Sourcepub fn extend_to_point(self, paragraph: &Paragraph, point: Point) -> Self
pub fn extend_to_point(self, paragraph: &Paragraph, point: Point) -> Self
The selection with its focus dragged to the position nearest point. A selection
begun by word or line keeps extending by whole words or lines.
Sourcepub fn shift_click(self, paragraph: &Paragraph, point: Point) -> Self
pub fn shift_click(self, paragraph: &Paragraph, point: Point) -> Self
The selection extended to point by a shift+click: the focus lands where clicked,
and a word or line selection keeps the far side of its word or line as the anchor.
Sourcepub fn moved(self, paragraph: &Paragraph, motion: Motion, extend: bool) -> Self
pub fn moved(self, paragraph: &Paragraph, motion: Motion, extend: bool) -> Self
The selection after motion: the caret moves, or with extend the focus moves and
the anchor stays (shift + navigation keys).
Sourcepub fn refreshed(self, paragraph: &Paragraph) -> Self
pub fn refreshed(self, paragraph: &Paragraph) -> Self
The same anchor and focus (clamped to paragraph’s text), with everything derived
from the layout recomputed: call after Paragraph::reflow or to carry a selection to
a new paragraph. Pixel-based state (the sticky column of Up/Down, a word or line
drag’s granularity) is dropped, since the lines it was measured on are gone.
Sourcepub fn focus_affinity(&self) -> Affinity
pub fn focus_affinity(&self) -> Affinity
The caret’s affinity at the focus.
Sourcepub fn caret_rect(&self, paragraph: &Paragraph, width: f32) -> Rect
pub fn caret_rect(&self, paragraph: &Paragraph, width: f32) -> Rect
The caret’s rectangle at the focus, width pixels wide, relative to the paragraph’s
top-left corner.
Sourcepub fn for_each_rect(&self, paragraph: &Paragraph, f: impl FnMut(Rect))
pub fn for_each_rect(&self, paragraph: &Paragraph, f: impl FnMut(Rect))
Calls f with each rectangle covering the selection (one or more per line), relative
to the paragraph’s top-left corner. Nothing for a caret.