Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tauri): use WRY as webview engine #1190

Merged
merged 14 commits into from
Feb 8, 2021
Merged
5 changes: 5 additions & 0 deletions .changes/wry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": minor
---

Use [WRY](https://github.com/tauri-apps/wry) as Webview interface, thanks to @wusyong.
2 changes: 1 addition & 1 deletion api/src/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function uid(): string {
* @param args
*/
function invoke(args: any): void {
window.__TAURI_INVOKE_HANDLER__(args);
window.__TAURI_INVOKE_HANDLER__(JSON.stringify(args));
}

function transformCallback(
Expand Down
16 changes: 8 additions & 8 deletions cli/core/src/templates/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,24 @@ if (!String.prototype.startsWith) {

if (window.__TAURI_INVOKE_HANDLER__) {
window.__TAURI_INVOKE_HANDLER__(
_objectSpread(
JSON.stringify(_objectSpread(
{
callback: callback,
error: error
},
args
)
))
)
} else {
window.addEventListener('DOMContentLoaded', function () {
window.__TAURI_INVOKE_HANDLER__(
_objectSpread(
JSON.stringify(_objectSpread(
{
callback: callback,
error: error
},
args
)
))
)
})
}
Expand Down Expand Up @@ -182,10 +182,10 @@ if (!String.prototype.startsWith) {
target.href.startsWith('http') &&
target.target === '_blank'
) {
window.__TAURI_INVOKE_HANDLER__({
window.__TAURI_INVOKE_HANDLER__(JSON.stringify({
cmd: 'open',
uri: target.href
})
}))
e.preventDefault()
}
break
Expand Down Expand Up @@ -294,10 +294,10 @@ if (!String.prototype.startsWith) {
})

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

window.confirm = function (message) {
Expand Down
2 changes: 1 addition & 1 deletion cli/tauri.js/templates/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mod cmd;

fn main() {
tauri::AppBuilder::<tauri::flavors::Official>::new()
tauri::AppBuilder::<tauri::flavors::Wry>::new()
.invoke_handler(|_webview, arg| async move {
use cmd::Cmd::*;
match serde_json::from_str(&arg) {
Expand Down
8 changes: 5 additions & 3 deletions cli/tauri.js/test/jest/fixtures/app/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@
})

setTimeout(function () {
window.__TAURI_INVOKE_HANDLER__({
cmd: 'exit'
})
window.__TAURI_INVOKE_HANDLER__(
JSON.stringify({
cmd: 'exit'
})
)
}, 15000)
</script>
</body>
Expand Down
16 changes: 6 additions & 10 deletions tauri-api/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::Serialize;
use serde_json::Value as JsonValue;
use std::fmt::Display;

/// Formats a function name and argument to be evaluated as callback.
///
Expand All @@ -25,10 +24,7 @@ use std::fmt::Display;
/// }).expect("failed to serialize"));
/// assert!(cb.contains(r#"window["callback-function-name"]({"value":"some value"})"#));
/// ```
pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
function_name: S,
arg: T,
) -> String {
pub fn format_callback<T: Into<JsonValue>, S: AsRef<str>>(function_name: S, arg: T) -> String {
format!(
r#"
if (window["{fn}"]) {{
Expand All @@ -37,7 +33,7 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
console.warn("[TAURI] Couldn't find callback id {fn} in window. This happens when the app is reloaded while Rust is running an asynchronous operation.")
}}
"#,
fn = function_name,
fn = function_name.as_ref(),
arg = arg.into().to_string()
)
}
Expand All @@ -57,17 +53,17 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
/// ```
/// use tauri_api::rpc::format_callback_result;
/// let res: Result<u8, &str> = Ok(5);
/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
/// assert!(cb.contains(r#"window["success_cb"](5)"#));
///
/// let res: Result<&str, &str> = Err("error message here");
/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
/// assert!(cb.contains(r#"window["error_cb"]("error message here")"#));
/// ```
pub fn format_callback_result<T: Serialize, E: Serialize>(
result: Result<T, E>,
success_callback: String,
error_callback: String,
success_callback: impl AsRef<str>,
error_callback: impl AsRef<str>,
) -> crate::Result<String> {
let rpc = match result {
Ok(res) => format_callback(success_callback, serde_json::to_value(res)?),
Expand Down
3 changes: 1 addition & 2 deletions tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ features = [ "all-api" ]
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
webview_official = "0.2.0"
tauri_includedir = "0.6.0"
phf = "0.8.0"
base64 = "0.13.0"
Expand All @@ -33,10 +32,10 @@ async-trait = "0.1"
uuid = { version = "0.8.2", features = [ "v4" ] }
anyhow = "1.0.38"
thiserror = "1.0.23"
envmnt = "0.8.4"
once_cell = "1.5.2"
tauri-api = { version = "0.7.5", path = "../tauri-api" }
urlencoding = "1.1.1"
wry = { git = "https://github.com/tauri-apps/wry", rev = "42f4f2133f7921ed5adc47908787094da8abeac5" }

[target."cfg(target_os = \"windows\")".dependencies]
runas = "0.2"
Expand Down
Loading