Skip to content

Commit 4d239eb

Browse files
refactor: improve type definitions and clean up menu function in global keybinds
1 parent b17ed33 commit 4d239eb

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/plugins/global-keybinds/index.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { globalShortcut, ipcMain } from 'electron';
22
import prompt, { type KeybindOptions } from 'custom-electron-prompt';
33

44
import { eventRace } from './utils';
5-
5+
import type { BackendContext } from '@/types/contexts';
66
import { createPlugin } from '@/utils';
77

88
import promptOptions from '@/providers/prompt-options';
@@ -59,7 +59,7 @@ export default createPlugin({
5959
addedVersion: '3.12.x',
6060
restartNeeded: false,
6161
config: Object.assign({}, defaultConfig),
62-
menu: async ({ setConfig, getConfig, window, refresh }) => {
62+
menu: async ({ setConfig, getConfig, window }) => {
6363
const config = await getConfig();
6464

6565
function changeOptions(
@@ -159,13 +159,7 @@ export default createPlugin({
159159
getConfig,
160160
ipc,
161161
window,
162-
}: {
163-
getConfig: () =>
164-
| Promise<GlobalKeybindsPluginConfig>
165-
| GlobalKeybindsPluginConfig;
166-
ipc: any;
167-
window: Electron.BrowserWindow;
168-
}) {
162+
}: BackendContext<GlobalKeybindsPluginConfig>) {
169163
globalShortcut.unregisterAll();
170164
const config = await getConfig();
171165

@@ -180,13 +174,16 @@ export default createPlugin({
180174
return accelerator.replace(/'(.)'/g, '$1');
181175
}
182176

183-
Object.entries(config).forEach(([key, value]: [string, any]) => {
177+
Object.entries(config).forEach(([key, value]) => {
178+
if (key === 'enabled' || key === 'dubleTapToogleWindowVisibility')
179+
return;
180+
const keybind = value as KeybindsOptions;
181+
184182
try {
185-
if (key === 'enabled') return;
186-
if (!value?.value) return;
183+
if (!keybind?.value) return;
187184
if (key === 'tooglePlay' && config.dubleTapToogleWindowVisibility) {
188185
globalShortcut.register(
189-
parseAcelerator(value.value),
186+
parseAcelerator(keybind.value),
190187
eventRace({
191188
single: () => {
192189
ipc.send(key, true);
@@ -200,15 +197,15 @@ export default createPlugin({
200197
return;
201198
}
202199

203-
globalShortcut.register(parseAcelerator(value.value), () => {
200+
globalShortcut.register(parseAcelerator(keybind.value), () => {
204201
console.log(
205202
`Global Keybinds Plugin: Triggered shortcut for ${key}`,
206203
);
207204
ipc.send(key, true);
208205
});
209206
} catch (error) {
210207
console.error(
211-
`Global Keybinds Plugin: Error registering shortcut ${value.value}: ${error}`,
208+
`Global Keybinds Plugin: Error registering shortcut ${keybind.value}: ${error}`,
212209
);
213210
}
214211
});
@@ -219,10 +216,18 @@ export default createPlugin({
219216
});
220217

221218
ipcMain.on('global-keybinds:refresh', async () => {
222-
registerShortcuts({ getConfig, ipc, window });
219+
registerShortcuts({
220+
getConfig,
221+
ipc,
222+
window,
223+
} as BackendContext<GlobalKeybindsPluginConfig>);
223224
});
224225

225-
await registerShortcuts({ getConfig, ipc, window });
226+
await registerShortcuts({
227+
getConfig,
228+
ipc,
229+
window,
230+
} as BackendContext<GlobalKeybindsPluginConfig>);
226231
},
227232
stop() {
228233
globalShortcut.unregisterAll();

0 commit comments

Comments
 (0)