mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-16 13:03:31 +08:00
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
43 lines
1.0 KiB
TypeScript
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()
|
|
})
|
|
}
|