-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathmain.ts
99 lines (83 loc) · 2.78 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { BrowserWindow, ipcMain, globalShortcut } from 'electron';
import is from 'electron-is';
import { register as registerElectronLocalShortcut } from 'electron-localshortcut';
import registerMPRIS from './mpris';
import getSongControls from '@/providers/song-controls';
import type { ShortcutMappingType, ShortcutsPluginConfig } from './index';
import type { BackendContext } from '@/types/contexts';
function _registerGlobalShortcut(
webContents: Electron.WebContents,
shortcut: string,
action: (webContents: Electron.WebContents) => void,
) {
globalShortcut.register(shortcut, () => {
action(webContents);
});
}
function _registerLocalShortcut(
win: BrowserWindow,
shortcut: string,
action: (webContents: Electron.WebContents) => void,
) {
registerElectronLocalShortcut(win, shortcut, () => {
action(win.webContents);
});
}
export const onMainLoad = async ({
getConfig,
window,
}: BackendContext<ShortcutsPluginConfig>) => {
const config = await getConfig();
const songControls = getSongControls(window);
const { playPause, next, previous, search } = songControls;
if (config.overrideMediaKeys) {
_registerGlobalShortcut(window.webContents, 'MediaPlayPause', playPause);
_registerGlobalShortcut(window.webContents, 'MediaNextTrack', next);
_registerGlobalShortcut(window.webContents, 'MediaPreviousTrack', previous);
}
_registerLocalShortcut(window, 'CommandOrControl+F', search);
_registerLocalShortcut(window, 'CommandOrControl+L', search);
if (is.linux()) {
ipcMain.once('ytmd:video-src-changed', (_) => {
registerMPRIS(window);
});
}
const { global, local } = config;
const shortcutOptions = { global, local };
for (const optionType in shortcutOptions) {
registerAllShortcuts(
shortcutOptions[optionType as 'global' | 'local'],
optionType,
);
}
function registerAllShortcuts(container: ShortcutMappingType, type: string) {
for (const _action in container) {
// HACK: _action is detected as string, but it's actually a key of ShortcutMappingType
const action = _action as keyof ShortcutMappingType;
if (!container[action]) {
continue; // Action accelerator is empty
}
console.debug(
`Registering ${type} shortcut`,
container[action],
':',
action,
);
const actionCallback: () => void = songControls[action];
if (typeof actionCallback !== 'function') {
console.warn('Invalid action', action);
continue;
}
if (type === 'global') {
_registerGlobalShortcut(
window.webContents,
container[action],
actionCallback,
);
} else {
// Type === "local"
_registerLocalShortcut(window, local[action], actionCallback);
}
}
}
};