mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import { BrowserWindow, globalShortcut } from 'electron';
|
|
import is from 'electron-is';
|
|
import electronLocalshortcut from 'electron-localshortcut';
|
|
|
|
import registerMPRIS from './mpris';
|
|
|
|
import builder, { ShortcutMappingType } from './index';
|
|
|
|
import getSongControls from '../../providers/song-controls';
|
|
|
|
|
|
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) {
|
|
electronLocalshortcut.register(win, shortcut, () => {
|
|
action(win.webContents);
|
|
});
|
|
}
|
|
|
|
export default builder.createMain(({ getConfig }) => {
|
|
return {
|
|
async onLoad(win) {
|
|
const config = await getConfig();
|
|
|
|
const songControls = getSongControls(win);
|
|
const { playPause, next, previous, search } = songControls;
|
|
|
|
if (config.overrideMediaKeys) {
|
|
_registerGlobalShortcut(win.webContents, 'MediaPlayPause', playPause);
|
|
_registerGlobalShortcut(win.webContents, 'MediaNextTrack', next);
|
|
_registerGlobalShortcut(win.webContents, 'MediaPreviousTrack', previous);
|
|
}
|
|
|
|
_registerLocalShortcut(win, 'CommandOrControl+F', search);
|
|
_registerLocalShortcut(win, 'CommandOrControl+L', search);
|
|
|
|
if (is.linux()) {
|
|
registerMPRIS(win);
|
|
}
|
|
|
|
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(win.webContents, container[action], actionCallback);
|
|
} else { // Type === "local"
|
|
_registerLocalShortcut(win, local[action], actionCallback);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
});
|