Skip to main content

TextSelection

Struct TextSelection 

Source
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

Source

pub fn caret_at(paragraph: &Paragraph, index: usize) -> Self

A caret before the character at byte index (clamped to the text).

Source

pub fn caret_with( paragraph: &Paragraph, index: usize, affinity: Affinity, ) -> Self

A caret at byte index (clamped to the text) with the given affinity.

Source

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.

Source

pub fn select_all(paragraph: &Paragraph) -> Self

The whole text, focus at the end.

Source

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.

Source

pub fn word_at_point(paragraph: &Paragraph, point: Point) -> Self

The word under point (a double click).

Source

pub fn line_at_point(paragraph: &Paragraph, point: Point) -> Self

The visual line under point (a triple click).

Source

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.

Source

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.

Source

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).

Source

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.

Source

pub fn collapsed(self) -> Self

A caret at the focus.

Source

pub fn is_caret(&self) -> bool

Whether nothing is selected (a caret).

Source

pub fn range(&self) -> Range<usize> ⓘ

The selected byte range, in text order.

Source

pub fn anchor(&self) -> usize

Where the selection started (stays put while extending).

Source

pub fn focus(&self) -> usize

Where the selection ends: the caret’s position.

Source

pub fn focus_affinity(&self) -> Affinity

The caret’s affinity at the focus.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for TextSelection

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for TextSelection

Source§

impl Debug for TextSelection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TextSelection

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for TextSelection

Source§

impl PartialEq for TextSelection

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TextSelection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Brush for T
where T: Clone + PartialEq + Default + Debug,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.