fix(tauri) webview initialization on windows, fixes #879 (#885)

This commit is contained in:
Lucas Fernandes Nogueira 2020-07-23 08:38:40 -03:00 committed by GitHub
parent 44b1be56e4
commit 4abd12c2a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 70 additions and 79 deletions

View File

@ -0,0 +1,6 @@
---
"tauri": minor
"tauri.js": minor
---
Fixes the Webview initialization on Windows.

View File

@ -7,28 +7,6 @@ if (!String.prototype.startsWith) {
}
(function () {
function webviewBind (name) {
var RPC = window._rpc = (window._rpc || { nextSeq: 1 });
window[name] = function () {
var seq = RPC.nextSeq++;
var promise = new Promise(function (resolve, reject) {
RPC[seq] = {
resolve: resolve,
reject: reject,
};
});
window.external.invoke(JSON.stringify({
id: seq,
method: name,
params: Array.prototype.slice.call(arguments),
}));
return promise;
}
}
if (!window.__TAURI_INVOKE_HANDLER__) {
webviewBind('__TAURI_INVOKE_HANDLER__')
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)

View File

@ -20,7 +20,7 @@ features = [ "all-api" ]
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
webview_official = "0.0.1"
webview_official = "0.0.2"
tauri_includedir = "0.6.0"
phf = "0.8.0"
base64 = "0.12.3"

View File

@ -2,8 +2,8 @@ use webview_official::Webview;
mod runner;
type InvokeHandler = Box<dyn FnMut(&mut Webview, &str) -> Result<(), String>>;
type Setup = Box<dyn FnMut(&mut Webview, String)>;
type InvokeHandler = Box<dyn FnMut(&mut Webview<'_>, &str) -> Result<(), String>>;
type Setup = Box<dyn FnMut(&mut Webview<'_>, String)>;
/// The application runner.
pub struct App {
@ -26,7 +26,7 @@ impl App {
/// The message is considered consumed if the handler exists and returns an Ok Result.
pub(crate) fn run_invoke_handler(
&mut self,
webview: &mut Webview,
webview: &mut Webview<'_>,
arg: &str,
) -> Result<bool, String> {
if let Some(ref mut invoke_handler) = self.invoke_handler {
@ -37,7 +37,7 @@ impl App {
}
/// Runs the setup callback if defined.
pub(crate) fn run_setup(&mut self, webview: &mut Webview, source: String) {
pub(crate) fn run_setup(&mut self, webview: &mut Webview<'_>, source: String) {
if let Some(ref mut setup) = self.setup {
setup(webview, source);
}
@ -71,7 +71,7 @@ impl AppBuilder {
}
/// Defines the JS message handler callback.
pub fn invoke_handler<F: FnMut(&mut Webview, &str) -> Result<(), String> + 'static>(
pub fn invoke_handler<F: FnMut(&mut Webview<'_>, &str) -> Result<(), String> + 'static>(
mut self,
invoke_handler: F,
) -> Self {
@ -80,7 +80,7 @@ impl AppBuilder {
}
/// Defines the setup callback.
pub fn setup<F: FnMut(&mut Webview, String) + 'static>(mut self, setup: F) -> Self {
pub fn setup<F: FnMut(&mut Webview<'_>, String) + 'static>(mut self, setup: F) -> Self {
self.setup = Some(Box::new(setup));
self
}

View File

@ -236,7 +236,7 @@ fn build_webview(
application: &mut App,
content: Content<String>,
splashscreen_content: Option<Content<String>>,
) -> crate::Result<Webview> {
) -> crate::Result<Webview<'_>> {
let config = get()?;
let content_clone = match content {
Content::Html(ref html) => Content::Html(html.clone()),
@ -264,28 +264,30 @@ fn build_webview(
},
};
let mut webview = WebviewBuilder::new()
.init(&format!(
r#"
{event_init}
if (window.__TAURI_INVOKE_HANDLER__) {{
let init = format!(
r#"
{event_init}
if (window.__TAURI_INVOKE_HANDLER__) {{
window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
}} else {{
window.addEventListener('DOMContentLoaded', function () {{
window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
}} else {{
window.addEventListener('DOMContentLoaded', function () {{
window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
}})
}}
{plugin_init}
"#,
event_init = init(),
plugin_init = crate::plugin::init_script()
))
}})
}}
{plugin_init}
"#,
event_init = init(),
plugin_init = crate::plugin::init_script()
);
let mut webview = WebviewBuilder::new()
.init(Box::leak(init.into_boxed_str()))
.title(Box::leak(title))
.width(width as usize)
.height(height as usize)
.resize(resizable)
.debug(debug)
.url(&url)
.url(Box::leak(url.into_boxed_str()))
.build();
// TODO waiting for webview window API
// webview.set_fullscreen(fullscreen);

View File

@ -18,7 +18,7 @@ mod notification;
use webview_official::Webview;
#[allow(unused_variables)]
pub(crate) fn handle(webview: &mut Webview, arg: &str) -> crate::Result<()> {
pub(crate) fn handle(webview: &mut Webview<'_>, arg: &str) -> crate::Result<()> {
use cmd::Cmd::*;
match serde_json::from_str(arg) {
Err(e) => Err(e.into()),
@ -304,13 +304,13 @@ pub(crate) fn handle(webview: &mut Webview, arg: &str) -> crate::Result<()> {
}
#[allow(dead_code)]
fn api_error(webview: &mut Webview, error_fn: String, message: &str) {
fn api_error(webview: &mut Webview<'_>, error_fn: String, message: &str) {
let reject_code = tauri_api::rpc::format_callback(error_fn, message);
webview.eval(&reject_code)
}
#[allow(dead_code)]
fn allowlist_error(webview: &mut Webview, error_fn: String, allowlist_key: &str) {
fn allowlist_error(webview: &mut Webview<'_>, error_fn: String, allowlist_key: &str) {
api_error(
webview,
error_fn,
@ -322,7 +322,7 @@ fn allowlist_error(webview: &mut Webview, error_fn: String, allowlist_key: &str)
}
#[allow(dead_code)]
fn throw_allowlist_error(webview: &mut Webview, allowlist_key: &str) {
fn throw_allowlist_error(webview: &mut Webview<'_>, allowlist_key: &str) {
let reject_code = format!(
r#"throw new Error("'{}' not on the allowlist")"#,
allowlist_key

View File

@ -2,7 +2,7 @@ use std::path::PathBuf;
use webview_official::Webview;
pub fn load(
webview: &mut Webview,
webview: &mut Webview<'_>,
asset: String,
asset_type: String,
callback: String,

View File

@ -19,7 +19,7 @@ fn map_response(response: Response) -> JsonValue {
/// Shows an open dialog.
#[cfg(open_dialog)]
pub fn open(
webview: &mut Webview,
webview: &mut Webview<'_>,
options: OpenDialogOptions,
callback: String,
error: String,
@ -45,7 +45,7 @@ pub fn open(
/// Shows a save dialog.
#[cfg(save_dialog)]
pub fn save(
webview: &mut Webview,
webview: &mut Webview<'_>,
options: SaveDialogOptions,
callback: String,
error: String,
@ -66,7 +66,7 @@ pub fn message(title: String, message: String) {
/// Shows a dialog with a yes/no question.
pub fn ask(
webview: &mut Webview,
webview: &mut Webview<'_>,
title: String,
message: String,
callback: String,

View File

@ -14,7 +14,7 @@ use super::cmd::{DirOperationOptions, FileOperationOptions};
/// Reads a directory.
#[cfg(read_dir)]
pub fn read_dir(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
options: Option<DirOperationOptions>,
callback: String,
@ -38,7 +38,7 @@ pub fn read_dir(
/// Copies a file.
#[cfg(copy_file)]
pub fn copy_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
source: PathBuf,
destination: PathBuf,
options: Option<FileOperationOptions>,
@ -65,7 +65,7 @@ pub fn copy_file(
/// Creates a directory.
#[cfg(create_dir)]
pub fn create_dir(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
options: Option<DirOperationOptions>,
callback: String,
@ -96,7 +96,7 @@ pub fn create_dir(
/// Removes a directory.
#[cfg(remove_dir)]
pub fn remove_dir(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
options: Option<DirOperationOptions>,
callback: String,
@ -127,7 +127,7 @@ pub fn remove_dir(
/// Removes a file
#[cfg(remove_file)]
pub fn remove_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
options: Option<FileOperationOptions>,
callback: String,
@ -147,7 +147,7 @@ pub fn remove_file(
/// Renames a file.
#[cfg(rename_file)]
pub fn rename_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
old_path: PathBuf,
new_path: PathBuf,
options: Option<FileOperationOptions>,
@ -174,7 +174,7 @@ pub fn rename_file(
/// Writes a text file.
#[cfg(write_file)]
pub fn write_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
contents: String,
options: Option<FileOperationOptions>,
@ -196,7 +196,7 @@ pub fn write_file(
/// Writes a binary file.
#[cfg(write_binary_file)]
pub fn write_binary_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
contents: String,
options: Option<FileOperationOptions>,
@ -222,7 +222,7 @@ pub fn write_binary_file(
/// Reads a text file.
#[cfg(read_text_file)]
pub fn read_text_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
options: Option<FileOperationOptions>,
callback: String,
@ -239,7 +239,7 @@ pub fn read_text_file(
/// Reads a binary file.
#[cfg(read_binary_file)]
pub fn read_binary_file(
webview: &mut Webview,
webview: &mut Webview<'_>,
path: PathBuf,
options: Option<FileOperationOptions>,
callback: String,

View File

@ -3,7 +3,7 @@ use webview_official::Webview;
/// Makes an HTTP request and resolves the response to the webview
pub fn make_request(
webview: &mut Webview,
webview: &mut Webview<'_>,
options: HttpRequestOptions,
callback: String,
error: String,

View File

@ -2,7 +2,12 @@ use super::cmd::NotificationOptions;
use serde_json::Value as JsonValue;
use webview_official::Webview;
pub fn send(webview: &mut Webview, options: NotificationOptions, callback: String, error: String) {
pub fn send(
webview: &mut Webview<'_>,
options: NotificationOptions,
callback: String,
error: String,
) {
crate::execute_promise(
webview,
move || {
@ -21,7 +26,7 @@ pub fn send(webview: &mut Webview, options: NotificationOptions, callback: Strin
);
}
pub fn is_permission_granted(webview: &mut Webview, callback: String, error: String) {
pub fn is_permission_granted(webview: &mut Webview<'_>, callback: String, error: String) {
crate::execute_promise(
webview,
move || {
@ -38,7 +43,7 @@ pub fn is_permission_granted(webview: &mut Webview, callback: String, error: Str
}
pub fn request_permission(
webview: &mut Webview,
webview: &mut Webview<'_>,
callback: String,
error: String,
) -> crate::Result<()> {

View File

@ -2,7 +2,7 @@ use webview_official::Webview;
/// Validates a salt.
pub fn validate(
webview: &mut Webview,
webview: &mut Webview<'_>,
salt: String,
callback: String,
error: String,

View File

@ -61,7 +61,7 @@ pub fn spawn<F: FnOnce() -> () + Send + 'static>(task: F) {
/// Synchronously executes the given task
/// and evaluates its Result to the JS promise described by the `callback` and `error` function names.
pub fn execute_promise_sync<R: Serialize, F: FnOnce() -> crate::Result<R> + Send + 'static>(
webview: &mut Webview,
webview: &mut Webview<'_>,
task: F,
callback: String,
error: String,
@ -78,7 +78,7 @@ pub fn execute_promise_sync<R: Serialize, F: FnOnce() -> crate::Result<R> + Send
/// If the Result `is_ok()`, the callback will be the `success_callback` function name and the argument will be the Ok value.
/// If the Result `is_err()`, the callback will be the `error_callback` function name and the argument will be the Err value.
pub fn execute_promise<R: Serialize, F: FnOnce() -> crate::Result<R> + Send + 'static>(
webview: &mut Webview,
webview: &mut Webview<'_>,
task: F,
success_callback: String,
error_callback: String,
@ -103,7 +103,7 @@ pub fn execute_promise<R: Serialize, F: FnOnce() -> crate::Result<R> + Send + 's
/// Calls the given command and evaluates its output to the JS promise described by the `callback` and `error` function names.
pub fn call(
webview: &mut Webview,
webview: &mut Webview<'_>,
command: String,
args: Vec<String>,
callback: String,
@ -118,7 +118,7 @@ pub fn call(
}
/// Closes the splashscreen.
pub fn close_splashscreen(webview: &mut Webview) -> crate::Result<()> {
pub fn close_splashscreen(webview: &mut Webview<'_>) -> crate::Result<()> {
// send a signal to the runner so it knows that it should redirect to the main app content
webview.eval(r#"window.__TAURI_INVOKE_HANDLER__({ cmd: "closeSplashscreen" })"#);

View File

@ -9,15 +9,15 @@ pub trait Plugin {
}
/// Callback invoked when the webview is created.
#[allow(unused_variables)]
fn created(&self, webview: &mut Webview) {}
fn created(&self, webview: &mut Webview<'_>) {}
/// Callback invoked when the webview is ready.
#[allow(unused_variables)]
fn ready(&self, webview: &mut Webview) {}
fn ready(&self, webview: &mut Webview<'_>) {}
/// Add invoke_handler API extension commands.
#[allow(unused_variables)]
fn extend_api(&self, webview: &mut Webview, payload: &str) -> Result<bool, String> {
fn extend_api(&self, webview: &mut Webview<'_>, payload: &str) -> Result<bool, String> {
Err("unknown variant".to_string())
}
}
@ -53,19 +53,19 @@ pub(crate) fn init_script() -> String {
init
}
pub(crate) fn created(webview: &mut Webview) {
pub(crate) fn created(webview: &mut Webview<'_>) {
run_plugin(|ext| {
ext.created(webview);
});
}
pub(crate) fn ready(webview: &mut Webview) {
pub(crate) fn ready(webview: &mut Webview<'_>) {
run_plugin(|ext| {
ext.ready(webview);
});
}
pub(crate) fn extend_api(webview: &mut Webview, arg: &str) -> Result<bool, String> {
pub(crate) fn extend_api(webview: &mut Webview<'_>, arg: &str) -> Result<bool, String> {
PLUGINS.with(|plugins| {
let exts = plugins.lock().unwrap();
for ext in exts.iter() {