From 72e847258d85a1697ccebb08bbf9832d5ae4058c Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Tue, 29 Oct 2024 14:48:48 +0800 Subject: [PATCH] feat: add instance lock and second instance handling --- src/main/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/index.ts b/src/main/index.ts index 868fd372..c2c0f7d2 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -7,6 +7,11 @@ import { registerZoomShortcut } from './shortcut' import { updateUserDataPath } from './utils/upgrade' import { createMainWindow } from './window' +// Check for single instance lock +if (!app.requestSingleInstanceLock()) { + app.quit() +} + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. @@ -42,6 +47,15 @@ app.whenReady().then(async () => { } }) +// Listen for second instance +app.on('second-instance', () => { + const mainWindow = BrowserWindow.getAllWindows()[0] + if (mainWindow) { + mainWindow.isMinimized() && mainWindow.restore() + mainWindow.focus() + } +}) + // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q.