Skip to main content

insert_char

Function insert_char 

Source
pub fn insert_char(
    edit: &mut TextEdit,
    config: &Config,
    metrics: &Metrics<'_>,
    ch: char,
    max_len: Option<u32>,
) -> bool
Expand description

Types one character at the caret, replacing any selection.

Exactly one undo item, whether or not a selection was replaced — which is what makes a run of typing undo one keystroke at a time.

A field that has filled its plate and may not scroll refuses outright — see is_text_overflow, which is InsertWord’s and InsertReturn’s first statement upstream.


let mut edit = TextEdit::new("", &config, &metrics, true);
for ch in "ABC".chars() {
    ops::insert_char(&mut edit, &config, &metrics, ch, None);
}
assert_eq!(edit.text, "ABC");

// One undo item per keystroke, so a run undoes one character at a time.
ops::undo(&mut edit, &config, &metrics);
assert_eq!(edit.text, "AB");

// Typing over a selection replaces it, still in one item.
edit.set_selection(0, 2);
ops::insert_char(&mut edit, &config, &metrics, 'Z', None);
assert_eq!(edit.text, "Z");