refactor(browser): read a document preview's page id from its guest policy

This commit is contained in:
Kelvin Amoaba
2026-11-11 07:29:09 +00:00
parent 0a52d32e93
commit 77b8282f34
3 changed files with 28 additions and 23 deletions
@@ -66,6 +66,12 @@ export function readDocPreviewGuestBoundGrantId(guest: Electron.WebContents): st
return previewGuests.get(guest)?.readBoundGrantId() ?? null
}
/** Browser page of the guest's live grant; null before the grant binds or once it is revoked. */
export function readDocPreviewGuestBoundPageId(guest: Electron.WebContents): string | null {
const grantId = readDocPreviewGuestBoundGrantId(guest)
return (grantId && getDocPreviewGrant(grantId)?.browserPageId) || null
}
/** URL of the guest's committed document; unlike getURL(), pushState cannot rewrite it. */
export function readDocPreviewGuestDocumentUrl(guest: Electron.WebContents): string {
return previewGuests.get(guest)?.readDocumentUrl() ?? ''
@@ -5,9 +5,8 @@ import { BrowserManagerGuestCleanup } from './browser-manager-guest-cleanup'
import { allowPageUnload } from '../../navigation/browser-guest-unload-policy'
import {
installDocPreviewGuestPolicy,
readDocPreviewGuestBoundGrantId
readDocPreviewGuestBoundPageId
} from '../../../document-preview/doc-preview-guest-policy'
import { getDocPreviewGrant } from '../../../document-preview/doc-preview-grant-registry'
import { setupGuestEscapeForwarding } from '../../input/shortcuts/browser-guest-escape-forwarding'
import { setupGuestShortcutForwarding } from '../../input/shortcuts/browser-guest-shortcut-forwarding'
import { forwardGuestMousePress } from '../../input/browser-guest-mouse-press'
@@ -94,14 +93,11 @@ export abstract class BrowserManagerGuestPolicy extends BrowserManagerGuestClean
host: Electron.WebContents
): void {
const disposeDocPolicy = installDocPreviewGuestPolicy(guest, host)
const readBrowserPageId = (): string | null => {
const grantId = readDocPreviewGuestBoundGrantId(guest)
return (grantId && getDocPreviewGrant(grantId)?.browserPageId) || null
}
const resolveHost = (): Electron.WebContents | null => (host.isDestroyed() ? null : host)
const disposeEscapeForwarding = setupGuestEscapeForwarding({
guest,
resolveTarget: () => {
const browserPageId = readBrowserPageId()
const browserPageId = readDocPreviewGuestBoundPageId(guest)
return browserPageId ? { browserPageId, renderer: host } : null
},
hasActiveGrabOp: (browserPageId) => this.hasActiveGrabOp(browserPageId)
@@ -111,16 +107,20 @@ export abstract class BrowserManagerGuestPolicy extends BrowserManagerGuestClean
const disposeShortcutForwarding = setupGuestShortcutForwarding({
browserTabId: `workspace-doc:${guest.id}`,
guest,
resolveRenderer: () => (host.isDestroyed() ? null : host),
resolveRenderer: resolveHost,
getKeybindings: () => this.settingsResolver?.().keybindings,
browserPage: false
})
// Why: a press in the preview never reaches the host DOM, so its split stays inactive and
// forwarded shortcuts like Close would act on another split (#22144).
// forwarded shortcuts like Close would act on another split (#22144). Presses only: previews
// have no history listener for the side buttons a release would send.
const reportPress = (_event: Electron.Event, mouse: Electron.MouseInputEvent): void => {
const browserPageId = mouse.type === 'mouseDown' ? readBrowserPageId() : null
if (browserPageId && !host.isDestroyed()) {
forwardGuestMousePress(guest, browserPageId, () => host, mouse)
if (mouse.type !== 'mouseDown') {
return
}
const browserPageId = readDocPreviewGuestBoundPageId(guest)
if (browserPageId) {
forwardGuestMousePress(guest, browserPageId, resolveHost, mouse)
}
}
guest.on('before-mouse-event', reportPress)
@@ -62,22 +62,21 @@ describe('workspace document guest input', () => {
it('forwards Ctrl+Tab from a focused document preview to its host window', () => {
const { guest, send } = attachDocumentGuest()
const input = { shift: false, alt: false, meta: false, isAutoRepeat: false }
guest.emit(
'before-input-event',
{ preventDefault: vi.fn() },
{ ...input, type: 'keyDown', key: 'Tab', code: 'Tab', control: true }
)
guest.emit(
'before-input-event',
{ preventDefault: vi.fn() },
{ ...input, type: 'keyUp', key: 'Control', code: 'ControlLeft', control: false }
{
type: 'keyDown',
key: 'Tab',
code: 'Tab',
control: true,
shift: false,
alt: false,
meta: false
}
)
expect(send.mock.calls).toEqual([
['ui:ctrlTabKeyDown', { shiftKey: false }],
['ui:ctrlTabKeyUp']
])
expect(send.mock.calls).toEqual([['ui:ctrlTabKeyDown', { shiftKey: false }]])
})
it('reports a press in the preview so its split becomes active', () => {