summaryrefslogtreecommitdiffhomepage
path: root/app/notification-controller.js
blob: 1d6138d404fdf529884fec879dca603cd1c1314b (plain)
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
// @flow

import { remote } from 'electron';

export default class NotificationController {
  _activeNotification: ?Notification;

  show(message: string) {
    const lastNotification = this._activeNotification;
    const sameAsLastNotification = lastNotification && lastNotification.body === message;

    if (sameAsLastNotification || remote.getCurrentWindow().isVisible()) {
      return;
    }

    const newNotification = new Notification(remote.app.getName(), { body: message, silent: true });

    this._activeNotification = newNotification;

    newNotification.addEventListener('show', () => {
      // If the notification is closed too soon, it might still get shown. If that happens, close()
      // should be called again so that it is closed immediately.
      if (this._activeNotification !== newNotification) {
        newNotification.close();
      }
    });

    if (lastNotification) {
      lastNotification.close();
    }
  }
}