From fbf4b3b8b5e39c61975e67efc990c45f62de76d8 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 8 Oct 2023 19:41:39 +0900 Subject: [PATCH] fix: missing icons taskbar-mediacontrol --- plugins/notifications/interactive.ts | 12 ++++++------ plugins/notifications/utils.ts | 26 +++----------------------- plugins/taskbar-mediacontrol/back.ts | 8 ++++++-- plugins/utils.ts | 21 ++++++++++++++++++++- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/plugins/notifications/interactive.ts b/plugins/notifications/interactive.ts index cfb2247b..ad674b27 100644 --- a/plugins/notifications/interactive.ts +++ b/plugins/notifications/interactive.ts @@ -2,14 +2,14 @@ import path from 'node:path'; import { app, BrowserWindow, ipcMain, Notification } from 'electron'; -import { icons, notificationImage, saveTempIcon, secondsToMinutes, ToastStyles } from './utils'; +import { notificationImage, secondsToMinutes, ToastStyles } from './utils'; import config from './config'; 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 { getMediaIconLocation } from '../utils'; +import { getMediaIconLocation, mediaIcons, saveMediaIcon } from '../utils'; let songControls: ReturnType; let savedNotification: Notification | undefined; @@ -23,7 +23,7 @@ export default (win: BrowserWindow) => { ipcMain.on('timeChanged', (_, t: number) => currentSeconds = t); if (app.isPackaged) { - saveTempIcon(); + saveMediaIcon(); } let savedSongInfo: SongInfo; @@ -152,9 +152,9 @@ const getXml = (songInfo: SongInfo, iconSrc: string) => { } } }; -const display = (kind: keyof typeof icons) => { +const display = (kind: keyof typeof mediaIcons) => { if (config.get('toastStyle') === ToastStyles.legacy) { - return `content="${icons[kind]}"`; + return `content="${mediaIcons[kind]}"`; } return `\ @@ -163,7 +163,7 @@ const display = (kind: keyof typeof icons) => { `; }; -const getButton = (kind: keyof typeof icons) => +const getButton = (kind: keyof typeof mediaIcons) => ``; const getButtons = (isPaused: boolean) => `\ diff --git a/plugins/notifications/utils.ts b/plugins/notifications/utils.ts index 7c9c8a31..d25017c0 100644 --- a/plugins/notifications/utils.ts +++ b/plugins/notifications/utils.ts @@ -9,7 +9,7 @@ import { cache } from '../../providers/decorators'; import { SongInfo } from '../../providers/song-info'; import { getAssetsDirectoryLocation } from '../utils'; -const icon = 'assets/youtube-music.png'; +const defaultIcon = path.join(getAssetsDirectoryLocation(), 'youtube-music.png'); const userData = app.getPath('userData'); const temporaryIcon = path.join(userData, 'tempIcon.png'); const temporaryBanner = path.join(userData, 'tempBanner.png'); @@ -25,13 +25,6 @@ export const ToastStyles = { legacy: 7, }; -export const icons = { - play: '\u{1405}', // ᐅ - pause: '\u{2016}', // ‖ - next: '\u{1433}', // ᐳ - previous: '\u{1438}', // ᐸ -}; - export const urgencyLevels = [ { name: 'Low', value: 'low' }, { name: 'Normal', value: 'normal' }, @@ -52,7 +45,7 @@ const nativeImageToLogo = cache((nativeImage: NativeImage) => { export const notificationImage = (songInfo: SongInfo) => { if (!songInfo.image) { - return icon; + return defaultIcon; } if (!config.get('interactive')) { @@ -76,25 +69,12 @@ export const saveImage = cache((img: NativeImage, savePath: string) => { fs.writeFileSync(savePath, img.toPNG()); } catch (error: unknown) { console.log(`Error writing song icon to disk:\n${String(error)}`); - return icon; + return defaultIcon; } return savePath; }); -export const saveTempIcon = () => { - for (const kind of Object.keys(icons)) { - const destinationPath = path.join(userData, 'icons', `${kind}.png`); - if (fs.existsSync(destinationPath)) { - continue; - } - - const iconPath = path.resolve(path.resolve(getAssetsDirectoryLocation(), 'media-icons-black'), `${kind}.png`); - fs.mkdirSync(path.dirname(destinationPath), { recursive: true }); - fs.copyFile(iconPath, destinationPath, () => {}); - } -}; - export const snakeToCamel = (string_: string) => string_.replaceAll(/([-_][a-z]|^[a-z])/g, (group) => group.toUpperCase() .replace('-', ' ') diff --git a/plugins/taskbar-mediacontrol/back.ts b/plugins/taskbar-mediacontrol/back.ts index 9579d509..2a59df8d 100644 --- a/plugins/taskbar-mediacontrol/back.ts +++ b/plugins/taskbar-mediacontrol/back.ts @@ -1,16 +1,20 @@ import path from 'node:path'; -import { BrowserWindow, nativeImage } from 'electron'; +import { app, BrowserWindow, nativeImage } from 'electron'; import getSongControls from '../../providers/song-controls'; import registerCallback, { SongInfo } from '../../providers/song-info'; -import { getMediaIconLocation } from '../utils'; +import { getMediaIconLocation, saveMediaIcon } from '../utils'; export default (win: BrowserWindow) => { let currentSongInfo: SongInfo; const { playPause, next, previous } = getSongControls(win); + if (app.isPackaged) { + saveMediaIcon(); + } + const setThumbar = (win: BrowserWindow, songInfo: SongInfo) => { // Wait for song to start before setting thumbar if (!songInfo?.title) { diff --git a/plugins/utils.ts b/plugins/utils.ts index 44beb152..5d464f28 100644 --- a/plugins/utils.ts +++ b/plugins/utils.ts @@ -2,7 +2,6 @@ import fs from 'node:fs'; import path from 'node:path'; import { app, ipcMain, ipcRenderer } from 'electron'; - import is from 'electron-is'; import { ValueOf } from '../utils/type-utils'; @@ -15,6 +14,26 @@ export const getMediaIconLocation = () => ? path.resolve(app.getPath('userData'), 'icons') : path.resolve(getAssetsDirectoryLocation(), 'media-icons-black'); +export const mediaIcons = { + play: '\u{1405}', // ᐅ + pause: '\u{2016}', // ‖ + next: '\u{1433}', // ᐳ + previous: '\u{1438}', // ᐸ +}; + +export const saveMediaIcon = () => { + for (const kind of Object.keys(mediaIcons)) { + const destinationPath = path.join(getMediaIconLocation(), `${kind}.png`); + if (fs.existsSync(destinationPath)) { + continue; + } + + const iconPath = path.resolve(path.resolve(getAssetsDirectoryLocation(), 'media-icons-black'), `${kind}.png`); + fs.mkdirSync(path.dirname(destinationPath), { recursive: true }); + fs.copyFile(iconPath, destinationPath, () => {}); + } +}; + // Creates a DOM element from an HTML string export const ElementFromHtml = (html: string): HTMLElement => { const template = document.createElement('template');