From 02bc5e41a5b12340822172a7cc7d932d31961199 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Fri, 16 Jun 2017 20:29:16 +0530 Subject: [PATCH 1/3] keep app running in background on close --- app/main/index.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/app/main/index.js b/app/main/index.js index 56e74311..4c628ac7 100644 --- a/app/main/index.js +++ b/app/main/index.js @@ -19,6 +19,8 @@ const conf = new Configstore(); // Prevent window being garbage collected let mainWindow; +let isQuitting = false; + // Load this url in main window const mainURL = 'file://' + path.join(__dirname, '../renderer', 'main.html'); @@ -46,11 +48,11 @@ const iconPath = () => { return APP_ICON + (process.platform === 'win32' ? '.ico' : '.png'); }; -function onClosed() { - // Dereference the window - // For multiple windows, store them in an array - mainWindow = null; -} +// function onClosed() { +// // Dereference the window +// // For multiple windows, store them in an array +// mainWindow = null; +// } function createMainWindow() { const win = new electron.BrowserWindow({ @@ -79,7 +81,19 @@ function createMainWindow() { win.loadURL(mainURL); - win.on('closed', onClosed); + // win.on('closed', onClosed); + win.on('close', e => { + if (!isQuitting) { + e.preventDefault(); + + if (process.platform === 'darwin') { + app.hide(); + } else { + win.hide(); + } + } + }); + win.setTitle('Zulip'); // Let's save browser window position @@ -136,9 +150,6 @@ app.on('certificate-error', (event, webContents, url, error, certificate, callba app.on('window-all-closed', () => { // Unregister all the shortcuts so that they don't interfare with other apps electronLocalshortcut.unregisterAll(mainWindow); - if (process.platform !== 'darwin') { - app.quit(); - } }); app.on('activate', () => { @@ -201,3 +212,7 @@ app.on('will-quit', () => { // Unregister all the shortcuts so that they don't interfare with other apps electronLocalshortcut.unregisterAll(mainWindow); }); + +app.on('before-quit', () => { + isQuitting = true; +}); From f4f4836887dde212cdc01665545b6b033d948abd Mon Sep 17 00:00:00 2001 From: akashnimare Date: Fri, 16 Jun 2017 20:39:16 +0530 Subject: [PATCH 2/3] add focus option in tray + handle quit event properly --- app/main/index.js | 8 ++++++++ app/renderer/js/tray.js | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/main/index.js b/app/main/index.js index 4c628ac7..f77cff65 100644 --- a/app/main/index.js +++ b/app/main/index.js @@ -196,6 +196,14 @@ app.on('ready', () => { mainWindow.webContents.send('destroytray'); }); + ipc.on('focus-app', () => { + mainWindow.show(); + }); + + ipc.on('quit-app', () => { + app.quit(); + }); + ipc.on('reload-main', () => { page.reload(); }); diff --git a/app/renderer/js/tray.js b/app/renderer/js/tray.js index 8c7aade7..4a5cc46f 100644 --- a/app/renderer/js/tray.js +++ b/app/renderer/js/tray.js @@ -123,6 +123,15 @@ const createTray = function () { { type: 'separator' }, + { + label: 'Focus', + click() { + ipcRenderer.send('focus-app'); + } + }, + { + type: 'separator' + }, { label: 'Manage Zulip servers', click() { @@ -144,7 +153,7 @@ const createTray = function () { { label: 'Quit', click() { - remote.getCurrentWindow().close(); + ipcRenderer.send('quit-app'); } } ]); From c2e6e9603f05110fa91327643dd79aced25d34a7 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Fri, 16 Jun 2017 21:45:45 +0530 Subject: [PATCH 3/3] fixed hide/quit logic #169, #160 --- app/main/index.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/app/main/index.js b/app/main/index.js index f77cff65..b4eef4cd 100644 --- a/app/main/index.js +++ b/app/main/index.js @@ -48,12 +48,6 @@ const iconPath = () => { return APP_ICON + (process.platform === 'win32' ? '.ico' : '.png'); }; -// function onClosed() { -// // Dereference the window -// // For multiple windows, store them in an array -// mainWindow = null; -// } - function createMainWindow() { const win = new electron.BrowserWindow({ // This settings needs to be saved in config @@ -81,7 +75,7 @@ function createMainWindow() { win.loadURL(mainURL); - // win.on('closed', onClosed); + // Keep the app running in background on close event win.on('close', e => { if (!isQuitting) { e.preventDefault(); @@ -161,7 +155,6 @@ app.on('activate', () => { app.on('ready', () => { electron.Menu.setApplicationMenu(appMenu); mainWindow = createMainWindow(); - // Not using for now // tray.create(); const page = mainWindow.webContents;