Skip to content

Commit 0245833

Browse files
authored
feat(tauri) make window.alert and window.confirm available, fix #848 (#854)
1 parent 42a8bb0 commit 0245833

6 files changed

Lines changed: 118 additions & 7 deletions

File tree

.changes/js-prompt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Use native dialog on `window.alert` and `window.confirm`.
6+
Since every communication with the webview is asynchronous, the `window.confirm` returns a Promise resolving to a boolean value.

cli/tauri.js/templates/tauri.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,18 @@ if (!String.prototype.startsWith) {
247247
setNotificationPermission(response ? 'granted' : 'denied')
248248
}
249249
})
250+
251+
window.alert = function (message) {
252+
window.__TAURI_INVOKE_HANDLER__({
253+
cmd: 'messageDialog',
254+
message: message
255+
})
256+
}
257+
258+
window.confirm = function (message) {
259+
return window.__TAURI__.promisified({
260+
cmd: 'askDialog',
261+
message: message
262+
})
263+
}
250264
})()

tauri-api/src/dialog.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ pub fn ask(message: impl AsRef<str>, title: impl AsRef<str>) -> DialogSelection
3434
.show()
3535
}
3636

37+
/// Displays a message dialog.
38+
pub fn message(message: impl AsRef<str>, title: impl AsRef<str>) {
39+
DialogBuilder::new()
40+
.message(message.as_ref())
41+
.title(title.as_ref())
42+
.style(DialogStyle::Info)
43+
.build()
44+
.show();
45+
}
46+
3747
/// Open single select file dialog
3848
pub fn select(
3949
filter_list: Option<impl AsRef<str>>,

tauri/src/endpoints.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod salt;
77
mod asset;
88
#[cfg(open)]
99
mod browser;
10-
#[cfg(any(open_dialog, save_dialog))]
1110
mod dialog;
1211
#[cfg(event)]
1312
mod event;
@@ -208,6 +207,37 @@ pub(crate) fn handle(webview: &mut Webview, arg: &str) -> crate::Result<()> {
208207
#[cfg(not(save_dialog))]
209208
throw_whitelist_error(webview, "saveDialog");
210209
}
210+
MessageDialog { message } => {
211+
let exe = std::env::current_exe()?;
212+
let exe_dir = exe.parent().expect("failed to get exe directory");
213+
let app_name = exe
214+
.file_name()
215+
.expect("failed to get exe filename")
216+
.to_string_lossy();
217+
dialog::message(app_name.to_string(), message);
218+
}
219+
AskDialog {
220+
title,
221+
message,
222+
callback,
223+
error,
224+
} => {
225+
let exe = std::env::current_exe()?;
226+
dialog::ask(
227+
webview,
228+
title.unwrap_or_else(|| {
229+
let exe_dir = exe.parent().expect("failed to get exe directory");
230+
exe
231+
.file_name()
232+
.expect("failed to get exe filename")
233+
.to_string_lossy()
234+
.to_string()
235+
}),
236+
message,
237+
callback,
238+
error,
239+
)?;
240+
}
211241
HttpRequest {
212242
options,
213243
callback,

tauri/src/endpoints/cmd.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ pub enum Cmd {
140140
error: String,
141141
},
142142
/// The set webview title API.
143-
SetTitle { title: String },
143+
SetTitle {
144+
title: String,
145+
},
144146
/// The execute script API.
145147
Execute {
146148
command: String,
@@ -149,7 +151,9 @@ pub enum Cmd {
149151
error: String,
150152
},
151153
/// The open URL in browser API
152-
Open { uri: String },
154+
Open {
155+
uri: String,
156+
},
153157
ValidateSalt {
154158
salt: String,
155159
callback: String,
@@ -178,6 +182,15 @@ pub enum Cmd {
178182
callback: String,
179183
error: String,
180184
},
185+
MessageDialog {
186+
message: String,
187+
},
188+
AskDialog {
189+
title: Option<String>,
190+
message: String,
191+
callback: String,
192+
error: String,
193+
},
181194
/// The HTTP request API.
182195
HttpRequest {
183196
options: Box<HttpRequestOptions>,
@@ -194,15 +207,24 @@ pub enum Cmd {
194207
error: String,
195208
},
196209
/// The get CLI matches API.
197-
CliMatches { callback: String, error: String },
210+
CliMatches {
211+
callback: String,
212+
error: String,
213+
},
198214
/// The show notification API.
199215
Notification {
200216
options: NotificationOptions,
201217
callback: String,
202218
error: String,
203219
},
204220
/// The request notification permission API.
205-
RequestNotificationPermission { callback: String, error: String },
221+
RequestNotificationPermission {
222+
callback: String,
223+
error: String,
224+
},
206225
/// The notification permission check API.
207-
IsNotificationPermissionGranted { callback: String, error: String },
226+
IsNotificationPermissionGranted {
227+
callback: String,
228+
error: String,
229+
},
208230
}

tauri/src/endpoints/dialog.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use super::cmd::{OpenDialogOptions, SaveDialogOptions};
2-
use crate::api::dialog::{pick_folder, save_file, select, select_multiple, Response};
2+
use crate::api::dialog::{
3+
ask as ask_dialog, message as message_dialog, pick_folder, save_file, select, select_multiple,
4+
DialogSelection, Response,
5+
};
36
use serde_json::Value as JsonValue;
47
use webview_rust_sys::Webview;
58

69
/// maps a dialog response to a JS value to eval
10+
#[cfg(any(open_dialog, save_dialog))]
711
fn map_response(response: Response) -> JsonValue {
812
match response {
913
Response::Okay(path) => path.into(),
@@ -54,3 +58,28 @@ pub fn save(
5458
)?;
5559
Ok(())
5660
}
61+
62+
/// Shows a message in a dialog.
63+
pub fn message(title: String, message: String) {
64+
message_dialog(message, title);
65+
}
66+
67+
/// Shows a dialog with a yes/no question.
68+
pub fn ask(
69+
webview: &mut Webview,
70+
title: String,
71+
message: String,
72+
callback: String,
73+
error: String,
74+
) -> crate::Result<()> {
75+
crate::execute_promise_sync(
76+
webview,
77+
move || match ask_dialog(message, title) {
78+
DialogSelection::Yes => Ok(true),
79+
_ => Ok(false),
80+
},
81+
callback,
82+
error,
83+
)?;
84+
Ok(())
85+
}

0 commit comments

Comments
 (0)