diff --git a/src/main/actions/cleanup.js b/src/main/actions/cleanup.js index a178d08..c0a164b 100644 --- a/src/main/actions/cleanup.js +++ b/src/main/actions/cleanup.js @@ -19,11 +19,38 @@ export const getReadyToQuitApp = async () => { cleanup(); if (global.backgroundWindow) { + // Set flag to allow background window destruction + global.allowBackgroundWindowDestruction = true; + + let timeoutHandle; + ipcMain.once("shutdown-success", () => { - console.log("shudown sucess"); - global.backgroundWindow?.close(); - resolve() + clearTimeout(timeoutHandle); + if ( + global.backgroundWindow && + !global.backgroundWindow.isDestroyed() + ) { + // When app is actually quitting, use the original destroy + if (global.backgroundWindow._originalDestroy) { + global.backgroundWindow._originalDestroy(); + } else { + global.backgroundWindow.destroy(); + } + } + resolve(); }); + + // Timeout fallback in case shutdown-success never comes + timeoutHandle = setTimeout(() => { + if (global.backgroundWindow && !global.backgroundWindow.isDestroyed()) { + if (global.backgroundWindow._originalDestroy) { + global.backgroundWindow._originalDestroy(); + } else { + global.backgroundWindow.destroy(); + } + } + resolve(); + }, 2000); } else { resolve(); } diff --git a/src/main/actions/startBackgroundProcess.js b/src/main/actions/startBackgroundProcess.js index 8b04d73..9ca03f4 100644 --- a/src/main/actions/startBackgroundProcess.js +++ b/src/main/actions/startBackgroundProcess.js @@ -69,11 +69,55 @@ const startBackgroundProcess = async () => { }) } + // Prevent closing - hide instead of destroying the window + const closeHandler = (event) => { + event.preventDefault(); + event.returnValue = false; + if (!backgroundWindow.isDestroyed()) { + backgroundWindow.hide(); + } + return false; + }; + + backgroundWindow.on("close", closeHandler); + + // Store the close handler so it can't be easily removed + backgroundWindow._preventCloseHandler = closeHandler; + + // Override the destroy method to prevent accidental destruction + const originalDestroy = backgroundWindow.destroy.bind(backgroundWindow); + backgroundWindow.destroy = () => { + // Allow destruction if we're in quit mode + if (global.allowBackgroundWindowDestruction && !backgroundWindow.isDestroyed()) { + originalDestroy(); + return; + } + if (!backgroundWindow.isDestroyed()) { + backgroundWindow.hide(); + } + }; + + // Store reference to restore destroy if really needed during app quit + backgroundWindow._originalDestroy = originalDestroy; + + // Override removeAllListeners to prevent removal of close handler + const originalRemoveAllListeners = backgroundWindow.removeAllListeners.bind(backgroundWindow); + backgroundWindow.removeAllListeners = (eventName) => { + if (eventName === 'close' || eventName === undefined) { + // Re-attach the close handler after removal + originalRemoveAllListeners.call(backgroundWindow, eventName); + backgroundWindow.on("close", closeHandler); + return backgroundWindow; + } + return originalRemoveAllListeners.call(backgroundWindow, eventName); + }; + // Setup IPC forwarding setupIPCForwardingToBackground(backgroundWindow); // Set state global.isBackgroundProcessActive = true; + global.backgroundProcessStarted = true; backgroundWindow.webContents.on("did-finish-load", () => { resolve(true);