electron-demo.js/notifications.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

var electron = require('electron');
var ipc = electron.ipcRenderer;
// This file runs in a render thread of the Electron app. It must be required (directly or
// indirectly) from one of the html files.
2017-04-24 21:11:42 +00:00
var count = 0;
var target = 0;
function notifyUser() {
ipc.send('notification', count, target);
count += 1;
var notif = new window.Notification('Annoying Alert ' + count, {
body: 'See what happens when you try to click on it.',
silent: true,
});
2017-04-24 21:11:42 +00:00
// On my system notifications are unclickable and seem to be unclosable as well,
// so not all of this code is fully tested.
notif.onclick = function () {
console.log('notification ' + count + ' clicked');
notif.close();
};
notif.onclose = function () {
if (count < target) {
notifyUser();
}
else {
count = 0;
target = 0;
ipc.send('notification', count, target);
}
};
}
2017-04-20 18:58:11 +00:00
2017-04-24 21:11:42 +00:00
function queueNotification() {
target += 1;
if (count === 0) {
notifyUser();
} else {
ipc.send('notification', count-1, target);
2017-04-20 18:58:11 +00:00
}
}
2017-04-24 21:11:42 +00:00
function clearQueue() {
target = count;
if (count > 0) {
ipc.send('notification', count-1, target);
}
}
document.body.addEventListener('click', function (ev) {
2017-04-24 21:11:42 +00:00
if (ev.target.matches('.notify .queue')) {
queueNotification();
}
2017-04-24 21:11:42 +00:00
else if (ev.target.matches('.notify .clear')) {
clearQueue();
}
});