pub struct TextInputState {
pub value: String,
pub cursor: usize,
pub placeholder: String,
pub max_length: Option<usize>,
pub validation_error: Option<String>,
pub masked: bool,
pub suggestions: Vec<String>,
pub suggestion_index: usize,
pub show_suggestions: bool,
/* private fields */
}Expand description
State for a single-line text input widget.
Pass a mutable reference to Context::text_input each frame. The widget
handles all keyboard events when focused.
§Example
let mut input = TextInputState::with_placeholder("Type here...");
ui.text_input(&mut input);
println!("{}", input.value);Fields§
§value: StringThe current input text.
cursor: usizeCursor position as a character index into value.
placeholder: StringPlaceholder text shown when value is empty.
max_length: Option<usize>Maximum character count. Input is rejected beyond this limit.
validation_error: Option<String>The most recent validation error message, if any.
masked: boolWhen true, input is displayed as • characters (for passwords).
suggestions: Vec<String>Autocomplete candidates shown below the input.
suggestion_index: usizeHighlighted index within the currently shown suggestions.
show_suggestions: boolWhether the suggestions popup should be rendered.
Implementations§
Source§impl TextInputState
impl TextInputState
Sourcepub fn with_placeholder(p: impl Into<String>) -> Self
pub fn with_placeholder(p: impl Into<String>) -> Self
Create a text input with placeholder text shown when the value is empty.
Sourcepub fn max_length(self, len: usize) -> Self
pub fn max_length(self, len: usize) -> Self
Set the maximum allowed character count.
Sourcepub fn validate(&mut self, validator: impl Fn(&str) -> Result<(), String>)
pub fn validate(&mut self, validator: impl Fn(&str) -> Result<(), String>)
Validate the current value and store the latest error message.
Sets TextInputState::validation_error to None when validation
succeeds, or to Some(error) when validation fails.
This is a backward-compatible shorthand that runs a single validator.
For multiple validators, use add_validator and run_validators.
Sourcepub fn add_validator(
&mut self,
f: impl Fn(&str) -> Result<(), String> + 'static,
)
pub fn add_validator( &mut self, f: impl Fn(&str) -> Result<(), String> + 'static, )
Add a validator function that produces its own error message.
Multiple validators can be added. Call run_validators
to execute all validators and collect their errors.
§Note on cloning
Validators are not preserved across Clone because closures are
not Clone. Re-register after cloning the state.
Sourcepub fn run_validators(&mut self)
pub fn run_validators(&mut self)
Run all registered validators and collect their error messages.
Updates validation_errors with all errors from all validators.
Also updates validation_error to the first error for backward compatibility.
§Note on cloning
Validators do not survive Clone. Calling this on a cloned state with
no re-registered validators clears validation_errors without re-running
any check. Re-register validators on the clone first.
Sourcepub fn set_suggestions(&mut self, suggestions: Vec<String>)
pub fn set_suggestions(&mut self, suggestions: Vec<String>)
Set autocomplete suggestions and reset popup state.
Sourcepub fn matched_suggestions(&self) -> Vec<&str>
pub fn matched_suggestions(&self) -> Vec<&str>
Return suggestions that start with the current input (case-insensitive).
Trait Implementations§
Source§impl Clone for TextInputState
impl Clone for TextInputState
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
§Clone behavior
validators registered via TextInputState::add_validator are not
cloned because closures are not Clone. validation_errors is preserved
in the clone, but it becomes stale — calling
TextInputState::run_validators on the clone will clear errors without
re-running any validation.
Re-register validators on the clone before calling run_validators().
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more