Skip to main content

MultiCursorState

Struct MultiCursorState 

Source
pub struct MultiCursorState {
    pub selections: Vec<IdentifiedSelection>,
    pub node_id: DomNodeId,
    pub contenteditable_key: u64,
}
Expand description

Multi-cursor state for a contenteditable element (Sublime Text style).

Replaces the split CursorManager + SelectionManager pattern for text editing. Supports multiple simultaneous cursors/selections, each with a stable ID.

§Invariants

  • selections is sorted by position and non-overlapping.
  • The primary selection is the last one added (highest index).
  • After any mutation, merge_overlapping() is called to maintain invariants.

Fields§

§selections: Vec<IdentifiedSelection>

Sorted by position, non-overlapping. Primary = last added (highest index).

§node_id: DomNodeId

The DOM node this multi-cursor state applies to.

§contenteditable_key: u64

Stable key that survives DOM rebuilds (from calculate_contenteditable_key).

Implementations§

Source§

impl MultiCursorState

Source

pub fn new_with_cursor( cursor: TextCursor, node_id: DomNodeId, contenteditable_key: u64, ) -> Self

Create a new MultiCursorState with a single cursor.

Source

pub fn add_cursor(&mut self, cursor: TextCursor) -> SelectionId

Add a cursor, merging if it overlaps with existing selections. Returns the SelectionId of the new (or merged) cursor.

Source

pub fn add_selection(&mut self, range: SelectionRange) -> SelectionId

Add a selection range, merging if it overlaps. Returns the SelectionId of the new (or merged) selection.

Source

pub fn remove_selection(&mut self, id: SelectionId) -> bool

Remove a selection by its stable ID. Returns true if found and removed.

Source

pub fn get_primary(&self) -> Option<&IdentifiedSelection>

Get the primary selection (last added = highest index).

Source

pub fn get_primary_mut(&mut self) -> Option<&mut IdentifiedSelection>

Get a mutable reference to the primary selection.

Source

pub fn get_primary_cursor(&self) -> Option<TextCursor>

Get the primary cursor position (for scroll-into-view, IME, etc.)

Source

pub fn to_selections(&self) -> Vec<Selection>

Convert to a Vec for passing to edit_text().

Source

pub fn update_from_edit_result(&mut self, new_selections: &[Selection])

Update selections from the result of edit_text().

Preserves existing IDs where possible (by index), assigns new IDs for extras.

Source

pub fn set_single_cursor(&mut self, cursor: TextCursor)

Set all selections to a single cursor (e.g., on plain click without Ctrl).

Source

pub fn set_single_range(&mut self, range: SelectionRange)

Set all selections to a single range.

Source

pub fn len(&self) -> usize

Number of active cursors/selections.

Source

pub fn is_empty(&self) -> bool

Whether there are no selections (should not normally happen).

Source

pub fn merge_overlapping(&mut self)

Sort selections by position and merge any that overlap.

Source

pub fn move_all_cursors( &mut self, extend_selection: bool, move_fn: impl Fn(&TextCursor) -> TextCursor, )

Move all cursors using a movement function. Merges collisions afterward.

move_fn takes a TextCursor and returns the new TextCursor after movement. If extend_selection is true, the anchor stays and only the focus moves, creating or extending a range.

Source

pub fn remap_node_ids( &mut self, dom_id: DomId, node_id_map: &BTreeMap<NodeId, NodeId>, )

Remap the NodeId in node_id after DOM reconciliation.

If the node was removed (not in the map), the multi-cursor state is cleared.

Trait Implementations§

Source§

impl Clone for MultiCursorState

Source§

fn clone(&self) -> MultiCursorState

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 Debug for MultiCursorState

Source§

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

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

impl PartialEq for MultiCursorState

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

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

impl StructuralPartialEq for MultiCursorState

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

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 = Infallible

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

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

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.