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