implement global shortcuts with electron api
This commit is contained in:
@@ -14,6 +14,7 @@ import { app, BrowserWindow, nativeTheme } from "electron";
|
||||
|
||||
import { DATA_DIR } from "./constants";
|
||||
import { createFirstLaunchTour } from "./firstLaunch";
|
||||
import { registerKeyBinds } from "./keyBinds";
|
||||
import { createWindows, mainWin } from "./mainWindow";
|
||||
import { registerMediaPermissionsHandler } from "./mediaPermissions";
|
||||
import { registerScreenShareHandler } from "./screenShare";
|
||||
@@ -59,6 +60,9 @@ function init() {
|
||||
app.commandLine.appendSwitch("disable-smooth-scrolling");
|
||||
}
|
||||
|
||||
// Enable Wayland Portal for global shortcuts
|
||||
enabledFeatures.add("GlobalShortcutsPortal");
|
||||
|
||||
// disable renderer backgrounding to prevent the app from unloading when in the background
|
||||
// https://github.com/electron/electron/issues/2822
|
||||
// https://github.com/GoogleChrome/chrome-launcher/blob/5a27dd574d47a75fec0fb50f7b774ebf8a9791ba/docs/chrome-flags-for-tools.md#task-throttling
|
||||
@@ -115,6 +119,7 @@ function init() {
|
||||
|
||||
registerScreenShareHandler();
|
||||
registerMediaPermissionsHandler();
|
||||
registerKeyBinds();
|
||||
|
||||
bootstrap();
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
|
||||
* Copyright (c) 2026 Vendicated and Vesktop contributors
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { app, globalShortcut } from "electron";
|
||||
import { IpcCommands } from "shared/IpcEvents";
|
||||
|
||||
import { sendRendererCommand } from "./ipcCommands";
|
||||
import { Settings } from "./settings";
|
||||
|
||||
function unregisterKeyBinds() {
|
||||
globalShortcut.unregisterAll();
|
||||
}
|
||||
|
||||
export async function registerKeyBinds() {
|
||||
await app.whenReady();
|
||||
|
||||
unregisterKeyBinds();
|
||||
|
||||
for (const { action, enabled, key } of Settings.store.keyBinds || []) {
|
||||
if (!enabled || !key || action === "unassigned") continue;
|
||||
|
||||
console.log(
|
||||
key,
|
||||
"SUCCESS",
|
||||
globalShortcut.register(key, () => sendRendererCommand(IpcCommands.HANDLE_KEY_BIND, action))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Settings.addChangeListener("keyBinds", registerKeyBinds);
|
||||
|
||||
app.on("will-quit", unregisterKeyBinds);
|
||||
Reference in New Issue
Block a user