51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import type { BrowserWindow } from 'electron';
|
|
|
|
/** Keep a fullscreen Space alive until macOS has finished leaving it. */
|
|
export function registerMainWindowClose(
|
|
window: BrowserWindow,
|
|
options: {
|
|
platform: NodeJS.Platform;
|
|
isQuitting: () => boolean;
|
|
hideOnClose: boolean;
|
|
},
|
|
): void {
|
|
let pendingClose = false;
|
|
let leavingFullscreen = false;
|
|
|
|
const cancelPendingClose = (): void => {
|
|
pendingClose = false;
|
|
window.removeListener('leave-full-screen', finishClose);
|
|
};
|
|
const finishClose = (): void => {
|
|
pendingClose = false;
|
|
if (!window.isDestroyed() && !options.isQuitting()) window.close();
|
|
};
|
|
|
|
window.on('leave-full-screen', () => { leavingFullscreen = false; });
|
|
window.on('close', (event) => {
|
|
if (options.isQuitting()) {
|
|
cancelPendingClose();
|
|
return;
|
|
}
|
|
if (pendingClose) {
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
if (options.platform === 'darwin' && window.isFullScreen()) {
|
|
event.preventDefault();
|
|
pendingClose = true;
|
|
window.once('leave-full-screen', finishClose);
|
|
if (!leavingFullscreen) {
|
|
leavingFullscreen = true;
|
|
window.setFullScreen(false);
|
|
}
|
|
return;
|
|
}
|
|
if (options.hideOnClose) {
|
|
event.preventDefault();
|
|
window.hide();
|
|
}
|
|
});
|
|
window.once('closed', cancelPendingClose);
|
|
}
|