pub struct TextFieldState {
pub inner: MutableState<Rc<RefCell<TextFieldStateInner>>>,
/* private fields */
}Expand description
Re-export the UI crate so applications can depend on a single crate. 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: MutableState<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>) -> TextFieldState
pub fn new(initial_text: impl Into<String>) -> TextFieldState
Creates a new text field state with the given initial text.
Sourcepub fn with_selection(
initial_text: impl Into<String>,
selection: TextRange,
) -> TextFieldState
pub fn with_selection( initial_text: impl Into<String>, selection: TextRange, ) -> TextFieldState
Creates a state with initial text and selection.
pub fn id(&self) -> u64
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) -> boolwhere
F: FnOnce(&mut TextFieldBuffer),
pub fn edit<F>(&self, f: F) -> boolwhere
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.)
Returns false if another edit is already in progress.
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).
Trait Implementations§
Source§impl Clone for TextFieldState
impl Clone for TextFieldState
Source§fn clone(&self) -> TextFieldState
fn clone(&self) -> TextFieldState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for TextFieldState
Source§impl Debug for TextFieldState
impl Debug for TextFieldState
Source§impl Default for TextFieldState
impl Default for TextFieldState
Source§fn default() -> TextFieldState
fn default() -> TextFieldState
Auto Trait Implementations§
impl Freeze for TextFieldState
impl RefUnwindSafe for TextFieldState
impl Send for TextFieldState
impl Sync for TextFieldState
impl Unpin for TextFieldState
impl UnsafeUnpin for TextFieldState
impl UnwindSafe for TextFieldState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.