Behave like a single-window Mac app on Mac

This commit is contained in:
David Baker 2016-10-19 18:05:33 +01:00
parent 31607f0776
commit 3c55629208

View file

@ -2,6 +2,7 @@
const {app, BrowserWindow} = require('electron'); const {app, BrowserWindow} = require('electron');
let mainWindow = null; let mainWindow = null;
let appQuitting = false;
app.on('ready', () => { app.on('ready', () => {
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
@ -12,8 +13,26 @@ app.on('ready', () => {
mainWindow.on('closed', () => { mainWindow.on('closed', () => {
mainWindow = null; mainWindow = null;
}); });
mainWindow.on('close', (e) => {
if (process.platform == 'darwin' && !appQuitting) {
// On Mac, closing the window just hides it
// (this is generally how single-window Mac apps
// behave, eg. Mail.app)
e.preventDefault();
mainWindow.hide();
return false;
}
});
}); });
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
app.quit(); app.quit();
}); });
app.on('activate', () => {
mainWindow.show();
});
app.on('before-quit', () => {
appQuitting = true;
});