Incular logoIncular
API ReferenceText & Media

TextEditingController

Shared editing handle with every read, write, movement, and IME method.

Cloneable (Rc<RefCell>) reactive handle. UTF-8 safe (char-boundary clamping), observable, restoration-aware.

GroupMethods
Createnew, with_text, restored, from_value
Readvalue, text, selection, composing, selected_text, preedit
Writeset_value, set_text, clear, set_selection, replace_selection, insert_text, delete_backward, delete_forward
Movemove_left, move_right, move_home, move_end, select_all
IMEset_preedit, commit_preedit, clear_preedit
Observeobserve, add_listener, remove_listener
Restorebind_restoration, unbind_restoration

Creating and binding

A full editing workout — copy, paste, and run:

use incular::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let controller = TextEditingController::with_text("hello");
    controller.select_all();
    controller.replace_selection("rust");
    controller.move_home(false);
    controller.insert_text("!");
    controller.set_selection(TextSelection::new(0, 4));
    controller.set_composing(None);
    let text = controller.text();
    let selected = controller.selected_text();
    let app = Application::new(move |_cx| {
        Container::builder()
            .padding(EdgeInsets::all(24.0))
            .alignment(Alignment::CENTER)
            .child(
                Column::builder()
                    .main_axis_size(MainAxisSize::Min)
                    .spacing(8.0)
                    .children(Vec::<Widget>::from([
                        EditableText::new(
                            TextEditingController::with_text(text.clone()),
                        ).into(),
                        Text::new(format!(
                            "Selected: {selected}"
                        )).into(),
                    ]))
                    .build(),
            )
            .build()
            .into()
    })?;
    incular::run(app)?;
    Ok(())
}

TextEditingController::new(), restored(scope, key), from_value(value), bind_restoration / unbind_restoration cover the remaining lifecycle.

Reading

value() (TextEditingValue), text(), selection(), composing(), selected_text(), preedit() / preedit_selection(), caret_affinity() — see the workout program above reading text() and selected_text().

Writing

set_value, set_text, clear, set_selection (plus set_selection_with_affinity), set_composing, apply_delta, replace_selection, insert_text / insert, delete_backward (backspace alias), delete_forward (delete alias) — exercised above.

Movement

move_left / move_right / move_home / move_end (pass true to extend the selection), plus select_all().

IME preedit

set_preedit / commit_preedit / clear_preedit manage transient composition state (never serialized by restoration).

Observation

observe (returns a Subscription) and add_listener / remove_listener react to value changes without rebuilding.

TextEditingValue { text, selection, composing } with new / empty / with_selection / with_composing / replace. TextEditingDelta { range, replacement, selection, composing } with replace / apply.

Related: EditableText, TextSelection, Restoration.

On this page