Skip to content

Commit 4abd12c

Browse files
authored
fix(tauri) webview initialization on windows, fixes #879 (#885)
1 parent 44b1be5 commit 4abd12c

File tree

14 files changed

+70
-79
lines changed

14 files changed

+70
-79
lines changed

.changes/tauri-windows.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": minor
3+
"tauri.js": minor
4+
---
5+
6+
Fixes the Webview initialization on Windows.

cli/tauri.js/templates/tauri.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,6 @@ if (!String.prototype.startsWith) {
77
}
88

99
(function () {
10-
function webviewBind (name) {
11-
var RPC = window._rpc = (window._rpc || { nextSeq: 1 });
12-
window[name] = function () {
13-
var seq = RPC.nextSeq++;
14-
var promise = new Promise(function (resolve, reject) {
15-
RPC[seq] = {
16-
resolve: resolve,
17-
reject: reject,
18-
};
19-
});
20-
window.external.invoke(JSON.stringify({
21-
id: seq,
22-
method: name,
23-
params: Array.prototype.slice.call(arguments),
24-
}));
25-
return promise;
26-
}
27-
}
28-
if (!window.__TAURI_INVOKE_HANDLER__) {
29-
webviewBind('__TAURI_INVOKE_HANDLER__')
30-
}
31-
3210
function s4() {
3311
return Math.floor((1 + Math.random()) * 0x10000)
3412
.toString(16)

tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ features = [ "all-api" ]
2020
[dependencies]
2121
serde_json = "1.0"
2222
serde = { version = "1.0", features = [ "derive" ] }
23-
webview_official = "0.0.1"
23+
webview_official = "0.0.2"
2424
tauri_includedir = "0.6.0"
2525
phf = "0.8.0"
2626
base64 = "0.12.3"

tauri/src/app.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use webview_official::Webview;
22

33
mod runner;
44

5-
type InvokeHandler = Box<dyn FnMut(&mut Webview, &str) -> Result<(), String>>;
6-
type Setup = Box<dyn FnMut(&mut Webview, String)>;
5+
type InvokeHandler = Box<dyn FnMut(&mut Webview<'_>, &str) -> Result<(), String>>;
6+
type Setup = Box<dyn FnMut(&mut Webview<'_>, String)>;
77

88
/// The application runner.
99
pub struct App {
@@ -26,7 +26,7 @@ impl App {
2626
/// The message is considered consumed if the handler exists and returns an Ok Result.
2727
pub(crate) fn run_invoke_handler(
2828
&mut self,
29-
webview: &mut Webview,
29+
webview: &mut Webview<'_>,
3030
arg: &str,
3131
) -> Result<bool, String> {
3232
if let Some(ref mut invoke_handler) = self.invoke_handler {
@@ -37,7 +37,7 @@ impl App {
3737
}
3838

3939
/// Runs the setup callback if defined.
40-
pub(crate) fn run_setup(&mut self, webview: &mut Webview, source: String) {
40+
pub(crate) fn run_setup(&mut self, webview: &mut Webview<'_>, source: String) {
4141
if let Some(ref mut setup) = self.setup {
4242
setup(webview, source);
4343
}
@@ -71,7 +71,7 @@ impl AppBuilder {
7171
}
7272

7373
/// Defines the JS message handler callback.
74-
pub fn invoke_handler<F: FnMut(&mut Webview, &str) -> Result<(), String> + 'static>(
74+
pub fn invoke_handler<F: FnMut(&mut Webview<'_>, &str) -> Result<(), String> + 'static>(
7575
mut self,
7676
invoke_handler: F,
7777
) -> Self {
@@ -80,7 +80,7 @@ impl AppBuilder {
8080
}
8181

8282
/// Defines the setup callback.
83-
pub fn setup<F: FnMut(&mut Webview, String) + 'static>(mut self, setup: F) -> Self {
83+
pub fn setup<F: FnMut(&mut Webview<'_>, String) + 'static>(mut self, setup: F) -> Self {
8484
self.setup = Some(Box::new(setup));
8585
self
8686
}

tauri/src/app/runner.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn build_webview(
236236
application: &mut App,
237237
content: Content<String>,
238238
splashscreen_content: Option<Content<String>>,
239-
) -> crate::Result<Webview> {
239+
) -> crate::Result<Webview<'_>> {
240240
let config = get()?;
241241
let content_clone = match content {
242242
Content::Html(ref html) => Content::Html(html.clone()),
@@ -264,28 +264,30 @@ fn build_webview(
264264
},
265265
};
266266

267-
let mut webview = WebviewBuilder::new()
268-
.init(&format!(
269-
r#"
270-
{event_init}
271-
if (window.__TAURI_INVOKE_HANDLER__) {{
267+
let init = format!(
268+
r#"
269+
{event_init}
270+
if (window.__TAURI_INVOKE_HANDLER__) {{
271+
window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
272+
}} else {{
273+
window.addEventListener('DOMContentLoaded', function () {{
272274
window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
273-
}} else {{
274-
window.addEventListener('DOMContentLoaded', function () {{
275-
window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
276-
}})
277-
}}
278-
{plugin_init}
279-
"#,
280-
event_init = init(),
281-
plugin_init = crate::plugin::init_script()
282-
))
275+
}})
276+
}}
277+
{plugin_init}
278+
"#,
279+
event_init = init(),
280+
plugin_init = crate::plugin::init_script()
281+
);
282+
283+
let mut webview = WebviewBuilder::new()
284+
.init(Box::leak(init.into_boxed_str()))
283285
.title(Box::leak(title))
284286
.width(width as usize)
285287
.height(height as usize)
286288
.resize(resizable)
287289
.debug(debug)
288-
.url(&url)
290+
.url(Box::leak(url.into_boxed_str()))
289291
.build();
290292
// TODO waiting for webview window API
291293
// webview.set_fullscreen(fullscreen);

tauri/src/endpoints.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod notification;
1818
use webview_official::Webview;
1919

2020
#[allow(unused_variables)]
21-
pub(crate) fn handle(webview: &mut Webview, arg: &str) -> crate::Result<()> {
21+
pub(crate) fn handle(webview: &mut Webview<'_>, arg: &str) -> crate::Result<()> {
2222
use cmd::Cmd::*;
2323
match serde_json::from_str(arg) {
2424
Err(e) => Err(e.into()),
@@ -304,13 +304,13 @@ pub(crate) fn handle(webview: &mut Webview, arg: &str) -> crate::Result<()> {
304304
}
305305

306306
#[allow(dead_code)]
307-
fn api_error(webview: &mut Webview, error_fn: String, message: &str) {
307+
fn api_error(webview: &mut Webview<'_>, error_fn: String, message: &str) {
308308
let reject_code = tauri_api::rpc::format_callback(error_fn, message);
309309
webview.eval(&reject_code)
310310
}
311311

312312
#[allow(dead_code)]
313-
fn allowlist_error(webview: &mut Webview, error_fn: String, allowlist_key: &str) {
313+
fn allowlist_error(webview: &mut Webview<'_>, error_fn: String, allowlist_key: &str) {
314314
api_error(
315315
webview,
316316
error_fn,
@@ -322,7 +322,7 @@ fn allowlist_error(webview: &mut Webview, error_fn: String, allowlist_key: &str)
322322
}
323323

324324
#[allow(dead_code)]
325-
fn throw_allowlist_error(webview: &mut Webview, allowlist_key: &str) {
325+
fn throw_allowlist_error(webview: &mut Webview<'_>, allowlist_key: &str) {
326326
let reject_code = format!(
327327
r#"throw new Error("'{}' not on the allowlist")"#,
328328
allowlist_key

tauri/src/endpoints/asset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::PathBuf;
22
use webview_official::Webview;
33

44
pub fn load(
5-
webview: &mut Webview,
5+
webview: &mut Webview<'_>,
66
asset: String,
77
asset_type: String,
88
callback: String,

tauri/src/endpoints/dialog.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn map_response(response: Response) -> JsonValue {
1919
/// Shows an open dialog.
2020
#[cfg(open_dialog)]
2121
pub fn open(
22-
webview: &mut Webview,
22+
webview: &mut Webview<'_>,
2323
options: OpenDialogOptions,
2424
callback: String,
2525
error: String,
@@ -45,7 +45,7 @@ pub fn open(
4545
/// Shows a save dialog.
4646
#[cfg(save_dialog)]
4747
pub fn save(
48-
webview: &mut Webview,
48+
webview: &mut Webview<'_>,
4949
options: SaveDialogOptions,
5050
callback: String,
5151
error: String,
@@ -66,7 +66,7 @@ pub fn message(title: String, message: String) {
6666

6767
/// Shows a dialog with a yes/no question.
6868
pub fn ask(
69-
webview: &mut Webview,
69+
webview: &mut Webview<'_>,
7070
title: String,
7171
message: String,
7272
callback: String,

tauri/src/endpoints/file_system.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::cmd::{DirOperationOptions, FileOperationOptions};
1414
/// Reads a directory.
1515
#[cfg(read_dir)]
1616
pub fn read_dir(
17-
webview: &mut Webview,
17+
webview: &mut Webview<'_>,
1818
path: PathBuf,
1919
options: Option<DirOperationOptions>,
2020
callback: String,
@@ -38,7 +38,7 @@ pub fn read_dir(
3838
/// Copies a file.
3939
#[cfg(copy_file)]
4040
pub fn copy_file(
41-
webview: &mut Webview,
41+
webview: &mut Webview<'_>,
4242
source: PathBuf,
4343
destination: PathBuf,
4444
options: Option<FileOperationOptions>,
@@ -65,7 +65,7 @@ pub fn copy_file(
6565
/// Creates a directory.
6666
#[cfg(create_dir)]
6767
pub fn create_dir(
68-
webview: &mut Webview,
68+
webview: &mut Webview<'_>,
6969
path: PathBuf,
7070
options: Option<DirOperationOptions>,
7171
callback: String,
@@ -96,7 +96,7 @@ pub fn create_dir(
9696
/// Removes a directory.
9797
#[cfg(remove_dir)]
9898
pub fn remove_dir(
99-
webview: &mut Webview,
99+
webview: &mut Webview<'_>,
100100
path: PathBuf,
101101
options: Option<DirOperationOptions>,
102102
callback: String,
@@ -127,7 +127,7 @@ pub fn remove_dir(
127127
/// Removes a file
128128
#[cfg(remove_file)]
129129
pub fn remove_file(
130-
webview: &mut Webview,
130+
webview: &mut Webview<'_>,
131131
path: PathBuf,
132132
options: Option<FileOperationOptions>,
133133
callback: String,
@@ -147,7 +147,7 @@ pub fn remove_file(
147147
/// Renames a file.
148148
#[cfg(rename_file)]
149149
pub fn rename_file(
150-
webview: &mut Webview,
150+
webview: &mut Webview<'_>,
151151
old_path: PathBuf,
152152
new_path: PathBuf,
153153
options: Option<FileOperationOptions>,
@@ -174,7 +174,7 @@ pub fn rename_file(
174174
/// Writes a text file.
175175
#[cfg(write_file)]
176176
pub fn write_file(
177-
webview: &mut Webview,
177+
webview: &mut Webview<'_>,
178178
path: PathBuf,
179179
contents: String,
180180
options: Option<FileOperationOptions>,
@@ -196,7 +196,7 @@ pub fn write_file(
196196
/// Writes a binary file.
197197
#[cfg(write_binary_file)]
198198
pub fn write_binary_file(
199-
webview: &mut Webview,
199+
webview: &mut Webview<'_>,
200200
path: PathBuf,
201201
contents: String,
202202
options: Option<FileOperationOptions>,
@@ -222,7 +222,7 @@ pub fn write_binary_file(
222222
/// Reads a text file.
223223
#[cfg(read_text_file)]
224224
pub fn read_text_file(
225-
webview: &mut Webview,
225+
webview: &mut Webview<'_>,
226226
path: PathBuf,
227227
options: Option<FileOperationOptions>,
228228
callback: String,
@@ -239,7 +239,7 @@ pub fn read_text_file(
239239
/// Reads a binary file.
240240
#[cfg(read_binary_file)]
241241
pub fn read_binary_file(
242-
webview: &mut Webview,
242+
webview: &mut Webview<'_>,
243243
path: PathBuf,
244244
options: Option<FileOperationOptions>,
245245
callback: String,

tauri/src/endpoints/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use webview_official::Webview;
33

44
/// Makes an HTTP request and resolves the response to the webview
55
pub fn make_request(
6-
webview: &mut Webview,
6+
webview: &mut Webview<'_>,
77
options: HttpRequestOptions,
88
callback: String,
99
error: String,

0 commit comments

Comments
 (0)