2015-10-30 06:30:18 -07:00
|
|
|
'use strict';
|
2015-10-23 09:44:10 -07:00
|
|
|
|
2016-06-12 05:06:49 -07:00
|
|
|
const {
|
|
|
|
app,
|
|
|
|
BrowserWindow,
|
|
|
|
Menu,
|
|
|
|
Tray,
|
|
|
|
ipcMain,
|
|
|
|
nativeImage,
|
2016-06-12 05:33:00 -07:00
|
|
|
dialog,
|
|
|
|
systemPreferences
|
2016-06-12 05:06:49 -07:00
|
|
|
} = require('electron');
|
2016-05-13 12:47:30 -07:00
|
|
|
|
2016-07-04 00:39:42 -07:00
|
|
|
process.on('uncaughtException', (error) => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
|
2016-06-10 17:17:18 -07:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
var cmd = process.argv[1];
|
|
|
|
if (cmd === '--squirrel-uninstall') {
|
|
|
|
var AutoLaunch = require('auto-launch');
|
|
|
|
var appLauncher = new AutoLaunch({
|
|
|
|
name: 'Mattermost'
|
|
|
|
});
|
|
|
|
appLauncher.isEnabled().then(function(enabled) {
|
|
|
|
if (enabled)
|
|
|
|
appLauncher.disable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require('electron-squirrel-startup');
|
2016-05-13 12:47:30 -07:00
|
|
|
|
2015-12-04 22:41:33 -08:00
|
|
|
const fs = require('fs');
|
2016-01-30 07:50:43 -08:00
|
|
|
const path = require('path');
|
2015-12-04 22:41:33 -08:00
|
|
|
|
2015-12-23 02:25:14 -08:00
|
|
|
var settings = require('./common/settings');
|
2016-06-16 06:10:55 -07:00
|
|
|
const osVersion = require('./common/osVersion');
|
2016-03-06 04:58:52 -08:00
|
|
|
var certificateStore = require('./main/certificateStore').load(path.resolve(app.getPath('userData'), 'certificate.json'));
|
2016-01-30 07:50:43 -08:00
|
|
|
var appMenu = require('./main/menus/app');
|
2016-05-23 07:16:26 -07:00
|
|
|
const allowProtocolDialog = require('./main/allowProtocolDialog');
|
2015-10-23 09:44:10 -07:00
|
|
|
|
2015-12-12 03:32:06 -08:00
|
|
|
var argv = require('yargs').argv;
|
|
|
|
|
2015-10-24 02:47:37 -07:00
|
|
|
var client = null;
|
2015-12-12 03:32:06 -08:00
|
|
|
if (argv.livereload) {
|
2015-10-24 02:47:37 -07:00
|
|
|
client = require('electron-connect').client.create();
|
2015-12-23 05:04:21 -08:00
|
|
|
client.on('reload', function() {
|
|
|
|
mainWindow.reload();
|
2015-10-24 02:47:37 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-12 03:32:06 -08:00
|
|
|
if (argv['config-file']) {
|
|
|
|
global['config-file'] = argv['config-file'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
global['config-file'] = app.getPath('userData') + '/config.json'
|
|
|
|
}
|
|
|
|
|
2016-01-14 05:50:12 -08:00
|
|
|
var config = {};
|
2015-12-23 02:25:14 -08:00
|
|
|
try {
|
|
|
|
var configFile = global['config-file'];
|
2016-01-14 05:50:12 -08:00
|
|
|
config = settings.readFileSync(configFile);
|
2015-12-23 02:25:14 -08:00
|
|
|
if (config.version != settings.version) {
|
|
|
|
config = settings.upgrade(config);
|
|
|
|
settings.writeFileSync(configFile, config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
2016-01-30 07:50:43 -08:00
|
|
|
config = settings.loadDefault();
|
2015-12-23 02:25:14 -08:00
|
|
|
console.log('Failed to read or upgrade config.json');
|
|
|
|
}
|
2016-06-17 07:50:41 -07:00
|
|
|
ipcMain.on('update-config', () => {
|
|
|
|
config = settings.readFileSync(configFile);
|
|
|
|
});
|
2015-12-23 02:25:14 -08:00
|
|
|
|
2016-06-12 05:33:00 -07:00
|
|
|
// Only for OS X
|
|
|
|
const switchMenuIconImages = function(icons, isDarkMode) {
|
|
|
|
if (isDarkMode) {
|
|
|
|
icons.normal = icons.clicked.normal;
|
|
|
|
icons.unread = icons.clicked.unread;
|
|
|
|
icons.mention = icons.clicked.mention;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
icons.normal = icons.light.normal;
|
|
|
|
icons.unread = icons.light.unread;
|
|
|
|
icons.mention = icons.light.mention;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
// Keep a global reference of the window object, if you don't, the window will
|
|
|
|
// be closed automatically when the JavaScript object is garbage collected.
|
|
|
|
var mainWindow = null;
|
2015-10-30 06:30:18 -07:00
|
|
|
var trayIcon = null;
|
2016-04-07 07:15:12 -07:00
|
|
|
const trayImages = function() {
|
|
|
|
switch (process.platform) {
|
|
|
|
case 'win32':
|
|
|
|
return {
|
2016-06-06 07:30:51 -07:00
|
|
|
normal: nativeImage.createFromPath(path.resolve(__dirname, 'resources/windows/tray.ico')),
|
|
|
|
unread: nativeImage.createFromPath(path.resolve(__dirname, 'resources/windows/tray_unread.ico')),
|
|
|
|
mention: nativeImage.createFromPath(path.resolve(__dirname, 'resources/windows/tray_mention.ico'))
|
2016-04-07 07:15:12 -07:00
|
|
|
};
|
|
|
|
case 'darwin':
|
2016-06-12 05:33:00 -07:00
|
|
|
const icons = {
|
|
|
|
light: {
|
|
|
|
normal: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/MenuIcon.png')),
|
|
|
|
unread: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/MenuIconUnread.png')),
|
|
|
|
mention: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/MenuIconMention.png'))
|
|
|
|
},
|
2016-06-12 04:51:43 -07:00
|
|
|
clicked: {
|
|
|
|
normal: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/ClickedMenuIcon.png')),
|
|
|
|
unread: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/ClickedMenuIconUnread.png')),
|
|
|
|
mention: nativeImage.createFromPath(path.resolve(__dirname, 'resources/osx/ClickedMenuIconMention.png'))
|
|
|
|
}
|
2016-04-07 07:15:12 -07:00
|
|
|
};
|
2016-06-12 05:33:00 -07:00
|
|
|
switchMenuIconImages(icons, systemPreferences.isDarkMode());
|
|
|
|
return icons;
|
2016-05-07 13:21:07 -07:00
|
|
|
case 'linux':
|
2016-05-07 16:01:56 -07:00
|
|
|
var resourcesDir = 'resources/linux/' + (config.trayIconTheme || 'light') + '/';
|
2016-05-07 13:39:14 -07:00
|
|
|
return {
|
|
|
|
normal: nativeImage.createFromPath(path.resolve(__dirname, resourcesDir + 'MenuIconTemplate.png')),
|
|
|
|
unread: nativeImage.createFromPath(path.resolve(__dirname, resourcesDir + 'MenuIconUnreadTemplate.png')),
|
|
|
|
mention: nativeImage.createFromPath(path.resolve(__dirname, resourcesDir + 'MenuIconMentionTemplate.png'))
|
|
|
|
};
|
2016-04-07 07:15:12 -07:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}();
|
2015-10-23 09:44:10 -07:00
|
|
|
var willAppQuit = false;
|
|
|
|
|
2016-04-11 05:51:24 -07:00
|
|
|
function shouldShowTrayIcon() {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-07 13:21:07 -07:00
|
|
|
if (['darwin', 'linux'].includes(process.platform) && config.showTrayIcon === true) {
|
2016-04-11 05:51:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
// Quit when all windows are closed.
|
|
|
|
app.on('window-all-closed', function() {
|
|
|
|
// On OS X it is common for applications and their menu bar
|
|
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// For win32, auto-hide menu bar.
|
|
|
|
app.on('browser-window-created', function(event, window) {
|
2016-01-23 02:59:39 -08:00
|
|
|
if (process.platform === 'win32' || process.platform === 'linux') {
|
2016-01-14 05:50:12 -08:00
|
|
|
if (config.hideMenuBar) {
|
|
|
|
window.setAutoHideMenuBar(true);
|
|
|
|
window.setMenuBarVisibility(false);
|
|
|
|
}
|
2015-10-23 09:44:10 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// For OSX, show hidden mainWindow when clicking dock icon.
|
|
|
|
app.on('activate', function(event) {
|
|
|
|
mainWindow.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('before-quit', function() {
|
|
|
|
willAppQuit = true;
|
|
|
|
});
|
|
|
|
|
2016-03-06 04:58:52 -08:00
|
|
|
app.on('certificate-error', function(event, webContents, url, error, certificate, callback) {
|
|
|
|
if (certificateStore.isTrusted(url, certificate)) {
|
|
|
|
event.preventDefault();
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var detail = `URL: ${url}\nError: ${error}`;
|
|
|
|
if (certificateStore.isExisting(url)) {
|
|
|
|
detail = `Certificate is different from previous one.\n\n` + detail;
|
|
|
|
}
|
|
|
|
|
2016-06-12 05:06:49 -07:00
|
|
|
dialog.showMessageBox(mainWindow, {
|
2016-03-06 04:58:52 -08:00
|
|
|
title: 'Certificate error',
|
|
|
|
message: `Do you trust certificate from "${certificate.issuerName}"?`,
|
|
|
|
detail: detail,
|
|
|
|
type: 'warning',
|
|
|
|
buttons: [
|
|
|
|
'Yes',
|
|
|
|
'No'
|
|
|
|
],
|
|
|
|
cancelId: 1
|
|
|
|
}, function(response) {
|
|
|
|
if (response === 0) {
|
|
|
|
certificateStore.add(url, certificate);
|
|
|
|
certificateStore.save();
|
|
|
|
webContents.loadURL(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-21 06:43:18 -07:00
|
|
|
const loginCallbackMap = new Map();
|
|
|
|
|
2016-06-12 05:06:49 -07:00
|
|
|
ipcMain.on('login-credentials', function(event, request, user, password) {
|
2016-04-21 06:43:18 -07:00
|
|
|
const callback = loginCallbackMap.get(JSON.stringify(request));
|
|
|
|
if (callback != null) {
|
|
|
|
callback(user, password);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('login', function(event, webContents, request, authInfo, callback) {
|
|
|
|
event.preventDefault();
|
|
|
|
loginCallbackMap.set(JSON.stringify(request), callback);
|
|
|
|
mainWindow.webContents.send('login-request', request, authInfo);
|
|
|
|
});
|
|
|
|
|
2016-05-23 07:16:26 -07:00
|
|
|
allowProtocolDialog.init(mainWindow);
|
2016-05-21 21:39:52 -07:00
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
// This method will be called when Electron has finished
|
|
|
|
// initialization and is ready to create browser windows.
|
|
|
|
app.on('ready', function() {
|
2016-04-11 05:51:24 -07:00
|
|
|
if (shouldShowTrayIcon()) {
|
2016-04-06 08:49:20 -07:00
|
|
|
// set up tray icon
|
2016-04-07 07:15:12 -07:00
|
|
|
trayIcon = new Tray(trayImages.normal);
|
2016-06-12 05:40:59 -07:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
trayIcon.setPressedImage(trayImages.clicked.normal);
|
|
|
|
systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', (event, userInfo) => {
|
|
|
|
switchMenuIconImages(trayImages, systemPreferences.isDarkMode());
|
|
|
|
trayIcon.setImage(trayImages.normal);
|
|
|
|
});
|
|
|
|
}
|
2016-06-12 05:33:00 -07:00
|
|
|
|
2015-10-30 06:30:18 -07:00
|
|
|
trayIcon.setToolTip(app.getName());
|
2015-11-20 01:04:20 -08:00
|
|
|
trayIcon.on('click', function() {
|
2016-07-05 09:37:18 -07:00
|
|
|
if (!mainWindow.isVisible() || mainWindow.isMinimized()) {
|
|
|
|
mainWindow.show();
|
|
|
|
mainWindow.focus();
|
|
|
|
if (process.platform === 'darwin') {
|
2016-06-30 13:17:31 -07:00
|
|
|
app.dock.show();
|
|
|
|
}
|
2016-07-05 09:37:18 -07:00
|
|
|
}
|
|
|
|
else if ((process.platform === 'win32') && config.toggleWindowOnTrayIconClick) {
|
|
|
|
mainWindow.minimize();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mainWindow.focus();
|
2016-06-30 13:17:31 -07:00
|
|
|
}
|
2015-11-06 06:39:44 -08:00
|
|
|
});
|
2016-06-30 13:17:31 -07:00
|
|
|
|
2016-05-23 07:46:24 -07:00
|
|
|
trayIcon.on('right-click', () => {
|
|
|
|
trayIcon.popUpContextMenu();
|
|
|
|
});
|
2015-11-20 01:04:20 -08:00
|
|
|
trayIcon.on('balloon-click', function() {
|
2016-07-03 05:58:48 -07:00
|
|
|
if (process.platform === 'win32' || process.platform === 'darwin') {
|
|
|
|
mainWindow.show();
|
2016-06-27 00:06:20 -07:00
|
|
|
}
|
2016-07-03 05:58:48 -07:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.show();
|
|
|
|
}
|
|
|
|
|
2015-11-03 02:15:24 -08:00
|
|
|
mainWindow.focus();
|
|
|
|
});
|
2016-06-12 05:06:49 -07:00
|
|
|
ipcMain.on('notified', function(event, arg) {
|
2016-06-16 06:10:55 -07:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
if (config.notifications.flashWindow === 2) {
|
|
|
|
mainWindow.flashFrame(true);
|
|
|
|
}
|
|
|
|
// On Windows 8.1 and Windows 8, a shortcut with a Application User Model ID must be installed to the Start screen.
|
|
|
|
// In current version, use tray balloon for notification
|
|
|
|
if (osVersion.isLowerThanOrEqualWindows8_1()) {
|
|
|
|
trayIcon.displayBalloon({
|
|
|
|
icon: path.resolve(__dirname, 'resources/appicon.png'),
|
|
|
|
title: arg.title,
|
|
|
|
content: arg.options.body
|
|
|
|
});
|
|
|
|
}
|
2016-06-05 03:50:15 -07:00
|
|
|
}
|
2015-10-30 06:30:18 -07:00
|
|
|
});
|
2016-02-11 07:44:16 -08:00
|
|
|
|
|
|
|
// Set overlay icon from dataURL
|
2016-02-25 05:21:28 -08:00
|
|
|
// Set trayicon to show "dot"
|
2016-06-12 05:06:49 -07:00
|
|
|
ipcMain.on('update-unread', function(event, arg) {
|
2016-04-06 08:49:20 -07:00
|
|
|
if (process.platform === 'win32') {
|
2016-06-12 05:06:49 -07:00
|
|
|
const overlay = arg.overlayDataURL ? nativeImage.createFromDataURL(arg.overlayDataURL) : null;
|
2016-04-06 08:49:20 -07:00
|
|
|
mainWindow.setOverlayIcon(overlay, arg.description);
|
|
|
|
}
|
2016-02-25 05:21:28 -08:00
|
|
|
|
2016-04-07 07:15:12 -07:00
|
|
|
if (arg.mentionCount > 0) {
|
|
|
|
trayIcon.setImage(trayImages.mention);
|
2016-06-12 05:40:59 -07:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
trayIcon.setPressedImage(trayImages.clicked.mention);
|
|
|
|
}
|
2016-06-03 05:45:02 -07:00
|
|
|
trayIcon.setToolTip(arg.mentionCount + ' unread mentions');
|
2016-04-07 07:15:12 -07:00
|
|
|
}
|
|
|
|
else if (arg.unreadCount > 0) {
|
|
|
|
trayIcon.setImage(trayImages.unread);
|
2016-06-12 05:40:59 -07:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
trayIcon.setPressedImage(trayImages.clicked.unread);
|
|
|
|
}
|
2016-06-03 05:45:02 -07:00
|
|
|
trayIcon.setToolTip(arg.unreadCount + ' unread channels');
|
2016-04-07 07:15:12 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
trayIcon.setImage(trayImages.normal);
|
2016-06-12 05:40:59 -07:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
trayIcon.setPressedImage(trayImages.clicked.normal);
|
|
|
|
}
|
2016-06-03 05:45:02 -07:00
|
|
|
trayIcon.setToolTip(app.getName());
|
2016-02-25 05:21:28 -08:00
|
|
|
}
|
2016-02-11 07:44:16 -08:00
|
|
|
});
|
2015-10-30 06:30:18 -07:00
|
|
|
}
|
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
// Create the browser window.
|
2016-02-14 03:03:25 -08:00
|
|
|
var bounds_info_path = path.resolve(app.getPath("userData"), "bounds-info.json");
|
2015-12-04 22:41:33 -08:00
|
|
|
var window_options;
|
|
|
|
try {
|
2016-02-14 03:03:25 -08:00
|
|
|
window_options = JSON.parse(fs.readFileSync(bounds_info_path, 'utf-8'));
|
2015-12-04 22:41:33 -08:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// follow Electron's defaults
|
2015-12-05 02:32:24 -08:00
|
|
|
window_options = {};
|
2015-12-04 22:41:33 -08:00
|
|
|
}
|
2016-07-12 09:08:57 -07:00
|
|
|
if (process.platform === 'win32' || process.platform === 'linux') {
|
|
|
|
// On HiDPI(125%) Windows environment, the taskbar icon is pixelated. So this line is necessary. See #192.
|
|
|
|
// As the side effect, #98 reoccurs.
|
2016-03-20 08:28:02 -07:00
|
|
|
window_options.icon = path.resolve(__dirname, 'resources/appicon.png');
|
|
|
|
}
|
2016-05-23 07:57:46 -07:00
|
|
|
window_options.title = app.getName();
|
2015-12-04 22:41:33 -08:00
|
|
|
mainWindow = new BrowserWindow(window_options);
|
2016-07-04 00:39:42 -07:00
|
|
|
|
|
|
|
mainWindow.webContents.on('crashed', () => {
|
|
|
|
console.log('The application has crashed.');
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.on('unresponsive', () => {
|
|
|
|
console.log('The application has become unresponsive.')
|
|
|
|
});
|
|
|
|
|
2016-02-18 07:59:40 -08:00
|
|
|
mainWindow.setFullScreenable(true); // fullscreenable option has no effect.
|
2016-01-12 05:08:24 -08:00
|
|
|
if (window_options.maximized) {
|
|
|
|
mainWindow.maximize();
|
|
|
|
}
|
2016-02-18 07:59:40 -08:00
|
|
|
if (window_options.fullscreen) {
|
|
|
|
mainWindow.setFullScreen(true);
|
|
|
|
}
|
2015-10-23 09:44:10 -07:00
|
|
|
|
|
|
|
// and load the index.html of the app.
|
2015-12-22 06:35:22 -08:00
|
|
|
mainWindow.loadURL('file://' + __dirname + '/browser/index.html');
|
2015-10-23 09:44:10 -07:00
|
|
|
|
2016-05-21 00:13:22 -07:00
|
|
|
// Set application menu
|
2016-06-12 05:06:49 -07:00
|
|
|
ipcMain.on('update-menu', (event, config) => {
|
2016-05-21 00:13:22 -07:00
|
|
|
var app_menu = appMenu.createMenu(mainWindow, config);
|
|
|
|
Menu.setApplicationMenu(app_menu);
|
2016-07-03 13:41:15 -07:00
|
|
|
// set up context menu for tray icon
|
|
|
|
if (shouldShowTrayIcon()) {
|
|
|
|
const tray_menu = require('./main/menus/tray').createMenu(mainWindow, config);
|
|
|
|
trayIcon.setContextMenu(tray_menu);
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
// store the information, if the tray was initialized, for checking in the settings, if the application
|
|
|
|
// was restarted after setting "Show icon on menu bar"
|
|
|
|
if (trayIcon)
|
|
|
|
mainWindow.trayWasVisible = true;
|
|
|
|
else
|
|
|
|
mainWindow.trayWasVisible = false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-21 00:13:22 -07:00
|
|
|
});
|
2016-06-12 05:06:49 -07:00
|
|
|
ipcMain.emit('update-menu', true, config);
|
2016-05-21 00:13:22 -07:00
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
// Open the DevTools.
|
|
|
|
// mainWindow.openDevTools();
|
|
|
|
|
2016-01-12 05:08:24 -08:00
|
|
|
var saveWindowState = function(file, window) {
|
|
|
|
var window_state = window.getBounds();
|
|
|
|
window_state.maximized = window.isMaximized();
|
|
|
|
window_state.fullscreen = window.isFullScreen();
|
2016-05-03 08:36:54 -07:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(bounds_info_path, JSON.stringify(window_state));
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// [Linux] error happens only when the window state is changed before the config dir is creatied.
|
|
|
|
}
|
2016-01-12 05:08:24 -08:00
|
|
|
};
|
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
mainWindow.on('close', function(event) {
|
2015-12-05 02:32:24 -08:00
|
|
|
if (willAppQuit) { // when [Ctrl|Cmd]+Q
|
2016-01-12 05:08:24 -08:00
|
|
|
saveWindowState(bounds_info_path, mainWindow);
|
2015-12-05 02:32:24 -08:00
|
|
|
}
|
|
|
|
else { // Minimize or hide the window for close button.
|
2015-10-23 09:44:10 -07:00
|
|
|
event.preventDefault();
|
|
|
|
switch (process.platform) {
|
|
|
|
case 'win32':
|
2016-07-03 05:58:48 -07:00
|
|
|
mainWindow.hide();
|
2016-06-26 23:09:52 -07:00
|
|
|
break;
|
2015-11-10 05:18:14 -08:00
|
|
|
case 'linux':
|
2016-07-05 07:49:10 -07:00
|
|
|
if (config.minimizeToTray) {
|
|
|
|
mainWindow.hide();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mainWindow.minimize();
|
|
|
|
}
|
2015-10-23 09:44:10 -07:00
|
|
|
break;
|
|
|
|
case 'darwin':
|
2016-07-03 05:58:48 -07:00
|
|
|
mainWindow.hide();
|
2016-07-03 07:12:04 -07:00
|
|
|
if (config.minimizeToTray) {
|
|
|
|
app.dock.hide();
|
|
|
|
}
|
2015-10-23 09:44:10 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-04 22:41:33 -08:00
|
|
|
// App should save bounds when a window is closed.
|
|
|
|
// However, 'close' is not fired in some situations(shutdown, ctrl+c)
|
|
|
|
// because main process is killed in such situations.
|
|
|
|
// 'blur' event was effective in order to avoid this.
|
|
|
|
// Ideally, app should detect that OS is shutting down.
|
|
|
|
mainWindow.on('blur', function() {
|
2016-01-12 05:08:24 -08:00
|
|
|
saveWindowState(bounds_info_path, mainWindow);
|
2015-12-04 22:41:33 -08:00
|
|
|
});
|
|
|
|
|
2015-10-23 09:44:10 -07:00
|
|
|
// Emitted when the window is closed.
|
|
|
|
mainWindow.on('closed', function() {
|
|
|
|
// Dereference the window object, usually you would store windows
|
|
|
|
// in an array if your app supports multi windows, this is the time
|
|
|
|
// when you should delete the corresponding element.
|
|
|
|
mainWindow = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Deny drag&drop navigation in mainWindow.
|
|
|
|
// Drag&drop is allowed in webview of index.html.
|
|
|
|
mainWindow.webContents.on('will-navigate', function(event, url) {
|
|
|
|
var dirname = __dirname;
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
dirname = '/' + dirname.replace(/\\/g, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
var index = url.indexOf('file://' + dirname);
|
|
|
|
if (index !== 0) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|