Skip to content

Commit

Permalink
Merge pull request #212 from SapuSeven/master
Browse files Browse the repository at this point in the history
Add configurable notification urgency
  • Loading branch information
th-ch authored Mar 29, 2021
2 parents 64c2b32 + 1a338fb commit e551765
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
4 changes: 4 additions & 0 deletions config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const defaultConfig = {
activityTimoutEnabled: true, // if enabled, the discord rich presence gets cleared when music paused after the time specified below
activityTimoutTime: 10 * 60 * 1000 // 10 minutes
},
notifications: {
enabled: false,
urgency: "normal"
}
},
};

Expand Down
13 changes: 7 additions & 6 deletions plugins/notifications/back.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Notification } = require("electron");
const getSongInfo = require("../../providers/song-info");

const notify = info => {
const notify = (info, options) => {
let notificationImage = "assets/youtube-music.png";

if (info.image) {
Expand All @@ -14,27 +14,28 @@ const notify = info => {
body: info.artist,
icon: notificationImage,
silent: true,
urgency: options.urgency,
};

// Send the notification
currentNotification = new Notification(notification);
currentNotification.show()

return currentNotification;
};

module.exports = (win) => {
module.exports = (win, options) => {
const registerCallback = getSongInfo(win);
let oldNotification;
win.on("ready-to-show", () => {
// Register the callback for new song information
registerCallback(songInfo => {
// If song is playing send notification
if (!songInfo.isPaused) {
if (!songInfo.isPaused) {
// 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);
setTimeout(()=>{ oldNotification = notify(songInfo, options) }, 10);
}
});
});
Expand Down
13 changes: 13 additions & 0 deletions plugins/notifications/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {urgencyLevels, setUrgency} = require("./utils");

module.exports = (win, options) => [
{
label: "Notification Priority",
submenu: urgencyLevels.map(level => ({
label: level.name,
type: "radio",
checked: options.urgency === level.value,
click: () => setUrgency(options, level.value)
})),
},
];
11 changes: 11 additions & 0 deletions plugins/notifications/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {setOptions} = require("../../config/plugins");

module.exports.urgencyLevels = [
{name: "Low", value: "low"},
{name: "Normal", value: "normal"},
{name: "High", value: "critical"},
];
module.exports.setUrgency = (options, level) => {
options.urgency = level
setOptions("notifications", options)
};

0 comments on commit e551765

Please sign in to comment.