Files
whats-that-music/desktop-client/tray.js
T

58 lines
1.8 KiB
JavaScript
Raw Normal View History

const { Tray, Menu, nativeImage, shell, clipboard } = require('electron');
const { TRAY_ICON_BASE64 } = require('./tray-icon');
/**
* @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() },
]);
}
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 };