From d222d4ffc1c846853b99b87c2287ed097c181f1b Mon Sep 17 00:00:00 2001 From: Tuxinal <24763016+tuxinal@users.noreply.github.com> Date: Sun, 7 Jan 2024 18:26:54 +0330 Subject: [PATCH] Minimize regex convert id to number enable desktop only shortcut actions --- src/main/ipc.ts | 6 ++--- src/preload/VesktopNative.ts | 5 ++-- src/renderer/index.ts | 2 +- src/renderer/patches/keybinds.ts | 46 +++++++++++++++++++------------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 0c7b11c..71dd512 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -142,15 +142,15 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) const registered_keybinds = {}; -handle(IpcEvents.KEYBIND_REGISTER, (_, id: string, shortcut: string) => { +handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string, options: any) => { globalShortcut.register(shortcut, () => { // false here implies `keyup` // electron's global shortcut system doesn't really register keyup or down as far as i can tell - mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks["${id}"](false)`); + mainWin.webContents.executeJavaScript(`Vesktop.keybindCallbacks[${id}](false)`); }); registered_keybinds[id] = shortcut; }); -handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: string) => { +handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => { globalShortcut.unregister(registered_keybinds[id]); }); diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index a755ae5..01af5cb 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -77,7 +77,8 @@ export const VesktopNative = { invoke(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc) }, keybind: { - register: (id: string, shortcut: string) => invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut), - uregister: (id: string) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) + register: (id: number, shortcut: string, options: any) => + invoke(IpcEvents.KEYBIND_REGISTER, id, shortcut), + uregister: (id: number) => invoke(IpcEvents.KEYBIND_UNREGISTER, id) } }; diff --git a/src/renderer/index.ts b/src/renderer/index.ts index a1c3de9..cd0e00c 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -21,7 +21,7 @@ export { Settings }; const InviteActions = findByPropsLazy("resolveInvite"); -export const keybindCallbacks: { [id: string]: Function } = {}; +export const keybindCallbacks: { [id: number]: Function } = {}; export async function openInviteModal(code: string) { const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal"); diff --git a/src/renderer/patches/keybinds.ts b/src/renderer/patches/keybinds.ts index 209b579..d159b57 100644 --- a/src/renderer/patches/keybinds.ts +++ b/src/renderer/patches/keybinds.ts @@ -4,39 +4,49 @@ * Copyright (c) 2023 Vendicated and Vencord contributors */ +import { findByPropsLazy } from "@vencord/types/webpack"; import { keybindCallbacks } from "renderer"; import { addPatch } from "./shared"; +const { toString } = findByPropsLazy("keyToCode"); addPatch({ patches: [ { find: ".default.Messages.KEYBINDS,children:", - replacement: { - match: /.\.isPlatformEmbedded/, - replace: "true" - } + replacement: [ + { + // eslint-disable-next-line no-useless-escape + match: /\i\.isPlatformEmbedded/g, + replace: "true" + }, + { + // eslint-disable-next-line no-useless-escape + match: /\(0,\i.isDesktop\)\(\)/g, + replace: "true" + } + ] }, { find: "[kb store] KeybindStore", - replacement: { - match: /(inputEventRegister\(parseInt\((.{1,2})\),(.{1,2}),(.{1,2}),(.{1,2})\);else\{)([^;]*;[^;]*;.{1,2}\.keyup&&.{1,2}\.bindGlobal\(\(0,(.{1,2}\.toString)\))/, - replace: "$1$self.registerKeybind($2,$3,$4,$7);return;$6" - } - }, - { - find: "[kb store] KeybindStore", - replacement: { - // WHY IS THE RADIX SPEICIFIED - match: /(inputEventUnregister\(parseInt\((.{1,2}),10\)\);else\{)/, - replace: "$1$self.unregisterKeybind($2);return;" - } + replacement: [ + { + // eslint-disable-next-line no-useless-escape + match: /inputEventRegister\((parseInt\(\i\),\i,\i,\i)\);else\{/, + replace: "$&$self.registerKeybind($1);return;" + }, + { + // eslint-disable-next-line no-useless-escape + match: /inputEventUnregister\((parseInt\(\i,10\))\);else\{/, + replace: "$&$self.unregisterKeybind($1);return;" + } + ] } ], - registerKeybind: function (id, shortcut, callback, toString) { + registerKeybind: function (id, shortcut, callback, options) { keybindCallbacks[id] = callback; - VesktopNative.keybind.register(id, toString(shortcut)); + VesktopNative.keybind.register(id, toString(shortcut), options); }, unregisterKeybind: function (id) { delete keybindCallbacks[id];