mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 11:01:45 +00:00
convert plugins
This commit is contained in:
@ -1,36 +1,46 @@
|
||||
import { createPluginBuilder } from '../utils/builder';
|
||||
import { createPlugin } from '@/utils';
|
||||
|
||||
import { onConfigChange, onMainLoad } from './main';
|
||||
import { onMenu } from './menu';
|
||||
|
||||
export interface NotificationsPluginConfig {
|
||||
enabled: boolean;
|
||||
unpauseNotification: boolean;
|
||||
/**
|
||||
* Has effect only on Linux
|
||||
*/
|
||||
urgency: 'low' | 'normal' | 'critical';
|
||||
/**
|
||||
* the following has effect only on Windows
|
||||
*/
|
||||
interactive: boolean;
|
||||
/**
|
||||
* See plugins/notifications/utils for more info
|
||||
*/
|
||||
toastStyle: number;
|
||||
refreshOnPlayPause: boolean;
|
||||
trayControls: boolean;
|
||||
hideButtonText: boolean;
|
||||
}
|
||||
|
||||
const builder = createPluginBuilder('notifications', {
|
||||
export const defaultConfig: NotificationsPluginConfig = {
|
||||
enabled: false,
|
||||
unpauseNotification: false,
|
||||
urgency: 'normal',
|
||||
interactive: true,
|
||||
toastStyle: 1,
|
||||
refreshOnPlayPause: false,
|
||||
trayControls: true,
|
||||
hideButtonText: false,
|
||||
};
|
||||
|
||||
export default createPlugin({
|
||||
name: 'Notifications',
|
||||
restartNeeded: true,
|
||||
config: {
|
||||
enabled: false,
|
||||
unpauseNotification: false,
|
||||
urgency: 'normal', // Has effect only on Linux
|
||||
// the following has effect only on Windows
|
||||
interactive: true,
|
||||
toastStyle: 1, // See plugins/notifications/utils for more info
|
||||
refreshOnPlayPause: false,
|
||||
trayControls: true,
|
||||
hideButtonText: false,
|
||||
} as NotificationsPluginConfig,
|
||||
config: defaultConfig,
|
||||
menu: onMenu,
|
||||
backend: {
|
||||
start: onMainLoad,
|
||||
onConfigChange,
|
||||
},
|
||||
});
|
||||
|
||||
export default builder;
|
||||
|
||||
declare global {
|
||||
interface PluginBuilderList {
|
||||
[builder.id]: typeof builder;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,21 +1,20 @@
|
||||
import { app, BrowserWindow, Notification } from 'electron';
|
||||
|
||||
import playIcon from '@assets/media-icons-black/play.png?asset&asarUnpack';
|
||||
import pauseIcon from '@assets/media-icons-black/pause.png?asset&asarUnpack';
|
||||
import nextIcon from '@assets/media-icons-black/next.png?asset&asarUnpack';
|
||||
import previousIcon from '@assets/media-icons-black/previous.png?asset&asarUnpack';
|
||||
|
||||
import { notificationImage, secondsToMinutes, ToastStyles } from './utils';
|
||||
|
||||
import getSongControls from '../../providers/song-controls';
|
||||
import registerCallback, { SongInfo } from '../../providers/song-info';
|
||||
import { changeProtocolHandler } from '../../providers/protocol-handler';
|
||||
import { setTrayOnClick, setTrayOnDoubleClick } from '../../tray';
|
||||
import { mediaIcons } from '../../types/media-icons';
|
||||
|
||||
import playIcon from '../../../assets/media-icons-black/play.png?asset&asarUnpack';
|
||||
import pauseIcon from '../../../assets/media-icons-black/pause.png?asset&asarUnpack';
|
||||
import nextIcon from '../../../assets/media-icons-black/next.png?asset&asarUnpack';
|
||||
import previousIcon from '../../../assets/media-icons-black/previous.png?asset&asarUnpack';
|
||||
|
||||
import { MainPluginContext } from '../utils/builder';
|
||||
import getSongControls from '@/providers/song-controls';
|
||||
import registerCallback, { SongInfo } from '@/providers/song-info';
|
||||
import { changeProtocolHandler } from '@/providers/protocol-handler';
|
||||
import { setTrayOnClick, setTrayOnDoubleClick } from '@/tray';
|
||||
import { mediaIcons } from '@/types/media-icons';
|
||||
|
||||
import type { NotificationsPluginConfig } from './index';
|
||||
import type { BackendContext } from '@/types/contexts';
|
||||
|
||||
let songControls: ReturnType<typeof getSongControls>;
|
||||
let savedNotification: Notification | undefined;
|
||||
@ -25,7 +24,7 @@ type Accessor<T> = () => T;
|
||||
export default (
|
||||
win: BrowserWindow,
|
||||
config: Accessor<NotificationsPluginConfig>,
|
||||
{ on, send }: MainPluginContext<NotificationsPluginConfig>,
|
||||
{ ipc: { on, send } }: BackendContext<NotificationsPluginConfig>,
|
||||
) => {
|
||||
const sendNotification = (songInfo: SongInfo) => {
|
||||
const iconSrc = notificationImage(songInfo, config());
|
||||
|
||||
@ -5,11 +5,12 @@ import is from 'electron-is';
|
||||
import { notificationImage } from './utils';
|
||||
import interactive from './interactive';
|
||||
|
||||
import builder, { NotificationsPluginConfig } from './index';
|
||||
import { defaultConfig, type NotificationsPluginConfig } from './index';
|
||||
import registerCallback, { type SongInfo } from '@/providers/song-info';
|
||||
|
||||
import registerCallback, { SongInfo } from '../../providers/song-info';
|
||||
import type { BackendContext } from '@/types/contexts';
|
||||
|
||||
let config: NotificationsPluginConfig = builder.config;
|
||||
let config: NotificationsPluginConfig = defaultConfig;
|
||||
|
||||
const notify = (info: SongInfo) => {
|
||||
// Send the notification
|
||||
@ -42,17 +43,14 @@ const setup = () => {
|
||||
});
|
||||
};
|
||||
|
||||
export default builder.createMain((context) => {
|
||||
return {
|
||||
async onLoad(win) {
|
||||
config = await context.getConfig();
|
||||
export const onMainLoad = async (context: BackendContext<NotificationsPluginConfig>) => {
|
||||
config = await context.getConfig();
|
||||
|
||||
// Register the callback for new song information
|
||||
if (is.windows() && config.interactive) interactive(win, () => config, context);
|
||||
else setup();
|
||||
},
|
||||
onConfigChange(newConfig) {
|
||||
config = newConfig;
|
||||
}
|
||||
};
|
||||
});
|
||||
// Register the callback for new song information
|
||||
if (is.windows() && config.interactive) interactive(context.window, () => config, context);
|
||||
else setup();
|
||||
};
|
||||
|
||||
export const onConfigChange = (newConfig: NotificationsPluginConfig) => {
|
||||
config = newConfig;
|
||||
};
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
import is from 'electron-is';
|
||||
|
||||
import { MenuItem } from 'electron';
|
||||
|
||||
import { snakeToCamel, ToastStyles, urgencyLevels } from './utils';
|
||||
|
||||
import builder, { NotificationsPluginConfig } from './index';
|
||||
import type { NotificationsPluginConfig } from './index';
|
||||
|
||||
import type { MenuTemplate } from '../../menu';
|
||||
import type { MenuTemplate } from '@/menu';
|
||||
import type { MenuContext } from '@/types/contexts';
|
||||
|
||||
export default builder.createMenu(async ({ getConfig, setConfig }) => {
|
||||
export const onMenu = async ({ getConfig, setConfig }: MenuContext<NotificationsPluginConfig>): Promise<MenuTemplate> => {
|
||||
const config = await getConfig();
|
||||
|
||||
const getToastStyleMenuItems = (options: NotificationsPluginConfig) => {
|
||||
@ -25,7 +25,7 @@ export default builder.createMenu(async ({ getConfig, setConfig }) => {
|
||||
}
|
||||
|
||||
return array as Electron.MenuItemConstructorOptions[];
|
||||
}
|
||||
};
|
||||
|
||||
const getMenu = (): MenuTemplate => {
|
||||
if (is.linux()) {
|
||||
@ -92,4 +92,4 @@ export default builder.createMenu(async ({ getConfig, setConfig }) => {
|
||||
click: (item) => setConfig({ unpauseNotification: item.checked }),
|
||||
},
|
||||
];
|
||||
});
|
||||
};
|
||||
|
||||
@ -3,12 +3,12 @@ import fs from 'node:fs';
|
||||
|
||||
import { app, NativeImage } from 'electron';
|
||||
|
||||
import { cache } from '../../providers/decorators';
|
||||
import { SongInfo } from '../../providers/song-info';
|
||||
import youtubeMusicIcon from '@assets/youtube-music.png?asset&asarUnpack';
|
||||
|
||||
import youtubeMusicIcon from '../../../assets/youtube-music.png?asset&asarUnpack';
|
||||
import {NotificationsPluginConfig} from "./index";
|
||||
import { cache } from '@/providers/decorators';
|
||||
import { SongInfo } from '@/providers/song-info';
|
||||
|
||||
import type { NotificationsPluginConfig } from './index';
|
||||
|
||||
const userData = app.getPath('userData');
|
||||
const temporaryIcon = path.join(userData, 'tempIcon.png');
|
||||
|
||||
Reference in New Issue
Block a user