✨ 终于调通了
This commit is contained in:
1390
src-tauri/Cargo.lock
generated
1390
src-tauri/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,10 @@
|
||||
[package]
|
||||
name = "app"
|
||||
version = "0.1.0"
|
||||
description = "Pake 打包工具"
|
||||
description = "🤱🏻 Turn any webpage into a desktop app with Rust."
|
||||
authors = ["Tw93"]
|
||||
license = ""
|
||||
repository = ""
|
||||
license = "MIT"
|
||||
repository = "https://github.com/tw93/Pake"
|
||||
default-run = "app"
|
||||
edition = "2021"
|
||||
rust-version = "1.61.0"
|
||||
@@ -17,11 +17,10 @@ tauri-build = { version = "1.2.1", features = [] }
|
||||
[dependencies]
|
||||
serde_json = "1.0.89"
|
||||
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"
|
||||
home = "0.5"
|
||||
tauri-utils = "1.2.1"
|
||||
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin-window-state", branch = "dev"}
|
||||
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
|
||||
|
||||
[features]
|
||||
# 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
|
||||
# DO NOT remove this
|
||||
custom-protocol = ["tauri/custom-protocol"]
|
||||
# Enable DevTools for debugging.
|
||||
devtools = []
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
{
|
||||
"windows": [
|
||||
{
|
||||
"url": "https://weread.qq.com/",
|
||||
"transparent": true,
|
||||
"fullscreen": false,
|
||||
"width": 1200,
|
||||
"height": 780,
|
||||
"resizable": true,
|
||||
"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
|
||||
"windows": [
|
||||
{
|
||||
"url": "https://weread.qq.com/",
|
||||
"transparent": true,
|
||||
"fullscreen": false,
|
||||
"width": 1200,
|
||||
"height": 780,
|
||||
"resizable": true,
|
||||
"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
|
||||
}
|
||||
}
|
||||
|
||||
21
src-tauri/src/cmd.rs
Normal file
21
src-tauri/src/cmd.rs
Normal 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();
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
use tauri::MenuItem;
|
||||
|
||||
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;
|
||||
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 user_agent = config.user_agent;
|
||||
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)),
|
||||
_ => panic!("url type only can be web or local"),
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
)]
|
||||
|
||||
use app::{get_data_dir, get_menu, get_pake_config, get_window, menu_event_handle};
|
||||
pub mod cmd;
|
||||
|
||||
pub fn run_app() {
|
||||
let (pake_config, tauri_config) = get_pake_config();
|
||||
@@ -45,7 +46,7 @@ pub fn run_app() {
|
||||
|
||||
tauri_app
|
||||
.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| {
|
||||
let _window = get_window(app, pake_config, data_dir);
|
||||
#[cfg(feature = "devtools")]
|
||||
|
||||
45
src-tauri/src/pake.js
vendored
45
src-tauri/src/pake.js
vendored
@@ -22,6 +22,43 @@ const ctrlKeyShortcuts = {
|
||||
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) => {
|
||||
const style = document.createElement('style');
|
||||
style.innerHTML = `
|
||||
@@ -275,16 +312,16 @@ window.addEventListener('DOMContentLoaded', (_event) => {
|
||||
|
||||
domEl.addEventListener('mousedown', (e) => {
|
||||
if (e.buttons === 1 && e.detail !== 2) {
|
||||
window.ipc.postMessage('drag_window');
|
||||
invoke('drag_window');
|
||||
}
|
||||
});
|
||||
|
||||
domEl.addEventListener('touchstart', () => {
|
||||
window.ipc.postMessage('drag_window');
|
||||
invoke('drag_window');
|
||||
});
|
||||
|
||||
domEl.addEventListener('dblclick', () => {
|
||||
window.ipc.postMessage('fullscreen');
|
||||
invoke('fullscreen');
|
||||
});
|
||||
|
||||
document.addEventListener('keyup', function (event) {
|
||||
@@ -316,7 +353,7 @@ window.addEventListener('DOMContentLoaded', (_event) => {
|
||||
target === '_blank' // a 标签内链接的 target 属性为 _blank 时
|
||||
) {
|
||||
e.preventDefault();
|
||||
window.ipc.postMessage(`open_browser:${origin.href}`);
|
||||
invoke('open_browser', { url: origin.href });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -13,9 +13,13 @@
|
||||
"systemTray": {
|
||||
"iconPath": "png/weread_512.png",
|
||||
"iconAsTemplate": true
|
||||
},
|
||||
"allowlist": {
|
||||
"all": true
|
||||
}
|
||||
},
|
||||
"build": {
|
||||
"withGlobalTauri": true,
|
||||
"devPath": "../dist",
|
||||
"distDir": "../dist",
|
||||
"beforeBuildCommand": "",
|
||||
|
||||
Reference in New Issue
Block a user