Skip to content

Commit f5ece43

Browse files
committed
fix(startup): enforce single-instance app lock to prevent duplicate menus
1 parent a831a05 commit f5ece43

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

electron/main.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ let tray: Tray | null = null
4949
let selectedSourceName = ''
5050
let editorHasUnsavedChanges = false
5151
let isForceClosing = false
52+
const hasSingleInstanceLock = app.requestSingleInstanceLock()
53+
54+
if (!hasSingleInstanceLock) {
55+
app.quit()
56+
}
5257

5358
function closeEditorWindowBypassingUnsavedPrompt(window: BrowserWindow | null) {
5459
if (!window || window.isDestroyed()) {
@@ -72,6 +77,20 @@ function createWindow() {
7277
mainWindow = createHudOverlayWindow()
7378
}
7479

80+
function focusOrCreateMainWindow() {
81+
if (BrowserWindow.getAllWindows().length === 0) {
82+
createWindow()
83+
return
84+
}
85+
86+
if (mainWindow && !mainWindow.isDestroyed()) {
87+
if (mainWindow.isMinimized()) mainWindow.restore()
88+
mainWindow.show()
89+
mainWindow.focus()
90+
mainWindow.moveTop()
91+
}
92+
}
93+
7594
function isEditorWindow(window: BrowserWindow) {
7695
return window.webContents.getURL().includes('windowType=editor')
7796
}
@@ -321,14 +340,11 @@ app.on('window-all-closed', () => {
321340
app.on('activate', () => {
322341
// On OS X it's common to re-create a window in the app when the
323342
// dock icon is clicked and there are no other windows open.
324-
if (BrowserWindow.getAllWindows().length === 0) {
325-
createWindow()
326-
} else if (mainWindow && !mainWindow.isDestroyed()) {
327-
if (mainWindow.isMinimized()) mainWindow.restore();
328-
mainWindow.show();
329-
mainWindow.focus();
330-
mainWindow.moveTop();
331-
}
343+
focusOrCreateMainWindow()
344+
})
345+
346+
app.on('second-instance', () => {
347+
focusOrCreateMainWindow()
332348
})
333349

334350

0 commit comments

Comments
 (0)