Skip to content

Commit

Permalink
fix: Avoid duplicate shortcut triggers making it impossible to hide t…
Browse files Browse the repository at this point in the history
…he app

fix Toggle command triggers double show/hide #10
  • Loading branch information
dubisdev committed Jul 21, 2024
1 parent 35626b0 commit 445e73d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 0 additions & 1 deletion src-tauri/capabilities/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div data-tauri-drag-region className={styles.inputBoxLayout}>
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useGlobalShortcut.ts
Original file line number Diff line number Diff line change
@@ -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()
}, [])
}
9 changes: 0 additions & 9 deletions src/utils/configureShortcuts.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down

0 comments on commit 445e73d

Please sign in to comment.