Prose.TextWithCaret

References
Represents a string with caret location and block selection. This is useful for mimicking text area inputs that are not tied to specific controls.

All positions (Caret, Block and the block bounds) are measured in grapheme clusters, not char code units. The text is segmented into complete grapheme runs (base character plus any combining marks, and whole surrogate pairs) via Prose.Graphemes, so the caret can never land inside a cluster and no edit can split one into invalid UTF-16.

Constructors

TextWithCaret

#
public TextWithCaret(string text, int caret, int block)
text (string)The text content.
caret (int)Caret position, measured in grapheme clusters.
block (int)Selection length, measured in grapheme clusters (signed, relative to the caret).

TextWithCaret

#
public TextWithCaret(string text)
text (string)

Properties

Block

#
public int Block { get; }
Gets the length of the selection block (in grapheme clusters), starting at the caret.

BlockEnd

#
public int BlockEnd { get; }
Gets the index of the right edge of the block.

BlockStart

#
public int BlockStart { get; }
Gets the index of the left edge of the block.

Caret

#
public int Caret { get; }
Gets the position of the caret, measured in grapheme clusters.

Empty

#
public static Prose.TextWithCaret Empty { get; }

IsEmpty

#
public bool IsEmpty { get; }
Gets whether the Text field is null or empty.

Length

#
public int Length { get; }
Gets the number of grapheme clusters in the text.

SelectedText

#
public string SelectedText { get; }
Gets the currently selected text.

Text

#
public string Text { get; }
Gets the string inside this instance.

Methods

ControlCaretLeft

#
public Prose.TextWithCaret ControlCaretLeft()
Perform the default caret+block modification based on a COMMAND+LEFT arrow key press.

ControlCaretRight

#
public Prose.TextWithCaret ControlCaretRight()
Perform the default caret+block modification based on a COMMAND+Right arrow key press.

DeleteAfter

#
public Prose.TextWithCaret DeleteAfter()
Delete either the block or, if there is no block, the grapheme after the caret.

DeleteBefore

#
public Prose.TextWithCaret DeleteBefore()
Delete either the block or, if there is no block, the grapheme before the caret.

DeleteBlock

#
public Prose.TextWithCaret DeleteBlock()
Delete the block, if any.

Equals

#
public override bool Equals(object obj)
obj (object)
Object.Equals

Equals

#
public bool Equals(TextWithCaret other)
other (Prose.TextWithCaret)

GetHashCode

#
public override int GetHashCode()

HandleKeyDown

#
public Prose.TextWithCaret HandleKeyDown(KeyEventArgs e, out CaretDeparture departure)
Handle a key press, additionally reporting when the key asks to leave this text altogether (see Prose.CaretDeparture). A departing key never edits the text; whether it counts as handled depends on the key: the four editing keys always do - a text context owns them even when it cannot act, and letting them bubble would trigger shortcuts elsewhere - while up and down stay unhandled, so hosts which don't accept the departure keep their existing behaviour.
e (KeyEventArgs)
departure (out Prose.CaretDeparture)

HandleKeyDown

#
public Prose.TextWithCaret HandleKeyDown(KeyEventArgs e)
e (KeyEventArgs)

Insert

#
public Prose.TextWithCaret Insert(string segment, bool adjust = true, bool select = false)
Replace the block with a text fragment, or insert the fragment at the caret.
segment (string)Text to insert.
adjust (bool = true)If true, the caret is moved to the end of the inserted segment.
select (bool = false)If true, the selection block covers the inserted segment.

IsCharacterInBlock

#
public bool IsCharacterInBlock(int characterIndex)
Gets whether the grapheme at the given index is inside the selection.
characterIndex (int)

IsPositionInBlock

#
public bool IsPositionInBlock(int caretPosition)
Gets whether the given caret position is inside the selection. This is true even if the block length is zero.
caretPosition (int)

MoveCaret

#
public Prose.TextWithCaret MoveCaret(int steps = 1, bool clearBlock = true)
Move the caret left or right.
steps (int = 1)
clearBlock (bool = true)

MoveCaretLeft

#
public Prose.TextWithCaret MoveCaretLeft()
Perform the default caret+block modification based on a LEFT arrow key press.

MoveCaretLeft

#
public Prose.TextWithCaret MoveCaretLeft(Keys modifiers)
Move the caret to the left, with default behaviour based on modifier keys.
modifiers (Keys)
Modified text or this text if the caret could not be moved.

MoveCaretRight

#
public Prose.TextWithCaret MoveCaretRight()
Perform the default caret+block modification based on a Right arrow key press.

MoveCaretRight

#
public Prose.TextWithCaret MoveCaretRight(Keys modifiers)
Move the caret to the right, with default behaviour based on modifier keys.
modifiers (Keys)
Modified text or this text if the caret could not be moved.

ReplaceBlock

#
public Prose.TextWithCaret ReplaceBlock(string segment, bool adjust = true, bool select = false)
Replace the contents of the selected block with a different text fragment. If there is no selected block, this method does nothing.
segment (string)Text to insert.
adjust (bool = true)If true, the caret is moved to the end of the inserted segment.
select (bool = false)If true, the selection block covers the inserted segment.

SelectAll

#
public Prose.TextWithCaret SelectAll(bool caretAtStart)
Select the entire text.
caretAtStart (bool)

SelectWordAt

#
public Prose.TextWithCaret SelectWordAt(int position)
Select the word surrounding a caret position. A 'word' here is a run of graphemes which all classify the same way (letters and digits, whitespace, symbols), i.e. exactly the runs the by-word caret jumps step over.

The caret ends up at the end of the run, where typing is expected to continue, with the block reaching back to its start.

position (int)Caret position to expand from, in grapheme clusters.

ShiftCaretLeft

#
public Prose.TextWithCaret ShiftCaretLeft()
Perform the default caret+block modification based on a LEFT+SHIFT arrow key press.

ShiftCaretRight

#
public Prose.TextWithCaret ShiftCaretRight()
Perform the default caret+block modification based on a RIGHT+SHIFT arrow key press.

ShiftControlCaretLeft

#
public Prose.TextWithCaret ShiftControlCaretLeft()
Perform the default caret+block modification based on a LEFT+SHIFT arrow key press.

ShiftControlCaretRight

#
public Prose.TextWithCaret ShiftControlCaretRight()
Perform the default caret+block modification based on a LEFT+SHIFT arrow key press.

ToString

#
public override string ToString()
Object.ToString

WithBlock

#
public Prose.TextWithCaret WithBlock(int block = 0)
Modify the block of this text.
block (int = 0)

WithCaret

#
public Prose.TextWithCaret WithCaret(int caret)
Set the caret of this text.
caret (int)

WithCaretAtEnd

#
public Prose.TextWithCaret WithCaretAtEnd()
Move the caret to the end of this text.

WithCaretAtStart

#
public Prose.TextWithCaret WithCaretAtStart()
Move the caret to the start of this text.

Operators