|
1 | 1 | const { Notification } = require("electron");
|
2 |
| - |
3 | 2 | const getSongInfo = require("../../providers/song-info");
|
4 | 3 |
|
5 |
| -const notify = (info, notification) => { |
| 4 | +const notify = (info) => { |
6 | 5 | let notificationImage = "assets/youtube-music.png";
|
7 | 6 |
|
8 | 7 | if (info.image) {
|
9 | 8 | notificationImage = info.image.resize({ height: 256, width: 256 });
|
10 | 9 | }
|
11 | 10 |
|
12 | 11 | // Fill the notification with content
|
13 |
| - notification.title = info.title || "Playing"; |
14 |
| - notification.body = info.artist; |
15 |
| - notification.icon = notificationImage; |
16 |
| - |
| 12 | + const notification = { |
| 13 | + title: info.title || "Playing", |
| 14 | + body: info.artist, |
| 15 | + icon: notificationImage, |
| 16 | + silent: true, |
| 17 | + }; |
| 18 | + |
17 | 19 | // Send the notification
|
18 |
| - notification.show(); |
| 20 | + currentNotification = new Notification(notification); |
| 21 | + currentNotification.show() |
| 22 | + |
| 23 | + return currentNotification; |
19 | 24 | };
|
20 | 25 |
|
21 | 26 | module.exports = (win) => {
|
22 | 27 | const registerCallback = getSongInfo(win);
|
23 |
| - |
24 |
| - // Create a notification |
25 |
| - let notification = new Notification( { |
26 |
| - title: "", |
27 |
| - body: "", |
28 |
| - icon: "assets/youtube-music.png", |
29 |
| - silent: true, |
30 |
| - }); |
31 |
| - |
| 28 | + let oldNotification; |
32 | 29 | win.on("ready-to-show", () => {
|
33 | 30 | // Register the callback for new song information
|
34 | 31 | registerCallback((songInfo) => {
|
35 | 32 | // If song is playing send notification
|
36 |
| - if (!songInfo.isPaused) { |
37 |
| - notify(songInfo, notification); |
| 33 | + if (!songInfo.isPaused) { |
| 34 | + // Close the old notification |
| 35 | + oldNotification?.close(); |
| 36 | + // This fixes a weird bug that would cause the notification to be updated instead of showing |
| 37 | + setTimeout(()=>{ oldNotification = notify(songInfo) }, 1); |
38 | 38 | }
|
39 | 39 | });
|
40 | 40 | });
|
|
0 commit comments