From 5b9e5a722834e45219f76fac2bb6ef4af89b7fd1 Mon Sep 17 00:00:00 2001 From: Tlntin Date: Sun, 20 Nov 2022 10:34:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6js=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/main.rs | 16 ---------------- src-tauri/src/pake.js | 30 +++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 8d25526..a1c7e1a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -165,16 +165,7 @@ fn main() -> wry::Result<()> { webbrowser::open(&href).expect("no browser"); } }; - #[cfg(target_os = "windows")] - let webview = WebViewBuilder::new(window)? - .with_url(&url.to_string())? - .with_devtools(cfg!(feature = "devtools")) - .with_initialization_script(include_str!("pake.js")) - .with_ipc_handler(handler) - .build()?; - - #[cfg(target_os = "linux")] let webview = WebViewBuilder::new(window)? .with_url(&url.to_string())? .with_devtools(cfg!(feature = "devtools")) @@ -182,13 +173,6 @@ fn main() -> wry::Result<()> { .with_ipc_handler(handler) .build()?; - #[cfg(target_os = "macos")] - let webview = WebViewBuilder::new(window)? - .with_url(&url.to_string())? - .with_devtools(cfg!(feature = "devtools")) - .with_initialization_script(include_str!("pake-mac.js")) - .with_ipc_handler(handler) - .build()?; #[cfg(feature = "devtools")] { webview.open_devtools(); diff --git a/src-tauri/src/pake.js b/src-tauri/src/pake.js index 8f03af6..b37ee6a 100644 --- a/src-tauri/src/pake.js +++ b/src-tauri/src/pake.js @@ -1,11 +1,25 @@ /** * @typedef {string} KeyboardKey `event.key` 的代号, * 见 - * @typedef {() => void} OnKeyDown 使用者按下 [CtrlKey] 时应该执行的行为 - * 以 Ctrl 键 为首的快捷键清单。 + * @typedef {() => void} OnKeyDown 使用者按下 [CtrlKey] 或者 ⌘ [KeyboardKey]时应该执行的行为 + * 以 Ctrl键或者Meta 键 (⌘) 为首的快捷键清单。 * 每个写在这里的 shortcuts 都会运行 {@link Event.preventDefault}. * @type {Record} */ + + const metaKeyShortcuts = { + 'ArrowUp': () => scrollTo(0, 0), + 'ArrowDown': () => scrollTo(0, document.body.scrollHeight), + '[': () => window.history.back(), + ']': () => window.history.forward(), + 'r': () => window.location.reload(), + '-': () => zoomOut(), + '=': () => zoomIn(), + '+': () => zoomIn(), + '0': () => zoomCommon(() => '100%'), +} + + const ctrlKeyShortcuts = { 'ArrowUp': () => scrollTo(0, 0), 'ArrowDown': () => scrollTo(0, document.body.scrollHeight), @@ -121,9 +135,15 @@ window.addEventListener('DOMContentLoaded', (_event) => { event.preventDefault(); f(); }; - - if (event.ctrlKey && event.key in ctrlKeyShortcuts) { - preventDefault(ctrlKeyShortcuts[event.key]); + if (/windows|linux/i.test(navigator.userAgent)) { + if (event.ctrlKey && event.key in ctrlKeyShortcuts) { + preventDefault(ctrlKeyShortcuts[event.key]); + } + } + if (/macintosh|mac os x/i.test(navigator.userAgent)) { + if (event.metaKey && event.key in metaKeyShortcuts) { + preventDefault(ctrlKeyShortcuts[event.key]); + } } });