mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-16 21:58:39 -09:00
use electron notification api
This commit is contained in:
parent
a6ba442c1d
commit
a81ba84f1e
2 changed files with 4 additions and 43 deletions
|
|
@ -1,5 +0,0 @@
|
||||||
<script>
|
|
||||||
require('electron').ipcRenderer.on('notification', (ev, { title, body }) => {
|
|
||||||
new Notification(title, { body });
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
@ -1,44 +1,10 @@
|
||||||
import {resolve} from 'path';
|
import {Notification} from 'electron';
|
||||||
import {app, BrowserWindow} from 'electron';
|
import {icon} from './config/paths';
|
||||||
import isDev from 'electron-is-dev';
|
|
||||||
|
|
||||||
let win: BrowserWindow;
|
export default function notify(title: string, body = '', details: any = {}) {
|
||||||
|
|
||||||
// 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 :'(
|
|
||||||
|
|
||||||
let buffer: string[][] = [];
|
|
||||||
|
|
||||||
function notify(title: string, body = '', details: any = {}) {
|
|
||||||
console.log(`[Notification] ${title}: ${body}`);
|
console.log(`[Notification] ${title}: ${body}`);
|
||||||
if (details.error) {
|
if (details.error) {
|
||||||
console.error(details.error);
|
console.error(details.error);
|
||||||
}
|
}
|
||||||
if (win) {
|
new Notification({title, body, ...(process.platform === 'linux' && {icon})}).show();
|
||||||
win.webContents.send('notification', {title, body});
|
|
||||||
} else {
|
|
||||||
buffer.push([title, body]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.on('ready', () => {
|
|
||||||
const win_ = new BrowserWindow({
|
|
||||||
show: false,
|
|
||||||
webPreferences: {
|
|
||||||
nodeIntegration: true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const url = `file://${resolve(isDev ? __dirname : app.getAppPath(), 'notify.html')}`;
|
|
||||||
win_.loadURL(url);
|
|
||||||
win_.webContents.on('dom-ready', () => {
|
|
||||||
win = win_;
|
|
||||||
buffer.forEach(([title, body]) => {
|
|
||||||
notify(title, body);
|
|
||||||
});
|
|
||||||
buffer = [];
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
export default notify;
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue