tauri_plugin_dialog/
models.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6
7/// Types of message, ask and confirm dialogs.
8#[non_exhaustive]
9#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)]
10pub enum MessageDialogKind {
11    /// Information dialog.
12    #[default]
13    Info,
14    /// Warning dialog.
15    Warning,
16    /// Error dialog.
17    Error,
18}
19
20impl<'de> Deserialize<'de> for MessageDialogKind {
21    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
22    where
23        D: Deserializer<'de>,
24    {
25        let s = String::deserialize(deserializer)?;
26        Ok(match s.to_lowercase().as_str() {
27            "info" => MessageDialogKind::Info,
28            "warning" => MessageDialogKind::Warning,
29            "error" => MessageDialogKind::Error,
30            _ => MessageDialogKind::Info,
31        })
32    }
33}
34
35impl Serialize for MessageDialogKind {
36    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
37    where
38        S: Serializer,
39    {
40        match self {
41            Self::Info => serializer.serialize_str("info"),
42            Self::Warning => serializer.serialize_str("warning"),
43            Self::Error => serializer.serialize_str("error"),
44        }
45    }
46}
47
48/// Set of button that will be displayed on the dialog
49#[non_exhaustive]
50#[derive(Debug, Default, Clone, Serialize, Deserialize)]
51pub enum MessageDialogButtons {
52    #[default]
53    /// A single `Ok` button with OS default dialog text
54    Ok,
55    /// 2 buttons `Ok` and `Cancel` with OS default dialog texts
56    OkCancel,
57    /// 2 buttons `Yes` and `No` with OS default dialog texts
58    YesNo,
59    /// 3 buttons `Yes`, `No` and `Cancel` with OS default dialog texts
60    YesNoCancel,
61    /// A single `Ok` button with custom text
62    OkCustom(String),
63    /// 2 buttons `Ok` and `Cancel` with custom texts
64    OkCancelCustom(String, String),
65    /// 3 buttons `Yes`, `No` and `Cancel` with custom texts
66    YesNoCancelCustom(String, String, String),
67}
68
69/// Result of a message dialog
70#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
71pub enum MessageDialogResult {
72    Yes,
73    No,
74    Ok,
75    #[default]
76    Cancel,
77    #[serde(untagged)]
78    Custom(String),
79}
80
81#[cfg(desktop)]
82impl From<rfd::MessageDialogResult> for MessageDialogResult {
83    fn from(result: rfd::MessageDialogResult) -> Self {
84        match result {
85            rfd::MessageDialogResult::Yes => Self::Yes,
86            rfd::MessageDialogResult::No => Self::No,
87            rfd::MessageDialogResult::Ok => Self::Ok,
88            rfd::MessageDialogResult::Cancel => Self::Cancel,
89            rfd::MessageDialogResult::Custom(s) => Self::Custom(s),
90        }
91    }
92}
93
94impl From<String> for MessageDialogResult {
95    fn from(value: String) -> Self {
96        match value.as_str() {
97            "Yes" => Self::Yes,
98            "No" => Self::No,
99            "Ok" => Self::Ok,
100            "Cancel" => Self::Cancel,
101            _ => Self::Custom(value),
102        }
103    }
104}