const { notificationImage, icons, save_temp_icons } = require("./utils"); const getSongControls = require('../../providers/song-controls'); const registerCallback = require("../../providers/song-info"); const { changeProtocolHandler } = require("../../providers/protocol-handler"); const { Notification, app } = require("electron"); const path = require('path'); let songControls; let config; let savedNotification; module.exports = (win, _config) => { songControls = getSongControls(win); config = _config; if (app.isPackaged && !config.smallInteractive) save_temp_icons(); let lastSongInfo = { url: undefined }; // Register songInfoCallback registerCallback(songInfo => { if (!songInfo.isPaused && (songInfo.url !== lastSongInfo.url || config.unpauseNotification)) { lastSongInfo = { ...songInfo }; sendXML(songInfo); } }); //TODO on app before close, close notification app.once("before-quit", () => { savedNotification?.close(); }); changeProtocolHandler( (cmd) => { if (Object.keys(songControls).includes(cmd)) { songControls[cmd](); if (cmd === 'pause' || (cmd === 'play' && !config.unpauseNotification)) { setImmediate(() => sendXML({ ...lastSongInfo, isPaused: cmd === 'pause' }) ); } } } ) } function sendXML(songInfo) { const imgSrc = notificationImage(songInfo, true); savedNotification?.close(); savedNotification = new Notification({ title: songInfo.title || "Playing", body: songInfo.artist, icon: imgSrc, silent: true, // https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/schema-root // https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-schema // https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/adaptive-interactive-toasts?tabs=xml // https://learn.microsoft.com/en-us/uwp/api/windows.ui.notifications.toasttemplatetype toastXml: get_xml_custom(), }); savedNotification.on("close", (_) => { savedNotification = undefined; }); savedNotification.show(); } const iconLocation = app.isPackaged ? path.resolve(app.getPath("userData"), 'icons') : path.resolve(__dirname, '..', '..', 'assets/media-icons-black'); const getButton = (kind) => ``; const display = (kind) => config.smallInteractive ? `content="${icons[kind]}"` : `content="${kind.charAt(0).toUpperCase() + kind.slice(1)}" imageUri="file:///${path.resolve(__dirname, iconLocation, `${kind}.png`)}"`; const get_xml = (songInfo, options, imgSrc) => ` ` // **************************************************** // // PREMADE TEMPLATES FOR TESTING // DELETE AFTER TESTING // **************************************************** // const get_xml_custom = () => xml_banner_centered_top; const xml_logo_ascii = ` `; const xml_logo_icons_notext =` `; const buttons_icons = ` `; const xml_logo_icons = ` `; const xml_hero = ` `; const xml_banner_bottom = ` `; const xml_banner_top_custom = ` `; const xml_banner_centered_bottom = ` `; const xml_banner_centered_top = ` `;