pub struct TextInputState { /* private fields */ }Expand description
A String value, a byte cursor into it, and a horizontal scroll offset.
The state a single-line editable text field needs, mirroring ListState’s
split between “what’s selected/typed” and “how it’s drawn”.
Holds no reference to any widget: the same TextInputState can be reused across frames (and
across a resized field) the way ListState is reused across a resized list.
cursor is a byte index into value, not a char or display-column index, and every mutating
method here maintains the invariant that it always lands on a char boundary: insert/
insert_str/backspace/delete never split a multi-byte character, and move_left/
move_right step by whole chars. Display-column math (where the caret actually draws, and
how far the field has scrolled) is a separate concern handled by
ensure_visible and the TextInput widget itself, via
retroglyph_core::text::width_usize: a byte or char count is the wrong unit once the value
contains a double-width character.
Handles typed characters (Event::Key(KeyCode::Char(c))) and pasted text (Event::Paste)
only, not IME/text composition or multi-line editing.
Cursor movement and deletion step by Unicode char (codepoint), not by grapheme cluster: a
base character plus a combining mark is two chars, so backspace there removes only the
mark and move_left stops between the two. A field over text with combining marks or
emoji-ZWJ sequences will show per-codepoint, not per-glyph, editing.
Implementations§
Source§impl TextInputState
impl TextInputState
Sourcepub fn set_value(&mut self, s: impl Into<String>)
pub fn set_value(&mut self, s: impl Into<String>)
Replace the entire content and move the cursor to its end. Resets the scroll offset to
zero. Call ensure_visible afterward if the new value should
scroll to keep the cursor (still at the end) in view.
Sets the cursor to value.len(), a trivially-valid char boundary, so this skips the
debug-only boundary guard the other mutators carry.
Sourcepub const fn cursor(&self) -> usize
pub const fn cursor(&self) -> usize
The cursor’s byte offset into value. Always on a char boundary.
Sourcepub const fn scroll(&self) -> u16
pub const fn scroll(&self) -> u16
The current horizontal scroll offset, in display columns from the start of value. See
ensure_visible.
Sourcepub fn insert_str(&mut self, s: &str)
pub fn insert_str(&mut self, s: &str)
Insert s at the cursor and move the cursor past it, e.g. from an
Event::Paste.
Sourcepub fn backspace(&mut self)
pub fn backspace(&mut self)
Delete the character before the cursor, if any, and move the cursor onto its place.
Sourcepub fn delete(&mut self)
pub fn delete(&mut self)
Delete the character at the cursor, if any. The cursor itself does not move.
Sourcepub fn move_right(&mut self)
pub fn move_right(&mut self)
Move the cursor one character right, if not already at the end.
Sourcepub const fn move_home(&mut self)
pub const fn move_home(&mut self)
Move the cursor to the start of the value.
Sets the cursor to 0, a trivially-valid char boundary, so this skips the debug-only
boundary guard the other mutators carry.
Sourcepub const fn move_end(&mut self)
pub const fn move_end(&mut self)
Move the cursor to the end of the value.
Sets the cursor to value.len(), a trivially-valid char boundary, so this skips the
debug-only boundary guard the other mutators carry.
Sourcepub fn handle_event(&mut self, event: &Event) -> bool
pub fn handle_event(&mut self, event: &Event) -> bool
Apply one input event: typed characters, Backspace/Delete, Left/Right/Home/End, and pasted text. Returns whether the event was consumed (so a caller can decide whether to fall through to its own key handling, e.g. Enter/Escape/Tab, none of which this consumes).
Key releases and auto-repeats other than presses are ignored except that auto-repeat
presses are treated the same as a press (matches FocusRing/
Shortcuts’s own is_down gating). Event
has no IME/composition variant to begin with; see the scope note above.
Sourcepub fn ensure_visible(&mut self, width: u16)
pub fn ensure_visible(&mut self, width: u16)
Nudge the scroll offset by the minimum amount needed to keep the cursor within a
width-column window, the way ListState::ensure_visible
keeps a selection in view.
A no-op if width is zero or the cursor is already visible. Call this once per frame
before rendering (with the actual, current field width, since that can change on resize)
rather than only after an edit: it’s cheap and idempotent.
Trait Implementations§
Source§impl Clone for TextInputState
impl Clone for TextInputState
Source§fn clone(&self) -> TextInputState
fn clone(&self) -> TextInputState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TextInputState
impl Debug for TextInputState
Source§impl Default for TextInputState
impl Default for TextInputState
Source§fn default() -> TextInputState
fn default() -> TextInputState
Source§impl PartialEq for TextInputState
impl PartialEq for TextInputState
Source§fn eq(&self, other: &TextInputState) -> bool
fn eq(&self, other: &TextInputState) -> bool
self and other values to be equal, and is used by ==.