mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
🐛 fix: handle will-prevent-unload event to allow app quit (#11406)
fix: handle will-prevent-unload event to allow app quit
This commit is contained in:
@@ -184,6 +184,20 @@ export default class Browser {
|
||||
this.setupReadyToShowListener(browserWindow);
|
||||
this.setupCloseListener(browserWindow);
|
||||
this.setupFocusListener(browserWindow);
|
||||
this.setupWillPreventUnloadListener(browserWindow);
|
||||
}
|
||||
|
||||
private setupWillPreventUnloadListener(browserWindow: BrowserWindow): void {
|
||||
logger.debug(`[${this.identifier}] Setting up 'will-prevent-unload' event listener.`);
|
||||
browserWindow.webContents.on('will-prevent-unload', (event) => {
|
||||
logger.debug(
|
||||
`[${this.identifier}] 'will-prevent-unload' fired. isQuiting: ${this.app.isQuiting}`,
|
||||
);
|
||||
if (this.app.isQuiting) {
|
||||
logger.info(`[${this.identifier}] App is quitting, ignoring beforeunload cancellation.`);
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private setupReadyToShowListener(browserWindow: BrowserWindow): void {
|
||||
|
||||
@@ -40,6 +40,7 @@ const { mockBrowserWindow, mockNativeTheme, mockIpcMain, mockScreen, MockBrowser
|
||||
onHeadersReceived: vi.fn(),
|
||||
},
|
||||
},
|
||||
on: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -646,4 +647,35 @@ describe('Browser', () => {
|
||||
expect(mockBrowserWindow.setBackgroundColor).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('will-prevent-unload event handling', () => {
|
||||
let willPreventUnloadHandler: (e: any) => void;
|
||||
|
||||
beforeEach(() => {
|
||||
// Get the will-prevent-unload handler registered during initialization
|
||||
willPreventUnloadHandler = mockBrowserWindow.webContents.on.mock.calls.find(
|
||||
(call) => call[0] === 'will-prevent-unload',
|
||||
)?.[1];
|
||||
});
|
||||
|
||||
it('should call preventDefault when app is quitting', () => {
|
||||
(mockApp as any).isQuiting = true;
|
||||
const mockEvent = { preventDefault: vi.fn() };
|
||||
|
||||
expect(willPreventUnloadHandler).toBeDefined();
|
||||
willPreventUnloadHandler(mockEvent);
|
||||
|
||||
expect(mockEvent.preventDefault).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call preventDefault when app is not quitting', () => {
|
||||
(mockApp as any).isQuiting = false;
|
||||
const mockEvent = { preventDefault: vi.fn() };
|
||||
|
||||
expect(willPreventUnloadHandler).toBeDefined();
|
||||
willPreventUnloadHandler(mockEvent);
|
||||
|
||||
expect(mockEvent.preventDefault).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user