Skip to main content

SelectionSnapshot Struct

Definition​

Assembly:Avalonia.Controls.Documents
Package:Avalonia.Controls.Documents

Value-type snapshot of a Avalonia.Controls.TextSelection captured at a point in time. Stores a frozen Avalonia.Controls.Documents.TextModel.TextPointer per endpoint together with the offset inside that endpoint's cached leaf and the child-index path from the document root down to that leaf.

public struct SelectionSnapshot

Inheritance: ValueType -> SelectionSnapshot

Implements: IEquatable<SelectionSnapshot>

Remarks​

Restoration uses a three-tier strategy:

  1. Fast path: if the captured leaf is still attached to the live tree (true for every non-structural edit and for the pre-edit side of a just-replayed structural edit), re-derive the global offset as Node.GlobalStartOffset + leafOffset.
  2. Path walk: when the captured leaf has been detached (structural Undo/Redo replaces node instances), walk the child-index path from the document root. MaterializeFromSnapshot preserves child ordering deterministically, so the path resolves to the correct rematerialized leaf.
  3. Last-resort fallback: offset-based pointer creation.

The leaf offset is frozen at capture time because the leaf's GlobalStartOffset may shift between capture and restore when earlier content changes. At restore time the global offset is re-derived from the (possibly replaced) leaf's current GlobalStartOffset plus this captured leafOffset.

Methods​

NameDescription
CaptureCaptures selection's two endpoints. Pass the result to Avalonia.Controls.Documents.Undo.UndoManager.BeginUndoUnit(string,Nullable{Avalonia.Controls.Documents.Undo.SelectionSnapshot}) as the pre-edit state, or to Avalonia.Controls.Documents.Undo.IUndoScope.SetSelectionAfter(Nullable{Avalonia.Controls.Documents.Undo.SelectionSnapshot}) as the post-edit state, to have the caret restored with the unit.
Equals (2 overloads)Returns if this snapshot equals other by reference equality on all components.
GetHashCodeNo summary available.

Capture Method​

Captures selection's two endpoints. Pass the result to Avalonia.Controls.Documents.Undo.UndoManager.BeginUndoUnit(string,Nullable{Avalonia.Controls.Documents.Undo.SelectionSnapshot}) as the pre-edit state, or to Avalonia.Controls.Documents.Undo.IUndoScope.SetSelectionAfter(Nullable{Avalonia.Controls.Documents.Undo.SelectionSnapshot}) as the post-edit state, to have the caret restored with the unit.

public Avalonia.Controls.Documents.Undo.SelectionSnapshot Capture(Avalonia.Controls.TextSelection selection)

Parameters​

selection Avalonia.Controls.TextSelection

The live selection to freeze.

Returns​

Avalonia.Controls.Documents.Undo.SelectionSnapshot

A snapshot of the selection's anchor and caret.

Exceptions​

Equals overloads​

Equals Method​

Returns if this snapshot equals other by reference equality on all components.

public bool Equals(Avalonia.Controls.Documents.Undo.SelectionSnapshot other)
Parameters​

other Avalonia.Controls.Documents.Undo.SelectionSnapshot

Returns​

bool

Equals Method​

public bool Equals(object obj)
Parameters​

obj object

Returns​

bool

GetHashCode Method​

public int GetHashCode()

Returns​

int

Properties​

NameDescription
AnchorFrozen anchor pointer. Shares its cached leaf with the source selection.
AnchorLeafOffsetOffset inside Avalonia.Controls.Documents.Undo.SelectionSnapshot.Anchor's leaf at capture time.
IsEmptyTrue when the anchor and moving pointers coincide.
MovingFrozen caret / moving pointer.
MovingLeafOffsetOffset inside Avalonia.Controls.Documents.Undo.SelectionSnapshot.Moving's leaf at capture time.

Anchor Property​

Frozen anchor pointer. Shares its cached leaf with the source selection.

public Avalonia.Controls.Documents.TextModel.TextPointer Anchor { get; set; }

AnchorLeafOffset Property​

Offset inside Avalonia.Controls.Documents.Undo.SelectionSnapshot.Anchor's leaf at capture time.

public int AnchorLeafOffset { get; set; }

IsEmpty Property​

True when the anchor and moving pointers coincide.

public bool IsEmpty { get; set; }

Moving Property​

Frozen caret / moving pointer.

public Avalonia.Controls.Documents.TextModel.TextPointer Moving { get; set; }

MovingLeafOffset Property​

Offset inside Avalonia.Controls.Documents.Undo.SelectionSnapshot.Moving's leaf at capture time.

public int MovingLeafOffset { get; set; }