Update tray script to support shown/hidden state on start-up.

This commit is contained in:
Zhongyi Tong
2017-07-03 01:44:10 +08:00
parent 81d3aa8a1b
commit 15b3af7b97

View File

@@ -9,6 +9,8 @@ const {Tray, Menu, nativeImage, BrowserWindow} = remote;
const APP_ICON = path.join(__dirname, '../../resources/tray', 'tray'); const APP_ICON = path.join(__dirname, '../../resources/tray', 'tray');
const ConfigUtil = require(__dirname + '/utils/config-util.js');
const iconPath = () => { const iconPath = () => {
if (process.platform === 'linux') { if (process.platform === 'linux') {
return APP_ICON + 'linux.png'; return APP_ICON + 'linux.png';
@@ -83,8 +85,6 @@ const renderCanvas = function (arg) {
} }
resolve(canvas); resolve(canvas);
} else {
reject(canvas);
} }
}); });
}; };
@@ -161,6 +161,8 @@ const createTray = function () {
}; };
ipcRenderer.on('destroytray', event => { ipcRenderer.on('destroytray', event => {
if (!window.tray) return;
window.tray.destroy(); window.tray.destroy();
if (window.tray.isDestroyed()) { if (window.tray.isDestroyed()) {
window.tray = null; window.tray = null;
@@ -172,6 +174,8 @@ ipcRenderer.on('destroytray', event => {
}); });
ipcRenderer.on('tray', (event, arg) => { ipcRenderer.on('tray', (event, arg) => {
if (!window.tray) return;
if (arg === 0) { if (arg === 0) {
unread = arg; unread = arg;
// Message Count // console.log("message count is zero."); // Message Count // console.log("message count is zero.");
@@ -186,21 +190,26 @@ ipcRenderer.on('tray', (event, arg) => {
} }
}); });
ipcRenderer.on('toggletray', event => { function toggleTray() {
if (event) { console.log(window.tray);
if (window.tray) { if (window.tray) {
window.tray.destroy(); window.tray.destroy();
if (window.tray.isDestroyed()) { if (window.tray.isDestroyed()) {
window.tray = null; window.tray = null;
} }
ConfigUtil.setConfigItem('trayIcon', false);
} else { } else {
createTray(); createTray();
renderNativeImage(unread).then(image => { renderNativeImage(unread).then(image => {
window.tray.setImage(image); window.tray.setImage(image);
window.tray.setToolTip(unread + ' unread messages'); window.tray.setToolTip(arg + ' unread messages');
}); });
ConfigUtil.setConfigItem('trayIcon', true);
} }
} }
});
createTray(); ipcRenderer.on('toggletray', toggleTray);
if (ConfigUtil.getConfigItem('trayIcon', true)) {
createTray();
}