pub struct ValText<T, E> { /* private fields */ }Expand description
A mutable TextBuffer that will validate it’s contents when changed.
And check an input before adding it to the text.
The default validator will simply attempt to parse the text as T,
but a custom validator function can be provided.
§Usage
use egui_typed_input::ValText;
let mut alphabetical_order: ValText<Vec<char>, ()> = ValText::new(
// parser
(|str| Ok(str.chars().collect::<Vec<_>>())),
// input validator
(|current_text, input, index| {
if input.chars().all(|c| c.is_ascii_alphabetic()) {
input.chars().all(|c| {
c.to_ascii_lowercase() >= current_text.chars().skip(index.saturating_sub(1)).take(1).last().unwrap_or('a')
})
} else { false }
}),
);
ui.text_edit_singleline(&mut alphabetical_order);
println!("alphabetical_order: {:?}", alphabetical_order.get_val());See hex color example (color_hex.rs) and number examples (number.rs) for more
Implementations§
Source§impl ValText<Color32, ParseHexColorError>
impl ValText<Color32, ParseHexColorError>
Sourcepub fn color_hex() -> Self
pub fn color_hex() -> Self
A hex color starting with #, parsed using Color32::from_hex.
Supports the 3, 4, 6, and 8-digit formats.
Source§impl<T: FromStr> ValText<T, T::Err>
impl<T: FromStr> ValText<T, T::Err>
Sourcepub fn number_int() -> Self
pub fn number_int() -> Self
Only allows (0,1,2,3,4,5,6,7,8,9) and (-,+) at the beginning
Sourcepub fn number_uint() -> Self
pub fn number_uint() -> Self
Only allows (0,1,2,3,4,5,6,7,8,9) and (+) at the beginning
Source§impl ValText<f64, PercentageParseError>
impl ValText<f64, PercentageParseError>
Sourcepub fn percentage() -> Self
pub fn percentage() -> Self
A numarical percentage in the range of 0-100.
Only allows (0,1,2,3,4,5,6,7,8,9,.) and (+) at the beginning
Source§impl ValText<f32, PercentageParseError>
impl ValText<f32, PercentageParseError>
Sourcepub fn percentage() -> Self
pub fn percentage() -> Self
A numarical percentage in the range of 0-100.
Only allows (0,1,2,3,4,5,6,7,8,9,.) and (+) at the beginning
Source§impl ValText<u32, PercentageParseError>
impl ValText<u32, PercentageParseError>
Sourcepub fn percentage_uint() -> Self
pub fn percentage_uint() -> Self
A numarical percentage in the range of 0-100.
Only allows (0,1,2,3,4,5,6,7,8,9) and (+) at the beginning
Source§impl<T, E> ValText<T, E>
impl<T, E> ValText<T, E>
pub fn new( value_parser: impl Fn(&str) -> Result<T, E> + 'static, input_validator: impl Fn(&str, &str, usize) -> bool + 'static, ) -> Self
pub fn new_box( value_parser: Box<dyn Fn(&str) -> Result<T, E>>, input_validator: Box<dyn Fn(&str, &str, usize) -> bool>, ) -> Self
pub fn with_parser(validator: impl Fn(&str) -> Result<T, E> + 'static) -> Self
Sourcepub fn with_parser_fixed_charset(
parser: impl Fn(&str) -> Result<T, E> + 'static,
charset: &'static [char],
) -> Self
pub fn with_parser_fixed_charset( parser: impl Fn(&str) -> Result<T, E> + 'static, charset: &'static [char], ) -> Self
Only chars in charset can be input
§Usage
ValText::with_parser_fixed_charset(|str| Ok(str.to_owned()), &['a', 'c']);Would allow ‘a’ and ‘c’ but no others.
Sourcepub const fn get_val(&self) -> Option<Result<&T, &E>>
pub const fn get_val(&self) -> Option<Result<&T, &E>>
ValText must be used before getting value
pub fn is_valid(&self) -> bool
Trait Implementations§
Source§impl<T: 'static, E> TextBuffer for ValText<T, E>
impl<T: 'static, E> TextBuffer for ValText<T, E>
Source§fn is_mutable(&self) -> bool
fn is_mutable(&self) -> bool
Source§fn delete_char_range(&mut self, char_range: Range<usize>)
fn delete_char_range(&mut self, char_range: Range<usize>)
char_range from this buffer. Read moreSource§fn take(&mut self) -> String
fn take(&mut self) -> String
fn byte_index_from_char_index(&self, char_index: usize) -> usize
fn char_index_from_byte_index(&self, char_index: usize) -> usize
Source§fn replace_with(&mut self, text: &str)
fn replace_with(&mut self, text: &str)
text