From 3f5454b839b7f4fc9fef7ee6f36e0bf055ba91cc Mon Sep 17 00:00:00 2001 From: Tw93 Date: Thu, 5 Dec 2024 17:33:13 +0800 Subject: [PATCH] :sparkles: 2.0 invoke compatible --- .gitignore | 1 - src-tauri/Cargo.lock | 78 +++++++++++++++++++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/capabilities/default.json | 18 +++++++ src-tauri/src/app/invoke.rs | 1 - src-tauri/src/inject/event.js | 25 +++------ src-tauri/src/lib.rs | 1 + 7 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 src-tauri/capabilities/default.json diff --git a/.gitignore b/.gitignore index e5d4351..b365d0d 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,3 @@ src-tauri/.cargo/ .next src-tauri/.pake/ src-tauri/gen -src-tauri/capabilities diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 72ebeb2..23674ba 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1743,6 +1743,25 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" +[[package]] +name = "is-docker" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3" +dependencies = [ + "once_cell", +] + +[[package]] +name = "is-wsl" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5" +dependencies = [ + "is-docker", + "once_cell", +] + [[package]] name = "itoa" version = "0.4.8" @@ -2390,12 +2409,33 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "open" +version = "5.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ecd52f0b8d15c40ce4820aa251ed5de032e5d91fab27f7db2f40d42a8bdf69c" +dependencies = [ + "is-wsl", + "libc", + "pathdiff", +] + [[package]] name = "option-ext" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "os_pipe" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ffd2b0a5634335b135d5728d84c5e0fd726954b87111f7506a61c502280d982" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "pake" version = "0.1.0" @@ -2409,6 +2449,7 @@ dependencies = [ "tauri-plugin-global-shortcut", "tauri-plugin-http", "tauri-plugin-oauth", + "tauri-plugin-shell", "tauri-plugin-window-state", ] @@ -2460,6 +2501,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "pathdiff" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" + [[package]] name = "pdb" version = "0.8.0" @@ -3346,6 +3393,16 @@ dependencies = [ "digest", ] +[[package]] +name = "shared_child" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09fa9338aed9a1df411814a5b2252f7cd206c55ae9bf2fa763f8de84603aa60c" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "shlex" version = "1.3.0" @@ -3849,6 +3906,27 @@ dependencies = [ "url", ] +[[package]] +name = "tauri-plugin-shell" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ad7880c5586b6b2104be451e3d7fc0f3800c84bda69e9ba81c828f87cb34267" +dependencies = [ + "encoding_rs", + "log", + "open", + "os_pipe", + "regex", + "schemars", + "serde", + "serde_json", + "shared_child", + "tauri", + "tauri-plugin", + "thiserror 1.0.69", + "tokio", +] + [[package]] name = "tauri-plugin-window-state" version = "2.0.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index cb6b25a..6dae424 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -27,6 +27,7 @@ tauri-plugin-clipboard-manager = "2.0.2" tauri-plugin-http = "2.0.3" [target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies] tauri-plugin-global-shortcut = { version = "2.0.1" } +tauri-plugin-shell = "2.0.0" [dev-dependencies] cargo-bloat = "0.12.1" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json new file mode 100644 index 0000000..653622b --- /dev/null +++ b/src-tauri/capabilities/default.json @@ -0,0 +1,18 @@ +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "pake-capability", + "description": "Capability for the pake app.", + "webviews": ["pake"], + "remote": { + "urls": ["https://*.*"] + }, + "permissions": [ + "core:window:allow-theme", + "core:window:allow-start-dragging", + "core:window:allow-toggle-maximize", + "core:window:allow-is-fullscreen", + "core:window:allow-set-fullscreen", + "core:webview:allow-internal-toggle-devtools", + "shell:allow-open" + ] +} diff --git a/src-tauri/src/app/invoke.rs b/src-tauri/src/app/invoke.rs index d71a3c4..374d643 100644 --- a/src-tauri/src/app/invoke.rs +++ b/src-tauri/src/app/invoke.rs @@ -4,7 +4,6 @@ use std::io::Write; use std::str::FromStr; use tauri::http::Method; use tauri_plugin_http::reqwest::{ClientBuilder, Request}; -// use tauri::http::{ClientBuilder, HttpRequestBuilder, ResponseType}; use tauri::{command, AppHandle, Manager, Url, WebviewWindow}; #[derive(serde::Deserialize)] diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js index e13247b..1213e1b 100644 --- a/src-tauri/src/inject/event.js +++ b/src-tauri/src/inject/event.js @@ -43,7 +43,7 @@ function isDownloadLink(url) { '3gp', '7z', 'ai', 'apk', 'avi', 'bmp', 'csv', 'dmg', 'doc', 'docx', 'fla', 'flv', 'gif', 'gz', 'gzip', 'ico', 'iso', 'indd', 'jar', 'jpeg', 'jpg', 'm3u8', 'mov', 'mp3', 'mp4', 'mpa', 'mpg', 'mpeg', 'msi', 'odt', - 'ogg', 'ogv', 'pdf', 'png', 'ppt', 'pptx', 'psd', 'rar', 'raw', 'rss', + 'ogg', 'ogv', 'pdf', 'png', 'ppt', 'pptx', 'psd', 'rar', 'raw', 'svg', 'swf', 'tar', 'tif', 'tiff', 'ts', 'txt', 'wav', 'webm', 'webp', 'wma', 'wmv', 'xls', 'xlsx', 'xml', 'zip', 'json', 'yaml', '7zip', 'mkv' ]; @@ -56,15 +56,11 @@ function externalDownLoadLink() { return ['quickref.me'].indexOf(location.hostname) > -1; } -// Directly jumping out without hostname address. -function externalTargetLink() { - return ['zbook.lol'].indexOf(location.hostname) > -1; -} document.addEventListener('DOMContentLoaded', () => { const tauri = window.__TAURI__; - const appWindow = tauri.window.appWindow; - const invoke = tauri.tauri.invoke; + const appWindow = tauri.window.getCurrentWindow(); + const invoke = tauri.core.invoke; if (!document.getElementById('pake-top-dom')) { const topDom = document.createElement('div'); @@ -75,19 +71,19 @@ document.addEventListener('DOMContentLoaded', () => { const domEl = document.getElementById('pake-top-dom'); domEl.addEventListener('touchstart', () => { - appWindow.startDragging().then(); + appWindow.startDragging(); }); domEl.addEventListener('mousedown', e => { e.preventDefault(); if (e.buttons === 1 && e.detail !== 2) { - appWindow.startDragging().then(); + appWindow.startDragging(); } }); domEl.addEventListener('dblclick', () => { appWindow.isFullscreen().then(fullscreen => { - appWindow.setFullscreen(!fullscreen).then(); + appWindow.setFullscreen(!fullscreen); }); }); @@ -176,8 +172,6 @@ document.addEventListener('DOMContentLoaded', () => { // case: download from dataURL -> convert dataURL -> } else if (url.startsWith('data:')) { downloadFromDataUri(url, filename); - } else if (isDownloadLink(url) || anchorEle.host !== window.location.host) { - handleExternalLink(e, url); } }, true, @@ -187,7 +181,6 @@ document.addEventListener('DOMContentLoaded', () => { }; } - const isExternalLink = link => window.location.host !== link.host; // process special download protocol['data:','blob:'] const isSpecialDownload = url => ['blob', 'data'].some(protocol => url.startsWith(protocol)); @@ -214,12 +207,6 @@ document.addEventListener('DOMContentLoaded', () => { const absoluteUrl = hrefUrl.href; let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl); - // Handling external link redirection. - if ((isExternalLink(absoluteUrl) && ['_blank', '_new'].includes(anchorElement.target)) || externalTargetLink()) { - handleExternalLink(e, absoluteUrl); - return; - } - // Process download links for Rust to handle. if (isDownloadRequired(absoluteUrl, anchorElement, e) && !externalDownLoadLink() && !isSpecialDownload(absoluteUrl)) { handleDownloadLink(e, absoluteUrl, filename); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index cf283cd..864ca4f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -26,6 +26,7 @@ pub fn run_app() { .plugin(windowStatePlugin::default().build()) .plugin(tauri_plugin_oauth::init()) .plugin(tauri_plugin_http::init()) + .plugin(tauri_plugin_shell::init()) .invoke_handler(tauri::generate_handler![ download_file, download_file_by_binary