capacitor_bindings/
dialog.rs

1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3use typed_builder::TypedBuilder;
4
5use crate::{
6    extern_functions::{alert, confirm, prompt},
7    helpers::*,
8};
9
10use crate::error::Error;
11
12pub struct Dialog;
13
14impl Dialog {
15    /// Show an alert dialog
16    pub async fn alert(options: impl Into<AlertOptions>) -> Result<(), Error> {
17        run_value_unit(options, alert).await
18    }
19
20    /// Show a prompt dialog
21    pub async fn prompt(options: impl Into<PromptOptions>) -> Result<PromptResult, Error> {
22        run_value_value(options, prompt).await
23    }
24
25    /// Show a confirmation dialog
26    pub async fn confirm(options: impl Into<ConfirmOptions>) -> Result<ConfirmResult, Error> {
27        run_value_value(options, confirm).await
28    }
29}
30
31#[skip_serializing_none]
32#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize, TypedBuilder)]
33#[serde(rename_all = "camelCase", default)]
34pub struct AlertOptions {
35    #[builder(setter(into))]
36    /// Title of the dialog.
37    pub title: String,
38    #[builder(setter(into))]
39    /// Message to show on the dialog.
40    pub message: String,
41    #[builder(setter(into))]
42    /// Text to use on the action button.
43    pub button_title: String,
44}
45
46#[skip_serializing_none]
47#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
48#[serde(rename_all = "camelCase", default)]
49pub struct PromptResult {
50    /// Text entered on the prompt.
51    pub value: String,
52    /// Whether if the prompt was canceled or accepted.
53    pub cancelled: bool,
54}
55
56#[skip_serializing_none]
57#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize, TypedBuilder)]
58#[serde(rename_all = "camelCase", default)]
59pub struct PromptOptions {
60    #[builder(setter(into))]
61    /// Title of the dialog.
62    pub title: String,
63    #[builder(setter(into))]
64    /// Message to show on the dialog.
65    pub message: String,
66    #[builder(setter(into))]
67    /// Text to use on the positive action button.
68    pub ok_button_title: String,
69    #[builder(setter(into))]
70    /// Text to use on the negative action button.
71    pub cancel_button_title: String,
72    #[builder(setter(into, strip_option), default)]
73    /// Placeholder text for hints.
74    pub input_placeholder: Option<String>,
75    #[builder(setter(into, strip_option), default)]
76    /// Prepopulated text.
77    pub input_text: Option<String>,
78}
79
80#[skip_serializing_none]
81#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
82#[serde(rename_all = "camelCase", default)]
83pub struct ConfirmResult {
84    /// true if the positive button was clicked, false otherwise.
85    pub value: bool,
86}
87
88#[skip_serializing_none]
89#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize, TypedBuilder)]
90#[serde(rename_all = "camelCase", default)]
91pub struct ConfirmOptions {
92    #[builder(setter(into))]
93    /// Title of the dialog.
94    pub title: String,
95    #[builder(setter(into))]
96    /// Message to show on the dialog.
97    pub message: String,
98    #[builder(setter(into))]
99    /// Text to use on the positive action button.
100    pub ok_button_title: String,
101    #[builder(setter(into))]
102    /// Text to use on the negative action button.
103    pub cancel_button_title: String,
104}