终于调通了

This commit is contained in:
Tw93
2023-04-05 17:01:36 +08:00
parent 9a55e6ae20
commit 6bbecdb19e
8 changed files with 1074 additions and 460 deletions

1390
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,10 @@
[package] [package]
name = "app" name = "app"
version = "0.1.0" version = "0.1.0"
description = "Pake 打包工具" description = "🤱🏻 Turn any webpage into a desktop app with Rust."
authors = ["Tw93"] authors = ["Tw93"]
license = "" license = "MIT"
repository = "" repository = "https://github.com/tw93/Pake"
default-run = "app" default-run = "app"
edition = "2021" edition = "2021"
rust-version = "1.61.0" rust-version = "1.61.0"
@@ -17,11 +17,10 @@ tauri-build = { version = "1.2.1", features = [] }
[dependencies] [dependencies]
serde_json = "1.0.89" serde_json = "1.0.89"
serde = { version = "1.0.150", features = ["derive"] } serde = { version = "1.0.150", features = ["derive"] }
tauri = { version = "1.2.4", features = ["system-tray"] } tauri = { version = "1.2.1", features = ["api-all", "devtools", "system-tray"] }
image = "0.24.5" image = "0.24.5"
home = "0.5" home = "0.5"
tauri-utils = "1.2.1" tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin-window-state", branch = "dev"}
[features] [features]
# by default Tauri runs in production mode # by default Tauri runs in production mode
@@ -30,5 +29,3 @@ default = ["custom-protocol"]
# this feature is used used for production builds where `devPath` points to the filesystem # this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this # DO NOT remove this
custom-protocol = ["tauri/custom-protocol"] custom-protocol = ["tauri/custom-protocol"]
# Enable DevTools for debugging.
devtools = []

View File

@@ -1,28 +1,28 @@
{ {
"windows": [ "windows": [
{ {
"url": "https://weread.qq.com/", "url": "https://weread.qq.com/",
"transparent": true, "transparent": true,
"fullscreen": false, "fullscreen": false,
"width": 1200, "width": 1200,
"height": 780, "height": 780,
"resizable": true, "resizable": true,
"url_type": "web" "url_type": "web"
}
],
"user_agent": {
"macos": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15",
"linux": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
"windows": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
},
"menu": {
"macos": true,
"linux": false,
"windows": false
},
"system_tray": {
"macos": false,
"linux": true,
"windows": true
} }
} ],
"user_agent": {
"macos": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15",
"linux": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
"windows": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
},
"menu": {
"macos": true,
"linux": false,
"windows": false
},
"system_tray": {
"macos": false,
"linux": true,
"windows": true
}
}

21
src-tauri/src/cmd.rs Normal file
View File

@@ -0,0 +1,21 @@
use tauri::{api, command, AppHandle, Manager};
#[command]
pub fn drag_window(app: AppHandle) {
app.get_window("pake").unwrap().start_dragging().unwrap();
}
#[command]
pub fn fullscreen(app: AppHandle) {
let win = app.get_window("pake").unwrap();
if win.is_fullscreen().unwrap() {
win.set_fullscreen(false).unwrap();
} else {
win.set_fullscreen(true).unwrap();
}
}
#[tauri::command]
pub fn open_browser(app: AppHandle, url: String) {
api::shell::open(&app.shell_scope(), url, None).unwrap();
}

View File

@@ -2,9 +2,9 @@
use tauri::MenuItem; use tauri::MenuItem;
use tauri::{ use tauri::{
App, Config, CustomMenuItem, Menu, Submenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl, App, Config, CustomMenuItem, Menu, Submenu, TitleBarStyle, Window, WindowBuilder,
WindowMenuEvent, WindowUrl,
}; };
use tauri_utils::TitleBarStyle;
mod pake; mod pake;
use pake::PakeConfig; use pake::PakeConfig;
@@ -155,7 +155,7 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: std::path::PathB
let window_config = config.windows.first().unwrap(); let window_config = config.windows.first().unwrap();
let user_agent = config.user_agent; let user_agent = config.user_agent;
let url = match window_config.url_type.as_str() { let url = match window_config.url_type.as_str() {
"web" => WindowUrl::External(window_config.url.parse().unwrap()), "web" =>WindowUrl::App(window_config.url.parse().unwrap()),
"local" => WindowUrl::App(std::path::PathBuf::from(&window_config.url)), "local" => WindowUrl::App(std::path::PathBuf::from(&window_config.url)),
_ => panic!("url type only can be web or local"), _ => panic!("url type only can be web or local"),
}; };

