🐛 整理下代码
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
#[cfg(target_os = "macos")]
|
||||
use tauri::MenuItem;
|
||||
|
||||
// #[cfg(target_os = "macos")]
|
||||
// #[macro_use]
|
||||
// extern crate objc;
|
||||
|
||||
use tauri::{
|
||||
window::PlatformWebview, App, Config, CustomMenuItem, Manager, Menu, Submenu, SystemTray,
|
||||
SystemTrayEvent, SystemTrayMenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl,
|
||||
App, Config, CustomMenuItem, Menu, Submenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl,
|
||||
};
|
||||
use tauri_utils::TitleBarStyle;
|
||||
|
||||
mod pake;
|
||||
use pake::PakeConfig;
|
||||
|
||||
@@ -41,98 +38,15 @@ pub fn get_menu() -> Menu {
|
||||
.add_item(close)
|
||||
.add_item(quit);
|
||||
let first_menu = Submenu::new("File", first_menu);
|
||||
// Hot Key
|
||||
// let top = CustomMenuItem::new("top", "Top (↑)");
|
||||
// let buttom = CustomMenuItem::new("buttom", "Bottom (↓)");
|
||||
// let previous = CustomMenuItem::new("previous", "Previous (←)");
|
||||
// let next = CustomMenuItem::new("next", "next (→)");
|
||||
// let refresh = CustomMenuItem::new("refresh", "Refresh");
|
||||
// let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)");
|
||||
// let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)");
|
||||
// let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset");
|
||||
// let hot_key = Menu::new()
|
||||
// .add_item(top)
|
||||
// .add_item(buttom)
|
||||
// .add_item(previous)
|
||||
// .add_item(next)
|
||||
// .add_item(refresh)
|
||||
// .add_item(zoom_in)
|
||||
// .add_item(zoom_out)
|
||||
// .add_item(zoom_reset);
|
||||
// let hot_key_menu = Submenu::new("Hot Key", hot_key);
|
||||
|
||||
// Help
|
||||
// let instructions = CustomMenuItem::new("instruction", "Instruction");
|
||||
// let about = CustomMenuItem::new("about", "About");
|
||||
// let help = Menu::new()
|
||||
// .add_item(instructions)
|
||||
// .add_item(about);
|
||||
// let help_menu = Submenu::new("Help", help);
|
||||
Menu::new().add_submenu(first_menu)
|
||||
// .add_submenu(hot_key_menu)
|
||||
}
|
||||
|
||||
// pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) {
|
||||
// #[cfg(target_os = "linux")]
|
||||
// {
|
||||
// // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html
|
||||
// // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html
|
||||
// use webkit2gtk::traits::WebViewExt;
|
||||
// webview.inner().set_zoom_level(zoom_value);
|
||||
// }
|
||||
|
||||
// #[cfg(windows)]
|
||||
// unsafe {
|
||||
// // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
|
||||
// webview.controller().SetZoomFactor(zoom_value).unwrap();
|
||||
// }
|
||||
|
||||
// #[cfg(target_os = "macos")]
|
||||
// unsafe {
|
||||
// let () = msg_send![webview.inner(), setPageZoom: zoom_value];
|
||||
// let () = msg_send![webview.controller(), removeAllUserScripts];
|
||||
// let bg_color: cocoa::base::id =
|
||||
// msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
|
||||
// let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub fn set_zoom_out(webview: PlatformWebview) {
|
||||
// set_zoom(webview, 1.25);
|
||||
// }
|
||||
|
||||
// pub fn set_zoom_in(webview: PlatformWebview) {
|
||||
// set_zoom(webview, 0.75);
|
||||
// }
|
||||
|
||||
// pub fn zoom_reset(webview: PlatformWebview) {
|
||||
// set_zoom(webview, 1.0);
|
||||
// }
|
||||
|
||||
pub fn menu_event_handle(event: WindowMenuEvent) {
|
||||
match event.menu_item_id() {
|
||||
"hide" => event.window().hide().expect("can't hide window"),
|
||||
"show" => event.window().show().expect("can't show window"),
|
||||
"close" => event.window().close().expect("can't close window"),
|
||||
"quit" => std::process::exit(0),
|
||||
// "zoom_out" => {
|
||||
// event
|
||||
// .window()
|
||||
// .with_webview(set_zoom_out)
|
||||
// .expect("can't set zoom out");
|
||||
// }
|
||||
// "zoom_in" => {
|
||||
// event
|
||||
// .window()
|
||||
// .with_webview(set_zoom_in)
|
||||
// .expect("can't set zoom in");
|
||||
// }
|
||||
// "reset" => {
|
||||
// event
|
||||
// .window()
|
||||
// .with_webview(zoom_reset)
|
||||
// .expect("can't reset zoom");
|
||||
// }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -251,9 +165,12 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: std::path::PathB
|
||||
.user_agent(user_agent.macos.as_str())
|
||||
.resizable(window_config.resizable)
|
||||
.fullscreen(window_config.fullscreen)
|
||||
// .transparent(window_config.transparent)
|
||||
//用于隐藏头部
|
||||
// .title_bar_style(tauri_utils::TitleBarStyle::Overlay)
|
||||
.title_bar_style(if window_config.transparent {
|
||||
TitleBarStyle::Overlay
|
||||
} else {
|
||||
TitleBarStyle::Visible
|
||||
})
|
||||
.inner_size(window_config.width, window_config.height)
|
||||
.initialization_script(include_str!("pake.js"));
|
||||
|
||||
|
||||
73
src-tauri/src/pake.js
vendored
73
src-tauri/src/pake.js
vendored
@@ -1,22 +1,13 @@
|
||||
/**
|
||||
* @typedef {string} KeyboardKey `event.key` 的代号,
|
||||
* 见 <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
|
||||
* @typedef {() => void} OnKeyDown 使用者按下 [CtrlKey] 或者 ⌘ [KeyboardKey]时应该执行的行为
|
||||
* 以 Ctrl键或者Meta 键 (⌘) 为首的快捷键清单。
|
||||
* 每个写在这里的 shortcuts 都会运行 {@link Event.preventDefault}.
|
||||
* @type {Record<KeyboardKey, OnKeyDown>}
|
||||
*/
|
||||
|
||||
const metaKeyShortcuts = {
|
||||
ArrowUp: () => scrollTo(0, 0),
|
||||
ArrowDown: () => scrollTo(0, document.body.scrollHeight),
|
||||
"[": () => window.history.back(),
|
||||
"]": () => window.history.forward(),
|
||||
'[': () => window.history.back(),
|
||||
']': () => window.history.forward(),
|
||||
r: () => window.location.reload(),
|
||||
"-": () => zoomOut(),
|
||||
"=": () => zoomIn(),
|
||||
"+": () => zoomIn(),
|
||||
0: () => zoomCommon(() => "100%"),
|
||||
'-': () => zoomOut(),
|
||||
'=': () => zoomIn(),
|
||||
'+': () => zoomIn(),
|
||||
0: () => zoomCommon(() => '100%'),
|
||||
};
|
||||
|
||||
const ctrlKeyShortcuts = {
|
||||
@@ -25,14 +16,14 @@ const ctrlKeyShortcuts = {
|
||||
ArrowLeft: () => window.history.back(),
|
||||
ArrowRight: () => window.history.forward(),
|
||||
r: () => window.location.reload(),
|
||||
"-": () => zoomOut(),
|
||||
"=": () => zoomIn(),
|
||||
"+": () => zoomIn(),
|
||||
0: () => zoomCommon(() => "100%"),
|
||||
'-': () => zoomOut(),
|
||||
'=': () => zoomIn(),
|
||||
'+': () => zoomIn(),
|
||||
0: () => zoomCommon(() => '100%'),
|
||||
};
|
||||
|
||||
window.addEventListener("DOMContentLoaded", (_event) => {
|
||||
const style = document.createElement("style");
|
||||
window.addEventListener('DOMContentLoaded', (_event) => {
|
||||
const style = document.createElement('style');
|
||||
style.innerHTML = `
|
||||
#page #footer-wrapper,
|
||||
.drawing-board .toolbar .toolbar-action,
|
||||
@@ -276,27 +267,27 @@ window.addEventListener("DOMContentLoaded", (_event) => {
|
||||
}
|
||||
`;
|
||||
document.head.append(style);
|
||||
const topDom = document.createElement("div");
|
||||
topDom.id = "pack-top-dom";
|
||||
const topDom = document.createElement('div');
|
||||
topDom.id = 'pack-top-dom';
|
||||
document.body.appendChild(topDom);
|
||||
|
||||
const domEl = document.getElementById("pack-top-dom");
|
||||
const domEl = document.getElementById('pack-top-dom');
|
||||
|
||||
domEl.addEventListener("mousedown", (e) => {
|
||||
domEl.addEventListener('mousedown', (e) => {
|
||||
if (e.buttons === 1 && e.detail !== 2) {
|
||||
window.ipc.postMessage("drag_window");
|
||||
window.ipc.postMessage('drag_window');
|
||||
}
|
||||
});
|
||||
|
||||
domEl.addEventListener("touchstart", () => {
|
||||
window.ipc.postMessage("drag_window");
|
||||
domEl.addEventListener('touchstart', () => {
|
||||
window.ipc.postMessage('drag_window');
|
||||
});
|
||||
|
||||
domEl.addEventListener("dblclick", () => {
|
||||
window.ipc.postMessage("fullscreen");
|
||||
domEl.addEventListener('dblclick', () => {
|
||||
window.ipc.postMessage('fullscreen');
|
||||
});
|
||||
|
||||
document.addEventListener("keyup", function (event) {
|
||||
document.addEventListener('keyup', function (event) {
|
||||
const preventDefault = (f) => {
|
||||
event.preventDefault();
|
||||
f();
|
||||
@@ -313,12 +304,12 @@ window.addEventListener("DOMContentLoaded", (_event) => {
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("click", (e) => {
|
||||
const origin = e.target.closest("a");
|
||||
document.addEventListener('click', (e) => {
|
||||
const origin = e.target.closest('a');
|
||||
if (origin && origin.href) {
|
||||
const target = origin.target
|
||||
origin.target = "_self";
|
||||
const hrefUrl = new URL(origin.href)
|
||||
const target = origin.target;
|
||||
origin.target = '_self';
|
||||
const hrefUrl = new URL(origin.href);
|
||||
|
||||
if (
|
||||
window.location.host !== hrefUrl.host && // 如果 a 标签内链接的域名和当前页面的域名不一致 且
|
||||
@@ -334,9 +325,9 @@ window.addEventListener("DOMContentLoaded", (_event) => {
|
||||
setDefaultZoom();
|
||||
|
||||
function setDefaultZoom() {
|
||||
const htmlZoom = window.localStorage.getItem("htmlZoom");
|
||||
const htmlZoom = window.localStorage.getItem('htmlZoom');
|
||||
if (htmlZoom) {
|
||||
document.getElementsByTagName("html")[0].style.zoom = htmlZoom;
|
||||
document.getElementsByTagName('html')[0].style.zoom = htmlZoom;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,11 +335,11 @@ function setDefaultZoom() {
|
||||
* @param {(htmlZoom: string) => string} [zoomRule]
|
||||
*/
|
||||
function zoomCommon(zoomRule) {
|
||||
const htmlZoom = window.localStorage.getItem("htmlZoom") || "100%";
|
||||
const html = document.getElementsByTagName("html")[0];
|
||||
const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%';
|
||||
const html = document.getElementsByTagName('html')[0];
|
||||
const zoom = zoomRule(htmlZoom);
|
||||
html.style.zoom = zoom;
|
||||
window.localStorage.setItem("htmlZoom", zoom);
|
||||
window.localStorage.setItem('htmlZoom', zoom);
|
||||
}
|
||||
|
||||
function zoomIn() {
|
||||
|
||||
Reference in New Issue
Block a user