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 21, 18:30 UTC
fix(desktop): long annotation strokes stay cheap to draw (#24573)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Victor committed
Nov 12, 2026 at 16:48 UTC
068563516ab5c2861a46eab157aa892cb669dccd
3 files changed
+183
-50
apps/desktop/src/preview/AnnotationStroke.test.ts
new
+73
@@ -0,0 +1,73 @@
1
+import type { PreviewAnnotationPoint } from "@t3tools/contracts";
2
+import { describe, expect, it } from "vite-plus/test";
3
+
4
+import {
5
+ addStrokePoint,
6
+ openStrokePath,
7
+ startStroke,
8
+ strokeBounds,
9
+ STROKE_SEGMENTS_PER_PATH,
10
+} from "./AnnotationStroke.ts";
11
+
12
+// The Draw tool used to rebuild the whole path and rescan every point on each move.
13
+const fullPath = (points: ReadonlyArray<PreviewAnnotationPoint>): string => {
14
+ let path = `M ${points[0]!.x} ${points[0]!.y}`;
15
+ for (let index = 1; index < points.length - 1; index += 1) {
16
+ const current = points[index]!;
17
+ const next = points[index + 1]!;
18
+ path += ` Q ${current.x} ${current.y} ${(current.x + next.x) / 2} ${(current.y + next.y) / 2}`;
19
+ }
20
+ const last = points[points.length - 1]!;
21
+ return `${path} L ${last.x} ${last.y}`;
22
+};
23
+
24
+const fullBounds = (points: ReadonlyArray<PreviewAnnotationPoint>, width: number) => {
25
+ const xs = points.map((point) => point.x);
26
+ const ys = points.map((point) => point.y);
27
+ const padding = width + 3;
28
+ const left = Math.min(...xs) - padding;
29
+ const top = Math.min(...ys) - padding;
30
+ const right = Math.max(...xs) + padding;
31
+ const bottom = Math.max(...ys) + padding;
32
+ return { x: left, y: top, width: right - left, height: bottom - top };
33
+};
34
+
35
+// Each later path starts with a move to where the previous one ended.
36
+const joinPaths = (paths: ReadonlyArray<string>): string =>
37
+ paths
38
+ .map((path, index) => {
39
+ if (index === 0) return path;
40
+ const resume = path.match(/^M (\S+ \S+)/)![1]!;
41
+ expect(paths[index - 1]!.endsWith(` ${resume}`)).toBe(true);
42
+ return path.slice(`M ${resume}`.length);
43
+ })
44
+ .join("");
45
+
46
+describe("stroke geometry", () => {
47
+ it("draws the same curve and bounds as a full rescan at every length", () => {
48
+ const points = Array.from({ length: STROKE_SEGMENTS_PER_PATH * 2 + 30 }, (_, index) => ({
49
+ x: index * 3 + ((index * 7) % 11),
50
+ y: 200 + ((index * 13) % 29) - index,
51
+ }));
52
+ const geometry = startStroke(points[0]!);
53
+ const closedPaths: string[] = [];
54
+ for (let index = 1; index < points.length; index += 1) {
55
+ const closedPath = addStrokePoint(geometry, points[index]!);
56
+ if (closedPath !== null) closedPaths.push(closedPath);
57
+ const drawn = points.slice(0, index + 1);
58
+ expect(joinPaths([...closedPaths, openStrokePath(geometry)])).toBe(fullPath(drawn));
59
+ expect(strokeBounds(geometry, 4)).toEqual(fullBounds(drawn, 4));
60
+ }
61
+ expect(closedPaths).toHaveLength(2);
62
+ expect(geometry.points).toEqual(points);
63
+ });
64
+
65
+ it("keeps the rewritten path short for a stroke past the engine's argument limit", () => {
66
+ const geometry = startStroke({ x: 0, y: 0 });
67
+ for (let index = 1; index < 250_000; index += 1) {
68
+ addStrokePoint(geometry, { x: index % 800, y: index % 600 });
69
+ }
70
+ expect(strokeBounds(geometry, 4)).toEqual({ x: -7, y: -7, width: 813, height: 613 });
71
+ expect(openStrokePath(geometry).length).toBeLessThan(STROKE_SEGMENTS_PER_PATH * 40);
72
+ });
73
+});
apps/desktop/src/preview/AnnotationStroke.ts
new
+69
@@ -0,0 +1,69 @@
1
+import type { PreviewAnnotationPoint, PreviewAnnotationRect } from "@t3tools/contracts";
2
+
3
+// Setting a path's `d` makes Chromium parse the whole string again, so a long
4
+// stroke is drawn as a run of paths and only the last one is rewritten per move.
5
+export const STROKE_SEGMENTS_PER_PATH = 100;
6
+
7
+export interface StrokeGeometry {
8
+ readonly points: PreviewAnnotationPoint[];
9
+ minX: number;
10
+ minY: number;
11
+ maxX: number;
12
+ maxY: number;
13
+ // The curve through the newest point is still open: where it ends depends on
14
+ // the point after it. This holds only the settled segments of the last path.
15
+ openPath: string;
16
+ openSegments: number;
17
+}
18
+
19
+export function startStroke(point: PreviewAnnotationPoint): StrokeGeometry {
20
+ return {
21
+ points: [point],
22
+ minX: point.x,
23
+ minY: point.y,
24
+ maxX: point.x,
25
+ maxY: point.y,
26
+ openPath: `M ${point.x} ${point.y}`,
27
+ openSegments: 0,
28
+ };
29
+}
30
+
31
+// Returns the final `d` of the path this point filled, if it filled one.
32
+export function addStrokePoint(
33
+ geometry: StrokeGeometry,
34
+ point: PreviewAnnotationPoint,
35
+): string | null {
36
+ const previous = geometry.points[geometry.points.length - 1]!;
37
+ let closedPath: string | null = null;
38
+ if (geometry.points.length > 1) {
39
+ const midX = (previous.x + point.x) / 2;
40
+ const midY = (previous.y + point.y) / 2;
41
+ geometry.openPath += ` Q ${previous.x} ${previous.y} ${midX} ${midY}`;
42
+ geometry.openSegments += 1;
43
+ if (geometry.openSegments === STROKE_SEGMENTS_PER_PATH) {
44
+ closedPath = geometry.openPath;
45
+ geometry.openPath = `M ${midX} ${midY}`;
46
+ geometry.openSegments = 0;
47
+ }
48
+ }
49
+ geometry.points.push(point);
50
+ geometry.minX = Math.min(geometry.minX, point.x);
51
+ geometry.minY = Math.min(geometry.minY, point.y);
52
+ geometry.maxX = Math.max(geometry.maxX, point.x);
53
+ geometry.maxY = Math.max(geometry.maxY, point.y);
54
+ return closedPath;
55
+}
56
+
57
+export function openStrokePath(geometry: StrokeGeometry): string {
58
+ const last = geometry.points[geometry.points.length - 1]!;
59
+ return `${geometry.openPath} L ${last.x} ${last.y}`;
60
+}
61
+
62
+export function strokeBounds(geometry: StrokeGeometry, width: number): PreviewAnnotationRect {
63
+ const padding = width + 3;
64
+ const left = geometry.minX - padding;
65
+ const top = geometry.minY - padding;
66
+ const right = geometry.maxX + padding;
67
+ const bottom = geometry.maxY + padding;
68
+ return { x: left, y: top, width: right - left, height: bottom - top };
69
+}
apps/desktop/src/preview/PickPreload.ts
+41
-50
@@ -20,6 +20,13 @@ import type {
20
21
import { createAnnotationIdSource } from "./AnnotationIds.ts";
22
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
23
+import {
24
+ addStrokePoint,
25
+ openStrokePath,
26
+ startStroke,
27
+ strokeBounds,
28
+ type StrokeGeometry,
29
+} from "./AnnotationStroke.ts";
30
import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts";
31
import { installRecordingCursor } from "./RecordingCursor.ts";
32
import { DEFAULT_RECORDING_INPUT_OPTIONS } from "./RecordingInput.ts";
@@ -546,34 +553,6 @@ function createUnitInput(unit: string, placeholder = "0"): HTMLInputElement {
553
return input;
554
}
555
549
-function pathFromPoints(points: ReadonlyArray<PreviewAnnotationPoint>): string {
550
- if (points.length === 0) return "";
551
- if (points.length === 1) return `M ${points[0]!.x} ${points[0]!.y} l 0.01 0.01`;
552
- let path = `M ${points[0]!.x} ${points[0]!.y}`;
553
- for (let index = 1; index < points.length - 1; index += 1) {
554
- const current = points[index]!;
555
- const next = points[index + 1]!;
556
- path += ` Q ${current.x} ${current.y} ${(current.x + next.x) / 2} ${(current.y + next.y) / 2}`;
557
- }
558
- const last = points[points.length - 1]!;
559
- path += ` L ${last.x} ${last.y}`;
560
- return path;
561
-}
562
-
563
-function strokeBounds(
564
- points: ReadonlyArray<PreviewAnnotationPoint>,
565
- width: number,
566
-): PreviewAnnotationRect {
567
- const xs = points.map((point) => point.x);
568
- const ys = points.map((point) => point.y);
569
- const padding = width + 3;
570
- const left = Math.min(...xs) - padding;
571
- const top = Math.min(...ys) - padding;
572
- const right = Math.max(...xs) + padding;
573
- const bottom = Math.max(...ys) + padding;
574
- return { x: left, y: top, width: right - left, height: bottom - top };
575
-}
576
-
556
function startAnnotation(
557
sendEnabled: boolean,
558
voice: DesktopPreviewAnnotationVoiceConfig | undefined,
@@ -910,7 +889,12 @@ function startAnnotation(
889
}
890
let tool: AnnotationTool = isPdf ? "marquee" : "select";
891
let dragStart: PreviewAnnotationPoint | null = null;
913
- let activeStroke: { target: PreviewAnnotationStrokeTarget; path: SVGPathElement } | null = null;
892
+ let activeStroke: {
893
+ target: PreviewAnnotationStrokeTarget;
894
+ geometry: StrokeGeometry;
895
+ group: SVGGElement;
896
+ path: SVGPathElement;
897
+ } | null = null;
898
let pendingCapture = false;
899
let editorExpanded = false;
900
let editorWasShown = false;
@@ -1510,15 +1494,18 @@ function startAnnotation(
1494
return;
1495
}
1496
if (tool === "draw" && activeStroke) {
1513
- activeStroke.target.points = [
1514
- ...activeStroke.target.points,
1515
- { x: event.clientX, y: event.clientY },
1516
- ];
1517
- activeStroke.target.bounds = strokeBounds(
1518
- activeStroke.target.points,
1519
- activeStroke.target.width,
1520
- );
1521
- activeStroke.path.setAttribute("d", pathFromPoints(activeStroke.target.points));
1497
+ const closedPath = addStrokePoint(activeStroke.geometry, {
1498
+ x: event.clientX,
1499
+ y: event.clientY,
1500
+ });
1501
+ activeStroke.target.bounds = strokeBounds(activeStroke.geometry, activeStroke.target.width);
1502
+ if (closedPath !== null) {
1503
+ activeStroke.path.setAttribute("d", closedPath);
1504
+ activeStroke.path = activeStroke.group.appendChild(
1505
+ document.createElementNS("http://www.w3.org/2000/svg", "path"),
1506
+ );
1507
+ }
1508
+ activeStroke.path.setAttribute("d", openStrokePath(activeStroke.geometry));
1509
}
1510
};
1511
@@ -1537,23 +1524,27 @@ function startAnnotation(
1524
}
1525
dragStart = { x: event.clientX, y: event.clientY };
1526
if (tool === "draw") {
1527
+ const geometry = startStroke(dragStart);
1528
const stroke: PreviewAnnotationStrokeTarget = {
1529
id: nextId("stroke"),
1530
color: annotationTheme?.primary ?? "#2563eb",
1531
width: 4,
1544
- points: [dragStart],
1532
+ points: geometry.points,
1533
bounds: { x: dragStart.x, y: dragStart.y, width: 1, height: 1 },
1534
};
1547
- const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
1548
- path.setAttribute(OVERLAY_ATTRIBUTE, "");
1549
- path.setAttribute("data-stroke-id", stroke.id);
1550
- path.setAttribute("fill", "none");
1551
- path.setAttribute("stroke", stroke.color);
1552
- path.setAttribute("stroke-width", String(stroke.width));
1553
- path.setAttribute("stroke-linecap", "round");
1554
- path.setAttribute("stroke-linejoin", "round");
1555
- svg.appendChild(path);
1556
- activeStroke = { target: stroke, path };
1535
+ const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
1536
+ group.setAttribute(OVERLAY_ATTRIBUTE, "");
1537
+ group.setAttribute("data-stroke-id", stroke.id);
1538
+ group.setAttribute("fill", "none");
1539
+ group.setAttribute("stroke", stroke.color);
1540
+ group.setAttribute("stroke-width", String(stroke.width));
1541
+ group.setAttribute("stroke-linecap", "round");
1542
+ group.setAttribute("stroke-linejoin", "round");
1543
+ const path = group.appendChild(
1544
+ document.createElementNS("http://www.w3.org/2000/svg", "path"),
1545
+ );
1546
+ svg.appendChild(group);
1547
+ activeStroke = { target: stroke, geometry, group, path };
1548
}
1549
};
1550
@@ -1580,7 +1571,7 @@ function startAnnotation(
1571
}
1572
} else if (tool === "draw" && activeStroke) {
1573
if (activeStroke.target.points.length > 1) strokes.push(activeStroke.target);
1583
- else activeStroke.path.remove();
1574
+ else activeStroke.group.remove();
1575
activeStroke = null;
1576
}
1577
dragStart = null;