fix(keybinds): unregister all keybinds on open
With the old way removed keybinds wouldn't get unregistered
This commit is contained in:
parent
073aa6621f
commit
913403c05b
3 changed files with 17 additions and 24 deletions
|
@ -9,25 +9,19 @@ import { globalShortcut } from "electron";
|
||||||
import { IpcEvents } from "../shared/IpcEvents";
|
import { IpcEvents } from "../shared/IpcEvents";
|
||||||
import { handle } from "./utils/ipcWrappers";
|
import { handle } from "./utils/ipcWrappers";
|
||||||
|
|
||||||
// mapping the discord ids to the electron accelerators
|
// saving the registered keybinds to unregister them when needed
|
||||||
const registeredKeybinds = new Map<string, string>();
|
const registeredKeybinds: string[] = [];
|
||||||
|
|
||||||
export function registerKeybindsHandler() {
|
export function registerKeybindsHandler() {
|
||||||
handle(IpcEvents.KEYBIND_REGISTER, async (_, id: string, shortcut: number[][], callback: () => void) => {
|
handle(IpcEvents.KEYBIND_REGISTER, async (_, id: string, shortcut: number[][], callback: () => void) => {
|
||||||
const accelerator = discordShortcutToElectronAccelerator(shortcut);
|
const accelerator = discordShortcutToElectronAccelerator(shortcut);
|
||||||
globalShortcut.register(accelerator, callback);
|
globalShortcut.register(accelerator, callback);
|
||||||
registeredKeybinds.set(id, accelerator);
|
registeredKeybinds.push(accelerator);
|
||||||
console.log("Registered keybind", id, shortcut, accelerator);
|
console.log("Registered keybind", id, shortcut, accelerator);
|
||||||
});
|
});
|
||||||
handle(IpcEvents.KEYBIND_UNREGISTER, async (_, id: string) => {
|
handle(IpcEvents.KEYBIND_UNREGISTER, async () => {
|
||||||
const keybind = registeredKeybinds.get(id);
|
registeredKeybinds.forEach(keybind => globalShortcut.unregister(keybind));
|
||||||
if (keybind) {
|
console.log("Unregistered keybinds");
|
||||||
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");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ export const VesktopNative = {
|
||||||
keybinds: {
|
keybinds: {
|
||||||
register: (id: string, shortcut: number[][], callback: () => void) =>
|
register: (id: string, shortcut: number[][], callback: () => void) =>
|
||||||
invoke<void>(IpcEvents.KEYBIND_REGISTER, id, shortcut, callback),
|
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. */
|
/** only available on Linux. */
|
||||||
virtmic: {
|
virtmic: {
|
||||||
|
|
|
@ -34,12 +34,16 @@ addPatch({
|
||||||
|
|
||||||
onceReady.then(() => {
|
onceReady.then(() => {
|
||||||
// register keybinds on load
|
// 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
|
// 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 }) => {
|
FluxDispatcher.subscribe("KEYBINDS_ENABLE_ALL_KEYBINDS", ({ enable }: { enable: boolean }) => {
|
||||||
console.log("keybinds enable all keybinds", enable);
|
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();
|
const keybinds = KeybindsStore.getState();
|
||||||
for (const keybind of Object.values(keybinds)) {
|
for (const keybind of Object.values(keybinds)) {
|
||||||
const { id, shortcut, action } = keybind;
|
|
||||||
if (!shouldAllowKeybind(keybind)) {
|
if (!shouldAllowKeybind(keybind)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enable) {
|
const { id, shortcut, action } = keybind;
|
||||||
VesktopNative.keybinds.register(id, shortcut, getKeybindHandler(action));
|
VesktopNative.keybinds.register(id, shortcut, getKeybindHandler(action));
|
||||||
console.log("keybind registered", action);
|
console.log("keybind registered", action);
|
||||||
} else {
|
|
||||||
VesktopNative.keybinds.unregister(id);
|
|
||||||
console.log("keybind unregistered", action);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue