diff --git a/src/main/index.ts b/src/main/index.ts index 0f0c72cd..b40950e3 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,5 +1,5 @@ import { electronApp, optimizer } from '@electron-toolkit/utils' -import { app, BrowserWindow } from 'electron' +import { app } from 'electron' import installExtension, { REDUX_DEVTOOLS } from 'electron-devtools-installer' import { registerIpc } from './ipc' @@ -46,9 +46,8 @@ if (!app.requestSingleInstanceLock()) { new TrayService() app.on('activate', function () { - // On macOS it's common to re-create a window in the app when the - // dock icon is clicked and there are no other windows open. - if (BrowserWindow.getAllWindows().length === 0) { + const mainWindow = windowService.getMainWindow() + if (!mainWindow || mainWindow.isDestroyed()) { windowService.createMainWindow() } else { windowService.showMainWindow() @@ -68,12 +67,7 @@ if (!app.requestSingleInstanceLock()) { // Listen for second instance app.on('second-instance', () => { - const mainWindow = BrowserWindow.getAllWindows()[0] - if (mainWindow) { - mainWindow.isMinimized() && mainWindow.restore() - mainWindow.show() - mainWindow.focus() - } + windowService.showMainWindow() }) app.on('browser-window-created', (_, window) => { diff --git a/src/main/services/WindowService.ts b/src/main/services/WindowService.ts index 1ce7eb8a..414c568d 100644 --- a/src/main/services/WindowService.ts +++ b/src/main/services/WindowService.ts @@ -28,6 +28,7 @@ export class WindowService { public createMainWindow(): BrowserWindow { if (this.mainWindow && !this.mainWindow.isDestroyed()) { + this.mainWindow.show() return this.mainWindow } @@ -248,17 +249,32 @@ export class WindowService { event.preventDefault() mainWindow.hide() }) + + mainWindow.on('closed', () => { + this.mainWindow = null + }) + + mainWindow.on('show', () => { + if (this.miniWindow && !this.miniWindow.isDestroyed()) { + this.miniWindow.hide() + } + }) } public showMainWindow() { - if (this.mainWindow) { + if (this.miniWindow && !this.miniWindow.isDestroyed()) { + this.miniWindow.hide() + } + + if (this.mainWindow && !this.mainWindow.isDestroyed()) { if (this.mainWindow.isMinimized()) { - return this.mainWindow.restore() + this.mainWindow.restore() } this.mainWindow.show() this.mainWindow.focus() } else { - this.createMainWindow() + this.mainWindow = this.createMainWindow() + this.mainWindow.focus() } } @@ -269,7 +285,10 @@ export class WindowService { return } - if (this.selectionMenuWindow) { + if (this.mainWindow && !this.mainWindow.isDestroyed()) { + this.mainWindow.hide() + } + if (this.selectionMenuWindow && !this.selectionMenuWindow.isDestroyed()) { this.selectionMenuWindow.hide() }