|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import { BrowserWindow, globalShortcut, ipcMain } from "electron"; |
| 3 | +import { |
| 4 | + DEFAULT_RECORDING_SHORTCUTS, |
| 5 | + mergeRecordingShortcuts, |
| 6 | + RECORDING_SHORTCUT_ACTIONS, |
| 7 | + type RecordingShortcutAction, |
| 8 | + type RecordingShortcutsConfig, |
| 9 | +} from "../../../src/lib/recordingShortcuts"; |
| 10 | +import { RECORDING_SHORTCUTS_FILE } from "../constants"; |
| 11 | +import { parseJsonWithByteOrderMark } from "../utils"; |
| 12 | + |
| 13 | +let registeredAccelerators: string[] = []; |
| 14 | +let currentConfig: RecordingShortcutsConfig = { ...DEFAULT_RECORDING_SHORTCUTS }; |
| 15 | + |
| 16 | +function broadcastRecordingShortcut(action: RecordingShortcutAction) { |
| 17 | + for (const window of BrowserWindow.getAllWindows()) { |
| 18 | + if (!window.isDestroyed()) { |
| 19 | + window.webContents.send("recording-shortcut", { action }); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function unregisterAll() { |
| 25 | + for (const accelerator of registeredAccelerators) { |
| 26 | + try { |
| 27 | + globalShortcut.unregister(accelerator); |
| 28 | + } catch (error) { |
| 29 | + console.warn(`[recording-shortcuts] Failed to unregister ${accelerator}:`, error); |
| 30 | + } |
| 31 | + } |
| 32 | + registeredAccelerators = []; |
| 33 | +} |
| 34 | + |
| 35 | +function registerFromConfig(config: RecordingShortcutsConfig): { |
| 36 | + success: boolean; |
| 37 | + config: RecordingShortcutsConfig; |
| 38 | + failed: RecordingShortcutAction[]; |
| 39 | +} { |
| 40 | + unregisterAll(); |
| 41 | + currentConfig = config; |
| 42 | + const failed: RecordingShortcutAction[] = []; |
| 43 | + const used = new Set<string>(); |
| 44 | + |
| 45 | + for (const action of RECORDING_SHORTCUT_ACTIONS) { |
| 46 | + const accelerator = config[action]; |
| 47 | + if (used.has(accelerator)) { |
| 48 | + console.warn( |
| 49 | + `[recording-shortcuts] Duplicate accelerator "${accelerator}" for ${action}`, |
| 50 | + ); |
| 51 | + failed.push(action); |
| 52 | + continue; |
| 53 | + } |
| 54 | + used.add(accelerator); |
| 55 | + |
| 56 | + try { |
| 57 | + const ok = globalShortcut.register(accelerator, () => { |
| 58 | + broadcastRecordingShortcut(action); |
| 59 | + }); |
| 60 | + if (!ok) { |
| 61 | + console.warn( |
| 62 | + `[recording-shortcuts] Could not register "${accelerator}" for ${action} (may be taken by the OS)`, |
| 63 | + ); |
| 64 | + failed.push(action); |
| 65 | + continue; |
| 66 | + } |
| 67 | + registeredAccelerators.push(accelerator); |
| 68 | + } catch (error) { |
| 69 | + console.warn( |
| 70 | + `[recording-shortcuts] Error registering "${accelerator}" for ${action}:`, |
| 71 | + error, |
| 72 | + ); |
| 73 | + failed.push(action); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return { success: failed.length === 0, config: currentConfig, failed }; |
| 78 | +} |
| 79 | + |
| 80 | +async function loadConfigFromDisk(): Promise<RecordingShortcutsConfig> { |
| 81 | + try { |
| 82 | + const data = await fs.readFile(RECORDING_SHORTCUTS_FILE, "utf-8"); |
| 83 | + return mergeRecordingShortcuts( |
| 84 | + parseJsonWithByteOrderMark<Partial<RecordingShortcutsConfig>>(data), |
| 85 | + ); |
| 86 | + } catch { |
| 87 | + return { ...DEFAULT_RECORDING_SHORTCUTS }; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function getRecordingShortcutsConfig(): RecordingShortcutsConfig { |
| 92 | + return currentConfig; |
| 93 | +} |
| 94 | + |
| 95 | +export function unregisterRecordingShortcuts() { |
| 96 | + unregisterAll(); |
| 97 | +} |
| 98 | + |
| 99 | +export async function initRecordingShortcuts() { |
| 100 | + const config = await loadConfigFromDisk(); |
| 101 | + return registerFromConfig(config); |
| 102 | +} |
| 103 | + |
| 104 | +export function registerRecordingShortcutHandlers() { |
| 105 | + ipcMain.handle("get-recording-shortcuts", async () => { |
| 106 | + return currentConfig; |
| 107 | + }); |
| 108 | + |
| 109 | + ipcMain.handle("save-recording-shortcuts", async (_, shortcuts: unknown) => { |
| 110 | + try { |
| 111 | + const next = mergeRecordingShortcuts( |
| 112 | + shortcuts as Partial<RecordingShortcutsConfig> | null, |
| 113 | + ); |
| 114 | + await fs.writeFile(RECORDING_SHORTCUTS_FILE, JSON.stringify(next, null, 2), "utf-8"); |
| 115 | + const result = registerFromConfig(next); |
| 116 | + return { |
| 117 | + success: result.success, |
| 118 | + config: result.config, |
| 119 | + failed: result.failed, |
| 120 | + }; |
| 121 | + } catch (error) { |
| 122 | + console.error("[recording-shortcuts] Failed to save:", error); |
| 123 | + return { |
| 124 | + success: false, |
| 125 | + config: currentConfig, |
| 126 | + failed: [...RECORDING_SHORTCUT_ACTIONS], |
| 127 | + error: String(error), |
| 128 | + }; |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + ipcMain.handle("reset-recording-shortcuts", async () => { |
| 133 | + try { |
| 134 | + await fs.writeFile( |
| 135 | + RECORDING_SHORTCUTS_FILE, |
| 136 | + JSON.stringify(DEFAULT_RECORDING_SHORTCUTS, null, 2), |
| 137 | + "utf-8", |
| 138 | + ); |
| 139 | + } catch { |
| 140 | + /* ignore write failure; still re-register defaults in memory */ |
| 141 | + } |
| 142 | + const result = registerFromConfig({ ...DEFAULT_RECORDING_SHORTCUTS }); |
| 143 | + return { success: result.success, config: result.config, failed: result.failed }; |
| 144 | + }); |
| 145 | +} |
0 commit comments