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>§suggestion_index: usize§show_suggestions: boolImplementations§
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.
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.