Skip to main content

TextInputState

Struct TextInputState 

Source
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

Source

pub const fn new() -> Self

An empty field: empty value, cursor at 0, scroll at 0.

Source

pub fn value(&self) -> &str

The current text content.

Source

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.

Source

pub const fn cursor(&self) -> usize

The cursor’s byte offset into value. Always on a char boundary.

Source

pub const fn scroll(&self) -> u16

The current horizontal scroll offset, in display columns from the start of value. See ensure_visible.

Source

pub fn insert(&mut self, c: char)

Insert c at the cursor and move the cursor past it.

Source

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.

Source

pub fn backspace(&mut self)

Delete the character before the cursor, if any, and move the cursor onto its place.

Source

pub fn delete(&mut self)

Delete the character at the cursor, if any. The cursor itself does not move.

Source

pub fn move_left(&mut self)

Move the cursor one character left, if not already at the start.

Source

pub fn move_right(&mut self)

Move the cursor one character right, if not already at the end.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> TextInputState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TextInputState

Source§

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

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

impl Default for TextInputState

Source§

fn default() -> TextInputState

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

impl PartialEq for TextInputState

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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.
Source§

impl Eq for TextInputState

Source§

impl StructuralPartialEq for TextInputState

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.