mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 03:11:46 +00:00
convert plugins
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
import { createPluginBuilder } from '../utils/builder';
|
||||
import { createPlugin } from '@/utils';
|
||||
import { onMainLoad } from './main';
|
||||
import { onMenu } from './menu';
|
||||
|
||||
export type ShortcutMappingType = {
|
||||
previous: string;
|
||||
@ -12,7 +14,7 @@ export type ShortcutsPluginConfig = {
|
||||
local: ShortcutMappingType;
|
||||
}
|
||||
|
||||
const builder = createPluginBuilder('shortcuts', {
|
||||
export default createPlugin({
|
||||
name: 'Shortcuts (& MPRIS)',
|
||||
restartNeeded: true,
|
||||
config: {
|
||||
@ -29,12 +31,7 @@ const builder = createPluginBuilder('shortcuts', {
|
||||
next: '',
|
||||
},
|
||||
} as ShortcutsPluginConfig,
|
||||
menu: onMenu,
|
||||
|
||||
backend: onMainLoad,
|
||||
});
|
||||
|
||||
export default builder;
|
||||
|
||||
declare global {
|
||||
interface PluginBuilderList {
|
||||
[builder.id]: typeof builder;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
import { BrowserWindow, globalShortcut } from 'electron';
|
||||
import is from 'electron-is';
|
||||
import electronLocalshortcut from 'electron-localshortcut';
|
||||
import { register as registerElectronLocalShortcut } from 'electron-localshortcut';
|
||||
|
||||
import registerMPRIS from './mpris';
|
||||
import getSongControls from '@/providers/song-controls';
|
||||
|
||||
import builder, { ShortcutMappingType } from './index';
|
||||
import type { ShortcutMappingType, ShortcutsPluginConfig } from './index';
|
||||
|
||||
import getSongControls from '../../providers/song-controls';
|
||||
import type { BackendContext } from '@/types/contexts';
|
||||
|
||||
|
||||
function _registerGlobalShortcut(webContents: Electron.WebContents, shortcut: string, action: (webContents: Electron.WebContents) => void) {
|
||||
@ -16,62 +17,58 @@ function _registerGlobalShortcut(webContents: Electron.WebContents, shortcut: st
|
||||
}
|
||||
|
||||
function _registerLocalShortcut(win: BrowserWindow, shortcut: string, action: (webContents: Electron.WebContents) => void) {
|
||||
electronLocalshortcut.register(win, shortcut, () => {
|
||||
registerElectronLocalShortcut(win, shortcut, () => {
|
||||
action(win.webContents);
|
||||
});
|
||||
}
|
||||
|
||||
export default builder.createMain(({ getConfig }) => {
|
||||
return {
|
||||
async onLoad(win) {
|
||||
const config = await getConfig();
|
||||
export const onMainLoad = async ({ getConfig, window }: BackendContext<ShortcutsPluginConfig>) => {
|
||||
const config = await getConfig();
|
||||
|
||||
const songControls = getSongControls(win);
|
||||
const { playPause, next, previous, search } = songControls;
|
||||
const songControls = getSongControls(window);
|
||||
const { playPause, next, previous, search } = songControls;
|
||||
|
||||
if (config.overrideMediaKeys) {
|
||||
_registerGlobalShortcut(win.webContents, 'MediaPlayPause', playPause);
|
||||
_registerGlobalShortcut(win.webContents, 'MediaNextTrack', next);
|
||||
_registerGlobalShortcut(win.webContents, 'MediaPreviousTrack', previous);
|
||||
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()) {
|
||||
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
|
||||
}
|
||||
|
||||
_registerLocalShortcut(win, 'CommandOrControl+F', search);
|
||||
_registerLocalShortcut(win, 'CommandOrControl+L', search);
|
||||
|
||||
if (is.linux()) {
|
||||
registerMPRIS(win);
|
||||
console.debug(`Registering ${type} shortcut`, container[action], ':', action);
|
||||
const actionCallback: () => void = songControls[action];
|
||||
if (typeof actionCallback !== 'function') {
|
||||
console.warn('Invalid action', action);
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (type === 'global') {
|
||||
_registerGlobalShortcut(window.webContents, container[action], actionCallback);
|
||||
} else { // Type === "local"
|
||||
_registerLocalShortcut(window, local[action], actionCallback);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
import prompt, { KeybindOptions } from 'custom-electron-prompt';
|
||||
|
||||
import builder, { ShortcutsPluginConfig } from './index';
|
||||
|
||||
import promptOptions from '../../providers/prompt-options';
|
||||
import promptOptions from '@/providers/prompt-options';
|
||||
|
||||
import type { ShortcutsPluginConfig } from './index';
|
||||
import type { BrowserWindow } from 'electron';
|
||||
import type { MenuContext } from '@/types/contexts';
|
||||
import type { MenuTemplate } from '@/menu';
|
||||
|
||||
export default builder.createMenu(async ({ window, getConfig, setConfig }) => {
|
||||
export const onMenu = async ({ window, getConfig, setConfig }: MenuContext<ShortcutsPluginConfig>): Promise<MenuTemplate> => {
|
||||
const config = await getConfig();
|
||||
|
||||
/**
|
||||
@ -52,4 +53,4 @@ export default builder.createMenu(async ({ window, getConfig, setConfig }) => {
|
||||
click: (item) => setConfig({ overrideMediaKeys: item.checked }),
|
||||
},
|
||||
];
|
||||
});
|
||||
};
|
||||
|
||||
@ -2,9 +2,9 @@ import { BrowserWindow, ipcMain } from 'electron';
|
||||
|
||||
import mpris, { Track } from '@jellybrick/mpris-service';
|
||||
|
||||
import registerCallback from '../../providers/song-info';
|
||||
import getSongControls from '../../providers/song-controls';
|
||||
import config from '../../config';
|
||||
import registerCallback from '@/providers/song-info';
|
||||
import getSongControls from '@/providers/song-controls';
|
||||
import config from '@/config';
|
||||
|
||||
function setupMPRIS() {
|
||||
const instance = new mpris({
|
||||
|
||||
Reference in New Issue
Block a user