Skip to content

Commit b1b09c7

Browse files
t3dotggclaude
andcommitted
fix(desktop): a finished press restores focus only from its own webview
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e05ea54 commit b1b09c7

2 files changed

Lines changed: 67 additions & 49 deletions

File tree

‎apps/desktop/src/preview/GuestInput.test.ts‎

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -124,51 +124,58 @@ describe("preview guest input", () => {
124124
},
125125
);
126126

127-
it("preserves host focus when a sibling tab's press overlaps a stuck one", async () => {
128-
const document = { activeElement: {} as object };
129-
const composer = {
130-
isConnected: true,
131-
focus: () => {
132-
document.activeElement = composer;
133-
},
134-
};
135-
document.activeElement = composer;
136-
const context = NodeVM.createContext({ document });
137-
const host = {
138-
isDestroyed: () => false,
139-
executeJavaScript: async (script: string) => NodeVM.runInContext(script, context),
140-
};
141-
const stuckPressFocused = Promise.withResolvers<void>();
142-
const stuckPressEnds = Promise.withResolvers<void>();
143-
const press = (id: number, stuck: boolean) => {
144-
const preview = { tagName: "WEBVIEW", getWebContentsId: () => id };
145-
const debuggee = {
146-
sendCommand: async () => {
147-
document.activeElement = preview;
148-
if (stuck) {
149-
stuckPressFocused.resolve();
150-
await stuckPressEnds.promise;
151-
}
152-
return {};
127+
it.each([{ userClicksStuckTab: false }, { userClicksStuckTab: true }])(
128+
"keeps the right focus when a sibling tab's press overlaps a stuck one, user clicks: $userClicksStuckTab",
129+
async ({ userClicksStuckTab }) => {
130+
const document = { activeElement: {} as object };
131+
const composer = {
132+
isConnected: true,
133+
focus: () => {
134+
document.activeElement = composer;
153135
},
154136
};
155-
return sendPageCommand(
156-
{ id, focusedFrame: null, hostWebContents: host } as unknown as Electron.WebContents,
157-
debuggee as unknown as Electron.Debugger,
158-
"Input.dispatchMouseEvent",
159-
{ type: "mousePressed", x: 10, y: 10, button: "left" },
137+
document.activeElement = composer;
138+
const context = NodeVM.createContext({ document });
139+
const host = {
140+
isDestroyed: () => false,
141+
executeJavaScript: async (script: string) => NodeVM.runInContext(script, context),
142+
};
143+
const previews = new Map(
144+
[7, 8].map((id) => [id, { tagName: "WEBVIEW", getWebContentsId: () => id }]),
160145
);
161-
};
162-
// A press held open, as by a page's confirm() on mousedown, must not block or win focus.
163-
const stuck = press(7, true);
164-
await stuckPressFocused.promise;
165-
await press(8, false);
166-
expect(document.activeElement).toBe(composer);
167-
stuckPressEnds.resolve();
168-
await stuck;
169-
expect(document.activeElement).toBe(composer);
170-
expect(Object.keys(context)).toEqual(["document"]);
171-
});
146+
const stuckPressFocused = Promise.withResolvers<void>();
147+
const stuckPressEnds = Promise.withResolvers<void>();
148+
const press = (id: number) => {
149+
const debuggee = {
150+
sendCommand: async () => {
151+
document.activeElement = previews.get(id)!;
152+
if (id === 7) {
153+
stuckPressFocused.resolve();
154+
await stuckPressEnds.promise;
155+
} else if (userClicksStuckTab) {
156+
document.activeElement = previews.get(7)!;
157+
}
158+
return {};
159+
},
160+
};
161+
return sendPageCommand(
162+
{ id, focusedFrame: null, hostWebContents: host } as unknown as Electron.WebContents,
163+
debuggee as unknown as Electron.Debugger,
164+
"Input.dispatchMouseEvent",
165+
{ type: "mousePressed", x: 10, y: 10, button: "left" },
166+
);
167+
};
168+
// A press held open, as by a page's confirm() on mousedown, must not block or win focus.
169+
const stuck = press(7);
170+
await stuckPressFocused.promise;
171+
await press(8);
172+
const expected = userClicksStuckTab ? previews.get(7) : composer;
173+
expect(document.activeElement).toBe(expected);
174+
stuckPressEnds.resolve();
175+
await stuck;
176+
expect(Object.keys(context)).toEqual(["document"]);
177+
},
178+
);
172179

173180
it.each([
174181
{

‎apps/desktop/src/preview/GuestInput.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const evaluate = async (debuggee: Electron.Debugger, expression: string) => {
9292
return reply.result?.value;
9393
};
9494

95-
/** Host focus shared by every guest press in flight in one window, so overlapping presses agree on it. */
95+
/** Host focus saved by the first of the presses in flight in one window, so a later press cannot save a webview. */
9696
const HOST_FOCUS = "globalThis.__t3PreviewFocus";
9797

9898
/** A CDP pointer press focuses the guest's native view even with focus emulation enabled. */
@@ -107,24 +107,35 @@ const sendPointerPress = async (
107107
if (!host || host.isDestroyed() || webContents.focusedFrame) {
108108
return debuggee.sendCommand(method, params, sessionId);
109109
}
110+
// Focus in a webview with a press in flight came from that press, so keep the earlier save.
110111
await host.executeJavaScript(`(() => {
111112
const state = (${HOST_FOCUS} ??= { saved: null, presses: [] });
112113
const active = document.activeElement;
113-
if (active && active !== document.body && active.tagName !== "WEBVIEW") state.saved = active;
114+
let pressed = false;
115+
try {
116+
pressed = active?.tagName === "WEBVIEW" && state.presses.includes(active.getWebContentsId());
117+
} catch {}
118+
if (!pressed) {
119+
state.saved = active && active !== document.body && active.tagName !== "WEBVIEW" ? active : null;
120+
}
114121
state.presses.push(${webContents.id});
115122
})()`);
116123
try {
117124
return await debuggee.sendCommand(method, params, sessionId);
118125
} finally {
119126
if (!host.isDestroyed()) {
127+
// Membership check: a host reload starts a new state without this press.
120128
await host.executeJavaScript(`(() => {
121129
const state = ${HOST_FOCUS};
122-
if (!state) return;
123-
const current = document.activeElement;
124-
if (state.saved?.isConnected && current?.tagName === "WEBVIEW" &&
125-
state.presses.includes(current.getWebContentsId())) state.saved.focus({ preventScroll: true });
126-
state.presses.splice(state.presses.indexOf(${webContents.id}), 1);
130+
const index = state ? state.presses.indexOf(${webContents.id}) : -1;
131+
if (index < 0) return;
132+
state.presses.splice(index, 1);
127133
if (state.presses.length === 0) delete ${HOST_FOCUS};
134+
const current = document.activeElement;
135+
try {
136+
if (state.saved?.isConnected && current?.tagName === "WEBVIEW" &&
137+
current.getWebContentsId() === ${webContents.id}) state.saved.focus({ preventScroll: true });
138+
} catch {}
128139
})()`);
129140
}
130141
}

0 commit comments

Comments
 (0)