Skip to content

Commit

Permalink
feature: import official webview rust binding (#846)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) and lucasfernog authored Jul 18, 2020
1 parent dac1db3 commit cd5b401
Show file tree
Hide file tree
Showing 31 changed files with 733 additions and 499 deletions.
8 changes: 8 additions & 0 deletions .changes/tauri-webview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"tauri": minor
---

Moving the webview implementation to [webview](https://github.com/webview/webview), with the [official Rust binding](https://github.com/webview/webview_rust).
This is a breaking change.
IE support has been dropped, so the `edge` object on `tauri.conf.json > tauri` no longer exists and you need to remove it.
`webview.handle()` has been replaced with `webview.as_mut()`.
6 changes: 3 additions & 3 deletions cli/tauri.js/api-src/tauri.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare global {
interface External {
invoke: (command: string) => void
interface Window {
__TAURI_INVOKE_HANDLER__: (command: string) => void
}
}

Expand All @@ -21,7 +21,7 @@ function uid(): string {
* @param args
*/
function invoke(args: any): void {
window.external.invoke(typeof args === 'object' ? JSON.stringify(args) : args)
window.__TAURI_INVOKE_HANDLER__(args)
}

function transformCallback(callback?: (response: any) => void, once = false): string {
Expand Down
4 changes: 0 additions & 4 deletions cli/tauri.js/src/api/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ async function printAppInfo(tauriDir: string): Promise<void> {
key: ' CSP',
value: config.tauri.security ? config.tauri.security.csp : 'unset'
})
printInfo({
key: ' Windows',
value: config.tauri.edge?.active ? 'Edge' : 'MSHTML'
})
printInfo({
key: ' distDir',
value: config.build
Expand Down
3 changes: 0 additions & 3 deletions cli/tauri.js/src/helpers/tauri-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ const getTauriConfig = (cfg: Partial<TauriConfig>): TauriConfig => {
security: {
csp: "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
},
edge: {
active: true
},
inliner: {
active: true
}
Expand Down
6 changes: 1 addition & 5 deletions cli/tauri.js/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Runner {
self.__parseHtml(cfg, indexDir, false)
.then(({ html }) => {
const headers: { [key: string]: string } = {}
if(proxyRes.headers['content-type']) {
if (proxyRes.headers['content-type']) {
headers['content-type'] = proxyRes.headers['content-type']
} else {
const charsetMatch = /charset="(\S+)"/g.exec(bodyStr)
Expand Down Expand Up @@ -527,10 +527,6 @@ class Runner {
tomlFeatures.push(...whitelist.map(toKebabCase))
}

if (cfg.tauri.edge.active) {
tomlFeatures.push('edge')
}

if (cfg.tauri.cli) {
tomlFeatures.push('cli')
}
Expand Down
3 changes: 0 additions & 3 deletions cli/tauri.js/src/template/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export default {
security: {
csp: "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
},
edge: {
active: true
},
inliner: {
active: true
}
Expand Down
12 changes: 0 additions & 12 deletions cli/tauri.js/src/types/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,6 @@
"$ref": "#/definitions/CliConfig",
"description": "app's CLI definition"
},
"edge": {
"additionalProperties": false,
"defaultProperties": [
],
"properties": {
"active": {
"type": "boolean"
}
},
"type": "object"
},
"embeddedServer": {
"additionalProperties": false,
"defaultProperties": [
Expand Down Expand Up @@ -496,7 +485,6 @@
},
"required": [
"bundle",
"edge",
"embeddedServer",
"inliner",
"security",
Expand Down
3 changes: 0 additions & 3 deletions cli/tauri.js/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,6 @@ export interface TauriConfig {
security: {
csp?: string
}
edge: {
active?: boolean
}
inliner: {
active?: boolean
}
Expand Down
12 changes: 0 additions & 12 deletions cli/tauri.js/src/types/config.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,6 @@ export const TauriConfigSchema = {
"$ref": "#/definitions/CliConfig",
"description": "app's CLI definition"
},
"edge": {
"additionalProperties": false,
"defaultProperties": [
],
"properties": {
"active": {
"type": "boolean"
}
},
"type": "object"
},
"embeddedServer": {
"additionalProperties": false,
"defaultProperties": [
Expand Down Expand Up @@ -506,7 +495,6 @@ export const TauriConfigSchema = {
},
"required": [
"bundle",
"edge",
"embeddedServer",
"inliner",
"security",
Expand Down
66 changes: 24 additions & 42 deletions cli/tauri.js/templates/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,29 @@ if (!String.prototype.startsWith) {
}
}

// makes the window.external.invoke API available after window.location.href changes
switch (navigator.platform) {
case "Macintosh":
case "MacPPC":
case "MacIntel":
case "Mac68K":
window.external = this
invoke = function (x) {
webkit.messageHandlers.invoke.postMessage(x);
}
break;
case "Windows":
case "WinCE":
case "Win32":
case "Win64":
break;
default:
window.external = this
invoke = function (x) {
window.webkit.messageHandlers.external.postMessage(x);
(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;
}
break;
}
}
if (!window.__TAURI_INVOKE_HANDLER__) {
webviewBind('__TAURI_INVOKE_HANDLER__')
}

(function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
Expand Down Expand Up @@ -89,9 +87,6 @@ switch (navigator.platform) {
if (!window.__TAURI__) {
window.__TAURI__ = {}
}
window.__TAURI__.invoke = function invoke(args) {
window.external.invoke(JSON.stringify(args))
}

window.__TAURI__.transformCallback = function transformCallback(callback) {
var once = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false
Expand All @@ -109,10 +104,10 @@ switch (navigator.platform) {
}

window.__TAURI__.promisified = function promisified(args) {
var _this = this;
var _this = this

return new Promise(function (resolve, reject) {
_this.invoke(_objectSpread({
window.__TAURI_INVOKE_HANDLER__(_objectSpread({
callback: _this.transformCallback(resolve),
error: _this.transformCallback(reject)
}, args))
Expand All @@ -127,19 +122,6 @@ switch (navigator.platform) {
})
}

// init tauri API
try {
window.__TAURI__.invoke({
cmd: 'init'
})
} catch (e) {
window.addEventListener('DOMContentLoaded', function () {
window.__TAURI__.invoke({
cmd: 'init'
})
}, true)
}

document.addEventListener('error', function (e) {
var target = e.target
while (target != null) {
Expand All @@ -161,7 +143,7 @@ switch (navigator.platform) {
while (target != null) {
if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
if (target.href && target.href.startsWith('http') && target.target === '_blank') {
window.__TAURI__.invoke({
window.__TAURI_INVOKE_HANDLER__({
cmd: 'open',
uri: target.href
})
Expand Down
7 changes: 3 additions & 4 deletions tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ features = [ "all-api" ]
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri-webview-sys = "0.5.0"
tauri-web-view = "0.6.2"
webview_rust = { version = "0.1", git = "https://github.com/webview/webview_rust.git", branch = "dev" }
tauri_includedir = "0.6.0"
phf = "0.8.0"
base64 = "0.12.3"
Expand All @@ -35,6 +34,7 @@ thiserror = "1.0.20"
envmnt = "0.8.3"
once_cell = "1.4.0"
tauri-api = { version = "0.7", path = "../tauri-api" }
urlencoding = "1.1.1"

[target."cfg(target_os = \"windows\")".dependencies]
runas = "0.2"
Expand All @@ -46,12 +46,11 @@ cfg_aliases = "0.1.0"
[dev-dependencies]
proptest = "0.10.0"
serde_json = "1.0"
tauri = { path = ".", features = [ "all-api", "edge" ] }
tauri = { path = ".", features = [ "all-api" ] }
serde = { version = "1.0", features = [ "derive" ] }

[features]
cli = [ "tauri-api/cli" ]
edge = [ "tauri-web-view/edge" ]
embedded-server = [ "tiny_http" ]
no-server = [ ]
all-api = [ "tauri-api/notification" ]
Expand Down
4 changes: 2 additions & 2 deletions tauri/examples/communication/dist/communication.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
document.getElementById('log').addEventListener('click', function () {
window.__TAURI__.invoke({
window.__TAURI__.tauri.invoke({
cmd: 'logOperation',
event: 'tauri-click',
payload: 'this payload is optional because we used Option in Rust'
})
})

document.getElementById('request').addEventListener('click', function () {
window.__TAURI__.promisified({
window.__TAURI__.tauri.promisified({
cmd: 'performRequest',
endpoint: 'dummy endpoint arg',
body: {
Expand Down
507 changes: 404 additions & 103 deletions tauri/examples/communication/dist/index.tauri.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tauri/examples/communication/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ icon = [
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri = { path = "../../..", features = [ "all-api", "edge", "cli" ] }
tauri = { path = "../../..", features = [ "all-api", "cli" ] }

[target."cfg(windows)".build-dependencies]
winres = "0.1"
Expand Down
4 changes: 2 additions & 2 deletions tauri/examples/communication/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ struct Reply {
fn main() {
tauri::AppBuilder::new()
.setup(|webview, _source| {
let handle = webview.handle();
let mut webview = webview.as_mut();
tauri::event::listen(String::from("js-event"), move |msg| {
println!("got js-event with message '{:?}'", msg);
let reply = Reply {
data: "something else".to_string(),
};

tauri::event::emit(&handle, String::from("rust-event"), Some(reply))
tauri::event::emit(&mut webview, String::from("rust-event"), Some(reply))
.expect("failed to emit");
});
})
Expand Down
3 changes: 0 additions & 3 deletions tauri/examples/communication/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
"security": {
"csp": "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
},
"edge": {
"active": true
},
"inliner": {
"active": true
}
Expand Down
14 changes: 7 additions & 7 deletions tauri/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use web_view::WebView;
use webview_rust_sys::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 {
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit cd5b401

Please sign in to comment.