Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #179 from zulip/window-close-fix
Browse files Browse the repository at this point in the history
Fixed window close/hide logic + keep app running in background on close
  • Loading branch information
akashnimare authored Jun 16, 2017
2 parents 68c9043 + c2e6e96 commit 48ff506
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
38 changes: 27 additions & 11 deletions app/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -46,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
Expand Down Expand Up @@ -79,7 +75,19 @@ 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();

if (process.platform === 'darwin') {
app.hide();
} else {
win.hide();
}
}
});

win.setTitle('Zulip');

// Let's save browser window position
Expand Down Expand Up @@ -136,9 +144,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', () => {
Expand All @@ -150,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;

Expand Down Expand Up @@ -185,6 +189,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();
});
Expand All @@ -201,3 +213,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;
});
11 changes: 10 additions & 1 deletion app/renderer/js/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ const createTray = function () {
{
type: 'separator'
},
{
label: 'Focus',
click() {
ipcRenderer.send('focus-app');
}
},
{
type: 'separator'
},
{
label: 'Manage Zulip servers',
click() {
Expand All @@ -144,7 +153,7 @@ const createTray = function () {
{
label: 'Quit',
click() {
remote.getCurrentWindow().close();
ipcRenderer.send('quit-app');
}
}
]);
Expand Down

0 comments on commit 48ff506

Please sign in to comment.