终于调通了

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

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::{
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"),
};

View File

@@ -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
View File

@@ -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 });
}
}
});