Skip to content

Commit 99ecf7b

Browse files
authored
feat(tauri): use WRY as webview engine (#1190)
1 parent 77a29f7 commit 99ecf7b

File tree

30 files changed

+3416
-976
lines changed

30 files changed

+3416
-976
lines changed

.changes/wry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor
3+
---
4+
5+
Use [WRY](https://github.com/tauri-apps/wry) as Webview interface, thanks to @wusyong.

api/src/tauri.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function uid(): string {
3434
* @param args
3535
*/
3636
function invoke(args: any): void {
37-
window.__TAURI_INVOKE_HANDLER__(args);
37+
window.__TAURI_INVOKE_HANDLER__(JSON.stringify(args));
3838
}
3939

4040
function transformCallback(

cli/core/src/templates/tauri.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,24 @@ if (!String.prototype.startsWith) {
118118

119119
if (window.__TAURI_INVOKE_HANDLER__) {
120120
window.__TAURI_INVOKE_HANDLER__(
121-
_objectSpread(
121+
JSON.stringify(_objectSpread(
122122
{
123123
callback: callback,
124124
error: error
125125
},
126126
args
127-
)
127+
))
128128
)
129129
} else {
130130
window.addEventListener('DOMContentLoaded', function () {
131131
window.__TAURI_INVOKE_HANDLER__(
132-
_objectSpread(
132+
JSON.stringify(_objectSpread(
133133
{
134134
callback: callback,
135135
error: error
136136
},
137137
args
138-
)
138+
))
139139
)
140140
})
141141
}
@@ -182,10 +182,10 @@ if (!String.prototype.startsWith) {
182182
target.href.startsWith('http') &&
183183
target.target === '_blank'
184184
) {
185-
window.__TAURI_INVOKE_HANDLER__({
185+
window.__TAURI_INVOKE_HANDLER__(JSON.stringify({
186186
cmd: 'open',
187187
uri: target.href
188-
})
188+
}))
189189
e.preventDefault()
190190
}
191191
break
@@ -294,10 +294,10 @@ if (!String.prototype.startsWith) {
294294
})
295295

296296
window.alert = function (message) {
297-
window.__TAURI_INVOKE_HANDLER__({
297+
window.__TAURI_INVOKE_HANDLER__(JSON.stringify({
298298
cmd: 'messageDialog',
299299
message: message
300-
})
300+
}))
301301
}
302302

303303
window.confirm = function (message) {

cli/tauri.js/templates/src-tauri/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
mod cmd;
77

88
fn main() {
9-
tauri::AppBuilder::<tauri::flavors::Official>::new()
9+
tauri::AppBuilder::<tauri::flavors::Wry>::new()
1010
.invoke_handler(|_webview, arg| async move {
1111
use cmd::Cmd::*;
1212
match serde_json::from_str(&arg) {

cli/tauri.js/test/jest/fixtures/app/dist/index.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@
136136
})
137137

138138
setTimeout(function () {
139-
window.__TAURI_INVOKE_HANDLER__({
140-
cmd: 'exit'
141-
})
139+
window.__TAURI_INVOKE_HANDLER__(
140+
JSON.stringify({
141+
cmd: 'exit'
142+
})
143+
)
142144
}, 15000)
143145
</script>
144146
</body>

tauri-api/src/rpc.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use serde::Serialize;
22
use serde_json::Value as JsonValue;
3-
use std::fmt::Display;
43

54
/// Formats a function name and argument to be evaluated as callback.
65
///
@@ -25,10 +24,7 @@ use std::fmt::Display;
2524
/// }).expect("failed to serialize"));
2625
/// assert!(cb.contains(r#"window["callback-function-name"]({"value":"some value"})"#));
2726
/// ```
28-
pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
29-
function_name: S,
30-
arg: T,
31-
) -> String {
27+
pub fn format_callback<T: Into<JsonValue>, S: AsRef<str>>(function_name: S, arg: T) -> String {
3228
format!(
3329
r#"
3430
if (window["{fn}"]) {{
@@ -37,7 +33,7 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
3733
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.")
3834
}}
3935
"#,
40-
fn = function_name,
36+
fn = function_name.as_ref(),
4137
arg = arg.into().to_string()
4238
)
4339
}
@@ -57,17 +53,17 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
5753
/// ```
5854
/// use tauri_api::rpc::format_callback_result;
5955
/// let res: Result<u8, &str> = Ok(5);
60-
/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
56+
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
6157
/// assert!(cb.contains(r#"window["success_cb"](5)"#));
6258
///
6359
/// let res: Result<&str, &str> = Err("error message here");
64-
/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
60+
/// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
6561
/// assert!(cb.contains(r#"window["error_cb"]("error message here")"#));
6662
/// ```
6763
pub fn format_callback_result<T: Serialize, E: Serialize>(
6864
result: Result<T, E>,
69-
success_callback: String,
70-
error_callback: String,
65+
success_callback: impl AsRef<str>,
66+
error_callback: impl AsRef<str>,
7167
) -> crate::Result<String> {
7268
let rpc = match result {
7369
Ok(res) => format_callback(success_callback, serde_json::to_value(res)?),

tauri/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ features = [ "all-api" ]
2020
[dependencies]
2121
serde_json = "1.0"
2222
serde = { version = "1.0", features = [ "derive" ] }
23-
webview_official = "0.2.0"
2423
tauri_includedir = "0.6.0"
2524
phf = "0.8.0"
2625
base64 = "0.13.0"
@@ -33,10 +32,10 @@ async-trait = "0.1"
3332
uuid = { version = "0.8.2", features = [ "v4" ] }
3433
anyhow = "1.0.38"
3534
thiserror = "1.0.23"
36-
envmnt = "0.8.4"
3735
once_cell = "1.5.2"
3836
tauri-api = { version = "0.7.5", path = "../tauri-api" }
3937
urlencoding = "1.1.1"
38+
wry = { git = "https://github.com/tauri-apps/wry", rev = "42f4f2133f7921ed5adc47908787094da8abeac5" }
4039

4140
[target."cfg(target_os = \"windows\")".dependencies]
4241
runas = "0.2"

0 commit comments

Comments
 (0)