mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-12 12:40:08 +08:00
a6c44b8529
Summary: - Updates support for ES6 code inside packages - Displays system tray icon with unread count on darwin, or with bubble on other platforms - Uses canvas api to dynamically generate icon image given unread count: - Adds CavasUtils.canvasFromImgAndText to do this - Adds config option to display system tray icon on darwin Test Plan: Need to write the tests for this. Reviewers: evan, bengotow Reviewed By: bengotow Differential Revision: https://phab.nylas.com/D2231
98 lines
2.6 KiB
JavaScript
98 lines
2.6 KiB
JavaScript
import path from 'path';
|
|
import remote from 'remote';
|
|
import NylasStore from 'nylas-store';
|
|
import {UnreadCountStore, CanvasUtils} from 'nylas-exports';
|
|
const WindowManager = remote.getGlobal('application').windowManager;
|
|
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',
|
|
click: ()=> WindowManager.sendToMainWindow('new-message'),
|
|
},
|
|
{
|
|
label: 'Preferences',
|
|
click: ()=> WindowManager.sendToMainWindow('open-preferences'),
|
|
},
|
|
{
|
|
type: 'separator',
|
|
},
|
|
{
|
|
label: 'Quit N1',
|
|
click: ()=> atom.quit(),
|
|
},
|
|
];
|
|
|
|
const _buildMenu = (platform)=> {
|
|
if (platform === 'darwin') {
|
|
menuTemplate.unshift({
|
|
label: 'Open inbox',
|
|
click: ()=> atom.focus(),
|
|
});
|
|
}
|
|
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();
|
|
// This is synchronous because it's a data url
|
|
img.src = this._baseIcon.toDataUrl();
|
|
const count = this._unreadCount || '';
|
|
const canvas = canvasWithSystemTrayIconAndText(img, count.toString());
|
|
return NativeImage.createFromDataUrl(canvas.toDataURL());
|
|
},
|
|
'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;
|