|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | const path = require('node:path') |
| 7 | +const { spawn } = require('node:child_process') |
7 | 8 | const { app, dialog, BrowserWindow, ipcMain, desktopCapturer, systemPreferences, shell } = require('electron') |
8 | 9 | const { setupMenu } = require('./app/app.menu.js') |
9 | 10 | const { setupReleaseNotificationScheduler } = require('./app/githubReleaseNotification.service.js') |
@@ -47,7 +48,7 @@ if (require('electron-squirrel-startup')) { |
47 | 48 | } |
48 | 49 |
|
49 | 50 | /** |
50 | | - * Only one instance is allowed at time |
| 51 | + * Only one instance is allowed at the same time |
51 | 52 | */ |
52 | 53 | if (!app.requestSingleInstanceLock()) { |
53 | 54 | app.quit() |
@@ -135,14 +136,38 @@ app.whenReady().then(async () => { |
135 | 136 | if (mainWindow.isMinimized()) { |
136 | 137 | mainWindow.restore() |
137 | 138 | } |
138 | | - // Show window if it's hidden in the system tray and focus it |
| 139 | + // Show the window in case it is hidden in the system tray and focus it |
139 | 140 | mainWindow.show() |
140 | 141 | } |
141 | 142 |
|
142 | 143 | /** |
143 | 144 | * Instead of creating a new app instance - focus existence one |
144 | 145 | */ |
145 | | - app.on('second-instance', () => focusMainWindow()) |
| 146 | + app.on('second-instance', (event, argv, cwd) => { |
| 147 | + // Instead of creating a new application instance - focus the current window |
| 148 | + if (process.execPath === argv[0]) { |
| 149 | + focusMainWindow() |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + // The second instance is another installation |
| 154 | + // Open the new instance and close the current one |
| 155 | + app.releaseSingleInstanceLock() |
| 156 | + try { |
| 157 | + const newInstance = spawn(path.resolve(argv[0]), argv.slice(1), { |
| 158 | + cwd, |
| 159 | + detached: true, |
| 160 | + stdio: 'ignore', |
| 161 | + }).on('spawn', () => { |
| 162 | + newInstance.unref() |
| 163 | + app.quit() |
| 164 | + }).on('error', (error) => { |
| 165 | + console.error('Failed to switch to the second instance', error) |
| 166 | + }) |
| 167 | + } catch (error) { |
| 168 | + console.error('Failed to switch to the second instance', error) |
| 169 | + } |
| 170 | + }) |
146 | 171 |
|
147 | 172 | app.on('certificate-error', (event, webContents, url, error, certificate, callback) => { |
148 | 173 | event.preventDefault() |
|
0 commit comments