2015-12-02 06:46:08 +08:00
|
|
|
import {remote, ipcRenderer} from 'electron';
|
2015-11-07 02:47:48 +08:00
|
|
|
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());
|
|
|
|
|
2015-12-21 13:30:41 +08:00
|
|
|
// Check in case there is no menu for the current platform
|
2015-11-07 02:47:48 +08:00
|
|
|
const menu = this._store.menu();
|
|
|
|
if (menu != null) this._tray.setContextMenu(menu);
|
|
|
|
|
|
|
|
this._unsubscribe = this._addEventListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
_addEventListeners() {
|
2015-11-24 14:09:17 +08:00
|
|
|
this._tray.addListener('click', this._onClicked.bind(this));
|
|
|
|
const unsubClicked = ()=> this._tray.removeListener('click', this._onClicked);
|
2015-11-07 02:47:48 +08:00
|
|
|
const unsubStore = this._store.listen(this._onChange.bind(this));
|
|
|
|
return ()=> {
|
|
|
|
unsubClicked();
|
|
|
|
unsubStore();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_onClicked() {
|
|
|
|
if (this._platform !== 'darwin') {
|
2015-12-02 06:46:08 +08:00
|
|
|
ipcRenderer.send('command', 'application:show-main-window');
|
2015-11-07 02:47:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_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;
|