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
impl TextFieldState
Sourcepub fn new(initial_text: impl Into<String>) -> Self
pub fn new(initial_text: impl Into<String>) -> Self
Creates a new text field state with the given initial text.
Sourcepub fn with_selection(
initial_text: impl Into<String>,
selection: TextRange,
) -> Self
pub fn with_selection( initial_text: impl Into<String>, selection: TextRange, ) -> Self
Creates a state with initial text and selection.
Sourcepub fn desired_column(&self) -> Option<usize>
pub fn desired_column(&self) -> Option<usize>
Gets the desired column for up/down navigation.
Sourcepub fn set_desired_column(&self, col: Option<usize>)
pub fn set_desired_column(&self, col: Option<usize>)
Sets the desired column for up/down navigation.
Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Returns the current text content. Creates composition dependency when read during composition.
Sourcepub fn composition(&self) -> Option<TextRange>
pub fn composition(&self) -> Option<TextRange>
Returns the current composition (IME) range, if any.
Sourcepub fn line_offsets(&self) -> Vec<usize>
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.
Sourcepub fn copy_selection(&self) -> Option<String>
pub fn copy_selection(&self) -> Option<String>
Copies the selected text without modifying the clipboard. Returns the selected text, or None if no selection.
Sourcepub fn value(&self) -> TextFieldValue
pub fn value(&self) -> TextFieldValue
Returns the current value snapshot. Creates composition dependency when read during composition.
Sourcepub fn add_listener(
&self,
listener: impl Fn(&TextFieldValue) + 'static,
) -> usize
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.
Sourcepub fn set_selection(&self, selection: TextRange)
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.
Sourcepub fn edit<F>(&self, f: F)where
F: FnOnce(&mut TextFieldBuffer),
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).
Sourcepub fn flush_undo_group(&self)
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).
Sourcepub fn set_text_and_select_all(&self, text: impl Into<String>)
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
impl Clone for TextFieldState
Source§fn clone(&self) -> TextFieldState
fn clone(&self) -> TextFieldState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more