#[non_exhaustive]
pub struct Selection { pub anchor: usize, pub active: usize, pub h_pos: Option<f64>, }
Expand description

A range of selected text, or a caret.

A caret is the blinking vertical bar where text is to be inserted. We represent it as a selection with zero length, where anchor == active. Indices are always expressed in UTF-8 bytes, and must be between 0 and the document length, inclusive.

As an example, if the input caret is at the start of the document hello world, we would expect both anchor and active to be 0. If the user holds shift and presses the right arrow key five times, we would expect the word hello to be selected, the anchor to still be 0, and the active to now be 5.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§anchor: usize

The ‘anchor’ end of the selection.

This is the end of the selection that stays unchanged while holding shift and pressing the arrow keys.

§active: usize

The ‘active’ end of the selection.

This is the end of the selection that moves while holding shift and pressing the arrow keys.

§h_pos: Option<f64>

The saved horizontal position, during vertical movement.

This should not be set by the IME; it will be tracked and handled by the text field.

Implementations§

§

impl Selection

pub fn new(anchor: usize, active: usize) -> Selection

Create a new Selection with the provided anchor and active positions.

Both positions refer to UTF-8 byte indices in some text.

If your selection is a caret, you can use Selection::caret instead.

pub fn caret(index: usize) -> Selection

Create a new caret (zero-length selection) at the provided UTF-8 byte index.

index must be a grapheme cluster boundary.

pub fn with_h_pos(self, h_pos: Option<f64>) -> Selection

Construct a new selection from this selection, with the provided h_pos.

Note

h_pos is used to track the pixel location of the cursor when moving vertically; lines may have available cursor positions at different positions, and arrowing down and then back up should always result in a cursor at the original starting location; doing this correctly requires tracking this state.

You probably don’t need to use this, unless you are implementing a new text field, or otherwise implementing vertical cursor motion, in which case you will want to set this during vertical motion if it is not already set.

pub fn constrained(self, s: &str) -> Selection

Create a new selection that is guaranteed to be valid for the provided text.

pub fn min(&self) -> usize

Return the position of the upstream end of the selection.

This is end with the lesser byte index.

Because of bidirectional text, this is not necessarily “left”.

pub fn max(&self) -> usize

Return the position of the downstream end of the selection.

This is the end with the greater byte index.

Because of bidirectional text, this is not necessarily “right”.

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

The sequential range of the document represented by this selection.

This is the range that would be replaced if text were inserted at this selection.

pub fn len(&self) -> usize

The length, in bytes of the selected region.

If the selection is a caret, this is 0.

pub fn is_caret(&self) -> bool

Returns true if the selection’s length is 0.

Trait Implementations§

§

impl Clone for Selection

§

fn clone(&self) -> Selection

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Selection

§

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

Formats the value using the given formatter. Read more
§

impl Default for Selection

§

fn default() -> Selection

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

impl PartialEq<Selection> for Selection

§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Copy for Selection

§

impl StructuralPartialEq for Selection

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AnyEq for Twhere T: Any + PartialEq<T>,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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> IsDefault for Twhere T: Default + PartialEq<T> + Copy,

§

fn is_default(&self) -> bool

Checks that type has a default value.
§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more