Mailspring/internal_packages/system-tray/lib/system-tray.es6
Juan Tejada a6c44b8529 feat(system-tray): add system-tray package
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
2015-11-06 11:12:38 -08:00

50 lines
1.1 KiB
JavaScript

import remote from 'remote';
import TrayStore from './tray-store';
const Tray = remote.require('tray');
class SystemTray {
constructor(platform) {
this._platform = platform;
this._store = new TrayStore(this._platform);
this._tray = new Tray(this._store.icon());
this._tray.setToolTip(this._store.tooltip());
const menu = this._store.menu();
if (menu != null) this._tray.setContextMenu(menu);
this._unsubscribe = this._addEventListeners();
}
_addEventListeners() {
this._tray.addListener('clicked', this._onClicked.bind(this));
const unsubClicked = ()=> this._tray.removeListener('clicked', this._onClicked);
const unsubStore = this._store.listen(this._onChange.bind(this));
return ()=> {
unsubClicked();
unsubStore();
};
}
_onClicked() {
if (this._platform !== 'darwin') {
atom.focus();
}
}
_onChange() {
const icon = this._store.icon();
const tooltip = this._store.tooltip();
this._tray.setImage(icon);
this._tray.setToolTip(tooltip);
}
destroy() {
this._tray.destroy();
this._unsubscribe();
}
}
export default SystemTray;