-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotifyManager.js
167 lines (141 loc) · 4.79 KB
/
NotifyManager.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const { BrowserWindow, screen, shell, ipcMain } = require('electron');
const Notify = require('./Notify');
const Extensions = require('./Extensions');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
module.exports = class NotifyManager {
/**
* @param {number} position
* 1 - bottom-right;
* 2 - top-right;
* 3 - top-left;
* 4 - bottom-left;
* @param {string} customStyle Your personal style for notifications
*/
constructor(position = 1, customStyle = '') {
this.loaded = false;
this.position = position;
this.onclickEvents = [];
this.activeNotifications = [];
const display = screen.getPrimaryDisplay();
this.win = new BrowserWindow({
width: 320,
height: display.workAreaSize.height + display.workArea.y,
skipTaskbar: true,
alwaysOnTop: true,
transparent: true,
show: false,
resizable: false,
minimizable: false,
fullscreenable: false,
focusable: false,
frame: false,
hasShadow: false,
x: (this.position == 1 || this.position == 2) ? display.workAreaSize.width + display.workArea.x - 320 : 0,
y: 0,
webPreferences: {
devTools: false,
preload: __dirname + '/files/preload.js'
}
});
this.win.setVisibleOnAllWorkspaces(true);
this.win.setIgnoreMouseEvents(true, { forward: true });
this.win.setFocusable(false);
(async () => {
await this.win.loadURL('file://' + __dirname + '/files/render.html');
this.win.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
this.win.send('load-position', this.position);
this.win.send('custom-style', customStyle);
this.loaded = true;
//this.win.webContents.openDevTools({mode: 'detach'});
ipcMain.on('notify-manager-set-visibly', (ev, boolean, focus) => {
if (ev.sender != this.win.webContents) {
return;
}
this.win.setIgnoreMouseEvents(!boolean, { forward: true });
if (boolean && focus) {
this.win.setFocusable(boolean);
} else {
this.win.setFocusable(false);
}
});
ipcMain.on('notify-manager-onclick', (ev, id) => {
if (ev.sender != this.win.webContents) {
return;
}
const element = this.onclickEvents.find(x => x.id == id);
if (!element) {
return;
}
try {
if (typeof element.event == 'function') {
element.event();
}
} catch (e) {
console.error(e);
}
});
ipcMain.on('notify-manager-destory', (ev, id) => {
if (ev.sender != this.win.webContents) {
return;
}
const notify = this.activeNotifications.find(x => x.id == id);
if (!notify) {
return;
}
Extensions.destroyNotify(notify, this);
});
})();
}
isLoaded() {
return this.loaded;
}
getWindow() {
return this.win;
}
/**
* @param {Notify} notify Notification to show
*/
async show(notify, onclick = null) {
while (!this.loaded) {
await delay(100);
}
this.win.showInactive();
this.win.send('show', {
id: notify.id,
title: notify.title,
body: notify.body,
time: notify.time,
image: notify.image,
sound: notify.sound,
}, !(!onclick));
this.activeNotifications.push(notify);
if (onclick) {
if (typeof onclick != 'function') {
console.error('onclick is not a function');
return notify;
}
const arrElement = {
event: onclick,
id: notify.id,
}
this.onclickEvents.push(arrElement);
}
return notify;
}
/**
* @param {Notify} notify Notification to destroy
*/
destroy(notify) {
if (!this.loaded) {
throw new Error('window not initialized yet');
}
this.win.send('destroy', {
id: notify.id,
sound: notify.sound
});
Extensions.destroyNotify(notify, this);
}
};