Global Hotkey (#1534)

Sets up a configurable global hotkey to focus the last window used in
the application. Note that this is established at startup and
configuration changes will not be applied until rebooting the app.
This commit is contained in:
Sylvie Crowe 2024-12-16 15:24:32 -08:00 committed by GitHub
parent 99b5a08a2e
commit 51bd45bd2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 56 additions and 2 deletions

View File

@ -166,3 +166,24 @@ export function ensureBoundsAreVisible(bounds: electron.Rectangle): electron.Rec
}
return bounds;
}
export function waveKeyToElectronKey(waveKey: string): string {
const waveParts = waveKey.split(":");
const electronParts: Array<string> = waveParts.map((part: string) => {
if (part == "ArrowUp") {
return "Up";
}
if (part == "ArrowDown") {
return "Down";
}
if (part == "ArrowLeft") {
return "Left";
}
if (part == "ArrowRight") {
return "Right";
}
return part;
});
return electronParts.join("+");
}

View File

@ -3,7 +3,7 @@
import { ClientService, FileService, ObjectService, WindowService, WorkspaceService } from "@/app/store/services";
import { fireAndForget } from "@/util/util";
import { BaseWindow, BaseWindowConstructorOptions, dialog, ipcMain, screen } from "electron";
import { BaseWindow, BaseWindowConstructorOptions, dialog, globalShortcut, ipcMain, screen } from "electron";
import path from "path";
import { debounce } from "throttle-debounce";
import {
@ -14,7 +14,7 @@ import {
setWasInFg,
} from "./emain-activity";
import { getOrCreateWebViewForTab, getWaveTabViewByWebContentsId, WaveTabView } from "./emain-tabview";
import { delay, ensureBoundsAreVisible } from "./emain-util";
import { delay, ensureBoundsAreVisible, waveKeyToElectronKey } from "./emain-util";
import { log } from "./log";
import { getElectronAppBasePath, unamePlatform } from "./platform";
import { updater } from "./updater";
@ -766,3 +766,23 @@ export async function relaunchBrowserWindows() {
win.show();
}
}
export function registerGlobalHotkey(rawGlobalHotKey: string) {
try {
const electronHotKey = waveKeyToElectronKey(rawGlobalHotKey);
console.log("registering globalhotkey of ", electronHotKey);
globalShortcut.register(electronHotKey, () => {
const selectedWindow = focusedWaveWindow;
const firstWaveWindow = getAllWaveWindows()[0];
if (focusedWaveWindow) {
selectedWindow.focus();
} else if (firstWaveWindow) {
firstWaveWindow.focus();
} else {
fireAndForget(createNewWaveWindow);
}
});
} catch (e) {
console.log("error registering global hotkey: ", e);
}
}

View File

@ -38,6 +38,7 @@ import {
getWaveWindowById,
getWaveWindowByWebContentsId,
getWaveWindowByWorkspaceId,
registerGlobalHotkey,
relaunchBrowserWindows,
WaveBrowserWindow,
} from "./emain-window";
@ -610,6 +611,10 @@ async function appMain() {
fireAndForget(createNewWaveWindow);
}
});
const rawGlobalHotKey = launchSettings?.["key:globalhotkey"];
if (rawGlobalHotKey) {
registerGlobalHotkey(rawGlobalHotKey);
}
}
appMain().catch((e) => {

View File

@ -657,6 +657,8 @@ declare global {
"window:magnifiedblocksize"?: number;
"window:magnifiedblockblurprimarypx"?: number;
"window:magnifiedblockblursecondarypx"?: number;
"key:*"?: boolean;
"key:globalhotkey"?: string;
"telemetry:*"?: boolean;
"telemetry:enabled"?: boolean;
"conn:*"?: boolean;

View File

@ -69,6 +69,9 @@ const (
ConfigKey_WindowMagnifiedBlockBlurPrimaryPx = "window:magnifiedblockblurprimarypx"
ConfigKey_WindowMagnifiedBlockBlurSecondaryPx = "window:magnifiedblockblursecondarypx"
ConfigKey_KeyClear = "key:*"
ConfigKey_KeyGlobalHotkey = "key:globalhotkey"
ConfigKey_TelemetryClear = "telemetry:*"
ConfigKey_TelemetryEnabled = "telemetry:enabled"

View File

@ -96,6 +96,9 @@ type SettingsType struct {
WindowMagnifiedBlockBlurPrimaryPx *int64 `json:"window:magnifiedblockblurprimarypx,omitempty"`
WindowMagnifiedBlockBlurSecondaryPx *int64 `json:"window:magnifiedblockblursecondarypx,omitempty"`
KeyClear bool `json:"key:*,omitempty"`
KeyGlobalHotkey string `json:"key:globalhotkey,omitempty"`
TelemetryClear bool `json:"telemetry:*,omitempty"`
TelemetryEnabled bool `json:"telemetry:enabled,omitempty"`