Refactor TrayIcon codes
This commit is contained in:
68
src/main.js
68
src/main.js
@@ -6,6 +6,7 @@ const BrowserWindow = electron.BrowserWindow; // Module to create native browser
|
|||||||
const Menu = electron.Menu;
|
const Menu = electron.Menu;
|
||||||
const Tray = electron.Tray;
|
const Tray = electron.Tray;
|
||||||
const ipc = electron.ipcMain;
|
const ipc = electron.ipcMain;
|
||||||
|
const nativeImage = electron.nativeImage;
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
@@ -48,6 +49,24 @@ catch (e) {
|
|||||||
// be closed automatically when the JavaScript object is garbage collected.
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
var mainWindow = null;
|
var mainWindow = null;
|
||||||
var trayIcon = null;
|
var trayIcon = null;
|
||||||
|
const trayImages = function() {
|
||||||
|
switch (process.platform) {
|
||||||
|
case 'win32':
|
||||||
|
return {
|
||||||
|
normal: nativeImage.createFromPath(path.resolve(__dirname, 'resources/tray.png')),
|
||||||
|
unread: nativeImage.createFromPath(path.resolve(__dirname, 'resources/tray_unread.png')),
|
||||||
|
mention: nativeImage.createFromPath(path.resolve(__dirname, 'resources/tray_mention.png'))
|
||||||
|
};
|
||||||
|
case 'darwin':
|
||||||
|
return {
|
||||||
|
normal: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/MenuIconTemplate.png')),
|
||||||
|
unread: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/MenuIconUnreadTemplate.png')),
|
||||||
|
mention: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/MenuIconMentionTemplate.png'))
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}();
|
||||||
var willAppQuit = false;
|
var willAppQuit = false;
|
||||||
|
|
||||||
app.on('login', function(event, webContents, request, authInfo, callback) {
|
app.on('login', function(event, webContents, request, authInfo, callback) {
|
||||||
@@ -127,19 +146,7 @@ app.on('certificate-error', function(event, webContents, url, error, certificate
|
|||||||
app.on('ready', function() {
|
app.on('ready', function() {
|
||||||
if (process.platform === 'win32' || process.platform === 'darwin') {
|
if (process.platform === 'win32' || process.platform === 'darwin') {
|
||||||
// set up tray icon
|
// set up tray icon
|
||||||
var tray_image = '';
|
trayIcon = new Tray(trayImages.normal);
|
||||||
switch (process.platform) {
|
|
||||||
case 'win32':
|
|
||||||
tray_image = path.resolve(__dirname, 'resources/tray.png');
|
|
||||||
break;
|
|
||||||
case 'darwin':
|
|
||||||
tray_image = path.resolve(__dirname, 'resources/osx/MenuIconTemplate.png');
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
process.exit(1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
trayIcon = new Tray(tray_image);
|
|
||||||
trayIcon.setToolTip(app.getName());
|
trayIcon.setToolTip(app.getName());
|
||||||
var tray_menu = require('./main/menus/tray').createDefault();
|
var tray_menu = require('./main/menus/tray').createDefault();
|
||||||
trayIcon.setContextMenu(tray_menu);
|
trayIcon.setContextMenu(tray_menu);
|
||||||
@@ -165,34 +172,15 @@ app.on('ready', function() {
|
|||||||
mainWindow.setOverlayIcon(overlay, arg.description);
|
mainWindow.setOverlayIcon(overlay, arg.description);
|
||||||
}
|
}
|
||||||
|
|
||||||
var tray_image = null;
|
if (arg.mentionCount > 0) {
|
||||||
switch (process.platform) {
|
trayIcon.setImage(trayImages.mention);
|
||||||
case 'win32':
|
}
|
||||||
if (arg.mentionCount > 0) {
|
else if (arg.unreadCount > 0) {
|
||||||
tray_image = 'tray_mention.png';
|
trayIcon.setImage(trayImages.unread);
|
||||||
}
|
}
|
||||||
else if (arg.unreadCount > 0) {
|
else {
|
||||||
tray_image = 'tray_unread.png';
|
trayIcon.setImage(trayImages.normal);
|
||||||
}
|
|
||||||
else {
|
|
||||||
tray_image = 'tray.png';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'darwin':
|
|
||||||
if (arg.mentionCount > 0) {
|
|
||||||
tray_image = 'osx/MenuIconMentionTemplate.png';
|
|
||||||
}
|
|
||||||
else if (arg.unreadCount > 0) {
|
|
||||||
tray_image = 'osx/MenuIconUnreadTemplate.png';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tray_image = 'osx/MenuIconTemplate.png';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
trayIcon.setImage(path.resolve(__dirname, 'resources', tray_image));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user