@devries-cameron20 / t3code Mirror · read-only / commits / dd6c05ec31
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 07:00 UTC

fix(preview): agent browser use no longer steals composer focus (#28613)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Nov 1, 2026 at 18:23 UTC dd6c05ec313c6c870c7a7688e87143da5084031d
5 files changed +94 -16
apps/desktop/src/preview/GuestInput.test.ts
+53
@@ -124,6 +124,59 @@ describe("preview guest input", () => {
124 },
125 );
126
127 + it.each([{ userClicksStuckTab: false }, { userClicksStuckTab: true }])(
128 + "keeps the right focus when a sibling press finishes beside 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;
135 + },
136 + };
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 }]),
145 + );
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 + );
179 +
180 it.each([
181 {
182 method: "Input.dispatchMouseEvent",
apps/desktop/src/preview/GuestInput.ts
+25 -10
@@ -92,7 +92,8 @@ const evaluate = async (debuggee: Electron.Debugger, expression: string) => {
92 return reply.result?.value;
93 };
94
95 -let focusSequence = 0;
95 +/** Host focus shared by the presses in flight in one window, so a later press keeps the field an earlier one saved. */
96 +const HOST_FOCUS = "globalThis.__t3PreviewFocus";
97
98 /** A CDP pointer press focuses the guest's native view even with focus emulation enabled. */
99 const sendPointerPress = async (
@@ -106,22 +107,36 @@ const sendPointerPress = async (
107 if (!host || host.isDestroyed() || webContents.focusedFrame) {
108 return debuggee.sendCommand(method, params, sessionId);
109 }
109 - const key = JSON.stringify(`__t3PreviewFocus${++focusSequence}`);
110 + // Focus in a webview with a press in flight came from that press, so keep the earlier save.
111 await host.executeJavaScript(`(() => {
112 + const state = (${HOST_FOCUS} ??= { saved: null, presses: [] });
113 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 - };
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 + }
121 + state.presses.push(${webContents.id});
122 })()`);
123 try {
124 return await debuggee.sendCommand(method, params, sessionId);
125 } finally {
126 if (!host.isDestroyed()) {
124 - await host.executeJavaScript(`globalThis[${key}]?.()`);
127 + // Membership check: a host reload starts a new state without this press.
128 + await host.executeJavaScript(`(() => {
129 + const state = ${HOST_FOCUS};
130 + const index = state ? state.presses.indexOf(${webContents.id}) : -1;
131 + if (index < 0) return;
132 + state.presses.splice(index, 1);
133 + 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 {}
139 + })()`);
140 }
141 }
142 };
apps/web/src/components/preview/agentOpenedPreviews.test.ts
+3
@@ -230,11 +230,14 @@ describe("revealServerTabs", () => {
230 expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).not.toBe(
231 browserSurfaceId,
232 );
233 + const userActionRevision = useRightPanelStore.getState().getUserActionRevision(ref);
234 revealServerTabs(ref, [tab("tab_a", true)], { autoShowFloatingPreview: true });
235 expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).toBe(
236 browserSurfaceId,
237 );
238 expect(floatingTabId()).toBeNull();
239 + // A user action moves keyboard focus into the panel; an agent reveal must not.
240 + expect(useRightPanelStore.getState().getUserActionRevision(ref)).toBe(userActionRevision);
241 });
242 });
243
apps/web/src/components/preview/agentOpenedPreviews.ts
+1 -1
@@ -49,7 +49,7 @@ export function revealServerTabs(
49 (surface) => surface.kind === "preview" && surface.resourceId === session.tabId,
50 );
51 if (surface && force) {
52 - useRightPanelStore.getState().activateSurface(threadRef, surface.id);
52 + useRightPanelStore.getState().revealSurface(threadRef, surface.id);
53 } else if (!surface) {
54 usePreviewMiniPlayerStore.getState().open(threadRef, browserMiniPlayerSource(session.tabId));
55 }
apps/web/src/rightPanelStore.ts
+12 -5
@@ -166,6 +166,8 @@ interface RightPanelStoreState {
166 activateTerminal: (ref: ScopedThreadRef, surfaceId: string, terminalId: string) => void;
167 closeTerminal: (ref: ScopedThreadRef, surfaceId: string, terminalId: string) => void;
168 activateSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
169 + /** Agent-driven activation. Not a user action, so it does not move keyboard focus. */
170 + revealSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
171 closeSurface: (ref: ScopedThreadRef, surfaceId: string) => void;
172 closeOtherSurfaces: (ref: ScopedThreadRef, surfaceId: string) => void;
173 closeSurfacesToRight: (ref: ScopedThreadRef, surfaceId: string) => void;
@@ -451,6 +453,11 @@ const userActionOnThread = (
453 : {}),
454 });
455
456 +const activated = (current: ThreadRightPanelState, surfaceId: string): ThreadRightPanelState =>
457 + current.surfaces.some((surface) => surface.id === surfaceId)
458 + ? { ...current, isOpen: true, activeSurfaceId: surfaceId }
459 + : current;
460 +
461 const closeAction = (
462 state: RightPanelStoreState,
463 threadKey: string,
@@ -928,11 +935,11 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
935 ),
936 activateSurface: (ref, surfaceId) =>
937 set((state) =>
931 - userAction(state, scopedThreadKey(ref), (current) =>
932 - current.surfaces.some((surface) => surface.id === surfaceId)
933 - ? { ...current, isOpen: true, activeSurfaceId: surfaceId }
934 - : current,
935 - ),
938 + userAction(state, scopedThreadKey(ref), (current) => activated(current, surfaceId)),
939 + ),
940 + revealSurface: (ref, surfaceId) =>
941 + set((state) =>
942 + automaticUpdate(state, scopedThreadKey(ref), (current) => activated(current, surfaceId)),
943 ),
944 closeSurface: (ref, surfaceId) =>
945 set((state) =>