From e6aa002758ff084462633d4c4d7a1d68b22b6b7b Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 16 Feb 2019 13:10:37 +0000 Subject: [PATCH] Desktop: Fixes #1215: Updated single instance logic for Electron 4 --- ElectronClient/app/ElectronAppWrapper.js | 29 ++++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/ElectronClient/app/ElectronAppWrapper.js b/ElectronClient/app/ElectronAppWrapper.js index 576b622f1d..75d08c9b63 100644 --- a/ElectronClient/app/ElectronAppWrapper.js +++ b/ElectronClient/app/ElectronAppWrapper.js @@ -191,19 +191,24 @@ class ElectronAppWrapper { ensureSingleInstance() { if (this.env_ === 'dev') return false; - return new Promise((resolve, reject) => { - const alreadyRunning = this.electronApp_.makeSingleInstance((commandLine, workingDirectory) => { - const win = this.window(); - if (!win) return; - if (win.isMinimized()) win.restore(); - win.show(); - win.focus(); - }); + const gotTheLock = this.electronApp_.requestSingleInstanceLock(); - if (alreadyRunning) this.electronApp_.quit(); + if (!gotTheLock) { + // Another instance is already running - exit + this.electronApp_.quit(); + return true; + } - resolve(alreadyRunning); + // Someone tried to open a second instance - focus our window instead + this.electronApp_.on('second-instance', (event, commandLine, workingDirectory) => { + const win = this.window(); + if (!win) return; + if (win.isMinimized()) win.restore(); + win.show(); + win.focus(); }); + + return false; } async start() { @@ -211,8 +216,8 @@ class ElectronAppWrapper { // the "ready" event. So we use the function below to make sure that the app is ready. await this.waitForElectronAppReady(); - const alreadyRunning = await this.ensureSingleInstance(); - if (alreadyRunning) return; + const alreadyRunning = this.ensureSingleInstance(); + if (alreadyRunning) return; this.createWindow();