Move tray state into createTray closure
Removes top-level let trayInstance, opts, currentTitle, currentArtist and the top-level buildMenu function. All mutable state and buildMenu now live inside the createTray closure. Public API and behaviour are unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+23
-26
@@ -14,30 +14,6 @@ const TRAY_ICON_BASE64 =
|
||||
'PB7S4/EuoISgYrLKD1dOhOPFP6+4R7ieoQPNv9/ULZ+zfis+0Y1e+F7oF3H5F+Xt' +
|
||||
'T5p0AqBYeEeEF8k/gtcS56z6f/E5UOLHL8A8a4yC0IHbH4AAAAASUVORK5CYII=';
|
||||
|
||||
let trayInstance = null;
|
||||
let opts = null;
|
||||
let currentTitle = null;
|
||||
let currentArtist = null;
|
||||
|
||||
function buildMenu() {
|
||||
const url = `http://localhost:${opts.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: () => opts.onShow && opts.onShow() },
|
||||
{ label: 'Open Overlay in Browser', click: () => shell.openExternal(url) },
|
||||
{ label: 'Copy OBS URL', click: () => clipboard.writeText(url) },
|
||||
{ type: 'separator' },
|
||||
{ label: 'Quit', click: () => opts.onQuit && opts.onQuit() },
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @param {number} options.port
|
||||
@@ -45,8 +21,29 @@ function buildMenu() {
|
||||
* @param {() => void} options.onQuit
|
||||
* @returns {{ update: (info: { title: string|null, artist: string|null }) => void, destroy: () => void }}
|
||||
*/
|
||||
function createTray(options) {
|
||||
opts = options;
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user