Skip to main content

Selection

Struct Selection 

Source
pub struct Selection {
    pub start: SelectionEnd,
    pub end: SelectionEnd,
    pub direction: Direction,
    /* private fields */
}
Expand description

A text selection.

Fields§

§start: SelectionEnd

The start of the selection.

§end: SelectionEnd

The end of the selection.

§direction: Direction

The last direction of the selection.

Implementations§

Source§

impl Selection

Source

pub fn new() -> Self

Creates a new empty Selection.

Source

pub fn clear(&mut self)

Clears the Selection.

Source

pub fn is_empty(&self) -> bool

A selection is empty when the start and end are the same.

Source

pub fn text(&self, paragraph: &Paragraph) -> String

Returns the selected text from the given Paragraph.

Source

pub fn active_end(&self) -> SelectionEnd

Returns the currently active SelectionEnd.

self.end if self.direction is Right, self.start otherwise.

Source

pub fn select_range(&mut self, start: SelectionEnd, end: SelectionEnd)

Select a new range.

self.start will be set to the smaller value, self.end to the larger.

§Example
use iced_selection::selection::{Selection, SelectionEnd};

let mut selection = Selection::default();

let start = SelectionEnd::new(5, 17);
let end = SelectionEnd::new(2, 8);

selection.select_range(start, end);

assert_eq!(selection.start, end);
assert_eq!(selection.end, start);
Source

pub fn change_selection(&mut self, new_end: SelectionEnd)

Updates the current selection by setting a new end point.

This method adjusts the selection range based on the provided new_end position. The current Direction is used to determine the new values:

  • If the current direction is Right (i.e., the selection goes from start to end), the range becomes (start, new_end). If new_end is before start, the direction is flipped to Left.

  • If it’s Left, the range becomes (new_end, end). If new_end is after end, the direction is flipped to Right.

§Example
use iced_selection::selection::{Direction, Selection, SelectionEnd};

let mut selection = Selection::default();

let start = SelectionEnd::new(5, 17);
let end = SelectionEnd::new(2, 8);

selection.select_range(start, end);

assert_eq!(selection.start, end);
assert_eq!(selection.end, start);
assert_eq!(selection.direction, Direction::Right);

let new_end = SelectionEnd::new(2, 2);

selection.change_selection(new_end);

assert_eq!(selection.start, new_end);
assert_eq!(selection.end, end);
assert_eq!(selection.direction, Direction::Left);
Source

pub fn change_selection_by_word( &mut self, new_end: SelectionEnd, paragraph: &Paragraph, )

Updates the current selection by setting a new end point, either to the start of the previous word, or to the next one’s end.

Source

pub fn change_selection_by_line( &mut self, new_line: usize, paragraph: &Paragraph, )

Updates the current selection by setting a new end point, either to the end of a following line, or the beginning of a previous one.

Source

pub fn select_word(&mut self, line: usize, index: usize, paragraph: &Paragraph)

Selects the word around the given grapheme position.

Source

pub fn select_left(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd to the left by one, wrapping to the previous line if possible and required.

Source

pub fn select_right(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd to the right by one, wrapping to the next line if possible and required.

Source

pub fn select_up(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd up by one, keeping track of the original grapheme index.

Source

pub fn select_down(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd down by one, keeping track of the original grapheme index.

Source

pub fn select_left_by_words(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd to the previous start of a word on its current line, or the previous line if it exists and index == 0.

Source

pub fn select_right_by_words(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd to the next end of a word on its current line, or the next line if it exists and index == line.len().

Source

pub fn select_line_beginning(&mut self)

Moves the active SelectionEnd to the beginning of its current line.

Source

pub fn select_line_end(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd to the end of its current line.

Source

pub fn select_beginning(&mut self)

Moves the active SelectionEnd to the beginning of the Paragraph.

Source

pub fn select_end(&mut self, paragraph: &Paragraph)

Moves the active SelectionEnd to the end of the Paragraph.

Source

pub fn select_line(&mut self, line: usize, paragraph: &Paragraph)

Selects an entire line.

Source

pub fn select_all(&mut self, paragraph: &Paragraph)

Selects the entire Paragraph.

Trait Implementations§

Source§

impl Clone for Selection

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 Selection

Source§

impl Debug for Selection

Source§

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

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

impl Default for Selection

Source§

fn default() -> Self

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

impl PartialEq for Selection

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 Selection

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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

§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.