Skip to content

Commit 6f33251

Browse files
committed
fix(app): handle opening new installation instance
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
1 parent 1514690 commit 6f33251

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/main.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
const path = require('node:path')
7+
const { spawn } = require('node:child_process')
78
const { app, dialog, BrowserWindow, ipcMain, desktopCapturer, systemPreferences, shell } = require('electron')
89
const { setupMenu } = require('./app/app.menu.js')
910
const { setupReleaseNotificationScheduler } = require('./app/githubReleaseNotification.service.js')
@@ -47,7 +48,7 @@ if (require('electron-squirrel-startup')) {
4748
}
4849

4950
/**
50-
* Only one instance is allowed at time
51+
* Only one instance is allowed at the same time
5152
*/
5253
if (!app.requestSingleInstanceLock()) {
5354
app.quit()
@@ -135,14 +136,38 @@ app.whenReady().then(async () => {
135136
if (mainWindow.isMinimized()) {
136137
mainWindow.restore()
137138
}
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
139140
mainWindow.show()
140141
}
141142

142143
/**
143144
* Instead of creating a new app instance - focus existence one
144145
*/
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+
})
146171

147172
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
148173
event.preventDefault()

0 commit comments

Comments
 (0)