Skip to content

Commit

Permalink
Minimize regex
Browse files Browse the repository at this point in the history
convert id to number
enable desktop only shortcut actions
  • Loading branch information
tuxinal committed Mar 8, 2024
1 parent 0503eff commit d222d4f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});

Expand Down
5 changes: 3 additions & 2 deletions src/preload/VesktopNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export const VesktopNative = {
invoke<void>(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc)
},
keybind: {
register: (id: string, shortcut: string) => invoke<void>(IpcEvents.KEYBIND_REGISTER, id, shortcut),
uregister: (id: string) => invoke<void>(IpcEvents.KEYBIND_UNREGISTER, id)
register: (id: number, shortcut: string, options: any) =>
invoke<void>(IpcEvents.KEYBIND_REGISTER, id, shortcut),
uregister: (id: number) => invoke<void>(IpcEvents.KEYBIND_UNREGISTER, id)
}
};
2 changes: 1 addition & 1 deletion src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
46 changes: 28 additions & 18 deletions src/renderer/patches/keybinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit d222d4f

Please sign in to comment.