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//! # let _scope = APP.defaults();
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!("save-dlg.msg", "Save file? All unsaved changes will be lost."));
38//! responses = vec![
39//! dialog::Response::cancel(),
40//! dialog::Response::new("discard", l10n!("save-dlg.discard", "Discard")),
41//! dialog::Response::new("save", l10n!("save-dlg.save", "Save")),
42//! ]
43//! })
44//! .wait_rsp()
45//! .await;
46//! if r.name == "save" {
47//! // save
48//! }
49//! # }
50//! ```
51//!
52//! The example above creates a custom dialog based on the warning dialog (`WarnStyle!`), it uses custom responses that are
53//! identified by name.
54//!
55//! Some of the dialogs provided are native by default (and only native on this release), the example below shows a native save file dialog:
56//!
57//! ```
58//! use zng::prelude::*;
59//!
60//! # async fn _demo() {
61//! let mut f = dialog::FileDialogFilters::default();
62//! f.push_filter("Text Files", &["txt", "md"]);
63//! f.push_filter("Text File", &["txt"]);
64//! f.push_filter("Markdown File", &["md"]);
65//! f.push_filter("All Files", &["*"]);
66//! let filters = f;
67//!
68//! let r = DIALOG
69//! .save_file("Save Text", "last/save/dir", "last-name.txt", filters)
70//! .wait_rsp()
71//! .await
72//! .into_path();
73//!
74//! if let Ok(Some(path)) = r {
75//! std::fs::write(path, "contents".as_bytes()).unwrap();
76//! }
77//! # }
78//! ```
79//!
80//! [`Dialog!`]: struct@Dialog
81//!
82//! # Full API
83//!
84//! See [`zng_wgt_dialog`] for the full view API.
85
86pub use zng_wgt_dialog::{
87 AskStyle, ConfirmStyle, DIALOG, DefaultStyle, Dialog, DialogButtonArgs, DialogKind, ErrorStyle, FileDialogFilters, FileDialogResponse,
88 InfoStyle, Response, Responses, WarnStyle, native_dialogs,
89};
90
91/// Modal dialog parent widget that fills the window.
92///
93/// Note that the actual [`DialogBackdrop!`] widget is not included in this module because it is instantiated by the [`DIALOG`] service.
94/// The backdrop can be customized by setting the [`backdrop::style_fn`].
95///
96/// ```
97/// use zng::prelude::*;
98///
99/// # let _scope = APP.defaults();
100/// # let _ =
101/// Window! {
102/// dialog::backdrop::style_fn = Style! {
103/// replace = true;
104/// color::filter::backdrop_blur = 2;
105/// };
106/// }
107/// # ;
108/// ```
109///
110/// The example above configures the backdrop to blur the window content when any dialog is open.
111///
112/// [`DialogBackdrop!`]: struct@zng_wgt_dialog::backdrop::DialogBackdrop
113/// [`backdrop::style_fn`]: fn@crate::dialog::backdrop::style_fn
114/// [`DIALOG`]: crate::dialog::DIALOG
115///
116/// # Full API
117///
118/// See [`zng_wgt_dialog::backdrop`] for the fill view API.
119pub mod backdrop {
120 pub use zng_wgt_dialog::backdrop::{DefaultStyle, style_fn};
121}