From 16973fc0343493ecc8e4e067db93c16a84158c46 Mon Sep 17 00:00:00 2001 From: Phantom <59059173+EurFelux@users.noreply.github.com> Date: Sun, 31 Aug 2025 20:41:32 +0800 Subject: [PATCH] fix(ipc): check mainWindow in ipc handler (#9712) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix(ipc): 添加主窗口检查并修复窗口重置逻辑 在Windows_ResetMinimumSize处理中添加主窗口存在性检查 移除不必要的可选链操作符,确保窗口操作安全 --- src/main/ipc.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 97aedd83ce..8b22fee49c 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -86,6 +86,12 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { // Initialize Python service with main window pythonService.setMainWindow(mainWindow) + const checkMainWindow = () => { + if (!mainWindow || mainWindow.isDestroyed()) { + throw new Error('Main window does not exist or has been destroyed') + } + } + ipcMain.handle(IpcChannel.App_Info, () => ({ version: app.getVersion(), isPackaged: app.isPackaged, @@ -562,19 +568,23 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { // window ipcMain.handle(IpcChannel.Windows_SetMinimumSize, (_, width: number, height: number) => { - mainWindow?.setMinimumSize(width, height) + checkMainWindow() + mainWindow.setMinimumSize(width, height) }) ipcMain.handle(IpcChannel.Windows_ResetMinimumSize, () => { - mainWindow?.setMinimumSize(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT) - const [width, height] = mainWindow?.getSize() ?? [MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT] + checkMainWindow() + + mainWindow.setMinimumSize(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT) + const [width, height] = mainWindow.getSize() ?? [MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT] if (width < MIN_WINDOW_WIDTH) { - mainWindow?.setSize(MIN_WINDOW_WIDTH, height) + mainWindow.setSize(MIN_WINDOW_WIDTH, height) } }) ipcMain.handle(IpcChannel.Windows_GetSize, () => { - const [width, height] = mainWindow?.getSize() ?? [MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT] + checkMainWindow() + const [width, height] = mainWindow.getSize() ?? [MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT] return [width, height] })