Files
aryx/src/main/services/globalHotkey.ts
T
David KayaandCopilot fb5970cfa7 fix: resolve hotkey registration failures and renderer crash
- Change default hotkey from Super+Shift+A to Alt+Shift+A — Win key
  combinations are frequently reserved by the OS and fail to register
- Make quick prompt window creation lazy (first hotkey press) so it
  cannot interfere with main window startup
- Track failed accelerators to prevent error log spam from repeated
  workspace-updated events
- Fix process.platform crash in renderer SettingsPanel — use isMac
  from @renderer/lib/platform instead (renderer has no node globals)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-13 11:54:10 +02:00

91 lines
2.7 KiB
TypeScript

import electron from 'electron';
import type { QuickPromptSettings } from '@shared/domain/tooling';
export class GlobalHotkeyService {
private currentAccelerator: string | undefined;
private lastFailedAccelerator: string | undefined;
private callback: (() => void) | undefined;
register(settings: QuickPromptSettings, callback: () => void): void {
this.callback = callback;
if (!settings.enabled) {
this.unregister();
return;
}
const accelerator = toElectronAccelerator(settings.hotkey);
// Already registered this accelerator — nothing to do
if (accelerator === this.currentAccelerator) return;
// Don't retry an accelerator that already failed (avoids log spam on
// repeated workspace-updated events during startup)
if (accelerator === this.lastFailedAccelerator) return;
this.unregister();
// Access globalShortcut lazily to ensure electron is fully initialised
const { globalShortcut } = electron;
if (!globalShortcut) {
console.error('[globalHotkey] electron.globalShortcut is not available');
return;
}
try {
const registered = globalShortcut.register(accelerator, callback);
if (registered) {
console.info(`[globalHotkey] Registered: ${accelerator}`);
this.currentAccelerator = accelerator;
this.lastFailedAccelerator = undefined;
} else {
console.warn(
`[globalHotkey] Failed to register accelerator: ${accelerator}` +
' — it may be reserved by the OS or another application',
);
this.currentAccelerator = undefined;
this.lastFailedAccelerator = accelerator;
}
} catch (err) {
console.error(`[globalHotkey] Error registering ${accelerator}:`, err);
this.currentAccelerator = undefined;
this.lastFailedAccelerator = accelerator;
}
}
/** Re-registers with updated settings (e.g. hotkey string changed). */
update(settings: QuickPromptSettings): void {
if (!this.callback) return;
this.register(settings, this.callback);
}
unregister(): void {
if (this.currentAccelerator) {
try {
const { globalShortcut } = electron;
globalShortcut?.unregister(this.currentAccelerator);
} catch {
// best-effort
}
this.currentAccelerator = undefined;
}
this.lastFailedAccelerator = undefined;
}
dispose(): void {
this.unregister();
this.callback = undefined;
}
}
/**
* Convert a portable hotkey string to an Electron accelerator.
*
* - "Super" → "Meta" (Win key on Windows, Cmd on macOS)
* - Strings already using Electron-native names pass through unchanged.
*/
function toElectronAccelerator(hotkey: string): string {
return hotkey.replace(/\bSuper\b/gi, 'Meta');
}