Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RecordingCursor.ts

196 lines7.9 KB

1import type {
2 DesktopPreviewAnnotationTheme,
3 DesktopPreviewRecordingInput,
4} from "@t3tools/contracts";
5​
6import {
7 DEFAULT_RECORDING_INPUT_OPTIONS,
8 recordingKeyLabel,
9 recordingKeysAreSensitive,
10 type RecordingInputOptions,
11 type RecordingKeyPress,
12} from "./RecordingInput.ts";
13​
14/**
15 * Chromium's capture cursor uses native window bounds, which do not follow a
16 * webview's CSS placement or scale. Draw it in the guest's coordinate space
17 * while recording, and make the native cursor transparent to avoid two cursors.
18 */
19export function installRecordingCursor(
20 document: Document,
21 window: Window,
22 options: RecordingInputOptions = DEFAULT_RECORDING_INPUT_OPTIONS,
23 emit: (input: DesktopPreviewRecordingInput) => void = () => {},
24) {
25 const style = document.createElement("style");
26 style.textContent =
27 "html, html * { cursor: none !important; } @media (prefers-reduced-motion: reduce) { [data-t3code-recording-agent-cursor] { transition: none !important; } }";
28 const cursor = document.createElement("div");
29 cursor.setAttribute("aria-hidden", "true");
30 cursor.setAttribute("data-t3code-recording-cursor", "");
31 cursor.style.cssText =
32 "position:fixed;left:0;top:0;width:16px;height:24px;pointer-events:none;z-index:2147483647;display:none;";
33 cursor.innerHTML =
34 '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="24" viewBox="0 0 16 24"><path d="M1 1v18l4-4 4 8 3-1.5-4-8H15Z" fill="black" stroke="white" stroke-width="1.5" stroke-linejoin="round"/></svg>';
35 const agentCursor = document.createElement("div");
36 agentCursor.setAttribute("aria-hidden", "true");
37 agentCursor.setAttribute("data-t3code-recording-agent-cursor", "");
38 agentCursor.style.cssText =
39 "position:fixed;left:0;top:0;width:20px;height:20px;pointer-events:none;z-index:2147483647;display:none;filter:drop-shadow(0 1px 2px #0003);transition:transform 150ms ease-out,opacity 150ms ease-out;";
40 agentCursor.innerHTML =
41 '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="var(--recording-cursor-background,white)" stroke="var(--recording-cursor-primary,#2563eb)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="transform:translate(-2px,-2px)"><path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/></svg>';
42 document.documentElement.append(style, cursor, agentCursor);
43 let controller: "human" | "agent" | "none" = "none";
44 let humanPoint: { readonly x: number; readonly y: number } | null = null;
45 const drawHuman = () => {
46 if (!humanPoint) return;
47 cursor.style.transform = `translate(${humanPoint.x}px, ${humanPoint.y}px)`;
48 cursor.style.display = "block";
49 };
50 let agentActive = false;
51 let agentTimer: number | undefined;
52 const setController = (
53 next: typeof controller,
54 point?: { readonly x: number; readonly y: number },
55 ) => {
56 if (point) humanPoint = point;
57 controller = next;
58 if (next === "agent") cursor.style.display = "none";
59 if (next === "human") drawHuman();
60 if (!agentActive) agentCursor.style.opacity = next === "human" ? "0.18" : "0.35";
61 };
62 const setTheme = (
63 theme: Pick<DesktopPreviewAnnotationTheme, "primary" | "background"> | null,
64 ) => {
65 agentCursor.style.setProperty("--recording-cursor-primary", theme?.primary ?? "#2563eb");
66 agentCursor.style.setProperty("--recording-cursor-background", theme?.background ?? "white");
67 };
68 let lastKeyLabel: string | null = null;
69 let pointerHeld = false;
70 let pointerFrame: number | undefined;
71 let pendingPointer: DesktopPreviewRecordingInput | undefined;
72 const cancelPendingPointer = () => {
73 if (pointerFrame !== undefined) window.cancelAnimationFrame(pointerFrame);
74 pointerFrame = undefined;
75 pendingPointer = undefined;
76 };
77 const keyPress = (input: RecordingKeyPress, held = false) => {
78 if (!options.showKeyPresses) return;
79 const label = recordingKeysAreSensitive(document)
80 ? null
81 : recordingKeyLabel(input, /Mac/.test(window.navigator.platform));
82 lastKeyLabel = label;
83 emit({ type: "key", label, held, width: window.innerWidth });
84 };
85 const pointer = (
86 point: { readonly x: number; readonly y: number },
87 phase: "move" | "down" | "up" | "click",
88 ) => {
89 if (!options.showMousePresses || (phase === "move" && !pointerHeld)) return;
90 if (phase === "down") pointerHeld = true;
91 if (phase === "up") pointerHeld = false;
92 const input: DesktopPreviewRecordingInput = {
93 type: "pointer",
94 phase,
95 ...point,
96 width: window.innerWidth,
97 height: window.innerHeight,
98 };
99 if (phase === "move") {
100 pendingPointer = input;
101 pointerFrame ??= window.requestAnimationFrame(() => {
102 pointerFrame = undefined;
103 if (pendingPointer) emit(pendingPointer);
104 pendingPointer = undefined;
105 });
106 } else {
107 cancelPendingPointer();
108 emit(input);
109 }
110 };
111 const move = (
112 point: { readonly x: number; readonly y: number },
113 phase: "move" | "click" = "move",
114 ) => {
115 agentCursor.style.transform = `translate(${point.x}px, ${point.y}px)`;
116 agentCursor.style.display = "block";
117 agentCursor.style.opacity = "1";
118 agentActive = true;
119 window.clearTimeout(agentTimer);
120 agentTimer = window.setTimeout(() => {
121 agentActive = false;
122 agentCursor.style.opacity = controller === "human" ? "0.18" : "0.35";
123 }, 700);
124 pointer(point, phase);
125 };
126 const moveHuman = (point: { readonly x: number; readonly y: number }) => {
127 if (controller === "agent") return;
128 humanPoint = point;
129 drawHuman();
130 };
131 const pointerMove = (event: PointerEvent) => {
132 if (event.pointerType === "touch") return;
133 const point = { x: event.clientX, y: event.clientY };
134 moveHuman(point);
135 pointer(point, "move");
136 };
137 const pointerDown = (event: PointerEvent) => {
138 if (event.pointerType === "touch") return;
139 moveHuman({ x: event.clientX, y: event.clientY });
140 pointer({ x: event.clientX, y: event.clientY }, "down");
141 };
142 const pointerUp = (event: PointerEvent) => {
143 if (event.pointerType !== "touch") pointer({ x: event.clientX, y: event.clientY }, "up");
144 };
145 const keyDown = (event: KeyboardEvent) => {
146 if (event.isComposing || event.repeat) return;
147 keyPress(event, true);
148 };
149 const keyUp = () => {
150 if (!options.showKeyPresses) return;
151 emit({
152 type: "key",
153 label: recordingKeysAreSensitive(document) ? null : lastKeyLabel,
154 held: false,
155 width: window.innerWidth,
156 });
157 };
158 const hide = () => {
159 cursor.style.display = "none";
160 pointerHeld = false;
161 cancelPendingPointer();
162 if (options.showKeyPresses || options.showMousePresses) emit({ type: "clear" });
163 };
164 const leave = (event: PointerEvent) => {
165 if (event.relatedTarget === null) hide();
166 };
167 window.addEventListener("pointermove", pointerMove, true);
168 window.addEventListener("pointerdown", pointerDown, true);
169 window.addEventListener("pointerup", pointerUp, true);
170 window.addEventListener("pointercancel", pointerUp, true);
171 window.addEventListener("keydown", keyDown, true);
172 window.addEventListener("keyup", keyUp, true);
173 window.addEventListener("pointerout", leave, true);
174 window.addEventListener("blur", hide);
175 return {
176 move,
177 keyPress,
178 setController,
179 setTheme,
180 dispose: () => {
181 window.removeEventListener("pointermove", pointerMove, true);
182 window.removeEventListener("pointerdown", pointerDown, true);
183 window.removeEventListener("pointerup", pointerUp, true);
184 window.removeEventListener("pointercancel", pointerUp, true);
185 window.removeEventListener("keydown", keyDown, true);
186 window.removeEventListener("keyup", keyUp, true);
187 window.removeEventListener("pointerout", leave, true);
188 window.removeEventListener("blur", hide);
189 cancelPendingPointer();
190 window.clearTimeout(agentTimer);
191 cursor.remove();
192 agentCursor.remove();
193 style.remove();
194 },
195 };