fix(desktop): reject clicks in hidden previews #8495
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 9 commits into
main fromt3code/fix-preview-hidden-clicks-3717opened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/desktop/src/preview/Manager.ts lines 3409 to 3411OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3305,5 +3393,19 @@33053393 input: PreviewAutomationClickInput,33063394 ) {33073395 const wc = yield* requireWebContents(tabId);3308- yield* withControlSession(tabId, wc, "click", (send) =>3309- performAutomationClick(tabId, input, send),3396+ const attachment = (yield* Ref.get(attachedRef)).get(wc.id);3397+ if (attachment?.webContents !== wc) {3398+ return yield* new PreviewWebContentsNotFoundError({ tabId, webContentsId: wc.id });3399+ }3400+ return yield* Effect.gen(function* () {3401+ yield* attachment.presentation.semaphore.withPermit(3402+ assertAutomationClickVisible(tabId, wc, attachment),3403+ );3404+ yield* withControlSession(tabId, wc, "click", (send) =>3405+ performAutomationClick(tabId, wc, input, attachment, send),3406+ );3407+ return { _tag: "Dispatched" } as const;3408+ }).pipe(3409+ Effect.catchTag("PreviewAutomationTabNotVisibleError", () =>3410+ Effect.succeed({ _tag: "NotSent", reason: "tab-not-visible" } as const),3411+ ),
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 231OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -210,4 +227,5 @@210227 const { snapshot, tabId } = resolvePreviewAutomationTarget(state, requestedTabId);211228 const runtimeTabId = tabId ? previewRuntimeTabId(threadRef, state.serverEpoch, tabId) : null;212229 const visible = runtimeTabId213- ? (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible ?? false)230+ ? Boolean(231+ useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible &&
- apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 89 to 89OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -79,4 +84,6 @@7984 const waitForPreviewPresentation = async (runtimeTabId: string): Promise<void> => {8085 const deadline = Date.now() + PREVIEW_PRESENTATION_SETTLE_TIMEOUT_MS;8186 while (Date.now() <= deadline) {82- if (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible) return;87+ const visible = useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible;88+ const webview = findPreviewWebview(runtimeTabId);89+ if (visible && webview?.getAttribute("data-preview-main-visible") === "true") return;apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 230 to 233OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -210,4 +227,7 @@210227 const { snapshot, tabId } = resolvePreviewAutomationTarget(state, requestedTabId);211228 const runtimeTabId = tabId ? previewRuntimeTabId(threadRef, state.serverEpoch, tabId) : null;212229 const visible = runtimeTabId213- ? (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible ?? false)230+ ? Boolean(231+ useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible &&232+ findPreviewWebview(runtimeTabId)?.getAttribute("data-preview-main-visible") === "true",233+ )
- apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 230 to 233OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -210,4 +227,7 @@210227 const { snapshot, tabId } = resolvePreviewAutomationTarget(state, requestedTabId);211228 const runtimeTabId = tabId ? previewRuntimeTabId(threadRef, state.serverEpoch, tabId) : null;212229 const visible = runtimeTabId213- ? (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible ?? false)230+ ? Boolean(231+ useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible &&232+ findPreviewWebview(runtimeTabId)?.getAttribute("data-preview-main-visible") === "true",233+ )apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 89 to 89OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -79,4 +84,6 @@7984 const waitForPreviewPresentation = async (runtimeTabId: string): Promise<void> => {8085 const deadline = Date.now() + PREVIEW_PRESENTATION_SETTLE_TIMEOUT_MS;8186 while (Date.now() <= deadline) {82- if (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible) return;87+ const visible = useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible;88+ const webview = findPreviewWebview(runtimeTabId);89+ if (visible && webview?.getAttribute("data-preview-main-visible") === "true") return;
- apps/web/src/browser/HostedBrowserWebview.tsx lines 75 to 77OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -67,7 +67,11 @@6767 fittedSourceContent: current?.fittedSourceContent ?? null,6868 rect: resolveBrowserSurfacePanelRect(state.byTabId, runtimeTabId),6969 visible: current?.visible ?? false,70+ automationClickHolds: current?.automationClickHolds ?? 0,7071 };7172 }),7273 );7374 usePreviewBridge({ threadRef, tabId, runtimeTabId });75+ const desiredActive = presentation.visible && presentation.rect !== null;76+ const active =77+ (desiredActive || presentation.automationClickHolds > 0) && presentation.rect !== null;
- apps/web/src/browser/hostedBrowserWebviewStyle.ts lines 43 to 43OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -25,11 +26,18 @@2526 readonly hiddenSize: HostedBrowserWebviewSize;27+ readonly interactive: boolean;2628 }): HostedBrowserWebviewWrapperStyle {27- const { active, cornerRadius = 0, hiddenSize, rect } = input;29+ const {30+ active,31+ automationClickHeld = false,32+ cornerRadius = 0,33+ hiddenSize,34+ interactive,35+ rect,36+ } = input;2837 if (active && rect) {2938 return {3039 left: rect.x,3140 top: rect.y,3241 width: rect.width,3342 height: rect.height,34- zIndex: 30,35- pointerEvents: "auto",43+ zIndex: automationClickHeld ? 31 : 30,
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 636OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -594,0 +616,21 @@616+ throw new PreviewAutomationTabNotVisibleHostError({617+ requestId: request.requestId,618+ operation: request.operation,619+ environmentId,620+ threadId: request.threadId,621+ tabId: ready.tabId,622+ });623+ }624+ try {625+ const webview = findPreviewWebview(ready.runtimeTabId);626+ const registration = readPreviewWebviewRegistration(webview);627+ const setWebviewVisibility = ready.bridge.setWebviewVisibility;628+ if (!registration || !setWebviewVisibility) {629+ throw new PreviewAutomationTargetUnavailableError({630+ requestId: request.requestId,631+ operation: request.operation,632+ environmentId,633+ threadId: request.threadId,634+ tabId: ready.tabId,635+ bridgeAvailable: Boolean(setWebviewVisibility),636+ });
- apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 334 to 347Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -123,0 +314,21 @@314+ const currentPresentation = useBrowserSurfaceStore.getState().byTabId[runtimeTabId];315+ const currentWebview = findPreviewWebview(runtimeTabId);316+ const currentRegistration = readPreviewWebviewRegistration(currentWebview);317+ return (318+ currentPresentation?.visible === true &&319+ currentPresentation.owner === presentationOwner &&320+ currentWebview === webview &&321+ currentRegistration?.webContentsId === registration.webContentsId &&322+ currentRegistration.attachmentId === registration.attachmentId323+ );324+ };325+ const setCapturedWebviewHidden = () => {326+ if (!registration) return Promise.resolve();327+ webview.removeAttribute("data-preview-main-visible");328+ return setWebviewVisibility(329+ runtimeTabId,330+ registration.webContentsId,331+ registration.attachmentId,332+ false,333+ ).catch(() => undefined);334+ };
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 378Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -123,0 +321,21 @@321+ currentRegistration?.webContentsId === registration.webContentsId &&322+ currentRegistration.attachmentId === registration.attachmentId323+ );324+ };325+ const setCapturedWebviewHidden = () => {326+ if (!registration) return Promise.resolve();327+ webview.removeAttribute("data-preview-main-visible");328+ return setWebviewVisibility(329+ runtimeTabId,330+ registration.webContentsId,331+ registration.attachmentId,332+ false,333+ ).catch(() => undefined);334+ };335+ const assertRuntimeCurrentBeforeDispatch = () => {336+ try {337+ assertRuntimeCurrent();338+ } catch (cause) {339+ void setCapturedWebviewHidden();340+ throw cause;341+ }
- apps/desktop/src/preview/Manager.ts lines 4127 to 4127OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3998,3 +4121,7 @@39984121 }39994122 }400041234124+export class PreviewAutomationTabNotVisibleError extends Schema.TaggedErrorClass<PreviewAutomationTabNotVisibleError>()(4125+ "PreviewAutomationTabNotVisibleError",4126+ {4127+ operation: Schema.Literal("click"),apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 417 to 422OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -123,0 +402,21 @@402+ }403+ try {404+ assertPreviewClickBeforeDeadline(context, timing);405+ await awaitPreviewClickStep(406+ () =>407+ setWebviewVisibility(408+ runtimeTabId,409+ capturedRegistration.webContentsId,410+ capturedRegistration.attachmentId,411+ true,412+ ),413+ context,414+ timing,415+ { invalidationSignal: invalidation.signal },416+ );417+ } catch (cause) {418+ if (!invalidated && targetIsCurrent()) {419+ throw cause;420+ }421+ if (!invalidated) void setCapturedWebviewHidden();422+ throw new PreviewAutomationTabNotVisibleHostError(context);
- apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 659 to 660OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -302,3 +654,7 @@302654303655 const handleRequest = useCallback(304656 async (request: PreviewAutomationRequest): Promise<unknown> => {657+ const clickTiming = {658+ deadline:659+ performance.now() + Math.max(0, request.timeoutMs - PREVIEW_CLICK_RESPONSE_RESERVE_MS),660+ timeoutMs: request.timeoutMs,
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 469Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -123,0 +428,21 @@428+ // This second false starts after the true acknowledgement, so a hide429+ // during that acknowledgement cannot leave the old attachment visible.430+ void setCapturedWebviewHidden();431+ throw new PreviewAutomationTabNotVisibleHostError(context);432+ }433+ webview.setAttribute("data-preview-main-visible", "true");434+ assertPreviewClickBeforeDeadline(context, timing);435+ assertRuntimeCurrentBeforeDispatch();436+ const result = await awaitPreviewClickStep(437+ () =>438+ bridge.automation.click(439+ runtimeTabId,440+ input,441+ capturedRegistration.webContentsId,442+ capturedRegistration.attachmentId,443+ ),444+ context,445+ timing,446+ {447+ timeoutError: () => new PreviewAutomationClickDeliveryUnconfirmedHostError(context),448+ },apps/desktop/src/preview/Manager.ts line 2118Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2041,0 +2076,21 @@2076+ }2077+ yield* attachment.presentation.semaphore.withPermit(2078+ Effect.gen(function* () {2079+ const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId);2080+ const wc = webContents.fromId(webContentsId);2081+ const currentAttachment = (yield* Ref.get(attachedRef)).get(webContentsId);2082+ if (2083+ tab?.webContentsId !== webContentsId ||2084+ !wc ||2085+ wc.isDestroyed() ||2086+ currentAttachment !== attachment ||2087+ attachment.attachmentId !== attachmentId ||2088+ attachment.webContents !== wc ||2089+ !attachment.presentation.active2090+ ) {2091+ return yield* new PreviewWebContentsNotFoundError({ tabId, webContentsId });2092+ }2093+ attachment.presentation.visible = visible;2094+ }),2095+ );2096+ });
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/web/src/components/preview/PreviewAutomationHosts.tsx lines 449 to 449OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -123,0 +429,21 @@429+ if (!invalidated && targetIsCurrent()) {430+ throw cause;431+ }432+ if (!invalidated) void setCapturedWebviewHidden();433+ throw new PreviewAutomationTabNotVisibleHostError({ ...context, cause });434+ }435+ assertPreviewClickBeforeDeadline(context, timing);436+ assertRuntimeCurrentBeforeDispatch();437+ if (invalidated || !targetIsCurrent()) {438+ invalidate();439+ // This second false starts after the true acknowledgement, so a hide440+ // during that acknowledgement cannot leave the old attachment visible.441+ void setCapturedWebviewHidden();442+ throw new PreviewAutomationTabNotVisibleHostError(context);443+ }444+ webview.setAttribute("data-preview-main-visible", "true");445+ assertPreviewClickBeforeDeadline(context, timing);446+ assertRuntimeCurrentBeforeDispatch();447+ const result = await awaitPreviewClickStep(448+ () => {449+ const timeoutMs = Math.floor(timing.deadline - performance.now());apps/web/src/components/preview/PreviewAutomationHosts.tsx line 987Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -587,5 +966,15 @@587966 return await ready.bridge.automation.snapshot(ready.runtimeTabId);588967 }589968 case "click": {590- const ready = await requireReadyTab();591- return await ready.bridge.automation.click(969+ const clickTabId = tabId;970+ if (!clickTabId) {971+ throw new PreviewAutomationTargetUnavailableError(unavailableTarget);972+ }973+ const clickContext = {974+ requestId: request.requestId,975+ operation: request.operation,976+ environmentId,977+ threadId: request.threadId,978+ tabId: clickTabId,979+ };980+ const ready = await requireReadyTab(clickContext);
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 464Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -123,0 +437,21 @@437+ if (invalidated || !targetIsCurrent()) {438+ invalidate();439+ // This second false starts after the true acknowledgement, so a hide440+ // during that acknowledgement cannot leave the old attachment visible.441+ void setCapturedWebviewHidden();442+ throw new PreviewAutomationTabNotVisibleHostError(context);443+ }444+ webview.setAttribute("data-preview-main-visible", "true");445+ assertPreviewClickBeforeDeadline(context, timing);446+ assertRuntimeCurrentBeforeDispatch();447+ const result = await awaitPreviewClickStep(448+ () => {449+ const timeoutMs = Math.floor(timing.deadline - performance.now());450+ if (timeoutMs <= 0) throw makePreviewClickTimeoutError(context, timing);451+ return bridge.automation.click(452+ runtimeTabId,453+ { ...input, timeoutMs },454+ capturedRegistration.webContentsId,455+ capturedRegistration.attachmentId,456+ );457+ },
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
00855b1f-164f-4036-9c1c-4182771496feYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Comment `@coderabbitai help` to get the list of available commands.