forked from hankbao/electron-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
486 lines (435 loc) · 13.8 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
'use strict'
const path = require('path')
const async = require('async')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
// One animation at a time
const AnimationQueue = function(options) {
this.options = options
this.queue = []
this.running = false
}
AnimationQueue.prototype.push = function(object) {
if (this.running) {
this.queue.push(object)
} else {
this.running = true
this.animate(object)
}
}
AnimationQueue.prototype.animate = function(object) {
let self = this
object.func.apply(null, object.args)
.then(function() {
if (self.queue.length > 0) {
// Run next animation
self.animate.call(self, self.queue.shift())
} else {
self.running = false
}
})
.catch(function(err) {
log('electron-notify encountered an error!')
log('Please submit the error stack and code samples to: https://github.com/hankbao/electron-notify/issues')
log(err.stack)
})
}
AnimationQueue.prototype.clear = function() {
this.queue = []
}
let config = {
width: 300,
height: 65,
padding: 10,
borderRadius: 5,
displayTime: 5000,
animationSteps: 5,
animationStepMs: 5,
animateInParallel: true,
appIcon: null,
pathToModule: '',
logging: true,
defaultStyleContainer: {
backgroundColor: '#f0f0f0',
overflow: 'hidden',
padding: 8,
border: '1px solid #CCC',
fontFamily: 'Arial',
fontSize: 12,
position: 'relative',
lineHeight: '15px'
},
defaultStyleAppIcon: {
overflow: 'hidden',
float: 'left',
height: 40,
width: 40,
marginRight: 10,
},
defaultStyleImage: {
overflow: 'hidden',
float: 'right',
height: 40,
width: 40,
marginLeft: 10,
},
defaultStyleClose: {
position: 'absolute',
top: 1,
right: 3,
fontSize: 11,
color: '#CCC'
},
defaultStyleText: {
margin: 0,
overflow: 'hidden',
cursor: 'default'
},
defaultWindow: {
alwaysOnTop: true,
skipTaskbar: true,
resizable: false,
show: false,
frame: false,
transparent: true,
acceptFirstMouse: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
allowDisplayingInsecureContent: true
}
},
htmlTemplate: '<html>\n'
+ '<head></head>\n'
+ '<body style="overflow: hidden; -webkit-user-select: none;">\n'
+ '<div id="container">\n'
+ ' <img src="" id="appIcon" />\n'
+ ' <img src="" id="image" />\n'
+ ' <div id="text">\n'
+ ' <b id="title"></b>\n'
+ ' <p id="message"></p>\n'
+ ' </div>\n'
+ ' <div id="close">X</div>\n'
+ '</div>\n'
+ '</body>\n'
+ '</html>'
}
function setConfig(customConfig) {
config = Object.assign(config, customConfig)
calcDimensions()
}
function updateTemplatePath() {
let templatePath = path.join(__dirname, 'notification.html')
// Tricky stuff, sometimes this doesn't work,
// especially when webpack is involved.
// Check if we have a file at that location
try {
require('fs').statSync(templatePath).isFile()
} catch (err) {
log('electron-notify: Could not find template ("' + templatePath + '").')
log('electron-notify: To use a different template you need to correct the config.templatePath or simply adapt config.htmlTemplate')
// TODO: No file => should we create our own temporary notification.html?
}
config.templatePath = 'file://' + templatePath
return config.templatePath
}
function getTemplatePath() {
if (config.templatePath === undefined) {
return updateTemplatePath()
}
return config.templatePath
}
function setTemplatePath(path) {
config.templatePath = path
}
let nextInsertPos = {}
function calcDimensions() {
// Calc totalHeight & totalWidth
config.totalHeight = config.height + config.padding
config.totalWidth = config.width + config.padding
// Calc pos of first notification:
config.firstPos = {
x: config.lowerRightCorner.x - config.totalWidth,
y: config.lowerRightCorner.y - config.totalHeight
}
// Set nextInsertPos
nextInsertPos.x = config.firstPos.x
nextInsertPos.y = config.firstPos.y
}
function setupConfig() {
// Use primary display only
let display = electron.screen.getPrimaryDisplay()
// Display notifications starting from lower right corner
// Calc lower right corner
config.lowerRightCorner = {}
config.lowerRightCorner.x = display.bounds.x + display.workArea.x + display.workAreaSize.width
config.lowerRightCorner.y = display.bounds.y + display.workArea.y + display.workAreaSize.height
calcDimensions()
// Maximum amount of Notifications we can show:
config.maxVisibleNotifications = Math.floor(display.workAreaSize.height / config.totalHeight)
config.maxVisibleNotifications = config.maxVisibleNotifications > 7 ? 7 : config.maxVisibleNotifications
}
setupConfig()
// Array of windows with currently showing notifications
let activeNotifications = []
// Recycle windows
let inactiveWindows = []
// If we cannot show all notifications, queue them
let notificationQueue = []
// To prevent executing mutliple animations at once
let animationQueue = new AnimationQueue()
// To prevent double-close notification window
let closedNotifications = {}
// Give each notification a unique id
let latestID = 0
function notify(notification) {
// Is it an object and only one argument?
if (arguments.length === 1 && typeof notification === 'object') {
// Use object instead of supplied parameters
notification.id = latestID
latestID++
animationQueue.push({
func: showNotification,
args: [ notification ]
})
return notification.id
} else {
// Since 1.0.0 all notification parameters need to be passed
// as object.
log('electron-notify: ERROR notify() only accepts a single object with notification parameters.')
}
}
function showNotification(notificationObj) {
return new Promise(function(resolve) {
// Can we show it?
if (activeNotifications.length < config.maxVisibleNotifications) {
// Get inactiveWindow or create new:
getWindow().then(function(notificationWindow) {
// Move window to position
calcInsertPos()
notificationWindow.setPosition(nextInsertPos.x, nextInsertPos.y)
// Add to activeNotifications
activeNotifications.push(notificationWindow)
// Display time per notification basis.
let displayTime = notificationObj.displayTime ? notificationObj.displayTime : config.displayTime
// Set timeout to hide notification
let timeoutId
let closeFunc = buildCloseNotification(notificationWindow, notificationObj, function() {
return timeoutId
})
let closeNotificationSafely = buildCloseNotificationSafely(closeFunc)
timeoutId = setTimeout(function() {
closeNotificationSafely('timeout')
}, displayTime)
// Trigger onShowFunc if existent
if (notificationObj.onShowFunc) {
notificationObj.onShowFunc({
event: 'show',
id: notificationObj.id,
closeNotification: closeNotificationSafely
})
}
// Save onClickFunc in notification window
if (notificationObj.onClickFunc) {
notificationWindow.electronNotifyOnClickFunc = notificationObj.onClickFunc
} else {
delete notificationWindow.electronNotifyOnClickFunc
}
if (notificationObj.onCloseFunc) {
notificationWindow.electronNotifyOnCloseFunc = notificationObj.onCloseFunc
} else {
delete notificationWindow.electronNotifyOnCloseFunc
}
// Set contents, ...
notificationWindow.webContents.send('electron-notify-set-contents', notificationObj)
// Show window
notificationWindow.showInactive()
resolve(notificationWindow)
})
} else { // Add to notificationQueue
notificationQueue.push(notificationObj)
resolve()
}
})
}
// Close notification function
function buildCloseNotification(notificationWindow, notificationObj, getTimeoutId) {
return function(event) {
if (closedNotifications[notificationObj.id]) {
delete closedNotifications[notificationObj.id]
return new Promise(function(exitEarly) { exitEarly() })
} else {
closedNotifications[notificationObj.id] = true
}
if (notificationWindow.electronNotifyOnCloseFunc) {
notificationWindow.electronNotifyOnCloseFunc({
event: event,
id: notificationObj.id
})
delete notificationWindow.electronNotifyOnCloseFunc
}
// reset content
notificationWindow.webContents.send('electron-notify-reset')
if (getTimeoutId && typeof getTimeoutId === 'function') {
let timeoutId = getTimeoutId()
clearTimeout(timeoutId)
}
// Recycle window
let pos = activeNotifications.indexOf(notificationWindow)
activeNotifications.splice(pos, 1)
inactiveWindows.push(notificationWindow)
// Hide notification
notificationWindow.hide()
checkForQueuedNotifications()
// Move notifications down
return moveOneDown(pos)
}
}
// Always add to animationQueue to prevent erros (e.g. notification
// got closed while it was moving will produce an error)
function buildCloseNotificationSafely(closeFunc) {
return function(reason) {
if (reason === undefined)
reason = 'closedByAPI'
animationQueue.push({
func: closeFunc,
args: [ reason ]
})
}
}
ipc.on('electron-notify-close', function (event, winId, notificationObj) {
let closeFunc = buildCloseNotification(BrowserWindow.fromId(winId), notificationObj)
buildCloseNotificationSafely(closeFunc)('close')
})
ipc.on('electron-notify-click', function (event, winId, notificationObj) {
if (notificationObj.url) {
electron.shell.openExternal(notificationObj.url)
}
let notificationWindow = BrowserWindow.fromId(winId)
if (notificationWindow && notificationWindow.electronNotifyOnClickFunc) {
let closeFunc = buildCloseNotification(notificationWindow, notificationObj)
notificationWindow.electronNotifyOnClickFunc({
event: 'click',
id: notificationObj.id,
closeNotification: buildCloseNotificationSafely(closeFunc)
})
delete notificationWindow.electronNotifyOnClickFunc
}
})
/*
* Checks for queued notifications and add them
* to AnimationQueue if possible
*/
function checkForQueuedNotifications() {
if (notificationQueue.length > 0 &&
activeNotifications.length < config.maxVisibleNotifications) {
// Add new notification to animationQueue
animationQueue.push({
func: showNotification,
args: [ notificationQueue.shift() ]
})
}
}
/*
* Moves the notifications one position down,
* starting with notification at startPos
*
* @param {int} startPos
*/
function moveOneDown(startPos) {
return new Promise(function(resolve) {
if (startPos >= activeNotifications || startPos === -1) {
resolve()
return
}
// Build array with index of affected notifications
let notificationPosArray = []
for (let i = startPos; i < activeNotifications.length; i++) {
notificationPosArray.push(i)
}
// Start to animate all notifications at once or in parallel
let asyncFunc = async.map // Best performance
if (config.animateInParallel === false) {
asyncFunc = async.mapSeries // Sluggish
}
asyncFunc(notificationPosArray, moveNotificationAnimation, function() {
resolve()
})
})
}
function moveNotificationAnimation(i, done) {
// Get notification to move
let notificationWindow = activeNotifications[i]
// Calc new y position
let newY = config.lowerRightCorner.y - config.totalHeight * (i + 1)
// Get startPos, calc step size and start animationInterval
let startY = notificationWindow.getPosition()[1]
let step = (newY - startY) / config.animationSteps
let curStep = 1
let animationInterval = setInterval(function() {
// Abort condition
if (curStep === config.animationSteps) {
notificationWindow.setPosition(config.firstPos.x, newY)
clearInterval(animationInterval)
return done(null, 'done')
}
// Move one step down
notificationWindow.setPosition(config.firstPos.x, Math.trunc(startY + curStep * step))
curStep++
}, config.animationStepMs)
}
/*
* Find next possible insert position (on top)
*/
function calcInsertPos() {
if (activeNotifications.length < config.maxVisibleNotifications) {
nextInsertPos.y = config.lowerRightCorner.y - config.totalHeight * (activeNotifications.length + 1)
}
}
/*
* Get a window to display a notification. Use inactiveWindows or
* create a new window
* @return {Window}
*/
function getWindow() {
return new Promise(function(resolve) {
let notificationWindow
// Are there still inactiveWindows?
if (inactiveWindows.length > 0) {
notificationWindow = inactiveWindows.pop()
resolve(notificationWindow)
} else { // Or create a new window
let windowProperties = config.defaultWindow
windowProperties.width = config.width
windowProperties.height = config.height
notificationWindow = new BrowserWindow(windowProperties)
notificationWindow.setVisibleOnAllWorkspaces(true)
notificationWindow.loadURL(getTemplatePath())
notificationWindow.webContents.on('did-finish-load', function() {
// Done
notificationWindow.webContents.send('electron-notify-load-config', config)
resolve(notificationWindow)
})
}
})
}
function closeAll() {
// Clear out animation Queue and close windows
animationQueue.clear()
activeNotifications.forEach(window => window.close())
inactiveWindows.forEach(window => window.close())
// Reset certain vars
nextInsertPos = {}
activeNotifications = []
inactiveWindows = []
}
function log() {
if (config.logging === true) {
console.log.apply(console, arguments)
}
}
module.exports.notify = notify
module.exports.setConfig = setConfig
module.exports.getTemplatePath = getTemplatePath
module.exports.setTemplatePath = setTemplatePath
module.exports.closeAll = closeAll