cc-haha/desktop/electron/services/previewLifecycle.ts
程序员阿江(Relakkes) fc5924f1f1 fix(desktop): close preview on renderer reload (#680)
Close the native preview WebContentsView when the Electron renderer starts a top-level navigation so refreshed session pages cannot leave the in-app browser surface behind.

Fixes #680

Tested:
- bun run verify

Confidence: high
Scope-risk: narrow
2026-06-04 16:40:06 +08:00

43 lines
1.0 KiB
TypeScript

type NavigationDetails = {
isSameDocument?: boolean
isMainFrame?: boolean
}
export type PreviewCleanupWebContents = {
on(
event: 'did-start-navigation',
handler: (
details: NavigationDetails,
url?: string,
isInPlace?: boolean,
isMainFrame?: boolean,
) => void,
): unknown
}
function isMainFrameNavigation(
details: NavigationDetails,
deprecatedIsMainFrame?: boolean,
) {
return details.isMainFrame ?? deprecatedIsMainFrame === true
}
function isSameDocumentNavigation(
details: NavigationDetails,
deprecatedIsInPlace?: boolean,
) {
return details.isSameDocument ?? deprecatedIsInPlace === true
}
export function installPreviewCleanupOnRendererNavigation(
webContents: PreviewCleanupWebContents,
closePreview: () => void,
): void {
webContents.on('did-start-navigation', (details, _url, isInPlace, isMainFrame) => {
if (!isMainFrameNavigation(details, isMainFrame)) return
if (isSameDocumentNavigation(details, isInPlace)) return
closePreview()
})
}