Add global shortcuts

This commit is contained in:
Tuxinal 2024-01-07 15:56:07 +03:30
parent 777a0e83fe
commit 0503eff71e
6 changed files with 80 additions and 2 deletions

View file

@ -7,7 +7,17 @@
if (process.platform === "linux") import("./venmic");
import { execFile } from "child_process";
import { app, BrowserWindow, clipboard, dialog, nativeImage, RelaunchOptions, session, shell } from "electron";
import {
app,
BrowserWindow,
clipboard,
dialog,
globalShortcut,
nativeImage,
RelaunchOptions,
session,
shell
} from "electron";
import { mkdirSync, readFileSync, watch } from "fs";
import { open, readFile } from "fs/promises";
import { release } from "os";
@ -130,6 +140,20 @@ handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string)
});
});
const registered_keybinds = {};
handle(IpcEvents.KEYBIND_REGISTER, (_, id: string, shortcut: string) => {
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)`);
});
registered_keybinds[id] = shortcut;
});
handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: string) => {
globalShortcut.unregister(registered_keybinds[id]);
});
function readCss() {
return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => "");
}

View file

@ -75,5 +75,9 @@ export const VesktopNative = {
clipboard: {
copyImage: (imageBuffer: Uint8Array, imageSrc: string) =>
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)
}
};

View file

@ -21,6 +21,8 @@ export { Settings };
const InviteActions = findByPropsLazy("resolveInvite");
export const keybindCallbacks: { [id: string]: Function } = {};
export async function openInviteModal(code: string) {
const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal");
if (!invite) return false;

View file

@ -10,3 +10,4 @@ import "./platformClass";
import "./screenShareAudio";
import "./spellCheck";
import "./windowsTitleBar";
import "./keybinds";

View file

@ -0,0 +1,45 @@
/*
* SPDX-License-Identifier: GPL-3.0
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
import { keybindCallbacks } from "renderer";
import { addPatch } from "./shared";
addPatch({
patches: [
{
find: ".default.Messages.KEYBINDS,children:",
replacement: {
match: /.\.isPlatformEmbedded/,
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;"
}
}
],
registerKeybind: function (id, shortcut, callback, toString) {
keybindCallbacks[id] = callback;
VesktopNative.keybind.register(id, toString(shortcut));
},
unregisterKeybind: function (id) {
delete keybindCallbacks[id];
VesktopNative.keybind.uregister(id);
}
});

View file

@ -49,5 +49,7 @@ export const enum IpcEvents {
ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY",
CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE"
CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE",
KEYBIND_REGISTER = "VCD_KEYBIND_REGISTER",
KEYBIND_UNREGISTER = "VCD_KEYBIND_UNREGISTER"
}