Skip to content

Commit 04459af

Browse files
refactor(dialog)!: use enum instead of label for buttons (tauri-apps#1842)
* refactor(dialog)!: use enum instead of label * Add change file * Fix doc comment typo * Move ok and cancel to lib.rs
1 parent 3b2bd30 commit 04459af

File tree

5 files changed

+95
-57
lines changed

5 files changed

+95
-57
lines changed

.changes/native-dialog-button-text.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"dialog": patch:breaking
3+
---
4+
5+
Changed `MessageDialogBuilder::ok_button_label` and `MessageDialogBuilder::cancel_button_label` to `MessageDialogBuilder::buttons` which takes an enum now

plugins/dialog/src/commands.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use serde::{Deserialize, Serialize};
88
use tauri::{command, Manager, Runtime, State, Window};
99
use tauri_plugin_fs::FsExt;
1010

11-
use crate::{Dialog, FileDialogBuilder, FilePath, MessageDialogKind, Result};
11+
use crate::{
12+
Dialog, FileDialogBuilder, FilePath, MessageDialogButtons, MessageDialogKind, Result, CANCEL,
13+
OK,
14+
};
1215

1316
#[derive(Serialize)]
1417
#[serde(untagged)]
@@ -244,11 +247,12 @@ fn message_dialog<R: Runtime>(
244247
title: Option<String>,
245248
message: String,
246249
kind: Option<MessageDialogKind>,
247-
ok_button_label: Option<String>,
248-
cancel_button_label: Option<String>,
250+
buttons: MessageDialogButtons,
249251
) -> bool {
250252
let mut builder = dialog.message(message);
251253

254+
builder = builder.buttons(buttons);
255+
252256
if let Some(title) = title {
253257
builder = builder.title(title);
254258
}
@@ -262,14 +266,6 @@ fn message_dialog<R: Runtime>(
262266
builder = builder.kind(kind);
263267
}
264268

265-
if let Some(ok) = ok_button_label {
266-
builder = builder.ok_button_label(ok);
267-
}
268-
269-
if let Some(cancel) = cancel_button_label {
270-
builder = builder.cancel_button_label(cancel);
271-
}
272-
273269
builder.blocking_show()
274270
}
275271

@@ -288,8 +284,11 @@ pub(crate) async fn message<R: Runtime>(
288284
title,
289285
message,
290286
kind,
291-
ok_button_label,
292-
None,
287+
if let Some(ok_button_label) = ok_button_label {
288+
MessageDialogButtons::OkCustom(ok_button_label)
289+
} else {
290+
MessageDialogButtons::Ok
291+
},
293292
))
294293
}
295294

@@ -309,8 +308,7 @@ pub(crate) async fn ask<R: Runtime>(
309308
title,
310309
message,
311310
kind,
312-
Some(ok_button_label.unwrap_or_else(|| "Yes".into())),
313-
Some(cancel_button_label.unwrap_or_else(|| "No".into())),
311+
get_ok_cancel_type(ok_button_label, cancel_button_label),
314312
))
315313
}
316314

@@ -330,7 +328,22 @@ pub(crate) async fn confirm<R: Runtime>(
330328
title,
331329
message,
332330
kind,
333-
Some(ok_button_label.unwrap_or_else(|| "Ok".into())),
334-
Some(cancel_button_label.unwrap_or_else(|| "Cancel".into())),
331+
get_ok_cancel_type(ok_button_label, cancel_button_label),
335332
))
336333
}
334+
335+
fn get_ok_cancel_type(
336+
ok_button_label: Option<String>,
337+
cancel_button_label: Option<String>,
338+
) -> MessageDialogButtons {
339+
if let Some(ok_button_label) = ok_button_label {
340+
MessageDialogButtons::OkCancelCustom(
341+
ok_button_label,
342+
cancel_button_label.unwrap_or(CANCEL.to_string()),
343+
)
344+
} else if let Some(cancel_button_label) = cancel_button_label {
345+
MessageDialogButtons::OkCancelCustom(OK.to_string(), cancel_button_label)
346+
} else {
347+
MessageDialogButtons::OkCancel
348+
}
349+
}

plugins/dialog/src/desktop.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ use rfd::{AsyncFileDialog, AsyncMessageDialog};
1313
use serde::de::DeserializeOwned;
1414
use tauri::{plugin::PluginApi, AppHandle, Runtime};
1515

