fix(keybinds): unregister all keybinds on open

With the old way removed keybinds wouldn't get unregistered
This commit is contained in:
D3SOX 2024-02-01 16:07:19 +01:00
parent 073aa6621f
commit 913403c05b
No known key found for this signature in database
GPG key ID: 39EC1673FC37B048
3 changed files with 17 additions and 24 deletions

View file

@ -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<string, string>();
// 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");
});
}

View file

@ -62,7 +62,7 @@ export const VesktopNative = {
keybinds: {
register: (id: string, shortcut: number[][], callback: () => void) =>
invoke<void>(IpcEvents.KEYBIND_REGISTER, id, shortcut, callback),
unregister: (id: string) => invoke<void>(IpcEvents.KEYBIND_UNREGISTER, id)
unregister: () => invoke<void>(IpcEvents.KEYBIND_UNREGISTER)
},
/** only available on Linux. */
virtmic: {

View file

@ -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);
}
}