Merge pull request #144 from semvis123/master

Reuse the same notification, instead of creating a new one each time the song changes.
This commit is contained in:
th-ch
2021-02-06 22:30:35 +01:00
committed by GitHub

View File

@ -1,8 +1,7 @@
const { Notification } = require("electron"); const { Notification } = require("electron");
const getSongInfo = require("../../providers/song-info"); const getSongInfo = require("../../providers/song-info");
const notify = (info) => { const notify = info => {
let notificationImage = "assets/youtube-music.png"; let notificationImage = "assets/youtube-music.png";
if (info.image) { if (info.image) {
@ -16,19 +15,26 @@ const notify = (info) => {
icon: notificationImage, icon: notificationImage,
silent: true, silent: true,
}; };
// Send the notification // Send the notification
new Notification(notification).show(); currentNotification = new Notification(notification);
currentNotification.show()
return currentNotification;
}; };
module.exports = (win) => { module.exports = (win) => {
const registerCallback = getSongInfo(win); const registerCallback = getSongInfo(win);
let oldNotification;
win.on("ready-to-show", () => { win.on("ready-to-show", () => {
// Register the callback for new song information // Register the callback for new song information
registerCallback((songInfo) => { registerCallback(songInfo => {
// If song is playing send notification // If song is playing send notification
if (!songInfo.isPaused) { if (!songInfo.isPaused) {
notify(songInfo); // Close the old notification
oldNotification?.close();
// This fixes a weird bug that would cause the notification to be updated instead of showing
setTimeout(()=>{ oldNotification = notify(songInfo) }, 10);
} }
}); });
}); });