Skip to content

Commit

Permalink
Merge pull request #324 from Concordium/fix-macos-window-closed-issue
Browse files Browse the repository at this point in the history
Cleanup handlers and listeners on all windows closed
  • Loading branch information
orhoj authored Jun 20, 2022
2 parents 2f53bd9 + a7a0b70 commit 7f934f1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.4.2

### Fixed

- Fixed an issue on macOS where an error popup would show after closing the main application window and opening it again.
- Fixed an issue on macOS where identity creation was not possible after closing the main application window and opening it again.

## 1.4.1

### Added
Expand Down
26 changes: 19 additions & 7 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import path from 'path';
import { app, BrowserView, BrowserWindow, shell } from 'electron';
import ipcRendererCommands from './constants/ipcRendererCommands.json';
import { createMenu, addContextMenu } from './main/menu';
import initializeIpcHandlers from './main/ipcHandlers';
import initAutoUpdate from './main/autoUpdate';
import {
initializeIpcHandlers,
removeAllIpcHandlers,
} from './main/ipcHandlers';
import {
initAutoUpdate,
removeAutoUpdateHandlersAndListeners,
} from './main/autoUpdate';

let mainWindow: BrowserWindow | null = null;
let printWindow: BrowserWindow | null = null;
Expand Down Expand Up @@ -170,16 +176,22 @@ const createWindow = async () => {
initializeIpcHandlers(mainWindow, printWindow, browserView);
};

const isMac = process.platform === 'darwin';

app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
if (process.platform !== 'darwin') {
// Respect the macOS convention of having the application in memory even
// after all windows have been closed.
if (!isMac) {
app.quit();
} else {
// On macOS we have to clear all ipc handlers when all windows are closed,
// as we need to spawn new handlers that are connected to the newly created
// windows to avoid referencing destroyed objects.
removeAllIpcHandlers();
removeAutoUpdateHandlersAndListeners();
}
});

const isMac = process.platform === 'darwin';

if (process.env.E2E_BUILD === 'true') {
// eslint-disable-next-line promise/catch-or-return
app.whenReady().then(createWindow);
Expand Down
2 changes: 1 addition & 1 deletion app/main/autoUpdate/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './init';
export { initAutoUpdate, removeAutoUpdateHandlersAndListeners } from './init';
21 changes: 20 additions & 1 deletion app/main/autoUpdate/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,26 @@ const handleUpdateDownloaded = (mainWindow: BrowserWindow) => async (
}
};

export default function initAutoUpdate(mainWindow: BrowserWindow) {
/**
* Removes all ipcMain handlers and auto update listeners added by
* 'initAutoUpdate'.
*/
export function removeAutoUpdateHandlersAndListeners() {
autoUpdater.removeAllListeners('update-available');
autoUpdater.removeAllListeners('error');
autoUpdater.removeAllListeners('update-downloaded');
ipcMain.removeHandler(triggerAppUpdate);
ipcMain.removeHandler(quitAndInstallUpdate);
}

/**
* Initializes the auto update functionality by setting up handlers
* and auto update listeners.
*
* If adding a new handler to this method, then ensure that 'removeAutoUpdateHandlersAndListeners'
* above is updated to keep in sync with this method.
*/
export function initAutoUpdate(mainWindow: BrowserWindow) {
const canAutoUpdate =
process.platform === 'win32' ||
process.platform === 'darwin' ||
Expand Down
24 changes: 23 additions & 1 deletion app/main/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,29 @@ export function saveFileDialog(opts: SaveDialogOptions) {
return dialog.showSaveDialog(opts);
}

export default function initializeIpcHandlers(
/**
* Removes all ipcMain handlers registered by 'initializeIpcHandler'.
*/
export function removeAllIpcHandlers() {
ipcMain.removeHandler(ipcCommands.getUserDataPath);
ipcMain.removeHandler(ipcCommands.print);
ipcMain.removeHandler(ipcCommands.openUrl);
ipcMain.removeHandler(ipcCommands.httpsGet);
ipcMain.removeHandler(ipcCommands.createView);
ipcMain.removeHandler(ipcCommands.removeView);
ipcMain.removeHandler(ipcCommands.resizeView);
ipcMain.removeHandler(ipcCommands.saveFileDialog);
ipcMain.removeHandler(ipcCommands.openFileDialog);
ipcMain.removeHandler(ipcCommands.messageBox);
}

/**
* Initializes ipcMain handlers.
*
* If adding a new handler to this method, then ensure that 'removeAllIpcHandlers'
* above is updated to keep in sync with this method.
*/
export function initializeIpcHandlers(
mainWindow: BrowserWindow,
printWindow: BrowserWindow,
browserView: BrowserView
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "concordium-desktop-wallet",
"productName": "concordium-desktop-wallet",
"description": "concordium-desktop-wallet",
"version": "1.4.1",
"version": "1.4.2",
"main": "./main.prod.js",
"author": {
"name": "Concordium Software",
Expand Down

0 comments on commit 7f934f1

Please sign in to comment.