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
30 changes: 27 additions & 3 deletions src/main/actions/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@ export const getReadyToQuitApp = async () => {
cleanup();

if (global.backgroundWindow) {
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()
) {
if (global.backgroundWindow._originalDestroy) {
global.backgroundWindow._originalDestroy();
} else {
global.backgroundWindow.destroy();
}
}
resolve();
});

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
49 changes: 47 additions & 2 deletions src/main/actions/startBackgroundProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,61 @@ const startBackgroundProcess = async () => {
process.env.DEBUG_PROD === "true"
)
{
backgroundWindow.webContents.once('dom-ready', () => {
backgroundWindow.webContents.on('dom-ready', () => {
backgroundWindow.webContents.openDevTools();
})
});

backgroundWindow.on('show', () => {
if (!backgroundWindow.isDestroyed() && !backgroundWindow.webContents.isDevToolsOpened()) {
backgroundWindow.webContents.openDevTools();
}
});
}

const closeHandler = (event) => {
event.preventDefault();
event.returnValue = false;
if (!backgroundWindow.isDestroyed()) {
backgroundWindow.hide();
}
return false;
};

backgroundWindow.on("close", closeHandler);

backgroundWindow._preventCloseHandler = closeHandler;

const originalDestroy = backgroundWindow.destroy.bind(backgroundWindow);
backgroundWindow.destroy = () => {
if (global.allowBackgroundWindowDestruction && !backgroundWindow.isDestroyed()) {
originalDestroy();
return;
}
if (!backgroundWindow.isDestroyed()) {
backgroundWindow.hide();
}
};

backgroundWindow._originalDestroy = originalDestroy;

const originalRemoveAllListeners = backgroundWindow.removeAllListeners.bind(backgroundWindow);
backgroundWindow.removeAllListeners = (eventName) => {
// There is no eventName as 'close' we added undefined as fallback
// if someone tries to removeAllListeners without eventName or with 'close' eventName,
if (eventName === 'close' || eventName === undefined) {
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