Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions src/main/actions/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,30 @@ export const getReadyToQuitApp = async () => {
cleanup();

if (global.backgroundWindow) {
// Set flag to allow background window destruction
global.allowBackgroundWindowDestruction = true;

ipcMain.once("shutdown-success", () => {
console.log("shudown sucess");
global.backgroundWindow?.close();
// 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
Comment thread
rohitneharabrowserstack marked this conversation as resolved.
Outdated
setTimeout(() => {
if (global.backgroundWindow && !global.backgroundWindow.isDestroyed()) {
if (global.backgroundWindow._originalDestroy) {
global.backgroundWindow._originalDestroy();
} else {
global.backgroundWindow.destroy();
}
}
resolve();
}, 2000);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} 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) {
originalDestroy();
return;
}
if (!backgroundWindow.isDestroyed()) {
backgroundWindow.hide();
}
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

// 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) {
Comment thread
rohitneharabrowserstack marked this conversation as resolved.
// Re-attach the close handler after removal
originalRemoveAllListeners.call(backgroundWindow, eventName);
backgroundWindow.on("close", closeHandler);
return backgroundWindow;
}
return originalRemoveAllListeners.call(backgroundWindow, eventName);
};
Comment thread
rohitneharabrowserstack marked this conversation as resolved.

// Setup IPC forwarding
setupIPCForwardingToBackground(backgroundWindow);

// Set state
global.isBackgroundProcessActive = true;
global.backgroundProcessStarted = true;

backgroundWindow.webContents.on("did-finish-load", () => {
resolve(true);
Expand Down