16-
use crate::{models::*, FileDialogBuilder, FilePath, MessageDialogBuilder};
17-
18-
const OK: &str = "Ok";
16+
use crate::{models::*, FileDialogBuilder, FilePath, MessageDialogBuilder, OK};
1917

2018
pub fn init<R: Runtime, C: DeserializeOwned>(
2119
app: &AppHandle<R>,
@@ -109,22 +107,24 @@ impl<R: Runtime> From<FileDialogBuilder<R>> for AsyncFileDialog {
109107
}
110108
}
111109

110+
impl From<MessageDialogButtons> for rfd::MessageButtons {
111+
fn from(value: MessageDialogButtons) -> Self {
112+
match value {
113+
MessageDialogButtons::Ok => Self::Ok,
114+
MessageDialogButtons::OkCancel => Self::OkCancel,
115+
MessageDialogButtons::OkCustom(ok) => Self::OkCustom(ok),
116+
MessageDialogButtons::OkCancelCustom(ok, cancel) => Self::OkCancelCustom(ok, cancel),
117+
}
118+
}
119+
}
120+
112121
impl<R: Runtime> From<MessageDialogBuilder<R>> for AsyncMessageDialog {
113122
fn from(d: MessageDialogBuilder<R>) -> Self {
114123
let mut dialog = AsyncMessageDialog::new()
115124
.set_title(&d.title)
116125
.set_description(&d.message)
117-
.set_level(d.kind.into());
118-
119-
let buttons = match (d.ok_button_label, d.cancel_button_label) {
120-
(Some(ok), Some(cancel)) => Some(rfd::MessageButtons::OkCancelCustom(ok, cancel)),
121-
(Some(ok), None) => Some(rfd::MessageButtons::OkCustom(ok)),
122-
(None, Some(cancel)) => Some(rfd::MessageButtons::OkCancelCustom(OK.into(), cancel)),
123-
(None, None) => None,
124-
};
125-
if let Some(buttons) = buttons {
126-
dialog = dialog.set_buttons(buttons);
127-
}
126+
.set_level(d.kind.into())
127+
.set_buttons(d.buttons.into());
128128

129129
if let Some(parent) = d.parent {
130130
dialog = dialog.set_parent(&parent);
@@ -213,7 +213,11 @@ pub fn show_message_dialog<R: Runtime, F: FnOnce(bool) + Send + 'static>(
213213
) {
214214
use rfd::MessageDialogResult;
215215

216-
let ok_label = dialog.ok_button_label.clone();
216+
let ok_label = match &dialog.buttons {
217+
MessageDialogButtons::OkCustom(ok) => Some(ok.clone()),
218+
MessageDialogButtons::OkCancelCustom(ok, _) => Some(ok.clone()),
219+
_ => None,
220+
};
217221
let f = move |res| {
218222
f(match res {
219223
MessageDialogResult::Ok | MessageDialogResult::Yes => true,

plugins/dialog/src/lib.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ use desktop::*;
4141
#[cfg(mobile)]
4242
use mobile::*;
4343

44+
pub(crate) const OK: &str = "Ok";
45+
pub(crate) const CANCEL: &str = "Cancel";
46+
4447
macro_rules! blocking_fn {
4548
($self:ident, $fn:ident) => {{
4649
let (tx, rx) = sync_channel(0);
@@ -89,14 +92,13 @@ impl<R: Runtime> Dialog<R> {
8992
/// - Ask dialog:
9093
///
9194
/// ```
92-
/// use tauri_plugin_dialog::DialogExt;
95+
/// use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
9396
///
9497
/// tauri::Builder::default()
9598
/// .setup(|app| {
9699
/// app.dialog()
97100
/// .message("Are you sure?")
98-
/// .ok_button_label("Yes")
99-
/// .cancel_button_label("No")
101+
/// .buttons(MessageDialogButtons::OkCancelCustom("Yes", "No"))
100102
/// .show(|yes| {
101103
/// println!("user said {}", if yes { "yes" } else { "no" });
102104
/// });
@@ -107,13 +109,13 @@ impl<R: Runtime> Dialog<R> {
107109
/// - Message dialog with OK button:
108110
///
109111
/// ```
110-
/// use tauri_plugin_dialog::DialogExt;
112+
/// use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
111113
///
112114
/// tauri::Builder::default()
113115
/// .setup(|app| {
114116
/// app.dialog()
115117
/// .message("Job completed successfully")
116-
/// .ok_button_label("Ok")
118+
/// .buttons(MessageDialogButtons::Ok)
117119
/// .show(|_| {
118120
/// println!("dialog closed");
119121
/// });
@@ -129,16 +131,15 @@ impl<R: Runtime> Dialog<R> {
129131
/// but note that it cannot be executed on the main thread as it will freeze your application.
130132
///
131133
/// ```
132-
/// use tauri_plugin_dialog::DialogExt;
134+
/// use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
133135
///
134136
/// tauri::Builder::default()
135137
/// .setup(|app| {
136138
/// let handle = app.handle().clone();
137139
/// std::thread::spawn(move || {
138140
/// let yes = handle.dialog()
139141
/// .message("Are you sure?")
140-
/// .ok_button_label("Yes")
141-
/// .cancel_button_label("No")
142+
/// .buttons(MessageDialogButtons::OkCancelCustom("Yes", "No"))
142143
/// .blocking_show();
143144
/// });
144145
///
@@ -196,8 +197,7 @@ pub struct MessageDialogBuilder<R: Runtime> {
196197
pub(crate) title: String,
197198
pub(crate) message: String,
198199
pub(crate) kind: MessageDialogKind,
199-
pub(crate) ok_button_label: Option<String>,
200-
pub(crate) cancel_button_label: Option<String>,
200+
pub(crate) buttons: MessageDialogButtons,
201201
#[cfg(desktop)]
202202
pub(crate) parent: Option<crate::desktop::WindowHandle>,
203203
}
@@ -210,8 +210,8 @@ pub(crate) struct MessageDialogPayload<'a> {
210210
title: &'a String,
211211
message: &'a String,
212212
kind: &'a MessageDialogKind,
213-
ok_button_label: &'a Option<String>,
214-
cancel_button_label: &'a Option<String>,
213+
ok_button_label: Option<&'a str>,
214+
cancel_button_label: Option<&'a str>,
215215
}
216216

217217
// raw window handle :(
@@ -225,21 +225,28 @@ impl<R: Runtime> MessageDialogBuilder<R> {
225225
title: title.into(),
226226
message: message.into(),
227227
kind: Default::default(),
228-
ok_button_label: None,
229-
cancel_button_label: None,
228+
buttons: Default::default(),
230229
#[cfg(desktop)]
231230
parent: None,
232231
}
233232
}
234233

235234
#[cfg(mobile)]
236235
pub(crate) fn payload(&self) -> MessageDialogPayload<'_> {
236+
let (ok_button_label, cancel_button_label) = match &self.buttons {
237+
MessageDialogButtons::Ok => (Some(OK), None),
238+
MessageDialogButtons::OkCancel => (Some(OK), Some(CANCEL)),
239+
MessageDialogButtons::OkCustom(ok) => (Some(ok.as_str()), Some(CANCEL)),
240+
MessageDialogButtons::OkCancelCustom(ok, cancel) => {
241+
(Some(ok.as_str()), Some(cancel.as_str()))
242+
}
243+
};
237244
MessageDialogPayload {
238245
title: &self.title,
239246
message: &self.message,
240247
kind: &self.kind,
241-
ok_button_label: &self.ok_button_label,
242-
cancel_button_label: &self.cancel_button_label,
248+
ok_button_label,
249+
cancel_button_label,
243250
}
244251
}
245252

@@ -266,15 +273,9 @@ impl<R: Runtime> MessageDialogBuilder<R> {
266273
self
267274
}
268275

269-
/// Sets the label for the OK button.
270-
pub fn ok_button_label(mut self, label: impl Into<String>) -> Self {
271-
self.ok_button_label.replace(label.into());
272-
self
273-
}
274-
275-
/// Sets the label for the Cancel button.
276-
pub fn cancel_button_label(mut self, label: impl Into<String>) -> Self {
277-
self.cancel_button_label.replace(label.into());
276+
/// Sets the dialog buttons.
277+
pub fn buttons(mut self, buttons: MessageDialogButtons) -> Self {
278+
self.buttons = buttons;
278279
self
279280
}
280281

plugins/dialog/src/models.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,18 @@ impl Serialize for MessageDialogKind {
4949
}
5050
}
5151
}
52+
53+
/// Set of button that will be displayed on the dialog
54+
#[non_exhaustive]
55+
#[derive(Debug, Default, Clone)]
56+
pub 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+
/// A single `Ok` button with custom text
63+
OkCustom(String),
64+
/// 2 buttons `Ok` and `Cancel` with custom texts
65+
OkCancelCustom(String, String),
66+
}

0 commit comments

Comments
 (0)