From dfa175b8b23d2991e4742c6ca4888763ac485b05 Mon Sep 17 00:00:00 2001 From: alger Date: Fri, 17 Jan 2025 22:35:33 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=BA=94=E7=94=A8=E5=8D=95?= =?UTF-8?q?=E4=BE=8B=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.ts | 82 +++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 9ef9c61..ca5b686 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -47,44 +47,62 @@ function initialize() { initializeShortcuts(mainWindow); } -// 应用程序准备就绪时的处理 -app.whenReady().then(() => { - // 设置应用ID - electronApp.setAppUserModelId('com.alger.music'); +// 检查是否为第一个实例 +const isSingleInstance = app.requestSingleInstanceLock(); - // 监听窗口创建事件 - app.on('browser-window-created', (_, window) => { - optimizer.watchWindowShortcuts(window); +if (!isSingleInstance) { + app.quit(); +} else { + // 当第二个实例启动时,将焦点转移到第一个实例的窗口 + app.on('second-instance', () => { + if (mainWindow) { + if (mainWindow.isMinimized()) { + mainWindow.restore(); + } + mainWindow.show(); + mainWindow.focus(); + } }); - // 初始化应用 - initialize(); + // 应用程序准备就绪时的处理 + app.whenReady().then(() => { + // 设置应用ID + electronApp.setAppUserModelId('com.alger.music'); - // macOS 激活应用时的处理 - app.on('activate', () => { - if (mainWindow === null) initialize(); + // 监听窗口创建事件 + app.on('browser-window-created', (_, window) => { + optimizer.watchWindowShortcuts(window); + }); + + // 初始化应用 + initialize(); + + // macOS 激活应用时的处理 + app.on('activate', () => { + if (mainWindow === null) initialize(); + }); }); -}); -// 监听快捷键更新 -ipcMain.on('update-shortcuts', () => { - registerShortcuts(mainWindow); -}); + // 监听快捷键更新 + ipcMain.on('update-shortcuts', () => { + registerShortcuts(mainWindow); + }); -// 所有窗口关闭时的处理 -app.on('window-all-closed', () => { - if (process.platform !== 'darwin') { - app.quit(); - } -}); + // 所有窗口关闭时的处理 + app.on('window-all-closed', () => { + if (process.platform !== 'darwin') { + app.quit(); + } + }); -// 重启应用 -ipcMain.on('restart', () => { - app.relaunch(); - app.exit(0); -}); + // 重启应用 + ipcMain.on('restart', () => { + app.relaunch(); + app.exit(0); + }); -// 获取系统架构信息 -ipcMain.on('get-arch', (event) => { - event.returnValue = process.arch; -}); + // 获取系统架构信息 + ipcMain.on('get-arch', (event) => { + event.returnValue = process.arch; + }); +}