-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
44 lines (41 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const { nativeImage, ipcMain } = require('electron');
const BadgeGenerator = require('./badge_generator.js');
const badgeDescription = 'New notification';
const UPDATE_BADGE_EVENT = 'update-badge';
let currentOverlayIcon = { image: null, badgeDescription };
module.exports = class Badge {
constructor(win, opts = {}) {
this.win = win;
this.opts = opts;
this.generator = new BadgeGenerator(win, opts);
this.initListeners();
this.win.on('closed', () => { this.win = null; });
this.win.on('show', () => { this.win.setOverlayIcon(currentOverlayIcon.image, currentOverlayIcon.badgeDescription); });
}
update(badgeNumber) {
if (badgeNumber) {
this.generator.generate(badgeNumber).then((base64) => {
const image = nativeImage.createFromDataURL(base64);
currentOverlayIcon = {
image,
badgeDescription
}
this.win.setOverlayIcon(currentOverlayIcon.image, currentOverlayIcon.badgeDescription);
});
} else {
currentOverlayIcon = {
image: null,
badgeDescription
}
this.win.setOverlayIcon(currentOverlayIcon.image, currentOverlayIcon.badgeDescription);
}
}
initListeners() {
ipcMain.on(UPDATE_BADGE_EVENT, (event, badgeNumber) => {
if (this.win) {
this.update(badgeNumber);
}
event.returnValue = 'success';
});
}
}