hyper/app/notify.js

43 lines
913 B
JavaScript
Raw Normal View History

const {resolve} = require('path');
const {app, BrowserWindow} = require('electron');
2016-07-07 12:48:43 -08:00
const isDev = require('electron-is-dev');
let win;
// the hack of all hacks
// electron doesn't have a built in notification thing,
// so we launch a window on which we can use the
// HTML5 `Notification` API :'(
2016-07-07 14:17:11 -08:00
let buffer = [];
2016-07-07 12:48:43 -08:00
app.on('ready', () => {
const win_ = new BrowserWindow({
show: false
});
const url = 'file://' + resolve(
isDev ? __dirname : app.getAppPath(),
'notify.html'
);
win_.loadURL(url);
2016-07-07 14:17:11 -08:00
win_.webContents.on('dom-ready', () => {
win = win_;
buffer.forEach(([title, body]) => {
notify(title, body);
});
buffer = null;
});
2016-07-07 12:48:43 -08:00
});
function notify(title, body) {
2016-07-13 12:44:24 -08:00
console.log(`[Notification] ${title}: ${body}`);
2016-07-07 12:48:43 -08:00
if (win) {
win.webContents.send('notification', {title, body});
2016-07-07 14:17:11 -08:00
} else {
buffer.push([title, body]);
2016-07-07 12:48:43 -08:00
}
2016-07-07 14:17:11 -08:00
}
module.exports = notify;