Skip to main content

ValText

Struct ValText 

Source
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>

Source

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>

Source

pub fn number() -> Self

Only allows (0,1,2,3,4,5,6,7,8,9,.) and (-,+) at the beginning

Source

pub fn number_int() -> Self

Only allows (0,1,2,3,4,5,6,7,8,9) and (-,+) at the beginning

Source

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>

Source

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>

Source

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>

Source

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>

Source

pub fn new( value_parser: impl Fn(&str) -> Result<T, E> + 'static, input_validator: impl Fn(&str, &str, usize) -> bool + 'static, ) -> Self

Source

pub fn new_box( value_parser: Box<dyn Fn(&str) -> Result<T, E>>, input_validator: Box<dyn Fn(&str, &str, usize) -> bool>, ) -> Self

Source

pub fn with_parser(validator: impl Fn(&str) -> Result<T, E> + 'static) -> Self

Source

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.

Source

pub const fn get_val(&self) -> Option<Result<&T, &E>>

ValText must be used before getting value

Source

pub fn is_valid(&self) -> bool

Source§

impl<T: FromStr> ValText<Option<T>, T::Err>

Source

pub fn option_parse() -> Self

Source§

impl<T: Display, E> ValText<T, E>

Source

pub fn set_val(&mut self, val: T)

Trait Implementations§

Source§

impl<T: FromStr> Default for ValText<T, T::Err>

Source§

fn default() -> Self

Parse the text using FromStr

Source§

impl<T: 'static, E> TextBuffer for ValText<T, E>

Source§

fn is_mutable(&self) -> bool

Can this text be edited?
Source§

fn as_str(&self) -> &str

Returns this buffer as a str.
Source§

fn insert_text(&mut self, text: &str, char_index: usize) -> usize

Inserts text text into this buffer at character index char_index. Read more
Source§

fn delete_char_range(&mut self, char_range: Range<usize>)

Deletes a range of text char_range from this buffer. Read more
Source§

fn clear(&mut self)

Clears all characters in this buffer
Source§

fn take(&mut self) -> String

Clears all characters in this buffer and returns a string of the contents.
Source§

fn type_id(&self) -> TypeId

Returns a unique identifier for the implementing type. Read more
Source§

fn char_range(&self, char_range: Range<usize>) -> &str

Reads the given character range.
Source§

fn byte_index_from_char_index(&self, char_index: usize) -> usize

Source§

fn char_index_from_byte_index(&self, char_index: usize) -> usize

Source§

fn replace_with(&mut self, text: &str)

Replaces all contents of this string with text
Source§

fn insert_text_at( &mut self, ccursor: &mut CCursor, text_to_insert: &str, char_limit: usize, )

Source§

fn decrease_indentation(&mut self, ccursor: &mut CCursor)

Source§

fn delete_selected(&mut self, cursor_range: &CCursorRange) -> CCursor

Source§

fn delete_selected_ccursor_range(&mut self, _: [CCursor; 2]) -> CCursor

Source§

fn delete_previous_char(&mut self, ccursor: CCursor) -> CCursor

Source§

fn delete_next_char(&mut self, ccursor: CCursor) -> CCursor

Source§

fn delete_previous_word(&mut self, max_ccursor: CCursor) -> CCursor

Source§

fn delete_next_word(&mut self, min_ccursor: CCursor) -> CCursor

Source§

fn delete_paragraph_before_cursor( &mut self, galley: &Galley, cursor_range: &CCursorRange, ) -> CCursor

Source§

fn delete_paragraph_after_cursor( &mut self, galley: &Galley, cursor_range: &CCursorRange, ) -> CCursor

Auto Trait Implementations§

§

impl<T, E> Freeze for ValText<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> !RefUnwindSafe for ValText<T, E>

§

impl<T, E> !Send for ValText<T, E>

§

impl<T, E> !Sync for ValText<T, E>

§

impl<T, E> Unpin for ValText<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnsafeUnpin for ValText<T, E>
where T: UnsafeUnpin, E: UnsafeUnpin,

§

impl<T, E> !UnwindSafe for ValText<T, E>

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> 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, 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.