mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-14 03:41:46 +00:00
refactor notifications plugin
This commit is contained in:
@ -3,7 +3,7 @@ const is = require("electron-is");
|
|||||||
const registerCallback = require("../../providers/song-info");
|
const registerCallback = require("../../providers/song-info");
|
||||||
const { notificationImage } = require("./utils");
|
const { notificationImage } = require("./utils");
|
||||||
|
|
||||||
const { setupInteractive, notifyInteractive } = require("./interactive")
|
const setupInteractive = require("./interactive")
|
||||||
|
|
||||||
const notify = (info, options) => {
|
const notify = (info, options) => {
|
||||||
|
|
||||||
@ -23,37 +23,22 @@ const notify = (info, options) => {
|
|||||||
return currentNotification;
|
return currentNotification;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = (win, options) => {
|
const setup = (options) => {
|
||||||
const isInteractive = is.windows() && options.interactive;
|
|
||||||
//setup interactive notifications for windows
|
|
||||||
if (isInteractive) {
|
|
||||||
setupInteractive(win, options.unpauseNotification);
|
|
||||||
}
|
|
||||||
let oldNotification;
|
let oldNotification;
|
||||||
let oldURL = "";
|
|
||||||
win.once("ready-to-show", () => {
|
registerCallback(songInfo => {
|
||||||
// Register the callback for new song information
|
if (!songInfo.isPaused || options.unpauseNotification) {
|
||||||
registerCallback(songInfo => {
|
// Close the old notification
|
||||||
// on pause - reset url? and skip notification
|
oldNotification?.close();
|
||||||
if (songInfo.isPaused) {
|
// This fixes a weird bug that would cause the notification to be updated instead of showing
|
||||||
//reset oldURL if unpause notification option is on
|
setTimeout(() => { oldNotification = notify(songInfo, options) }, 10);
|
||||||
if (options.unpauseNotification) {
|
}
|
||||||
oldURL = "";
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// If url isn't the same as last one - send notification
|
|
||||||
if (songInfo.url !== oldURL) {
|
|
||||||
oldURL = songInfo.url;
|
|
||||||
if (isInteractive) {
|
|
||||||
notifyInteractive(songInfo);
|
|
||||||
} else {
|
|
||||||
// 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, options) }, 10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = (win, options) => {
|
||||||
|
// Register the callback for new song information
|
||||||
|
is.windows() && options.interactive ?
|
||||||
|
setupInteractive(win, options.unpauseNotification) :
|
||||||
|
setup(options);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,18 +1,25 @@
|
|||||||
const { notificationImage, icons } = require("./utils");
|
const { notificationImage, icons } = require("./utils");
|
||||||
const getSongControls = require('../../providers/song-controls');
|
const getSongControls = require('../../providers/song-controls');
|
||||||
|
const registerCallback = require("../../providers/song-info");
|
||||||
const notifier = require("node-notifier");
|
const notifier = require("node-notifier");
|
||||||
|
|
||||||
//store song controls reference on launch
|
//store song controls reference on launch
|
||||||
let controls;
|
let controls;
|
||||||
let notificationOnPause;
|
let notificationOnPause;
|
||||||
|
|
||||||
//Save controls and onPause option
|
module.exports = (win, unpauseNotification) => {
|
||||||
module.exports.setupInteractive = (win, unpauseNotification) => {
|
//Save controls and onPause option
|
||||||
const { playPause, next, previous } = getSongControls(win);
|
const { playPause, next, previous } = getSongControls(win);
|
||||||
controls = { playPause, next, previous };
|
controls = { playPause, next, previous };
|
||||||
|
|
||||||
notificationOnPause = unpauseNotification;
|
notificationOnPause = unpauseNotification;
|
||||||
|
|
||||||
|
// Register songInfoCallback
|
||||||
|
registerCallback(songInfo => {
|
||||||
|
if (!songInfo.isPaused || notificationOnPause) {
|
||||||
|
sendToaster(songInfo);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
win.webContents.once("closed", () => {
|
win.webContents.once("closed", () => {
|
||||||
deleteNotification()
|
deleteNotification()
|
||||||
});
|
});
|
||||||
@ -33,7 +40,7 @@ function deleteNotification() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//New notification
|
//New notification
|
||||||
module.exports.notifyInteractive = function sendToaster(songInfo) {
|
function sendToaster(songInfo) {
|
||||||
deleteNotification();
|
deleteNotification();
|
||||||
//download image and get path
|
//download image and get path
|
||||||
let imgSrc = notificationImage(songInfo, true);
|
let imgSrc = notificationImage(songInfo, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user