Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add global shortcuts #326

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"dependencies": {
"arrpc": "github:OpenAsar/arrpc#2234e9c9111f4c42ebcc3aa6a2215bfd979eef77",
"electron-updater": "^6.3.9"
"electron-updater": "^6.3.9",
"venbind": "^0.0.3"
},
"optionalDependencies": {
"@vencord/venmic": "^6.1.0"
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions scripts/build/build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,27 @@ async function copyVenmic() {
]).catch(() => console.warn("Failed to copy venmic. Building without venmic support"));
}

async function copyVenbind() {
if (process.platform === "win32") {
return Promise.all([
copyFile(
"./node_modules/venbind/prebuilds/windows-x86_64/venbind-windows-x86_64.node",
"./static/dist/venbind-windows-x86_64.node"
)
]).catch(() => console.warn("Failed to copy venbind. Building without venbind support"));
}

return Promise.all([
copyFile(
"./node_modules/venbind/prebuilds/linux-x86_64/venbind-linux-x86_64.node",
"./static/dist/venbind-linux-x86_64.node"
)
]).catch(() => console.warn("Failed to copy venbind. Building without venbind support"));
}

await Promise.all([
copyVenmic(),
copyVenbind(),
createContext({
...NodeCommonOpts,
entryPoints: ["src/main/index.ts"],
Expand Down
37 changes: 29 additions & 8 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { registerMediaPermissionsHandler } from "./mediaPermissions";
import { registerScreenShareHandler } from "./screenShare";
import { Settings, State } from "./settings";
import { isDeckGameMode } from "./utils/steamOS";
import { startVenbind } from "./venbind";

if (IS_DEV) {
require("source-map-support").install();
Expand Down Expand Up @@ -78,8 +79,18 @@ function init() {
// In the Flatpak on SteamOS the theme is detected as light, but SteamOS only has a dark mode, so we just override it
if (isDeckGameMode) nativeTheme.themeSource = "dark";

app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => {
if (data.IS_DEV) app.quit();
app.on("second-instance", (_event, cmdLine, _cwd, data: any) => {
const keybindIndex = cmdLine.indexOf("--keybind");

if (keybindIndex !== -1) {
if (cmdLine[keybindIndex + 2] === "keyup" || cmdLine[keybindIndex + 2] === "keydown") {
mainWin.webContents.executeJavaScript(
`Vesktop.triggerKeybind(${cmdLine[keybindIndex + 1]}, ${cmdLine[keybindIndex + 2] === "keydown" ? "false" : "true"})`
);
} else {
mainWin.webContents.executeJavaScript(`Vesktop.triggerKeybind(${cmdLine[keybindIndex + 1]}, true)`);
}
} else if (data.IS_DEV) app.quit();
else if (mainWin) {
if (mainWin.isMinimized()) mainWin.restore();
if (!mainWin.isVisible()) mainWin.show();
Expand All @@ -90,6 +101,7 @@ function init() {
app.whenReady().then(async () => {
if (process.platform === "win32") app.setAppUserModelId("dev.vencord.vesktop");

startVenbind();
registerScreenShareHandler();
registerMediaPermissionsHandler();

Expand All @@ -102,15 +114,24 @@ function init() {
}

if (!app.requestSingleInstanceLock({ IS_DEV })) {
if (IS_DEV) {
console.log("Vesktop is already running. Quitting previous instance...");
init();
} else {
console.log("Vesktop is already running. Quitting...");
if (process.argv.includes("--keybind")) {
app.quit();
} else {
if (IS_DEV) {
console.log("Vesktop is already running. Quitting previous instance...");
init();
} else {
console.log("Vesktop is already running. Quitting...");
app.quit();
}
}
} else {
init();
if (process.argv.includes("--keybind")) {
console.error("No instances running! cannot issue a keybind!");
app.quit();
} else {
init();
}
}

async function bootstrap() {
Expand Down
77 changes: 77 additions & 0 deletions src/main/venbind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 { join } from "path";
import { IpcEvents } from "shared/IpcEvents";
import { STATIC_DIR } from "shared/paths";
import type { Venbind as VenbindType } from "venbind";

import { mainWin } from "./mainWindow";
import { handle, handleSync } from "./utils/ipcWrappers";

let venbind: VenbindType | null = null;
export function obtainVenbind() {
if (venbind == null) {
// TODO?: make binary outputs consistant with node's apis
let os: string;
let arch: string;

switch (process.platform) {
case "linux":
os = "linux";
break;
case "win32":
os = "windows";
break;
// case "darwin":
// os = "darwin";
// break;
default:
return null;
}
switch (process.arch) {
case "x64":
arch = "x86_64";
break;
// case "arm64":
// arch = "aarch64";
// break;
default:
return null;
}

venbind = require(join(STATIC_DIR, `dist/venbind-${os}-${arch}.node`));
}
return venbind;
}

export function startVenbind() {
const venbind = obtainVenbind();
venbind?.startKeybinds((id, keyup) => {
mainWin.webContents.executeJavaScript(`Vesktop.triggerKeybind(${id}, ${keyup})`);
});
}

handle(IpcEvents.KEYBIND_REGISTER, (_, id: number, shortcut: string) => {
obtainVenbind()?.registerKeybind(shortcut, id);
});
handle(IpcEvents.KEYBIND_UNREGISTER, (_, id: number) => {
obtainVenbind()?.unregisterKeybind(id);
});
handleSync(IpcEvents.KEYBIND_SHOULD_PREREGISTER, _ => {
if (
process.platform === "linux" &&
(process.env.XDG_SESSION_TYPE === "wayland" ||
!!process.env.WAYLAND_DISPLAY ||
!!process.env.VENBIND_USE_XDG_PORTAL)
) {
return true;
}
return false;
});
handle(IpcEvents.KEYBIND_PREREGISTER, (_, actions: { id: number; name: string }[]) => {
obtainVenbind()?.preregisterKeybinds(actions);
});
6 changes: 6 additions & 0 deletions src/preload/VesktopNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,11 @@ export const VesktopNative = {
ipcRenderer.on(IpcEvents.IPC_COMMAND, (_, message) => cb(message));
},
respond: (response: IpcResponse) => ipcRenderer.send(IpcEvents.IPC_COMMAND, response)
},
keybind: {
register: (id: number, shortcut: string) => invoke<void>(IpcEvents.KEYBIND_REGISTER, id, shortcut),
unregister: (id: number) => invoke<void>(IpcEvents.KEYBIND_UNREGISTER, id),
shouldPreRegister: () => sendSync<boolean>(IpcEvents.KEYBIND_SHOULD_PREREGISTER),
preRegister: (actions: { id: number; name: string }[]) => invoke<void>(IpcEvents.KEYBIND_PREREGISTER, actions)
}
};
19 changes: 19 additions & 0 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@ import { VesktopLogger } from "./logger";
import { Settings } from "./settings";
export { Settings };

export const keybindCallbacks: {
[id: number]: {
onTrigger: Function;
keyEvents: {
keyup: boolean;
keydown: boolean;
};
};
} = {};

VesktopLogger.log("read if cute :3");
VesktopLogger.log("Vesktop v" + VesktopNative.app.getVersion());

export async function triggerKeybind(id: number, keyup: boolean) {
var cb = keybindCallbacks[id];
if (cb.keyEvents.keyup && keyup) {
cb.onTrigger(false);
} else if (cb.keyEvents.keydown && !keyup) {
cb.onTrigger(true);
}
}

const customSettingsSections = (
Vencord.Plugins.plugins.Settings as any as { customSections: ((ID: Record<string, unknown>) => any)[] }
).customSections;
Expand Down
1 change: 1 addition & 0 deletions src/renderer/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import "./windowsTitleBar";
import "./streamerMode";
import "./nativeFocus";
import "./hideDownloadAppsButton";
import "./keybinds";
Loading