mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 03:11:46 +00:00
feat: migrate to new plugin api
Co-authored-by: Su-Yong <simssy2205@gmail.com>
This commit is contained in:
40
src/plugins/shortcuts/index.ts
Normal file
40
src/plugins/shortcuts/index.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { createPluginBuilder } from '../utils/builder';
|
||||
|
||||
export type ShortcutMappingType = {
|
||||
previous: string;
|
||||
playPause: string;
|
||||
next: string;
|
||||
};
|
||||
export type ShortcutsPluginConfig = {
|
||||
enabled: boolean;
|
||||
overrideMediaKeys: boolean;
|
||||
global: ShortcutMappingType;
|
||||
local: ShortcutMappingType;
|
||||
}
|
||||
|
||||
const builder = createPluginBuilder('shortcuts', {
|
||||
name: 'Shortcuts (& MPRIS)',
|
||||
restartNeeded: true,
|
||||
config: {
|
||||
enabled: false,
|
||||
overrideMediaKeys: false,
|
||||
global: {
|
||||
previous: '',
|
||||
playPause: '',
|
||||
next: '',
|
||||
},
|
||||
local: {
|
||||
previous: '',
|
||||
playPause: '',
|
||||
next: '',
|
||||
},
|
||||
} as ShortcutsPluginConfig,
|
||||
});
|
||||
|
||||
export default builder;
|
||||
|
||||
declare global {
|
||||
interface PluginBuilderList {
|
||||
[builder.id]: typeof builder;
|
||||
}
|
||||
}
|
||||
@ -4,9 +4,10 @@ import electronLocalshortcut from 'electron-localshortcut';
|
||||
|
||||
import registerMPRIS from './mpris';
|
||||
|
||||
import builder, { ShortcutMappingType } from './index';
|
||||
|
||||
import getSongControls from '../../providers/song-controls';
|
||||
|
||||
import type { ConfigType } from '../../config/dynamic';
|
||||
|
||||
function _registerGlobalShortcut(webContents: Electron.WebContents, shortcut: string, action: (webContents: Electron.WebContents) => void) {
|
||||
globalShortcut.register(shortcut, () => {
|
||||
@ -20,50 +21,57 @@ function _registerLocalShortcut(win: BrowserWindow, shortcut: string, action: (w
|
||||
});
|
||||
}
|
||||
|
||||
function registerShortcuts(win: BrowserWindow, options: ConfigType<'shortcuts'>) {
|
||||
const songControls = getSongControls(win);
|
||||
const { playPause, next, previous, search } = songControls;
|
||||
export default builder.createMain(({ getConfig }) => {
|
||||
return {
|
||||
async onLoad(win) {
|
||||
const config = await getConfig();
|
||||
|
||||
if (options.overrideMediaKeys) {
|
||||
_registerGlobalShortcut(win.webContents, 'MediaPlayPause', playPause);
|
||||
_registerGlobalShortcut(win.webContents, 'MediaNextTrack', next);
|
||||
_registerGlobalShortcut(win.webContents, 'MediaPreviousTrack', previous);
|
||||
}
|
||||
const songControls = getSongControls(win);
|
||||
const { playPause, next, previous, search } = songControls;
|
||||
|
||||
_registerLocalShortcut(win, 'CommandOrControl+F', search);
|
||||
_registerLocalShortcut(win, 'CommandOrControl+L', search);
|
||||
|
||||
if (is.linux()) {
|
||||
registerMPRIS(win);
|
||||
}
|
||||
|
||||
const { global, local } = options;
|
||||
const shortcutOptions = { global, local };
|
||||
|
||||
for (const optionType in shortcutOptions) {
|
||||
registerAllShortcuts(shortcutOptions[optionType as 'global' | 'local'], optionType);
|
||||
}
|
||||
|
||||
function registerAllShortcuts(container: Record<string, string>, type: string) {
|
||||
for (const action in container) {
|
||||
if (!container[action]) {
|
||||
continue; // Action accelerator is empty
|
||||
if (config.overrideMediaKeys) {
|
||||
_registerGlobalShortcut(win.webContents, 'MediaPlayPause', playPause);
|
||||
_registerGlobalShortcut(win.webContents, 'MediaNextTrack', next);
|
||||
_registerGlobalShortcut(win.webContents, 'MediaPreviousTrack', previous);
|
||||
}
|
||||
|
||||
console.debug(`Registering ${type} shortcut`, container[action], ':', action);
|
||||
const actionCallback: () => void = songControls[action as keyof typeof songControls];
|
||||
if (typeof actionCallback !== 'function') {
|
||||
console.warn('Invalid action', action);
|
||||
continue;
|
||||
_registerLocalShortcut(win, 'CommandOrControl+F', search);
|
||||
_registerLocalShortcut(win, 'CommandOrControl+L', search);
|
||||
|
||||
if (is.linux()) {
|
||||
registerMPRIS(win);
|
||||
}
|
||||
|
||||
if (type === 'global') {
|
||||
_registerGlobalShortcut(win.webContents, container[action], actionCallback);
|
||||
} else { // Type === "local"
|
||||
_registerLocalShortcut(win, local[action], actionCallback);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default registerShortcuts;
|
||||
};
|
||||
});
|
||||
|
||||
@ -1,67 +1,55 @@
|
||||
import prompt, { KeybindOptions } from 'custom-electron-prompt';
|
||||
|
||||
import { BrowserWindow } from 'electron';
|
||||
|
||||
import { setMenuOptions } from '../../config/plugins';
|
||||
|
||||
import builder, { ShortcutsPluginConfig } from './index';
|
||||
|
||||
import promptOptions from '../../providers/prompt-options';
|
||||
import { MenuTemplate } from '../../menu';
|
||||
|
||||
import type { ConfigType } from '../../config/dynamic';
|
||||
import type { BrowserWindow } from 'electron';
|
||||
|
||||
export default (win: BrowserWindow, options: ConfigType<'shortcuts'>): MenuTemplate => [
|
||||
{
|
||||
label: 'Set Global Song Controls',
|
||||
click: () => promptKeybind(options, win),
|
||||
},
|
||||
{
|
||||
label: 'Override MediaKeys',
|
||||
type: 'checkbox',
|
||||
checked: options.overrideMediaKeys,
|
||||
click: (item) => setOption(options, 'overrideMediaKeys', item.checked),
|
||||
},
|
||||
];
|
||||
export default builder.createMenu(async ({ window, getConfig, setConfig }) => {
|
||||
const config = await getConfig();
|
||||
|
||||
function setOption<Key extends keyof ConfigType<'shortcuts'> = keyof ConfigType<'shortcuts'>>(
|
||||
options: ConfigType<'shortcuts'>,
|
||||
key: Key | null = null,
|
||||
newValue: ConfigType<'shortcuts'>[Key] | null = null,
|
||||
) {
|
||||
if (key && newValue !== null) {
|
||||
options[key] = newValue;
|
||||
/**
|
||||
* Helper function for keybind prompt
|
||||
*/
|
||||
const kb = (label_: string, value_: string, default_?: string): KeybindOptions => ({ value: value_, label: label_, default: default_ });
|
||||
|
||||
async function promptKeybind(config: ShortcutsPluginConfig, win: BrowserWindow) {
|
||||
const output = await prompt({
|
||||
title: 'Global Keybinds',
|
||||
label: 'Choose Global Keybinds for Songs Control:',
|
||||
type: 'keybind',
|
||||
keybindOptions: [ // If default=undefined then no default is used
|
||||
kb('Previous', 'previous', config.global?.previous),
|
||||
kb('Play / Pause', 'playPause', config.global?.playPause),
|
||||
kb('Next', 'next', config.global?.next),
|
||||
],
|
||||
height: 270,
|
||||
...promptOptions(),
|
||||
}, win);
|
||||
|
||||
if (output) {
|
||||
const newConfig = { ...config };
|
||||
|
||||
for (const { value, accelerator } of output) {
|
||||
newConfig.global[value as keyof ShortcutsPluginConfig['global']] = accelerator;
|
||||
}
|
||||
|
||||
setConfig(config);
|
||||
}
|
||||
// Else -> pressed cancel
|
||||
}
|
||||
|
||||
setMenuOptions('shortcuts', options);
|
||||
}
|
||||
|
||||
// Helper function for keybind prompt
|
||||
const kb = (label_: string, value_: string, default_: string): KeybindOptions => ({ value: value_, label: label_, default: default_ });
|
||||
|
||||
async function promptKeybind(options: ConfigType<'shortcuts'>, win: BrowserWindow) {
|
||||
const output = await prompt({
|
||||
title: 'Global Keybinds',
|
||||
label: 'Choose Global Keybinds for Songs Control:',
|
||||
type: 'keybind',
|
||||
keybindOptions: [ // If default=undefined then no default is used
|
||||
kb('Previous', 'previous', options.global?.previous),
|
||||
kb('Play / Pause', 'playPause', options.global?.playPause),
|
||||
kb('Next', 'next', options.global?.next),
|
||||
],
|
||||
height: 270,
|
||||
...promptOptions(),
|
||||
}, win);
|
||||
|
||||
if (output) {
|
||||
if (!options.global) {
|
||||
options.global = {};
|
||||
}
|
||||
|
||||
for (const { value, accelerator } of output) {
|
||||
options.global[value] = accelerator;
|
||||
}
|
||||
|
||||
setOption(options);
|
||||
}
|
||||
// Else -> pressed cancel
|
||||
}
|
||||
return [
|
||||
{
|
||||
label: 'Set Global Song Controls',
|
||||
click: () => promptKeybind(config, window),
|
||||
},
|
||||
{
|
||||
label: 'Override MediaKeys',
|
||||
type: 'checkbox',
|
||||
checked: config.overrideMediaKeys,
|
||||
click: (item) => setConfig({ overrideMediaKeys: item.checked }),
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
@ -32,7 +32,7 @@ function registerMPRIS(win: BrowserWindow) {
|
||||
|
||||
const player = setupMPRIS();
|
||||
|
||||
ipcMain.on('apiLoaded', () => {
|
||||
ipcMain.handle('apiLoaded', () => {
|
||||
win.webContents.send('setupSeekedListener', 'mpris');
|
||||
win.webContents.send('setupTimeChangedListener', 'mpris');
|
||||
win.webContents.send('setupRepeatChangedListener', 'mpris');
|
||||
|
||||
Reference in New Issue
Block a user