View File

@@ -4,6 +4,7 @@
)] )]
use app::{get_data_dir, get_menu, get_pake_config, get_window, menu_event_handle}; use app::{get_data_dir, get_menu, get_pake_config, get_window, menu_event_handle};
pub mod cmd;
pub fn run_app() { pub fn run_app() {
let (pake_config, tauri_config) = get_pake_config(); let (pake_config, tauri_config) = get_pake_config();
@@ -45,7 +46,7 @@ pub fn run_app() {
tauri_app tauri_app
.plugin(tauri_plugin_window_state::Builder::default().build()) .plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![]) .invoke_handler(tauri::generate_handler![cmd::drag_window, cmd::fullscreen])
.setup(|app| { .setup(|app| {
let _window = get_window(app, pake_config, data_dir); let _window = get_window(app, pake_config, data_dir);
#[cfg(feature = "devtools")] #[cfg(feature = "devtools")]

45
src-tauri/src/pake.js vendored
View File

@@ -22,6 +22,43 @@ const ctrlKeyShortcuts = {
0: () => zoomCommon(() => '100%'), 0: () => zoomCommon(() => '100%'),
}; };
const uid = () => window.crypto.getRandomValues(new Uint32Array(1))[0];
function transformCallback(callback = () => {}, once = false) {
const identifier = uid();
const prop = `_${identifier}`;
Object.defineProperty(window, prop, {
value: (result) => {
if (once) {
Reflect.deleteProperty(window, prop);
}
return callback(result);
},
writable: false,
configurable: true,
});
return identifier;
}
async function invoke(cmd, args) {
return new Promise((resolve, reject) => {
if (!window.__TAURI_POST_MESSAGE__)
reject('__TAURI_POST_MESSAGE__ does not exist~');
const callback = transformCallback((e) => {
resolve(e);
Reflect.deleteProperty(window, `_${error}`);
}, true);
const error = transformCallback((e) => {
reject(e);
Reflect.deleteProperty(window, `_${callback}`);
}, true);
window.__TAURI_POST_MESSAGE__({
cmd,
callback,
error,
...args,
});
});
}
window.addEventListener('DOMContentLoaded', (_event) => { window.addEventListener('DOMContentLoaded', (_event) => {
const style = document.createElement('style'); const style = document.createElement('style');
style.innerHTML = ` style.innerHTML = `
@@ -275,16 +312,16 @@ window.addEventListener('DOMContentLoaded', (_event) => {
domEl.addEventListener('mousedown', (e) => { domEl.addEventListener('mousedown', (e) => {
if (e.buttons === 1 && e.detail !== 2) { if (e.buttons === 1 && e.detail !== 2) {
window.ipc.postMessage('drag_window'); invoke('drag_window');
} }
}); });
domEl.addEventListener('touchstart', () => { domEl.addEventListener('touchstart', () => {
window.ipc.postMessage('drag_window'); invoke('drag_window');
}); });
domEl.addEventListener('dblclick', () => { domEl.addEventListener('dblclick', () => {
window.ipc.postMessage('fullscreen'); invoke('fullscreen');
}); });
document.addEventListener('keyup', function (event) { document.addEventListener('keyup', function (event) {
@@ -316,7 +353,7 @@ window.addEventListener('DOMContentLoaded', (_event) => {
target === '_blank' // a 标签内链接的 target 属性为 _blank 时 target === '_blank' // a 标签内链接的 target 属性为 _blank 时
) { ) {
e.preventDefault(); e.preventDefault();
window.ipc.postMessage(`open_browser:${origin.href}`); invoke('open_browser', { url: origin.href });
} }
} }
}); });

View File

@@ -13,9 +13,13 @@
"systemTray": { "systemTray": {
"iconPath": "png/weread_512.png", "iconPath": "png/weread_512.png",
"iconAsTemplate": true "iconAsTemplate": true
},
"allowlist": {
"all": true
} }
}, },
"build": { "build": {
"withGlobalTauri": true,
"devPath": "../dist", "devPath": "../dist",
"distDir": "../dist", "distDir": "../dist",
"beforeBuildCommand": "", "beforeBuildCommand": "",