The best way to control Claude Code, Codex and more from any computer or phone
fix(desktop): picked element keeps its screenshot when the page re-renders before Attach (#38357)
Julius Marminge committed
Nov 17, 2026 at 15:37 UTC
856c84e4b14653facaa3ee320cd165e4e1b103c1
2 files changed
+59
-5
apps/desktop/src/preview/PickPreload.test.ts
+43
-2
@@ -1,9 +1,10 @@
1
import { expect, it, vi } from "vite-plus/test";
2
3
-import { START_PICK_CHANNEL } from "./GuestProtocol.ts";
3
+import { ELEMENT_PICKED_CHANNEL, START_PICK_CHANNEL } from "./GuestProtocol.ts";
4
import "./PickPreload.ts";
5
6
const ipcListeners = vi.hoisted(() => new Map<string, Array<(...args: unknown[]) => void>>());
7
+const ipcSend = vi.hoisted(() => ({ calls: [] as unknown[][] }));
8
9
vi.mock("electron", () => ({
10
ipcRenderer: {
@@ -11,7 +12,9 @@ vi.mock("electron", () => ({
12
ipcListeners.set(channel, [...(ipcListeners.get(channel) ?? []), listener]);
13
},
14
off: () => {},
14
- send: () => {},
15
+ send: (...args: unknown[]) => {
16
+ ipcSend.calls.push(args);
17
+ },
18
invoke: async () => undefined,
19
},
20
}));
@@ -105,3 +108,41 @@ it("keeps the annotation when Escape dismisses an IME candidate in the comment",
108
expect(shadowRoot.host.isConnected).toBe(true);
109
expect(comment.value).toBe("draft");
110
});
111
+
112
+it.each([
113
+ { scrolled: false, rect: { x: 40, y: 60, width: 200, height: 120 } },
114
+ // The last rect would point at whatever now sits there, so no crop beats a wrong one.
115
+ { scrolled: true, rect: { x: 0, y: 0, width: 0, height: 0 } },
116
+])(
117
+ "submits a replaced element's last rect unless the page scrolled ($scrolled)",
118
+ async ({ scrolled, rect }) => {
119
+ ipcSend.calls.length = 0;
120
+ const card = document.body.appendChild(document.createElement("div"));
121
+ const pickedRect = new DOMRect(40, 60, 200, 120);
122
+ // Browsers report a detached element at 0x0 in the corner.
123
+ using _rect = vi
124
+ .spyOn(card, "getBoundingClientRect")
125
+ .mockImplementation(() => (card.isConnected ? pickedRect : new DOMRect()));
126
+ document.elementsFromPoint = () => [card];
127
+ const shadowRoot = startAnnotation();
128
+
129
+ card.dispatchEvent(
130
+ new PointerEvent("pointerdown", { button: 0, clientX: 100, clientY: 100, bubbles: true }),
131
+ );
132
+ // A polling page re-renders while the comment is written and replaces the picked node.
133
+ card.replaceWith(document.createElement("div"));
134
+ if (scrolled) window.dispatchEvent(new Event("scroll"));
135
+ const attach = Array.from(shadowRoot.querySelectorAll("button")).find(
136
+ (button) => button.textContent === "Attach",
137
+ );
138
+ if (!attach) throw new Error("Attach button not found");
139
+ attach.click();
140
+
141
+ await vi.waitFor(() =>
142
+ expect(ipcSend.calls.some(([channel]) => channel === ELEMENT_PICKED_CHANNEL)).toBe(true),
143
+ );
144
+ const [, annotation] =
145
+ ipcSend.calls.find(([channel]) => channel === ELEMENT_PICKED_CHANNEL) ?? [];
146
+ expect((annotation as { elements: Array<{ rect: unknown }> }).elements[0]?.rect).toEqual(rect);
147
+ },
148
+);
apps/desktop/src/preview/PickPreload.ts
+16
-3
@@ -151,6 +151,8 @@ interface SelectedElement {
151
outline: HTMLDivElement;
152
label: HTMLDivElement;
153
baselineStyles: Map<string, string>;
154
+ // Last rect measured while connected; a detached node reports 0x0.
155
+ rect: PreviewAnnotationRect;
156
}
157
158
interface AnnotationSession {
@@ -417,6 +419,7 @@ function updateSelectedVisual(target: SelectedElement): void {
419
target.label.style.display = "none";
420
return;
421
}
422
+ target.rect = rectFromDomRect(target.element.getBoundingClientRect());
423
const rect = visibleElementRect(target.element);
424
positionBox(target.outline, rect);
425
if (rect.width === 0 || rect.height === 0) {
@@ -966,6 +969,7 @@ function startAnnotation(
969
outline: createBox(PRIMARY, PRIMARY_FILL),
970
label: createLabel(),
971
baselineStyles: new Map(),
972
+ rect: rectFromDomRect(element.getBoundingClientRect()),
973
};
974
selected.set(element, target);
975
root.append(target.outline, target.label);
@@ -1392,6 +1396,13 @@ function startAnnotation(
1396
for (const target of selected.values()) updateSelectedVisual(target);
1397
queueEditorLayout();
1398
};
1399
+ const onScroll = (): void => {
1400
+ // A detached element's last rect no longer matches what is on screen once the page scrolls.
1401
+ for (const target of selected.values()) {
1402
+ if (!target.element.isConnected) target.rect = { x: 0, y: 0, width: 0, height: 0 };
1403
+ }
1404
+ repaint();
1405
+ };
1406
// A scrollbar appearing narrows the viewport without a window resize.
1407
const rootResizeObserver = new ResizeObserver(repaint);
1408
@@ -1626,7 +1637,7 @@ function startAnnotation(
1637
window.removeEventListener("focus", onWindowFocus);
1638
window.removeEventListener("keydown", onKeyDown, true);
1639
window.removeEventListener("keyup", onVoiceKeyUp, true);
1629
- window.removeEventListener("scroll", repaint, true);
1640
+ window.removeEventListener("scroll", onScroll, true);
1641
window.removeEventListener("resize", repaint);
1642
dragHandle.removeEventListener("pointerdown", onEditorPointerDown);
1643
dragHandle.removeEventListener("pointermove", onEditorPointerMove);
@@ -1723,7 +1734,9 @@ function startAnnotation(
1734
return {
1735
id: target.id,
1736
element,
1726
- rect: rectFromDomRect(target.element.getBoundingClientRect()),
1737
+ rect: target.element.isConnected
1738
+ ? rectFromDomRect(target.element.getBoundingClientRect())
1739
+ : target.rect,
1740
};
1741
}),
1742
)
@@ -1785,7 +1798,7 @@ function startAnnotation(
1798
window.addEventListener("focus", onWindowFocus);
1799
window.addEventListener("keydown", onKeyDown, { capture: true });
1800
window.addEventListener("keyup", onVoiceKeyUp, { capture: true });
1788
- window.addEventListener("scroll", repaint, { capture: true, passive: true });
1801
+ window.addEventListener("scroll", onScroll, { capture: true, passive: true });
1802
window.addEventListener("resize", repaint, { passive: true });
1803
// Bubble phase, so the overlay's own handlers still see these events.
1804
for (const type of PAGE_ISOLATED_EVENTS)