|
| 1 | +import { globalShortcut, ipcMain, type MenuItem } from 'electron'; |
| 2 | +import prompt, { type KeybindOptions } from 'custom-electron-prompt'; |
| 3 | +import { eventRace } from './utils'; |
| 4 | + |
| 5 | +import { createPlugin } from '@/utils'; |
| 6 | + |
| 7 | +import promptOptions from '@/providers/prompt-options'; |
| 8 | +import { onPlayerApiReady } from './renderer'; |
| 9 | +import { t } from '@/i18n'; |
| 10 | +import { restart } from '@/providers/app-controls'; |
| 11 | + |
| 12 | +export type GlobalKeybindsPluginConfig = { |
| 13 | + enabled: boolean; |
| 14 | + dubleTapToogleWindowVisibility: boolean; |
| 15 | + volumeUp: KeybindsOptions; |
| 16 | + volumeDown: KeybindsOptions; |
| 17 | + tooglePlay: KeybindsOptions; |
| 18 | + nextTrack: KeybindsOptions; |
| 19 | + previousTrack: KeybindsOptions; |
| 20 | + likeTrack: KeybindsOptions; |
| 21 | + dislikeTrack: KeybindsOptions; |
| 22 | +}; |
| 23 | + |
| 24 | +export type KeybindsOptions = { |
| 25 | + value: string; |
| 26 | + dobleTap?: boolean; |
| 27 | +}; |
| 28 | + |
| 29 | +const KeybindsOptionsFactory = (value = ''): KeybindsOptions => ({ |
| 30 | + value: value, |
| 31 | + dobleTap: false, |
| 32 | +}); |
| 33 | + |
| 34 | +const defaultConfig: GlobalKeybindsPluginConfig = { |
| 35 | + enabled: false, |
| 36 | + dubleTapToogleWindowVisibility: true, |
| 37 | + volumeUp: KeybindsOptionsFactory('Shift+Ctrl+Up'), |
| 38 | + volumeDown: KeybindsOptionsFactory('Shift+Ctrl+Down'), |
| 39 | + tooglePlay: KeybindsOptionsFactory('Shift+Ctrl+Space'), |
| 40 | + nextTrack: KeybindsOptionsFactory('Shift+Ctrl+Right'), |
| 41 | + previousTrack: KeybindsOptionsFactory('Shift+Ctrl+Left'), |
| 42 | + likeTrack: KeybindsOptionsFactory('Shift+Ctrl+='), |
| 43 | + dislikeTrack: KeybindsOptionsFactory('Shift+Ctrl+-'), |
| 44 | +}; |
| 45 | + |
| 46 | +const fields: Record<string, string> = { |
| 47 | + volumeUp: 'volume-up', |
| 48 | + volumeDown: 'volume-down', |
| 49 | + tooglePlay: 'toogle-play', |
| 50 | + nextTrack: 'next-track', |
| 51 | + previousTrack: 'previous-track', |
| 52 | + likeTrack: 'like-track', |
| 53 | + dislikeTrack: 'dislike-track', |
| 54 | +}; |
| 55 | + |
| 56 | +export default createPlugin({ |
| 57 | + name: () => t('plugins.global-keybinds.name'), |
| 58 | + description: () => t('plugins.global-keybinds.description'), |
| 59 | + addedVersion: '3.12.x', |
| 60 | + restartNeeded: false, |
| 61 | + config: Object.assign({}, defaultConfig), |
| 62 | + menu: async ({ setConfig, getConfig, window, refresh }) => { |
| 63 | + const config = await getConfig(); |
| 64 | + |
| 65 | + function changeOptions( |
| 66 | + changedOptions: Partial<GlobalKeybindsPluginConfig>, |
| 67 | + options: GlobalKeybindsPluginConfig, |
| 68 | + ) { |
| 69 | + for (const option in changedOptions) { |
| 70 | + // HACK: Weird TypeScript error |
| 71 | + (options as Record<string, unknown>)[option] = ( |
| 72 | + changedOptions as Record<string, unknown> |
| 73 | + )[option]; |
| 74 | + } |
| 75 | + |
| 76 | + setConfig(options); |
| 77 | + } |
| 78 | + |
| 79 | + // Helper function for globalShortcuts prompt |
| 80 | + const kb = ( |
| 81 | + label_: string, |
| 82 | + value_: string, |
| 83 | + default_: string, |
| 84 | + ): KeybindOptions => ({ |
| 85 | + value: value_, |
| 86 | + label: label_, |
| 87 | + default: default_ || undefined, |
| 88 | + }); |
| 89 | + |
| 90 | + async function promptGlobalShortcuts(options: GlobalKeybindsPluginConfig) { |
| 91 | + ipcMain.emit('global-keybinds:disable-all'); |
| 92 | + const output = await prompt( |
| 93 | + { |
| 94 | + width: 500, |
| 95 | + title: t('plugins.global-keybinds.prompt.title'), |
| 96 | + label: t('plugins.global-keybinds.prompt.label'), |
| 97 | + type: 'keybind', |
| 98 | + keybindOptions: Object.entries(fields).map(([key, field]) => |
| 99 | + kb( |
| 100 | + t(`plugins.global-keybinds.prompt.${field}`), |
| 101 | + key, |
| 102 | + ( |
| 103 | + options[ |
| 104 | + key as keyof GlobalKeybindsPluginConfig |
| 105 | + ] as KeybindsOptions |
| 106 | + )?.value || '', |
| 107 | + ), |
| 108 | + ), |
| 109 | + ...promptOptions(), |
| 110 | + }, |
| 111 | + window, |
| 112 | + ); |
| 113 | + |
| 114 | + if (output) { |
| 115 | + const newGlobalShortcuts: Partial<GlobalKeybindsPluginConfig> = |
| 116 | + Object.assign({}, defaultConfig, options); |
| 117 | + for (const { value, accelerator } of output) { |
| 118 | + if (!value) continue; |
| 119 | + const key = value as keyof GlobalKeybindsPluginConfig; |
| 120 | + if (key !== 'enabled') { |
| 121 | + (newGlobalShortcuts[key] as KeybindsOptions).value = accelerator; |
| 122 | + } |
| 123 | + } |
| 124 | + changeOptions({ ...newGlobalShortcuts }, options); |
| 125 | + } |
| 126 | + if (config.enabled) { |
| 127 | + console.log('Global Keybinds Plugin: Re-registering shortcuts'); |
| 128 | + ipcMain.emit('global-keybinds:refresh'); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return [ |
| 133 | + { |
| 134 | + label: t( |
| 135 | + 'plugins.global-keybinds.dubleTapToogleWindowVisibility.label', |
| 136 | + ), |
| 137 | + toolTip: t( |
| 138 | + 'plugins.global-keybinds.dubleTapToogleWindowVisibility.tooltip', |
| 139 | + ), |
| 140 | + checked: config.dubleTapToogleWindowVisibility, |
| 141 | + type: 'checkbox', |
| 142 | + click: (item) => { |
| 143 | + setConfig({ |
| 144 | + dubleTapToogleWindowVisibility: item.checked, |
| 145 | + }); |
| 146 | + ipcMain.emit('global-keybinds:refresh'); |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + label: t('plugins.global-keybinds.management'), |
| 151 | + click: () => promptGlobalShortcuts(config), |
| 152 | + }, |
| 153 | + ]; |
| 154 | + }, |
| 155 | + |
| 156 | + backend: { |
| 157 | + async start({ ipc, getConfig, window }) { |
| 158 | + async function registerShortcuts({ |
| 159 | + getConfig, |
| 160 | + ipc, |
| 161 | + window, |
| 162 | + }: { |
| 163 | + getConfig: () => |
| 164 | + | Promise<GlobalKeybindsPluginConfig> |
| 165 | + | GlobalKeybindsPluginConfig; |
| 166 | + ipc: any; |
| 167 | + window: Electron.BrowserWindow; |
| 168 | + }) { |
| 169 | + globalShortcut.unregisterAll(); |
| 170 | + const config = await getConfig(); |
| 171 | + |
| 172 | + if (!config.enabled) { |
| 173 | + console.log( |
| 174 | + 'Global Keybinds Plugin: Plugin is disabled, skipping shortcut registration', |
| 175 | + ); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + function parseAcelerator(accelerator: string) { |
| 180 | + return accelerator.replace(/'(.)'/g, '$1'); |
| 181 | + } |
| 182 | + |
| 183 | + Object.entries(config).forEach(([key, value]: [string, any]) => { |
| 184 | + try { |
| 185 | + if (key === 'enabled') return; |
| 186 | + if (!value?.value) return; |
| 187 | + if (key === 'tooglePlay' && config.dubleTapToogleWindowVisibility) { |
| 188 | + globalShortcut.register( |
| 189 | + parseAcelerator(value.value), |
| 190 | + eventRace({ |
| 191 | + single: () => { |
| 192 | + ipc.send(key, true); |
| 193 | + }, |
| 194 | + double: () => { |
| 195 | + if (window.isVisible()) window.hide(); |
| 196 | + else window.show(); |
| 197 | + }, |
| 198 | + }), |
| 199 | + ); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + globalShortcut.register(parseAcelerator(value.value), () => { |
| 204 | + console.log( |
| 205 | + `Global Keybinds Plugin: Triggered shortcut for ${key}`, |
| 206 | + ); |
| 207 | + ipc.send(key, true); |
| 208 | + }); |
| 209 | + } catch (error) { |
| 210 | + console.error( |
| 211 | + `Global Keybinds Plugin: Error registering shortcut ${value.value}: ${error}`, |
| 212 | + ); |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + ipcMain.on('global-keybinds:disable-all', () => { |
| 218 | + globalShortcut.unregisterAll(); |
| 219 | + }); |
| 220 | + |
| 221 | + ipcMain.on('global-keybinds:refresh', async () => { |
| 222 | + registerShortcuts({ getConfig, ipc, window }); |
| 223 | + }); |
| 224 | + |
| 225 | + await registerShortcuts({ getConfig, ipc, window }); |
| 226 | + }, |
| 227 | + stop() { |
| 228 | + globalShortcut.unregisterAll(); |
| 229 | + }, |
| 230 | + }, |
| 231 | + |
| 232 | + renderer: { |
| 233 | + onPlayerApiReady, |
| 234 | + }, |
| 235 | +}); |
0 commit comments