zng/dialog.rs
1#![cfg(feature = "dialog")]
2
3//! Modal dialog overlay widget and service.
4//!
5//! The [`DIALOG`] service provides custom and modal native dialogs.
6//!
7//! ```
8//! use zng::prelude::*;
9//!
10//! # fn example() {
11//! # let _ =
12//! Button! {
13//! child = Text!("Info, Warn, Error");
14//! on_click = async_hn!(|_| {
15//! DIALOG.info("Info", "Information message.").wait_rsp().await;
16//! DIALOG.warn("Warn", "Warning message.").wait_rsp().await;
17//! DIALOG.error("Error", "Error message.").wait_rsp().await;
18//! });
19//! // dialog::native_dialogs = true;
20//! }
21//! # ; }
22//! ```
23//!
24//! The example above shows 3 custom dialogs in sequence, info, warn and error. If `dialog::native_dialogs = true` is uncommented
25//! the example shows 3 native dialogs.
26//!
27//! Custom dialogs modal widgets, rendered in the window content, instantiated using the [`Dialog!`] widget.
28//!
29//! ```
30//! use zng::prelude::*;
31//!
32//! # async fn _demo() {
33//! let r = DIALOG
34//! .custom(dialog::Dialog! {
35//! style_fn = dialog::WarnStyle!();
36//! title = Text!(l10n!("save-dlg.title", "Save File?"));
37//! content = SelectableText!(l10n!(
38//! "save-dlg.msg",
39//! "Save file? All unsaved changes will be lost."
40//! ));
41//! responses = vec![
42//! dialog::Response::cancel(),
43//! dialog::Response::new("discard", l10n!("save-dlg.discard", "Discard")),
44//! dialog::Response::new("save", l10n!("save-dlg.save", "Save")),
45//! ];
46//! })
47//! .wait_rsp()
48//! .await;
49//! if r.name == "save" {
50//! // save
51//! }
52//! # }
53//! ```
54//!
55//! The example above creates a custom dialog based on the warning dialog (`WarnStyle!`), it uses custom responses that are
56//! identified by name.
57//!
58//! Some of the dialogs provided are native by default (and only native on this release), the example below shows a native save file dialog:
59//!
60//! ```
61//! use zng::prelude::*;
62//!
63//! # async fn _demo() {
64//! let mut f = dialog::FileDialogFilters::default();
65//! f.push_filter("Text Files", &["txt", "md"]);
66//! f.push_filter("Text File", &["txt"]);
67//! f.push_filter("Markdown File", &["md"]);
68//! f.push_filter("All Files", &["*"]);
69//! let filters = f;
70//!
71//! let r = DIALOG
72//! .save_file("Save Text", "last/save/dir", "last-name.txt", filters)
73//! .wait_rsp()
74//! .await
75//! .into_path();
76//!
77//! if let Ok(Some(path)) = r {
78//! std::fs::write(path, "contents".as_bytes()).unwrap();
79//! }
80//! # }
81//! ```
82//!
83//! [`Dialog!`]: struct@Dialog
84//!
85//! # Full API
86//!
87//! See [`zng_wgt_dialog`] for the full view API.
88
89pub use zng_wgt_dialog::{
90 AskStyle, ConfirmStyle, DIALOG, DefaultStyle, Dialog, DialogButtonArgs, DialogKind, ErrorStyle, FileDialogFilters, FileDialogResponse,
91 InfoStyle, Response, Responses, WarnStyle, ask_style_fn, confirm_style_fn, error_style_fn, info_style_fn, native_dialogs,
92 warn_style_fn,
93};
94
95/// Modal dialog parent widget that fills the window.
96///
97/// Note that the actual [`DialogBackdrop!`] widget is not included in this module because it is instantiated by the [`DIALOG`] service.
98/// The backdrop can be customized by setting the [`backdrop::style_fn`].
99///
100/// ```
101/// use zng::prelude::*;
102///
103/// # fn example() {
104/// # let _ =
105/// Window! {
106/// dialog::backdrop::style_fn = Style! {
107/// replace = true;
108/// color::filter::backdrop_blur = 2;
109/// };
110/// }
111/// # ; }
112/// ```
113///
114/// The example above configures the backdrop to blur the window content when any dialog is open.
115///
116/// [`DialogBackdrop!`]: struct@zng_wgt_dialog::backdrop::DialogBackdrop
117/// [`backdrop::style_fn`]: fn@crate::dialog::backdrop::style_fn
118/// [`DIALOG`]: crate::dialog::DIALOG
119///
120/// # Full API
121///
122/// See [`zng_wgt_dialog::backdrop`] for the fill view API.
123pub mod backdrop {
124 pub use zng_wgt_dialog::backdrop::{DefaultStyle, style_fn};
125}