@@ -41,6 +41,9 @@ use desktop::*;
41
41
#[ cfg( mobile) ]
42
42
use mobile:: * ;
43
43
44
+ pub ( crate ) const OK : & str = "Ok" ;
45
+ pub ( crate ) const CANCEL : & str = "Cancel" ;
46
+
44
47
macro_rules! blocking_fn {
45
48
( $self: ident, $fn: ident) => { {
46
49
let ( tx, rx) = sync_channel( 0 ) ;
@@ -89,14 +92,13 @@ impl<R: Runtime> Dialog<R> {
89
92
/// - Ask dialog:
90
93
///
91
94
/// ```
92
- /// use tauri_plugin_dialog::DialogExt;
95
+ /// use tauri_plugin_dialog::{ DialogExt, MessageDialogButtons} ;
93
96
///
94
97
/// tauri::Builder::default()
95
98
/// .setup(|app| {
96
99
/// app.dialog()
97
100
/// .message("Are you sure?")
98
- /// .ok_button_label("Yes")
99
- /// .cancel_button_label("No")
101
+ /// .buttons(MessageDialogButtons::OkCancelCustom("Yes", "No"))
100
102
/// .show(|yes| {
101
103
/// println!("user said {}", if yes { "yes" } else { "no" });
102
104
/// });
@@ -107,13 +109,13 @@ impl<R: Runtime> Dialog<R> {
107
109
/// - Message dialog with OK button:
108
110
///
109
111
/// ```
110
- /// use tauri_plugin_dialog::DialogExt;
112
+ /// use tauri_plugin_dialog::{ DialogExt, MessageDialogButtons} ;
111
113
///
112
114
/// tauri::Builder::default()
113
115
/// .setup(|app| {
114
116
/// app.dialog()
115
117
/// .message("Job completed successfully")
116
- /// .ok_button_label("Ok" )
118
+ /// .buttons(MessageDialogButtons::Ok )
117
119
/// .show(|_| {
118
120
/// println!("dialog closed");
119
121
/// });
@@ -129,16 +131,15 @@ impl<R: Runtime> Dialog<R> {
129
131
/// but note that it cannot be executed on the main thread as it will freeze your application.
130
132
///
131
133
/// ```
132
- /// use tauri_plugin_dialog::DialogExt;
134
+ /// use tauri_plugin_dialog::{ DialogExt, MessageDialogButtons} ;
133
135
///
134
136
/// tauri::Builder::default()
135
137
/// .setup(|app| {
136
138
/// let handle = app.handle().clone();
137
139
/// std::thread::spawn(move || {
138
140
/// let yes = handle.dialog()
139
141
/// .message("Are you sure?")
140
- /// .ok_button_label("Yes")
141
- /// .cancel_button_label("No")
142
+ /// .buttons(MessageDialogButtons::OkCancelCustom("Yes", "No"))
142
143
/// .blocking_show();
143
144
/// });
144
145
///
@@ -196,8 +197,7 @@ pub struct MessageDialogBuilder<R: Runtime> {
196
197
pub ( crate ) title : String ,
197
198
pub ( crate ) message : String ,
198
199
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 ,
201
201
#[ cfg( desktop) ]
202
202
pub ( crate ) parent : Option < crate :: desktop:: WindowHandle > ,
203
203
}
@@ -210,8 +210,8 @@ pub(crate) struct MessageDialogPayload<'a> {
210
210
title : & ' a String ,
211
211
message : & ' a String ,
212
212
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 > ,
215
215
}
216
216
217
217
// raw window handle :(
@@ -225,21 +225,28 @@ impl<R: Runtime> MessageDialogBuilder<R> {
225
225
title : title. into ( ) ,
226
226
message : message. into ( ) ,
227
227
kind : Default :: default ( ) ,
228
- ok_button_label : None ,
229
- cancel_button_label : None ,
228
+ buttons : Default :: default ( ) ,
230
229
#[ cfg( desktop) ]
231
230
parent : None ,
232
231
}
233
232
}
234
233
235
234
#[ cfg( mobile) ]
236
235
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
+ } ;
237
244
MessageDialogPayload {
238
245
title : & self . title ,
239
246
message : & self . message ,
240
247
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,
243
250
}
244
251
}
245
252
@@ -266,15 +273,9 @@ impl<R: Runtime> MessageDialogBuilder<R> {
266
273
self
267
274
}
268
275
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;
278
279
self
279
280
}
280
281
0 commit comments