|
1 |
| -const { nativeImage, Notification } = require("electron"); |
2 |
| - |
3 |
| -const { listenAction } = require("../utils"); |
4 |
| -const { ACTIONS, CHANNEL } = require("./actions.js"); |
| 1 | +const {Notification} = require('electron'); |
5 | 2 |
|
6 | 3 | function notify(info) {
|
7 |
| - let notificationImage = "assets/youtube-music.png"; |
| 4 | + let notificationImage = 'assets/youtube-music.png'; |
| 5 | + |
8 | 6 | if (info.image) {
|
9 |
| - notificationImage = nativeImage.createFromDataURL(info.image); |
| 7 | + notificationImage = info.image.resize({height: 256, width: 256}); |
10 | 8 | }
|
11 | 9 |
|
| 10 | + // Fill the notification with content |
12 | 11 | const notification = {
|
13 |
| - title: info.title || "Playing", |
| 12 | + title: info.title || 'Playing', |
14 | 13 | body: info.artist,
|
15 | 14 | icon: notificationImage,
|
16 |
| - silent: true, |
| 15 | + silent: true |
17 | 16 | };
|
| 17 | + // Send the notification |
18 | 18 | new Notification(notification).show();
|
19 | 19 | }
|
20 | 20 |
|
21 |
| -function listenAndNotify() { |
22 |
| - listenAction(CHANNEL, (event, action, imageSrc) => { |
23 |
| - switch (action) { |
24 |
| - case ACTIONS.NOTIFICATION: |
25 |
| - notify(imageSrc); |
26 |
| - break; |
27 |
| - default: |
28 |
| - console.log("Unknown action: " + action); |
29 |
| - } |
| 21 | +module.exports = win => { |
| 22 | + win.on('ready-to-show', () => { |
| 23 | + // Register the callback for new song information |
| 24 | + global.songInfo.onNewData(songInfo => { |
| 25 | + // If song is playing send notification |
| 26 | + if (!songInfo.isPaused) { |
| 27 | + notify(songInfo); |
| 28 | + } |
| 29 | + }); |
30 | 30 | });
|
31 |
| -} |
32 |
| - |
33 |
| -module.exports = listenAndNotify; |
| 31 | +}; |
0 commit comments