Skip to main content

Module input_dialog

Module input_dialog 

Source
Expand description

InputDialog — a QInputDialog-style modal that prompts the user for a single string. Built on the same present_modal infrastructure as MessageBox, with a TextInput body between the prompt and the Ok / Cancel buttons.

Use MessageBox when the dialog conveys information without requiring data; use InputDialog when the modal needs to capture exactly one short string. Forms longer than a single field belong in a custom Dialog.

InputDialog::new(tr!(rename_title()))
    .prompt(tr!(rename_prompt()))
    .default_text(current_name)
    .placeholder("New name")
    .on_result(|result, _ctx| {
        if let Some(name) = result {
            rename(name);
        }
    })
    .present(ctx);

§Live validation

validate runs on every keystroke and both disables OK and shows its message under the field, so a value the caller cannot accept can never be submitted:

InputDialog::new(tr!(save_as_template_title()))
    .validate(move |name| {
        if name.trim().is_empty() {
            Err(None)                                  // block, say nothing
        } else if let Some(clash) = taken(name) {
            Err(Some(tr!(duplicate(name = clash))))    // block, and explain
        } else {
            Ok(())
        }
    })
    .on_result(|result, _| { /* only ever called with a valid value */ })
    .present(ctx);

Err(None) is the “not yet” case — it disables OK without printing anything, which is what an untouched empty field wants: shouting at someone before they have typed is noise, and the greyed button already says the dialog is not ready. A message is withheld until the field has been edited for the same reason, so a caller can return Err(Some(..)) for the empty case without it flashing on open.

Structs§

InputDialog
A single-field input modal.

Type Aliases§

ValidateResult
Verdict from an InputDialog::validate callback.