#[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
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§
source§impl Selection
impl Selection
sourcepub fn new(anchor: usize, active: usize) -> 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.
sourcepub fn caret(index: usize) -> Selection
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.
sourcepub fn with_h_pos(self, h_pos: Option<f64>) -> Selection
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.
sourcepub fn constrained(self, s: &str) -> Selection
pub fn constrained(self, s: &str) -> Selection
Create a new selection that is guaranteed to be valid for the provided text.
sourcepub fn min(&self) -> usize
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”.
sourcepub fn max(&self) -> usize
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”.
sourcepub fn range(&self) -> Range<usize> ⓘ
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.