2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-11 07:41:04 +00:00

Hide app instead of exiting on OSX to fix #14

This commit is contained in:
Jia Hao 2016-01-21 12:25:33 +08:00
parent 877ee060f6
commit 5bceb97775

View File

@ -3,6 +3,7 @@
*/ */
var fs = require('fs'); var fs = require('fs');
var os = require('os');
var electron = require('electron'); var electron = require('electron');
var app = electron.app; var app = electron.app;
@ -17,13 +18,34 @@ var mainWindow = null;
var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8')); var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
app.on('window-all-closed', function() { app.on('window-all-closed', function () {
if (process.platform != 'darwin') { if (!isOSX()) {
app.quit(); app.quit();
} }
}); });
app.on('ready', function() { app.on('activate', function (event, hasVisibleWindows) {
if (isOSX()) {
// this is called when the dock is clicked
if (!hasVisibleWindows) {
mainWindow.show();
}
}
});
app.on('before-quit', () => {
// not fired when the close button on the window is clicked
if (isOSX()) {
// need to force a quit as a workaround here to simulate the osx app hiding behaviour
// Somehow sokution at https://github.com/atom/electron/issues/444#issuecomment-76492576 does not work,
// e.prevent default appears to persist
// might cause issues in the future as before-quit and will-quit events are not called
app.exit(0);
}
});
app.on('ready', function () {
mainWindow = new BrowserWindow( mainWindow = new BrowserWindow(
{ {
width: appArgs.width || 1280, width: appArgs.width || 1280,
@ -37,34 +59,37 @@ app.on('ready', function() {
buildMenu(app, mainWindow); buildMenu(app, mainWindow);
mainWindow.loadURL('file://' + __dirname + '/index.html'); mainWindow.loadURL('file://' + __dirname + '/index.html');
mainWindow.webContents.on('did-finish-load', function() { mainWindow.webContents.on('did-finish-load', function () {
mainWindow.webContents.send('params', JSON.stringify(appArgs)); mainWindow.webContents.send('params', JSON.stringify(appArgs));
}); });
// if the window is focused, clear the badge // if the window is focused, clear the badge
mainWindow.on('focus', function () { mainWindow.on('focus', function () {
if (process.platform === 'darwin') { if (isOSX()) {
app.dock.setBadge(''); app.dock.setBadge('');
} }
}); });
mainWindow.on('closed', function() { mainWindow.on('close', (e) => {
mainWindow = null; if (isOSX()) {
// this is called when exiting from clicking the cross button on the window
e.preventDefault();
mainWindow.hide();
}
}); });
}); });
// listen for a notification message // listen for a notification message
ipc.on('notification-message', function(event, arg) { ipc.on('notification-message', function (event, arg) {
if (arg === 'TITLE_CHANGED') { if (arg === 'TITLE_CHANGED') {
if (process.platform === 'darwin' && !mainWindow.isFocused()) { if (isOSX() && !mainWindow.isFocused()) {
app.dock.setBadge('●'); app.dock.setBadge('●');
} }
} }
}); });
app.on('window-all-closed', function() { function isOSX() {
app.quit(); return os.platform() === 'darwin';
}); }