Refresh right panel close controls for PR #4148
Keep native close focus scoped to the active hosted preview and preserve pointer ownership across webview blur transitions. Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
73e5aeb1b4
commit
137c50175e
3 files changed
+87
-44
No files matched your search
@@ -3173,6 +3173,10 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
},
|
||||
[activeThreadRef, cleanupRightPanelSurfaces, syncActivePreviewSurface],
|
||||
);
|
||||
const activeRightPanelPreviewTabId =
|
||||
activeRightPanelSurface?.kind === "preview"
|
||||
? (activeRightPanelSurface.resourceId ?? null)
|
||||
: null;
|
||||
const closeActiveRightPanelTarget = useCallback(
|
||||
(terminalFocusOwner = getTerminalFocusOwner()) => {
|
||||
const target = resolveRightPanelCloseTarget({
|
||||
@@ -3201,7 +3205,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
);
|
||||
const handleCloseRequest = useEffectEvent((event: Event) => {
|
||||
const target = resolveNativeCloseTarget({
|
||||
focusOwner: getNativeCloseFocusOwner(),
|
||||
focusOwner: getNativeCloseFocusOwner(activeRightPanelPreviewTabId),
|
||||
drawerTerminalOpen: terminalUiState.terminalOpen,
|
||||
rightPanelOpen,
|
||||
hasActiveRightPanelSurface: activeRightPanelSurface !== null,
|
||||
@@ -3231,32 +3235,34 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const recordFocus = (event: FocusEvent | PointerEvent) => {
|
||||
recordNativeCloseFocus(event.target);
|
||||
recordNativeCloseFocus(event.target, activeRightPanelPreviewTabId);
|
||||
};
|
||||
const recordFocusOut = (event: FocusEvent) => {
|
||||
recordNativeCloseFocusOut(event.relatedTarget, document.hasFocus());
|
||||
recordNativeCloseFocusOut(
|
||||
event.relatedTarget,
|
||||
document.hasFocus(),
|
||||
activeRightPanelPreviewTabId,
|
||||
);
|
||||
};
|
||||
const recordPointer = (event: PointerEvent) => {
|
||||
recordNativeClosePointer(event.target);
|
||||
recordNativeClosePointer(event.target, activeRightPanelPreviewTabId);
|
||||
};
|
||||
|
||||
recordNativeCloseFocus(document.activeElement);
|
||||
recordNativeCloseFocus(document.activeElement, activeRightPanelPreviewTabId);
|
||||
window.addEventListener("focusin", recordFocus, true);
|
||||
window.addEventListener("focusout", recordFocusOut, true);
|
||||
window.addEventListener("pointerdown", recordPointer, true);
|
||||
window.addEventListener("pointerup", finishNativeClosePointer, true);
|
||||
window.addEventListener("pointercancel", finishNativeClosePointer, true);
|
||||
window.addEventListener("blur", finishNativeClosePointer);
|
||||
return () => {
|
||||
window.removeEventListener("focusin", recordFocus, true);
|
||||
window.removeEventListener("focusout", recordFocusOut, true);
|
||||
window.removeEventListener("pointerdown", recordPointer, true);
|
||||
window.removeEventListener("pointerup", finishNativeClosePointer, true);
|
||||
window.removeEventListener("pointercancel", finishNativeClosePointer, true);
|
||||
window.removeEventListener("blur", finishNativeClosePointer);
|
||||
clearNativeCloseFocusOwner();
|
||||
};
|
||||
}, [routeThreadKey]);
|
||||
}, [activeRightPanelPreviewTabId, routeThreadKey]);
|
||||
useEffect(() => {
|
||||
if (!terminalUiState.terminalOpen) clearNativeCloseFocusOwner("drawer-terminal");
|
||||
if (!rightPanelOpen) {
|
||||
@@ -3952,10 +3958,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
|
||||
rightPanelFocus: isRightPanelFocused({
|
||||
rightPanelOpen,
|
||||
activePreviewTabId:
|
||||
activeRightPanelSurface?.kind === "preview"
|
||||
? (activeRightPanelSurface.resourceId ?? null)
|
||||
: null,
|
||||
activePreviewTabId: activeRightPanelPreviewTabId,
|
||||
}),
|
||||
};
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ class MockElement extends EventTarget {
|
||||
owner: "drawer" | "right-panel" | null = null;
|
||||
inRightPanel = false;
|
||||
inPreviewViewport = false;
|
||||
previewTabId: string | null = null;
|
||||
tagName = "div";
|
||||
|
||||
closest(selector: string): MockElement | null {
|
||||
@@ -27,6 +28,13 @@ class MockElement extends EventTarget {
|
||||
get dataset(): DOMStringMap {
|
||||
return { terminalOwner: this.owner ?? undefined } as DOMStringMap;
|
||||
}
|
||||
|
||||
getAttribute(name: string): string | null {
|
||||
if (name === "data-preview-tab" || name === "data-preview-viewport") {
|
||||
return this.previewTabId;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const originalDocument = globalThis.document;
|
||||
@@ -56,17 +64,17 @@ describe("native close focus", () => {
|
||||
const drawer = new MockElement();
|
||||
drawer.owner = "drawer";
|
||||
installDom(drawer);
|
||||
expect(getNativeCloseFocusOwner()).toBe("drawer-terminal");
|
||||
expect(getNativeCloseFocusOwner(null)).toBe("drawer-terminal");
|
||||
|
||||
const panelTerminal = new MockElement();
|
||||
panelTerminal.owner = "right-panel";
|
||||
installDom(panelTerminal);
|
||||
expect(getNativeCloseFocusOwner()).toBe("right-panel-terminal");
|
||||
expect(getNativeCloseFocusOwner(null)).toBe("right-panel-terminal");
|
||||
|
||||
const panelControl = new MockElement();
|
||||
panelControl.inRightPanel = true;
|
||||
installDom(panelControl);
|
||||
expect(getNativeCloseFocusOwner()).toBe("right-panel");
|
||||
expect(getNativeCloseFocusOwner(null)).toBe("right-panel");
|
||||
});
|
||||
|
||||
it("retains ownership only while focus is obscured by native UI", () => {
|
||||
@@ -74,13 +82,13 @@ describe("native close focus", () => {
|
||||
terminal.owner = "drawer";
|
||||
const body = new MockElement();
|
||||
installDom(body, body);
|
||||
recordNativeCloseFocus(terminal);
|
||||
recordNativeCloseFocusOut(null, false);
|
||||
recordNativeCloseFocus(terminal, null);
|
||||
recordNativeCloseFocusOut(null, false, null);
|
||||
|
||||
expect(getNativeCloseFocusOwner()).toBe("drawer-terminal");
|
||||
expect(getNativeCloseFocusOwner(null)).toBe("drawer-terminal");
|
||||
|
||||
recordNativeCloseFocusOut(null, true);
|
||||
expect(getNativeCloseFocusOwner()).toBeNull();
|
||||
recordNativeCloseFocusOut(null, true, null);
|
||||
expect(getNativeCloseFocusOwner(null)).toBeNull();
|
||||
});
|
||||
|
||||
it("preserves pointer ownership until the pointer gesture finishes", async () => {
|
||||
@@ -89,12 +97,12 @@ describe("native close focus", () => {
|
||||
const body = new MockElement();
|
||||
installDom(body, body);
|
||||
|
||||
recordNativeClosePointer(panelChrome);
|
||||
recordNativeClosePointer(panelChrome, null);
|
||||
await Promise.resolve();
|
||||
recordNativeCloseFocusOut(null, true);
|
||||
recordNativeCloseFocusOut(null, true, null);
|
||||
finishNativeClosePointer();
|
||||
|
||||
expect(getNativeCloseFocusOwner()).toBe("right-panel");
|
||||
expect(getNativeCloseFocusOwner(null)).toBe("right-panel");
|
||||
});
|
||||
|
||||
it("clears retained ownership on focus or pointer movement outside the panel", () => {
|
||||
@@ -102,10 +110,10 @@ describe("native close focus", () => {
|
||||
panel.inRightPanel = true;
|
||||
const outside = new MockElement();
|
||||
installDom(outside);
|
||||
recordNativeCloseFocus(panel);
|
||||
recordNativeCloseFocus(outside);
|
||||
recordNativeCloseFocus(panel, null);
|
||||
recordNativeCloseFocus(outside, null);
|
||||
|
||||
expect(getNativeCloseFocusOwner()).toBeNull();
|
||||
expect(getNativeCloseFocusOwner(null)).toBeNull();
|
||||
});
|
||||
|
||||
it("updates ownership directly from a focusout related target", () => {
|
||||
@@ -114,22 +122,35 @@ describe("native close focus", () => {
|
||||
const panel = new MockElement();
|
||||
panel.inRightPanel = true;
|
||||
installDom(panel);
|
||||
recordNativeCloseFocus(drawer);
|
||||
recordNativeCloseFocusOut(panel, true);
|
||||
recordNativeCloseFocus(drawer, null);
|
||||
recordNativeCloseFocusOut(panel, true, null);
|
||||
|
||||
expect(getNativeCloseFocusOwner()).toBe("right-panel");
|
||||
expect(getNativeCloseFocusOwner(null)).toBe("right-panel");
|
||||
});
|
||||
|
||||
it("treats hosted preview webviews and viewport chrome as right-panel focus", () => {
|
||||
const webview = new MockElement();
|
||||
webview.tagName = "webview";
|
||||
webview.previewTabId = "active-preview";
|
||||
installDom(webview);
|
||||
expect(getNativeCloseFocusOwner()).toBe("right-panel");
|
||||
expect(getNativeCloseFocusOwner("active-preview")).toBe("right-panel");
|
||||
|
||||
const viewport = new MockElement();
|
||||
viewport.inPreviewViewport = true;
|
||||
viewport.previewTabId = "active-preview";
|
||||
installDom(viewport);
|
||||
expect(getNativeCloseFocusOwner()).toBe("right-panel");
|
||||
expect(getNativeCloseFocusOwner("active-preview")).toBe("right-panel");
|
||||
});
|
||||
|
||||
it("ignores previews that are not the active right-panel preview", () => {
|
||||
const webview = new MockElement();
|
||||
webview.tagName = "webview";
|
||||
webview.previewTabId = "standalone-preview";
|
||||
installDom(webview);
|
||||
|
||||
recordNativeClosePointer(webview, "right-panel-preview");
|
||||
|
||||
expect(getNativeCloseFocusOwner("right-panel-preview")).toBeNull();
|
||||
});
|
||||
|
||||
it("ignores detached retained targets", () => {
|
||||
@@ -138,8 +159,8 @@ describe("native close focus", () => {
|
||||
terminal.isConnected = false;
|
||||
const body = new MockElement();
|
||||
installDom(body, body);
|
||||
recordNativeCloseFocus(terminal);
|
||||
recordNativeCloseFocus(terminal, null);
|
||||
|
||||
expect(getNativeCloseFocusOwner()).toBeNull();
|
||||
expect(getNativeCloseFocusOwner(null)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -6,29 +6,45 @@ const RIGHT_PANEL_SELECTOR =
|
||||
let retainedOwner: NativeCloseFocusOwner | null = null;
|
||||
let pointerTransitionActive = false;
|
||||
|
||||
function ownerForElement(element: Element): NativeCloseFocusOwner | null {
|
||||
function ownerForElement(
|
||||
element: Element,
|
||||
activePreviewTabId: string | null,
|
||||
): NativeCloseFocusOwner | null {
|
||||
if (!element.isConnected) return null;
|
||||
|
||||
const terminalOwner =
|
||||
element.closest<HTMLElement>("[data-terminal-owner]")?.dataset.terminalOwner;
|
||||
if (terminalOwner === "drawer") return "drawer-terminal";
|
||||
if (terminalOwner === "right-panel") return "right-panel-terminal";
|
||||
if (element.closest(RIGHT_PANEL_SELECTOR)) return "right-panel";
|
||||
if (element.tagName.toLowerCase() === "webview" || element.closest("[data-preview-viewport]")) {
|
||||
return "right-panel";
|
||||
|
||||
const previewViewport = element.closest<HTMLElement>("[data-preview-viewport]");
|
||||
const focusedPreviewTabId =
|
||||
element.tagName.toLowerCase() === "webview"
|
||||
? element.getAttribute("data-preview-tab")
|
||||
: (previewViewport?.getAttribute("data-preview-viewport") ?? null);
|
||||
if (focusedPreviewTabId !== null) {
|
||||
return focusedPreviewTabId === activePreviewTabId ? "right-panel" : null;
|
||||
}
|
||||
|
||||
if (element.closest(RIGHT_PANEL_SELECTOR)) return "right-panel";
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Records an in-document focus or pointer transition before native UI can obscure DOM focus. */
|
||||
export function recordNativeCloseFocus(target: EventTarget | null): void {
|
||||
retainedOwner = target instanceof Element ? ownerForElement(target) : null;
|
||||
export function recordNativeCloseFocus(
|
||||
target: EventTarget | null,
|
||||
activePreviewTabId: string | null,
|
||||
): void {
|
||||
retainedOwner = target instanceof Element ? ownerForElement(target, activePreviewTabId) : null;
|
||||
}
|
||||
|
||||
/** Records pointer intent before a non-focusable target can blur the previous owner to body. */
|
||||
export function recordNativeClosePointer(target: EventTarget | null): void {
|
||||
export function recordNativeClosePointer(
|
||||
target: EventTarget | null,
|
||||
activePreviewTabId: string | null,
|
||||
): void {
|
||||
pointerTransitionActive = true;
|
||||
recordNativeCloseFocus(target);
|
||||
recordNativeCloseFocus(target, activePreviewTabId);
|
||||
}
|
||||
|
||||
export function finishNativeClosePointer(): void {
|
||||
@@ -42,18 +58,21 @@ export function finishNativeClosePointer(): void {
|
||||
export function recordNativeCloseFocusOut(
|
||||
relatedTarget: EventTarget | null,
|
||||
documentHasFocus: boolean,
|
||||
activePreviewTabId: string | null,
|
||||
): void {
|
||||
if (relatedTarget instanceof Element) {
|
||||
retainedOwner = ownerForElement(relatedTarget);
|
||||
retainedOwner = ownerForElement(relatedTarget, activePreviewTabId);
|
||||
return;
|
||||
}
|
||||
if (documentHasFocus && !pointerTransitionActive) retainedOwner = null;
|
||||
}
|
||||
|
||||
export function getNativeCloseFocusOwner(): NativeCloseFocusOwner | null {
|
||||
export function getNativeCloseFocusOwner(
|
||||
activePreviewTabId: string | null,
|
||||
): NativeCloseFocusOwner | null {
|
||||
const activeElement = document.activeElement;
|
||||
if (activeElement instanceof Element && activeElement.isConnected) {
|
||||
const liveOwner = ownerForElement(activeElement);
|
||||
const liveOwner = ownerForElement(activeElement, activePreviewTabId);
|
||||
if (liveOwner !== null) {
|
||||
retainedOwner = liveOwner;
|
||||
return liveOwner;
|
||||
|
||||
Reference in new issue
Block a user