diff --git a/src/main/keybinds.ts b/src/main/keybinds.ts index a296f02..ad10589 100644 --- a/src/main/keybinds.ts +++ b/src/main/keybinds.ts @@ -9,25 +9,19 @@ import { globalShortcut } from "electron"; import { IpcEvents } from "../shared/IpcEvents"; import { handle } from "./utils/ipcWrappers"; -// mapping the discord ids to the electron accelerators -const registeredKeybinds = new Map(); +// saving the registered keybinds to unregister them when needed +const registeredKeybinds: string[] = []; export function registerKeybindsHandler() { handle(IpcEvents.KEYBIND_REGISTER, async (_, id: string, shortcut: number[][], callback: () => void) => { const accelerator = discordShortcutToElectronAccelerator(shortcut); globalShortcut.register(accelerator, callback); - registeredKeybinds.set(id, accelerator); + registeredKeybinds.push(accelerator); console.log("Registered keybind", id, shortcut, accelerator); }); - handle(IpcEvents.KEYBIND_UNREGISTER, async (_, id: string) => { - const keybind = registeredKeybinds.get(id); - if (keybind) { - globalShortcut.unregister(keybind); - registeredKeybinds.delete(id); - console.log("Unregistered keybind", id, keybind); - } else { - console.warn("Tried to unregister keybind", id, "but it was not registered"); - } + handle(IpcEvents.KEYBIND_UNREGISTER, async () => { + registeredKeybinds.forEach(keybind => globalShortcut.unregister(keybind)); + console.log("Unregistered keybinds"); }); } diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index ddbef0c..ea07699 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -62,7 +62,7 @@ export const VesktopNative = { keybinds: { register: (id: string, shortcut: number[][], callback: () => void) => invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut, callback), - unregister: (id: string) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) + unregister: () => invoke(IpcEvents.KEYBIND_UNREGISTER) }, /** only available on Linux. */ virtmic: { diff --git a/src/renderer/patches/keybinds.tsx b/src/renderer/patches/keybinds.tsx index b0c6ffd..e5da69c 100644 --- a/src/renderer/patches/keybinds.tsx +++ b/src/renderer/patches/keybinds.tsx @@ -34,12 +34,16 @@ addPatch({ onceReady.then(() => { // register keybinds on load - toggleAllKeybinds(true); + registerAllKeybinds(); // we only need this event as this gets fired when the keybinds page is opened/closed and this is the only place where we need to adjust our keybinds FluxDispatcher.subscribe("KEYBINDS_ENABLE_ALL_KEYBINDS", ({ enable }: { enable: boolean }) => { console.log("keybinds enable all keybinds", enable); - toggleAllKeybinds(enable); + if (enable) { + registerAllKeybinds(); + } else { + VesktopNative.keybinds.unregister(); + } }); }); @@ -63,20 +67,15 @@ function getKeybindHandler(action: Keybind["action"]) { }; } -function toggleAllKeybinds(enable: boolean) { +function registerAllKeybinds() { const keybinds = KeybindsStore.getState(); for (const keybind of Object.values(keybinds)) { - const { id, shortcut, action } = keybind; if (!shouldAllowKeybind(keybind)) { continue; } - if (enable) { - VesktopNative.keybinds.register(id, shortcut, getKeybindHandler(action)); - console.log("keybind registered", action); - } else { - VesktopNative.keybinds.unregister(id); - console.log("keybind unregistered", action); - } + const { id, shortcut, action } = keybind; + VesktopNative.keybinds.register(id, shortcut, getKeybindHandler(action)); + console.log("keybind registered", action); } }