2015-11-07 02:47:48 +08:00
|
|
|
import path from 'path';
|
|
|
|
import remote from 'remote';
|
2015-11-14 10:11:44 +08:00
|
|
|
import ipc from 'ipc';
|
2015-11-07 02:47:48 +08:00
|
|
|
import NylasStore from 'nylas-store';
|
|
|
|
import {UnreadCountStore, CanvasUtils} from 'nylas-exports';
|
|
|
|
const NativeImage = remote.require('native-image');
|
|
|
|
const Menu = remote.require('menu');
|
|
|
|
const {canvasWithSystemTrayIconAndText} = CanvasUtils;
|
|
|
|
|
|
|
|
// Must be absolute real system path
|
|
|
|
// https://github.com/atom/electron/issues/1299
|
|
|
|
const BASE_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas.png');
|
|
|
|
const UNREAD_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas-unread.png');
|
|
|
|
|
|
|
|
const menuTemplate = [
|
|
|
|
{
|
|
|
|
label: 'New Message',
|
2015-11-14 11:41:53 +08:00
|
|
|
click: ()=> ipc.send('command', 'application:new-message'),
|
2015-11-07 02:47:48 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Preferences',
|
2015-11-14 11:41:53 +08:00
|
|
|
click: ()=> ipc.send('command', 'application:open-preferences'),
|
2015-11-07 02:47:48 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Quit N1',
|
2015-11-14 11:41:53 +08:00
|
|
|
click: ()=> ipc.send('command', 'application:quit'),
|
2015-11-07 02:47:48 +08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2015-11-12 05:44:37 +08:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
menuTemplate.unshift({
|
2015-11-14 10:11:44 +08:00
|
|
|
label: 'Open Inbox',
|
2015-11-14 11:41:53 +08:00
|
|
|
click: ()=> ipc.send('command', 'application:show-main-window'),
|
2015-11-12 05:44:37 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const _buildMenu = ()=> {
|
2015-11-07 02:47:48 +08:00
|
|
|
return Menu.buildFromTemplate(menuTemplate);
|
|
|
|
};
|
|
|
|
|
|
|
|
class TrayStore extends NylasStore {
|
|
|
|
|
|
|
|
constructor(platform) {
|
|
|
|
super();
|
|
|
|
this._platform = platform;
|
|
|
|
|
|
|
|
this._unreadIcon = NativeImage.createFromPath(UNREAD_ICON_PATH);
|
|
|
|
this._baseIcon = NativeImage.createFromPath(BASE_ICON_PATH);
|
|
|
|
this._unreadCount = UnreadCountStore.count() || 0;
|
|
|
|
this._menu = _buildMenu(platform);
|
|
|
|
this._icon = this._getIconImg();
|
|
|
|
this.listenTo(UnreadCountStore, this._onUnreadCountChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
unreadCount() {
|
|
|
|
return this._unreadCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
icon() {
|
|
|
|
return this._icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
tooltip() {
|
|
|
|
return `${this._unreadCount} unread messages`;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu() {
|
|
|
|
return this._menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getIconImg(platform = this._platform, unreadCount = this._unreadCount) {
|
|
|
|
const imgHandlers = {
|
|
|
|
'darwin': ()=> {
|
|
|
|
const img = new Image();
|
2015-11-18 07:36:52 +08:00
|
|
|
// toDataUrl always returns the @1x image data, so the assets/darwin/
|
|
|
|
// contains an "@2x" image /without/ the @2x extension
|
2015-11-07 02:47:48 +08:00
|
|
|
img.src = this._baseIcon.toDataUrl();
|
|
|
|
const count = this._unreadCount || '';
|
|
|
|
const canvas = canvasWithSystemTrayIconAndText(img, count.toString());
|
2015-11-18 07:36:52 +08:00
|
|
|
const pngData = NativeImage.createFromDataUrl(canvas.toDataURL()).toPng();
|
|
|
|
|
|
|
|
// creating from a buffer allows us to specify that the image is @2x
|
|
|
|
const out2x = NativeImage.createFromBuffer(pngData, 2);
|
|
|
|
out2x.setTemplateImage(true);
|
|
|
|
return out2x;
|
2015-11-07 02:47:48 +08:00
|
|
|
},
|
|
|
|
'default': ()=> {
|
|
|
|
return unreadCount > 0 ? this._unreadIcon : this._baseIcon;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return imgHandlers[platform in imgHandlers ? platform : 'default']();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onUnreadCountChanged() {
|
|
|
|
this._unreadCount = UnreadCountStore.count();
|
|
|
|
this._icon = this._getIconImg();
|
|
|
|
this.trigger();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TrayStore;
|