2016-02-24 15:25:04 +08:00
|
|
|
import path from 'path';
|
2016-03-15 08:15:34 +08:00
|
|
|
import {ipcRenderer} from 'electron';
|
|
|
|
import {UnreadBadgeStore} from 'nylas-exports';
|
2016-02-24 15:25:04 +08:00
|
|
|
|
|
|
|
// Must be absolute real system path
|
|
|
|
// https://github.com/atom/electron/issues/1299
|
2016-03-15 08:15:34 +08:00
|
|
|
const INBOX_ZERO_ICON = path.join(__dirname, '..', 'assets', 'MenuItem-Inbox-Zero.png');
|
|
|
|
const INBOX_UNREAD_ICON = path.join(__dirname, '..', 'assets', 'MenuItem-Inbox-Full.png');
|
|
|
|
const INBOX_UNREAD_ALT_ICON = path.join(__dirname, '..', 'assets', 'MenuItem-Inbox-Full-NewItems.png');
|
2016-02-24 15:25:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SystemTrayIconStore {
|
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
static INBOX_ZERO_ICON = INBOX_ZERO_ICON;
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
static INBOX_UNREAD_ICON = INBOX_UNREAD_ICON;
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
static INBOX_UNREAD_ALT_ICON = INBOX_UNREAD_ALT_ICON;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this._windowBlurred = false;
|
|
|
|
this._unsubscribers = [];
|
2016-02-24 15:25:04 +08:00
|
|
|
}
|
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
activate() {
|
|
|
|
this._updateIcon()
|
|
|
|
this._unsubscribers.push(UnreadBadgeStore.listen(this._updateIcon));
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
ipcRenderer.on('browser-window-blur', this._onWindowBlur)
|
|
|
|
ipcRenderer.on('browser-window-focus', this._onWindowFocus)
|
|
|
|
this._unsubscribers.push(() => ipcRenderer.removeListener('browser-window-blur', this._onWindowBlur))
|
|
|
|
this._unsubscribers.push(() => ipcRenderer.removeListener('browser-window-focus', this._onWindowFocus))
|
|
|
|
}
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
_getIconImageData(unreadCount, isWindowBlurred) {
|
|
|
|
if (unreadCount === 0) {
|
|
|
|
return {iconPath: INBOX_ZERO_ICON, isTemplateImg: true};
|
|
|
|
}
|
|
|
|
return isWindowBlurred ?
|
|
|
|
{iconPath: INBOX_UNREAD_ALT_ICON, isTemplateImg: false} :
|
|
|
|
{iconPath: INBOX_UNREAD_ICON, isTemplateImg: true};
|
|
|
|
}
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
_onWindowBlur = ()=> {
|
|
|
|
// Set state to blurred, but don't trigger a change. The icon should only be
|
|
|
|
// updated when the count changes
|
|
|
|
this._windowBlurred = true;
|
|
|
|
};
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
_onWindowFocus = ()=> {
|
|
|
|
// Make sure that as long as the window is focused we never use the alt icon
|
|
|
|
this._windowBlurred = false;
|
|
|
|
this._updateIcon();
|
|
|
|
};
|
2016-02-24 15:25:04 +08:00
|
|
|
|
2016-03-15 08:15:34 +08:00
|
|
|
_updateIcon = () => {
|
|
|
|
const count = UnreadBadgeStore.count()
|
|
|
|
const unreadString = (+count).toLocaleString();
|
|
|
|
const {iconPath, isTemplateImg} = this._getIconImageData(count, this._windowBlurred);
|
|
|
|
ipcRenderer.send('update-system-tray', iconPath, unreadString, isTemplateImg);
|
2016-02-24 15:25:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
deactivate() {
|
2016-03-15 08:15:34 +08:00
|
|
|
this._unsubscribers.forEach(unsub => unsub())
|
2016-02-24 15:25:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SystemTrayIconStore;
|