Two ways to react on the Electron ‘close’ event

I have an Electron application that needs to save some data when it’s closed by the user (e.g. just after the user clicked on the “Close” button). For this example, we want to store the resolution (width and height of the application in pixels) to a config file. The next time, the application starts, these values should be read and the application resized accordingly.

As you probably know, Electron has one main process and several renderer process for the browser windows. The main.ts file hosts the main process implementation. There, we create a new BrowserWindow instance, defining a minimal size, initial width, and height.

The use of show: false first and win.show() later-on makes the startup smoother.

The screen size is saved to a yaml file and read into a model class at start-up.

In the following, I will present both approaches I tried.

Solution 1: Renderer Process

The BrowserWindow emits events when an App Command is invoked. The ones we are interested in are ‘close’ and ‘resize’. The BrowserWindow instance is part of the main electron process. From the renderer process, we can access it via the remote interface.

By adding a listener on the resize event, the screen resolution is written to a file every time the application is resized – which is a bit of an overhead. What we actually want to achieve is that these values are written once, right when the application is to be closed.

 

The Electron API for Browser Window mentions a close event, but it seems this is done by the main process, not the renderer process. Listening on this event in the renderer process was not successful, the event was never triggered.

 

On application start up, we read in our configuration from the configuration file. The BrowserWindow’s size can be set using the setSize() method.

 

Solution 2: IPC between main and renderer process

The second approach is to use both the main and the renderer process. On the main process, the BrowserWindow listens on the close event. When it happens, it sends a message via webContents to the renderer process. It also prevents the application from being immediately closed by calling event.preventDefault().

 

The renderer process is always listening on IPC messages from the main process. The ipcRenderer module provides a few methods to send synchronous and asynchronous messages from the render (web page) to the main process. When it receives the app-close event notification, it saves the data, then sends the main process an IPC message (e.g. closed);

 

The main process has previously set a hook to listen to the renderer IPC messages (ipcMain.on), so when the closed message arrives, it finally closes the program (e.g. via app.quit()). The ipcMain module handles asynchronous and synchronous messages sent from a renderer process.

Calling app.quit() sends another close event to the BrowserWindow, so it will loop unless you prevent it!

 

Overall, I prefer the second solution as it feels cleaner and the file is only written once and not every time the application is resized. This is also the way I implemented it.