From 65e871c16e9933551fb561b7fdd7be6ce3de62f4 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Sun, 26 Apr 2026 09:56:48 -0700 Subject: [PATCH] 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 --- desktop-client/tray.js | 49 ++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/desktop-client/tray.js b/desktop-client/tray.js index 59ba8cd..034a18b 100644 --- a/desktop-client/tray.js +++ b/desktop-client/tray.js @@ -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);