TextFieldState

Struct TextFieldState 

Source
pub struct TextFieldState {
    pub inner: Rc<RefCell<TextFieldStateInner>>,
    /* private fields */
}
Expand description

Observable state holder for text field content.

This is the primary API for managing text field state. All edits go through the edit method which provides a mutable buffer.

§Example

use cranpose_foundation::text::TextFieldState;

let state = TextFieldState::new("Hello");

// Edit the text
state.edit(|buffer| {
    buffer.place_cursor_at_end();
    buffer.insert(", World!");
});

assert_eq!(state.text(), "Hello, World!");

§Thread Safety

TextFieldState uses Rc<RefCell<...>> internally and is not thread-safe. It should only be used from the main thread.

Fields§

§inner: Rc<RefCell<TextFieldStateInner>>

Internal state for editing machinery. Public for cross-crate pointer-based identity comparison (Hash).

Implementations§

Source§

impl TextFieldState

Source

pub fn new(initial_text: impl Into<String>) -> Self

Creates a new text field state with the given initial text.

Source

pub fn with_selection( initial_text: impl Into<String>, selection: TextRange, ) -> Self

Creates a state with initial text and selection.

Source

pub fn desired_column(&self) -> Option<usize>

Gets the desired column for up/down navigation.

Source

pub fn set_desired_column(&self, col: Option<usize>)

Sets the desired column for up/down navigation.

Source

pub fn text(&self) -> String

Returns the current text content. Creates composition dependency when read during composition.

Source

pub fn selection(&self) -> TextRange

Returns the current selection range.

Source

pub fn composition(&self) -> Option<TextRange>

Returns the current composition (IME) range, if any.

Source

pub fn line_offsets(&self) -> Vec<usize>

Returns cached line start offsets for efficient multiline operations.

Each entry is the byte offset where a line starts. For example:

  • “ab\ncd” -> [0, 3] (line 0 starts at 0, line 1 starts at 3)
  • “” -> [0]

The cache is lazily computed on first access and invalidated on text change. This avoids O(n) string splitting on every frame during selection rendering.

Source

pub fn copy_selection(&self) -> Option<String>

Copies the selected text without modifying the clipboard. Returns the selected text, or None if no selection.

Source

pub fn value(&self) -> TextFieldValue

Returns the current value snapshot. Creates composition dependency when read during composition.

Source

pub fn add_listener( &self, listener: impl Fn(&TextFieldValue) + 'static, ) -> usize

Adds a listener that is called when the value changes.

Returns the listener index for removal.

Source

pub fn set_selection(&self, selection: TextRange)

Sets the selection directly without going through undo stack. Use this for transient selection changes like during drag selection.

Source

pub fn can_undo(&self) -> bool

Returns true if undo is available.

Source

pub fn can_redo(&self) -> bool

Returns true if redo is available.

Source

pub fn undo(&self) -> bool

Undoes the last edit. Returns true if undo was performed.

Source

pub fn redo(&self) -> bool

Redoes the last undone edit. Returns true if redo was performed.

Source

pub fn edit<F>(&self, f: F)
where F: FnOnce(&mut TextFieldBuffer),

Edits the text field content.

The provided closure receives a mutable buffer that can be used to modify the text and selection. After the closure returns, the changes are committed and listeners are notified.

§Undo Coalescing

Consecutive character insertions within the coalescing timeout are grouped into a single undo entry. The group breaks when:

  • Timeout expires (1 second between edits)
  • Whitespace or newline is typed
  • Cursor position jumps (non-consecutive insert)
  • A non-insert operation occurs (delete, paste multi-char, etc.)
§Panics

Panics if called while already editing (no concurrent or nested edits).

Source

pub fn flush_undo_group(&self)

Flushes any pending undo snapshot to the undo stack. Call this when a coalescing break is desired (e.g., focus lost).

Source

pub fn set_text(&self, text: impl Into<String>)

Sets the text and places cursor at end.

Source

pub fn set_text_and_select_all(&self, text: impl Into<String>)

Sets the text and selects all.

Trait Implementations§

Source§

impl Clone for TextFieldState

Source§

fn clone(&self) -> TextFieldState

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TextFieldState

Source§

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

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

impl Default for TextFieldState

Source§

fn default() -> Self

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

impl PartialEq for TextFieldState

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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.

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.