Skip to content

Commit e05ea54

Browse files
t3dotggclaude
andcommitted
fix(desktop): a stuck press in one Browser tab no longer blocks its siblings
Queue pointer commands per tab again, and share only the saved host focus across presses in flight in one window. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fcca49d commit e05ea54

2 files changed

Lines changed: 34 additions & 29 deletions

File tree

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

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

127-
it("preserves host focus when presses in two tabs of one window overlap", async () => {
127+
it("preserves host focus when a sibling tab's press overlaps a stuck one", async () => {
128128
const document = { activeElement: {} as object };
129129
const composer = {
130130
isConnected: true,
@@ -138,15 +138,17 @@ describe("preview guest input", () => {
138138
isDestroyed: () => false,
139139
executeJavaScript: async (script: string) => NodeVM.runInContext(script, context),
140140
};
141-
const firstPressFocused = Promise.withResolvers<void>();
142-
const pressesFinish = Promise.withResolvers<void>();
143-
const press = (id: number) => {
141+
const stuckPressFocused = Promise.withResolvers<void>();
142+
const stuckPressEnds = Promise.withResolvers<void>();
143+
const press = (id: number, stuck: boolean) => {
144144
const preview = { tagName: "WEBVIEW", getWebContentsId: () => id };
145145
const debuggee = {
146146
sendCommand: async () => {
147147
document.activeElement = preview;
148-
firstPressFocused.resolve();
149-
await pressesFinish.promise;
148+
if (stuck) {
149+
stuckPressFocused.resolve();
150+
await stuckPressEnds.promise;
151+
}
150152
return {};
151153
},
152154
};
@@ -157,12 +159,15 @@ describe("preview guest input", () => {
157159
{ type: "mousePressed", x: 10, y: 10, button: "left" },
158160
);
159161
};
160-
const first = press(7);
161-
await firstPressFocused.promise;
162-
const presses = [first, press(8)];
163-
pressesFinish.resolve();
164-
await Promise.all(presses);
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);
165166
expect(document.activeElement).toBe(composer);
167+
stuckPressEnds.resolve();
168+
await stuck;
169+
expect(document.activeElement).toBe(composer);
170+
expect(Object.keys(context)).toEqual(["document"]);
166171
});
167172

168173
it.each([

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

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

95-
let focusSequence = 0;
95+
/** Host focus shared by every guest press in flight in one window, so overlapping presses agree on it. */
96+
const HOST_FOCUS = "globalThis.__t3PreviewFocus";
9697

9798
/** A CDP pointer press focuses the guest's native view even with focus emulation enabled. */
9899
const sendPointerPress = async (
@@ -106,53 +107,52 @@ const sendPointerPress = async (
106107
if (!host || host.isDestroyed() || webContents.focusedFrame) {
107108
return debuggee.sendCommand(method, params, sessionId);
108109
}
109-
const key = JSON.stringify(`__t3PreviewFocus${++focusSequence}`);
110110
await host.executeJavaScript(`(() => {
111+
const state = (${HOST_FOCUS} ??= { saved: null, presses: [] });
111112
const active = document.activeElement;
112-
if (!active || active === document.body || active.tagName === "WEBVIEW") return;
113-
globalThis[${key}] = () => {
114-
delete globalThis[${key}];
115-
const current = document.activeElement;
116-
if (active.isConnected && current?.tagName === "WEBVIEW" &&
117-
current.getWebContentsId() === ${webContents.id}) active.focus({ preventScroll: true });
118-
};
113+
if (active && active !== document.body && active.tagName !== "WEBVIEW") state.saved = active;
114+
state.presses.push(${webContents.id});
119115
})()`);
120116
try {
121117
return await debuggee.sendCommand(method, params, sessionId);
122118
} finally {
123119
if (!host.isDestroyed()) {
124-
await host.executeJavaScript(`globalThis[${key}]?.()`);
120+
await host.executeJavaScript(`(() => {
121+
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);
127+
if (state.presses.length === 0) delete ${HOST_FOCUS};
128+
})()`);
125129
}
126130
}
127131
};
128132

129133
const pointerCommands = new WeakMap<Electron.WebContents, Promise<unknown>>();
130134

131-
/**
132-
* Focus capture crosses processes; later pointer events must not overtake the press.
133-
* Queued per window: a press in a sibling tab would capture that tab's webview as host focus.
134-
*/
135+
/** Focus capture crosses processes; later pointer events must not overtake the press. */
135136
const sendPointerCommand = async (
136137
webContents: Electron.WebContents,
137138
debuggee: Electron.Debugger,
138139
method: string,
139140
params: Record<string, unknown>,
140141
sessionId: string | undefined,
141142
) => {
142-
const queue = webContents.hostWebContents ?? webContents;
143-
const previous = pointerCommands.get(queue) ?? Promise.resolve();
143+
const previous = pointerCommands.get(webContents) ?? Promise.resolve();
144144
const current = previous
145145
.catch(() => {})
146146
.then(() =>
147147
params["type"] === "mousePressed" || params["type"] === "touchStart"
148148
? sendPointerPress(webContents, debuggee, method, params, sessionId)
149149
: debuggee.sendCommand(method, params, sessionId),
150150
);
151-
pointerCommands.set(queue, current);
151+
pointerCommands.set(webContents, current);
152152
try {
153153
return await current;
154154
} finally {
155-
if (pointerCommands.get(queue) === current) pointerCommands.delete(queue);
155+
if (pointerCommands.get(webContents) === current) pointerCommands.delete(webContents);
156156
}
157157
};
158158

0 commit comments

Comments
 (0)