Skip to content

Commit

Permalink
feat(tauri) make window.alert and window.confirm available, fix #848
Browse files Browse the repository at this point in the history
 (#854)
  • Loading branch information
lucasfernog committed Jul 18, 2020
1 parent 42a8bb0 commit 0245833
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changes/js-prompt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": patch
---

Use native dialog on `window.alert` and `window.confirm`.
Since every communication with the webview is asynchronous, the `window.confirm` returns a Promise resolving to a boolean value.
14 changes: 14 additions & 0 deletions cli/tauri.js/templates/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,18 @@ if (!String.prototype.startsWith) {
setNotificationPermission(response ? 'granted' : 'denied')
}
})

window.alert = function (message) {
window.__TAURI_INVOKE_HANDLER__({
cmd: 'messageDialog',
message: message
})
}

window.confirm = function (message) {
return window.__TAURI__.promisified({
cmd: 'askDialog',
message: message
})
}
})()
10 changes: 10 additions & 0 deletions tauri-api/src/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ pub fn ask(message: impl AsRef<str>, title: impl AsRef<str>) -> DialogSelection
.show()
}

/// Displays a message dialog.
pub fn message(message: impl AsRef<str>, title: impl AsRef<str>) {
DialogBuilder::new()
.message(message.as_ref())
.title(title.as_ref())
.style(DialogStyle::Info)
.build()
.show();
}

/// Open single select file dialog
pub fn select(
filter_list: Option<impl AsRef<str>>,
Expand Down
32 changes: 31 additions & 1 deletion tauri/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod salt;
mod asset;
#[cfg(open)]
mod browser;
#[cfg(any(open_dialog, save_dialog))]
mod dialog;
#[cfg(event)]
mod event;
Expand Down Expand Up @@ -208,6 +207,37 @@ pub(crate) fn handle(webview: &mut Webview, arg: &str) -> crate::Result<()> {
#[cfg(not(save_dialog))]
throw_whitelist_error(webview, "saveDialog");
}
MessageDialog { message } => {
let exe = std::env::current_exe()?;
let exe_dir = exe.parent().expect("failed to get exe directory");
let app_name = exe
.file_name()
.expect("failed to get exe filename")
.to_string_lossy();
dialog::message(app_name.to_string(), message);
}
AskDialog {
title,
message,
callback,
error,
} => {
let exe = std::env::current_exe()?;
dialog::ask(
webview,
title.unwrap_or_else(|| {
let exe_dir = exe.parent().expect("failed to get exe directory");
exe
.file_name()
.expect("failed to get exe filename")
.to_string_lossy()
.to_string()
}),
message,
callback,
error,
)?;
}
HttpRequest {
options,
callback,
Expand Down
32 changes: 27 additions & 5 deletions tauri/src/endpoints/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ pub enum Cmd {
error: String,
},
/// The set webview title API.
SetTitle { title: String },
SetTitle {
title: String,
},
/// The execute script API.
Execute {
command: String,
Expand All @@ -149,7 +151,9 @@ pub enum Cmd {
error: String,
},
/// The open URL in browser API
Open { uri: String },
Open {
uri: String,
},
ValidateSalt {
salt: String,
callback: String,
Expand Down Expand Up @@ -178,6 +182,15 @@ pub enum Cmd {
callback: String,
error: String,
},
MessageDialog {
message: String,
},
AskDialog {
title: Option<String>,
message: String,
callback: String,
error: String,
},
/// The HTTP request API.
HttpRequest {
options: Box<HttpRequestOptions>,
Expand All @@ -194,15 +207,24 @@ pub enum Cmd {
error: String,
},
/// The get CLI matches API.
CliMatches { callback: String, error: String },
CliMatches {
callback: String,
error: String,
},
/// The show notification API.
Notification {
options: NotificationOptions,
callback: String,
error: String,
},
/// The request notification permission API.
RequestNotificationPermission { callback: String, error: String },
RequestNotificationPermission {
callback: String,
error: String,
},
/// The notification permission check API.
IsNotificationPermissionGranted { callback: String, error: String },
IsNotificationPermissionGranted {
callback: String,
error: String,
},
}
31 changes: 30 additions & 1 deletion tauri/src/endpoints/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::cmd::{OpenDialogOptions, SaveDialogOptions};
use crate::api::dialog::{pick_folder, save_file, select, select_multiple, Response};
use crate::api::dialog::{
ask as ask_dialog, message as message_dialog, pick_folder, save_file, select, select_multiple,
DialogSelection, Response,
};
use serde_json::Value as JsonValue;
use webview_rust_sys::Webview;

/// maps a dialog response to a JS value to eval
#[cfg(any(open_dialog, save_dialog))]
fn map_response(response: Response) -> JsonValue {
match response {
Response::Okay(path) => path.into(),
Expand Down Expand Up @@ -54,3 +58,28 @@ pub fn save(
)?;
Ok(())
}

/// Shows a message in a dialog.
pub fn message(title: String, message: String) {
message_dialog(message, title);
}

/// Shows a dialog with a yes/no question.
pub fn ask(
webview: &mut Webview,
title: String,
message: String,
callback: String,
error: String,
) -> crate::Result<()> {
crate::execute_promise_sync(
webview,
move || match ask_dialog(message, title) {
DialogSelection::Yes => Ok(true),
_ => Ok(false),
},
callback,
error,
)?;
Ok(())
}

0 comments on commit 0245833

Please sign in to comment.