Files
2026-10-11 07:55:36 -07:00

69 lines
2.5 KiB
JavaScript
Executable File

const { nativeImage, globalShortcut } = require('electron');
const path = require('path');
function updateTaskbarButtons(mainWindow, state = {}) {
if (process.platform !== 'win32') return;
const playIcon = nativeImage.createFromPath(path.join(__dirname, '..', 'icons', state.isPlaying ? 'pause.png' : 'play.png'));
const nextIcon = nativeImage.createFromPath(path.join(__dirname, '..', 'icons', 'next.png'));
const prevIcon = nativeImage.createFromPath(path.join(__dirname, '..', 'icons', 'prev.png'));
const likeIcon = nativeImage.createFromPath(path.join(__dirname, '..', 'icons', state.isLiked ? 'unlike.png' : 'like.png'));
mainWindow.setThumbarButtons([
{
tooltip: 'Previous',
icon: prevIcon,
click: () => mainWindow.webContents.send('crosssound-media-action', { action: 'previous' })
},
{
tooltip: state.isPlaying ? 'Pause' : 'Play',
icon: playIcon,
click: () => mainWindow.webContents.send('crosssound-media-action', { action: 'playpause' })
},
{
tooltip: 'Next',
icon: nextIcon,
click: () => mainWindow.webContents.send('crosssound-media-action', { action: 'next' })
},
{
tooltip: state.isLiked ? 'Unlike' : 'Like',
icon: likeIcon,
click: () => mainWindow.webContents.send('crosssound-media-action', { action: 'like' })
}
]);
}
function tryRegister(key, handler) {
if (!key) return;
try {
globalShortcut.register(key, handler);
} catch (e) {
console.error(`Failed to register hotkey "${key}":`, e);
}
}
function registerMediaKeys(mainWindow, hotkeys = {}, torCallbacks = {}) {
const send = (action) => () => mainWindow?.webContents.send('crosssound-media-action', { action });
tryRegister(hotkeys.playpause || 'MediaPlayPause', send('playpause'));
tryRegister(hotkeys.next || 'MediaNextTrack', send('next'));
tryRegister(hotkeys.previous || 'MediaPreviousTrack', send('previous'));
tryRegister(hotkeys.like || 'F23', send('like'));
if (hotkeys.newcircuit) tryRegister(hotkeys.newcircuit, torCallbacks.newCircuit || (() => {}));
if (hotkeys.toggletor) tryRegister(hotkeys.toggletor, torCallbacks.toggleTor || (() => {}));
}
function reregisterMediaKeys(mainWindow, hotkeys, torCallbacks) {
globalShortcut.unregisterAll();
registerMediaKeys(mainWindow, hotkeys, torCallbacks);
}
function unregisterMediaKeys() {
globalShortcut.unregisterAll();
}
module.exports = {
updateTaskbarButtons,
registerMediaKeys,
reregisterMediaKeys,
unregisterMediaKeys,
};