Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/main/actions/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/actions/startBackgroundProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down