From 746871d0a2ce0f2f034981f1446bb46a12dd2ed5 Mon Sep 17 00:00:00 2001 From: maxichrome Date: Tue, 13 Sep 2022 19:04:51 -0500 Subject: [PATCH] use native/Tauri keybind to open settings --- apps/desktop/src-tauri/src/menu.rs | 24 +++++++++--------------- apps/desktop/src/index.tsx | 6 ++---- packages/interface/src/AppRouter.tsx | 8 ++++---- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/apps/desktop/src-tauri/src/menu.rs b/apps/desktop/src-tauri/src/menu.rs index 29660d18e..824ebf74a 100644 --- a/apps/desktop/src-tauri/src/menu.rs +++ b/apps/desktop/src-tauri/src/menu.rs @@ -6,10 +6,8 @@ use tauri::{ }; #[derive(Serialize, Clone)] -pub struct DOMKeyboardEvent { - #[serde(rename = "metaKey")] - meta_key: bool, - key: String, +pub struct DOMKeybindEvent { + action: String, } pub(crate) fn get_menu() -> Menu { @@ -27,9 +25,7 @@ fn custom_menu_bar() -> Menu { )) // TODO: fill out about metadata .add_native_item(MenuItem::Separator) .add_item( - // macOS 13 Ventura automatically changes "Preferences" to "Settings" for system-wide consistency. - // Use "Preferences" here to keep consistency on older versions - CustomMenuItem::new("open_settings".to_string(), "Preferences...") + CustomMenuItem::new("open_settings".to_string(), "Settings...") .accelerator("CmdOrCtrl+Comma"), ) .add_native_item(MenuItem::Separator) @@ -97,10 +93,9 @@ pub(crate) fn handle_menu_event(event: WindowMenuEvent) { "open_settings" => event .window() .emit( - "do_keyboard_input", - DOMKeyboardEvent { - meta_key: true, - key: ",".into(), + "exec_keybind", + DOMKeybindEvent { + action: "open_settings".into(), }, ) .unwrap(), @@ -120,10 +115,9 @@ pub(crate) fn handle_menu_event(event: WindowMenuEvent) { "open_search" => event .window() .emit( - "do_keyboard_input", - DOMKeyboardEvent { - meta_key: true, - key: "l".into(), + "exec_keybind", + DOMKeybindEvent { + action: "open_search".into(), }, ) .unwrap(), diff --git a/apps/desktop/src/index.tsx b/apps/desktop/src/index.tsx index ad21450db..f00e5e921 100644 --- a/apps/desktop/src/index.tsx +++ b/apps/desktop/src/index.tsx @@ -37,8 +37,8 @@ function App() { os.platform().then((platform) => setPlatform(getPlatform(platform))); invoke('app_ready'); - const unlisten = listen('do_keyboard_input', (input) => { - document.dispatchEvent(new KeyboardEvent('keydown', input.payload as any)); + const unlisten = listen('exec_keybind', (input) => { + document.dispatchEvent(new CustomEvent('exec_keybind', { detail: input.payload })); }); return () => { @@ -49,12 +49,10 @@ function App() { useEffect(() => { const focusListener = listen('tauri://focus', () => setFocused(true)); const blurListener = listen('tauri://blur', () => setFocused(false)); - const settingsNavigateListener = listen('navigate_to_settings', () => undefined); return () => { focusListener.then((unlisten) => unlisten()); blurListener.then((unlisten) => unlisten()); - settingsNavigateListener.then((unlisten) => unlisten()); }; }, []); diff --git a/packages/interface/src/AppRouter.tsx b/packages/interface/src/AppRouter.tsx index fcd48a274..6983e7f54 100644 --- a/packages/interface/src/AppRouter.tsx +++ b/packages/interface/src/AppRouter.tsx @@ -48,16 +48,16 @@ export function AppRouter() { }, [libraryState, libraryState.currentLibraryUuid, libraries]); useEffect(() => { - const handler = (e: KeyboardEvent) => { - if (e.metaKey && e.key === ',') { + const handleKeybind = (e: KeybindEvent) => { + if (e.detail.action === 'open_settings') { navigate('/settings'); e.preventDefault(); return; } }; - document.addEventListener('keydown', handler); - return () => document.removeEventListener('keydown', handler); + document.addEventListener('exec_keybind', handleKeybind); + return () => document.removeEventListener('exec_keybind', handleKeybind); }, [navigate]); return (