2026-04-26 09:50:59 -07:00
|
|
|
const { Tray, Menu, nativeImage, shell, clipboard } = require('electron');
|
2026-10-04 11:37:38 -07:00
|
|
|
const { TRAY_ICON_BASE64 } = require('./tray-icon');
|
2026-04-26 09:50:59 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Object} options
|
|
|
|
|
* @param {number} options.port
|
|
|
|
|
* @param {() => void} options.onShow
|
|
|
|
|
* @param {() => void} options.onQuit
|
|
|
|
|
* @returns {{ update: (info: { title: string|null, artist: string|null }) => void, destroy: () => void }}
|
|
|
|
|
*/
|
2026-04-26 09:56:48 -07:00
|
|
|
function createTray({ port, onShow, onQuit }) {
|
|
|
|
|
let trayInstance = null;
|
|
|
|
|
let currentTitle = null;
|
|
|
|
|
let currentArtist = null;
|
|
|
|
|
|
|
|
|
|
function buildMenu() {
|
|
|
|
|
const url = `http://localhost:${port}/`;
|
|
|
|
|
const nowPlaying =
|
|
|
|
|
currentTitle && currentArtist
|
|
|
|
|
? `${currentTitle} — ${currentArtist}`
|
|
|
|
|
: currentTitle || 'Nothing playing';
|
|
|
|
|
|
|
|
|
|
return Menu.buildFromTemplate([
|
|
|
|
|
{ label: "What's That Music", enabled: false },
|
|
|
|
|
{ label: `Now Playing: ${nowPlaying}`, enabled: false },
|
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
{ label: 'Show Player', click: () => onShow && onShow() },
|
|
|
|
|
{ label: 'Open Overlay in Browser', click: () => shell.openExternal(url) },
|
|
|
|
|
{ label: 'Copy OBS URL', click: () => clipboard.writeText(url) },
|
|
|
|
|
{ type: 'separator' },
|
|
|
|
|
{ label: 'Quit', click: () => onQuit && onQuit() },
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-04-26 09:50:59 -07:00
|
|
|
|
|
|
|
|
const icon = nativeImage.createFromDataURL(TRAY_ICON_BASE64);
|
|
|
|
|
icon.setTemplateImage(true);
|
|
|
|
|
|
|
|
|
|
trayInstance = new Tray(icon);
|
|
|
|
|
trayInstance.setToolTip("What's That Music");
|
|
|
|
|
trayInstance.setContextMenu(buildMenu());
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
update({ title, artist }) {
|
|
|
|
|
currentTitle = title || null;
|
|
|
|
|
currentArtist = artist || null;
|
|
|
|
|
if (trayInstance) trayInstance.setContextMenu(buildMenu());
|
|
|
|
|
},
|
|
|
|
|
destroy() {
|
|
|
|
|
if (trayInstance) {
|
|
|
|
|
trayInstance.destroy();
|
|
|
|
|
trayInstance = null;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { createTray };
|