Module dialog

Module dialog 

Source
Expand description

Modal dialog overlay widget and service.

The DIALOG service provides custom and modal native dialogs.

use zng::prelude::*;

Button! {
    child = Text!("Info, Warn, Error");
    on_click = async_hn!(|_| {
        DIALOG.info("Info", "Information message.").wait_rsp().await;
        DIALOG.warn("Warn", "Warning message.").wait_rsp().await;
        DIALOG.error("Error", "Error message.").wait_rsp().await;
    });
    // dialog::native_dialogs = true;
}

The example above shows 3 custom dialogs in sequence, info, warn and error. If dialog::native_dialogs = true is uncommented the example shows 3 native dialogs.

Custom dialogs modal widgets, rendered in the window content, instantiated using the Dialog! widget.

use zng::prelude::*;

let r = DIALOG
    .custom(dialog::Dialog! {
        style_fn = dialog::WarnStyle!();
        title = Text!(l10n!("save-dlg.title", "Save File?"));
        content = SelectableText!(l10n!(
            "save-dlg.msg",
            "Save file? All unsaved changes will be lost."
        ));
        responses = vec![
            dialog::Response::cancel(),
            dialog::Response::new("discard", l10n!("save-dlg.discard", "Discard")),
            dialog::Response::new("save", l10n!("save-dlg.save", "Save")),
        ];
    })
    .wait_rsp()
    .await;
if r.name == "save" {
    // save
}

The example above creates a custom dialog based on the warning dialog (WarnStyle!), it uses custom responses that are identified by name.

Some of the dialogs provided are native by default (and only native on this release), the example below shows a native save file dialog:

use zng::prelude::*;

let mut f = dialog::FileDialogFilters::default();
f.push_filter("Text Files", &["txt", "md"]);
f.push_filter("Text File", &["txt"]);
f.push_filter("Markdown File", &["md"]);
f.push_filter("All Files", &["*"]);
let filters = f;

let r = DIALOG
    .save_file("Save Text", "last/save/dir", "last-name.txt", filters)
    .wait_rsp()
    .await
    .into_path();

if let Ok(Some(path)) = r {
    std::fs::write(path, "contents".as_bytes()).unwrap();
}

§Full API

See zng_wgt_dialog for the full view API.

Modules§

backdrop
Modal dialog parent widget that fills the window.

Structs§

AskStyle
W Question style.
ConfirmStyle
W Confirmation style.
DIALOG
Dialog service.
DefaultStyle
W Dialog default style.
Dialog
W A modal dialog overlay container.
DialogButtonArgs
Arguments for button_fn.
DialogKind
Dialog kind options.
ErrorStyle
W Dialog error style.
FileDialogFilters
File dialog filters builder.
InfoStyle
W Dialog info style.
Response
Dialog response.
Responses
Response labels.
WarnStyle
W Dialog warn style.

Enums§

FileDialogResponse
Response to a message dialog.

Functions§

ask_style_fn
P Extends or replaces the AskStyle!.
confirm_style_fn
P Extends or replaces the ConfirmStyle!.
error_style_fn
P Extends or replaces the ErrorStyle!.
info_style_fn
P Extends or replaces the InfoStyle!.
native_dialogs
P Defines what native dialogs are used by dialogs opened on the context.
warn_style_fn
P Extends or replaces the WarnStyle!.