From 445e73d1423bcf499024cd7de9e9b26d8d3f7da6 Mon Sep 17 00:00:00 2001 From: dubisdev Date: Sun, 21 Jul 2024 11:43:31 +0200 Subject: [PATCH] fix: Avoid duplicate shortcut triggers making it impossible to hide the app fix Toggle command triggers double show/hide #10 --- src-tauri/capabilities/base.json | 1 - src/App.tsx | 2 ++ src/hooks/useGlobalShortcut.ts | 17 +++++++++++++++++ src/utils/configureShortcuts.ts | 9 --------- 4 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 src/hooks/useGlobalShortcut.ts diff --git a/src-tauri/capabilities/base.json b/src-tauri/capabilities/base.json index ce04ea0..837a423 100644 --- a/src-tauri/capabilities/base.json +++ b/src-tauri/capabilities/base.json @@ -19,7 +19,6 @@ "process:allow-exit", "process:allow-restart", "global-shortcut:allow-register", - "global-shortcut:allow-unregister", "global-shortcut:allow-unregister-all", "clipboard-manager:allow-write-text", "window:allow-start-dragging", diff --git a/src/App.tsx b/src/App.tsx index 44b477f..1f889dc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,10 +3,12 @@ import { ConsoleResult } from "@components/ConsoleResult"; import styles from "./app.module.css"; import { useBakgroundColor } from "./hooks/useBackgroundColor"; import { useExitOnClose } from "./hooks/useExitOnClose"; +import { useGlobalShortcut } from "./hooks/useGlobalShortcut"; export function App() { useExitOnClose(); useBakgroundColor(); + useGlobalShortcut(); return (
diff --git a/src/hooks/useGlobalShortcut.ts b/src/hooks/useGlobalShortcut.ts new file mode 100644 index 0000000..dff7dc4 --- /dev/null +++ b/src/hooks/useGlobalShortcut.ts @@ -0,0 +1,17 @@ +import { register, unregisterAll } from "@tauri-apps/plugin-global-shortcut" +import { toggleWindowVisibility } from "@utils/toggleWindowView" +import { useEffect } from "react" + +const registerGlobalShortcut = async () => { + await unregisterAll() + + await register("Alt+m", (s) => { + if (s.state === "Released") toggleWindowVisibility() + }) +} + +export const useGlobalShortcut = () => { + useEffect(() => { + registerGlobalShortcut() + }, []) +} diff --git a/src/utils/configureShortcuts.ts b/src/utils/configureShortcuts.ts index 65b2c7c..d197e2b 100644 --- a/src/utils/configureShortcuts.ts +++ b/src/utils/configureShortcuts.ts @@ -1,21 +1,12 @@ -import { register, unregister, unregisterAll } from "@tauri-apps/plugin-global-shortcut"; import { listen } from "@tauri-apps/api/event"; import { exit } from "@tauri-apps/plugin-process"; import { createSettingsPage } from "./createSettingsPage"; -import { toggleWindowVisibility } from "./toggleWindowView"; export const configureShortcuts = async () => { - try { - await unregisterAll(); - } catch { } // first time it will throw an error, so we ignore it - listen("open-settings", () => { createSettingsPage(); }) - // register global shortcuts (works even when the app is not focused) - await register("Alt+m", toggleWindowVisibility) - // register shortcuts when the window is focused window.addEventListener("keydown", async (e) => { if (isExitCommand(e)) {