pub struct TextFieldBuffer { /* private fields */ }Expand description
A mutable text buffer that can be edited.
This provides methods for changing text content:
replace- Replace a range with new textappend- Add text at the endinsert- Insert text at cursor positiondelete- Delete a range of text
And for manipulating cursor/selection:
§Example
use cranpose_foundation::text::{TextFieldBuffer, TextRange};
let mut buffer = TextFieldBuffer::new("Hello");
buffer.place_cursor_at_end();
buffer.insert(", World!");
assert_eq!(buffer.text(), "Hello, World!");Implementations§
Source§impl TextFieldBuffer
impl TextFieldBuffer
Sourcepub fn new(initial_text: impl Into<String>) -> Self
pub fn new(initial_text: impl Into<String>) -> Self
Creates a new buffer with the given initial text. Cursor is placed at the end of the text.
Sourcepub fn with_selection(text: impl Into<String>, selection: TextRange) -> Self
pub fn with_selection(text: impl Into<String>, selection: TextRange) -> Self
Creates a buffer with text and specified selection.
Sourcepub fn composition(&self) -> Option<TextRange>
pub fn composition(&self) -> Option<TextRange>
Returns the current composition (IME) range, if any.
Sourcepub fn has_selection(&self) -> bool
pub fn has_selection(&self) -> bool
Returns true if there’s a non-collapsed selection.
Sourcepub fn has_changes(&self) -> bool
pub fn has_changes(&self) -> bool
Returns true if any changes have been made.
Sourcepub fn replace(&mut self, range: TextRange, replacement: &str)
pub fn replace(&mut self, range: TextRange, replacement: &str)
Replaces text in the given range with new text.
The selection is adjusted based on the replacement:
- If replacing before selection, selection shifts
- If replacing within selection, cursor moves to end of replacement
Sourcepub fn insert(&mut self, text: &str)
pub fn insert(&mut self, text: &str)
Inserts text at the current cursor position (or replaces selection).
Sourcepub fn delete_before_cursor(&mut self)
pub fn delete_before_cursor(&mut self)
Deletes the character before the cursor (backspace).
Sourcepub fn delete_after_cursor(&mut self)
pub fn delete_after_cursor(&mut self)
Deletes the character after the cursor (delete key).
Sourcepub fn delete_surrounding(&mut self, before_bytes: usize, after_bytes: usize)
pub fn delete_surrounding(&mut self, before_bytes: usize, after_bytes: usize)
Deletes text surrounding the cursor or selection.
before_bytes and after_bytes are byte counts in UTF-8.
The deletion respects character boundaries and preserves any IME composition range.
Sourcepub fn place_cursor_at_end(&mut self)
pub fn place_cursor_at_end(&mut self)
Places the cursor at the end of the text.
Sourcepub fn place_cursor_at_start(&mut self)
pub fn place_cursor_at_start(&mut self)
Places the cursor at the start of the text.
Sourcepub fn place_cursor_before_char(&mut self, index: usize)
pub fn place_cursor_before_char(&mut self, index: usize)
Places the cursor before the character at the given index.
Sourcepub fn place_cursor_after_char(&mut self, index: usize)
pub fn place_cursor_after_char(&mut self, index: usize)
Places the cursor after the character at the given index.
Sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
Selects all text.
Sourcepub fn extend_selection_left(&mut self)
pub fn extend_selection_left(&mut self)
Extends the selection to the left by one character. If no selection exists, starts selection from current cursor position. The anchor (end) stays fixed while the cursor (start) moves left.
Sourcepub fn extend_selection_right(&mut self)
pub fn extend_selection_right(&mut self)
Extends the selection to the right by one character. If no selection exists, starts selection from current cursor position. The anchor (start stays at origin) while cursor (end) moves right.
Sourcepub fn set_composition(&mut self, range: Option<TextRange>)
pub fn set_composition(&mut self, range: Option<TextRange>)
Sets the composition (IME) range.
Sourcepub fn copy_selection(&self) -> Option<String>
pub fn copy_selection(&self) -> Option<String>
Returns the selected text for copy operations. Returns None if no selection.
Sourcepub fn cut_selection(&mut self) -> Option<String>
pub fn cut_selection(&mut self) -> Option<String>
Cuts the selected text (returns it and deletes from buffer). Returns the cut text, or None if no selection.
Trait Implementations§
Source§impl Clone for TextFieldBuffer
impl Clone for TextFieldBuffer
Source§fn clone(&self) -> TextFieldBuffer
fn clone(&self) -> TextFieldBuffer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more