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

main
tsx 326 lines 9.46 KB
Raw
1 import { Img, interpolate, staticFile } from "remotion";
2 import { BROWSER_VIEWPORT, type SceneCapture } from "../../shared/types.ts";
3 import { sans } from "../fonts.ts";
4 import { TrafficLights, Window } from "./Desktop.tsx";
5 import { TIMING, type SceneSchedule } from "./schedule.ts";
6
7 interface Page {
8 url: string;
9 title: string;
10 screenshot: string;
11 }
12
13 interface BrowserState {
14 /** Text in the address bar. */
15 address: string;
16 focused: boolean;
17 /** Page on screen (undefined: a new tab). */
18 page: Page | undefined;
19 /** 0–1 while loading, undefined otherwise. */
20 progress: number | undefined;
21 /** Opacity of the page that just finished loading. */
22 reveal: number;
23 /** The mouse pointer over the page (page pixels), if it has been used. */
24 pointer?: { x: number; y: number; pressed: number };
25 /** Field or button being acted on, highlighted briefly. */
26 focusRing?: { x: number; y: number; width: number; height: number; opacity: number };
27 }
28
29 const browserState = (capture: SceneCapture, plan: SceneSchedule, frame: number): BrowserState => {
30 let state: BrowserState = {
31 address: capture.start.browser?.url ?? "",
32 focused: false,
33 page: capture.start.browser,
34 progress: undefined,
35 reveal: 1,
36 };
37 for (const segment of plan.segments) {
38 if (segment.from > frame) break;
39 const { beat } = segment;
40 if (beat.kind === "browser.update" && state.page) {
41 const since = frame - segment.from - segment.switchFrames;
42 state = {
43 ...state,
44 page: { ...state.page, title: beat.title, screenshot: beat.screenshot },
45 reveal: interpolate(since, [0, 4], [0.85, 1], {
46 extrapolateLeft: "clamp",
47 extrapolateRight: "clamp",
48 }),
49 };
50 continue;
51 }
52 if (beat.kind === "browser.action" && state.page) {
53 const local = frame - segment.from - segment.switchFrames;
54 const { move, act } = TIMING.browserAction;
55 const target = {
56 x: beat.target.x + beat.target.width / 2,
57 y: beat.target.y + beat.target.height / 2,
58 };
59 const from = state.pointer ?? {
60 x: BROWSER_VIEWPORT.width * 0.62,
61 y: BROWSER_VIEWPORT.height * 0.78,
62 };
63 const t = interpolate(local, [0, move], [0, 1], {
64 extrapolateLeft: "clamp",
65 extrapolateRight: "clamp",
66 });
67 const ease = 1 - (1 - t) ** 3;
68 const acted = local >= move + act / 2;
69 state = {
70 ...state,
71 pointer: {
72 x: from.x + (target.x - from.x) * ease,
73 y: from.y + (target.y - from.y) * ease,
74 pressed:
75 beat.action === "click"
76 ? interpolate(local, [move, move + act / 2, move + act], [0, 1, 0], {
77 extrapolateLeft: "clamp",
78 extrapolateRight: "clamp",
79 })
80 : 0,
81 },
82 focusRing: {
83 ...beat.target,
84 opacity: interpolate(
85 local,
86 [move - 4, move, move + act + 10, move + act + 20],
87 [0, 1, 1, 0],
88 {
89 extrapolateLeft: "clamp",
90 extrapolateRight: "clamp",
91 },
92 ),
93 },
94 page: acted
95 ? { ...state.page, title: beat.title, screenshot: beat.screenshot }
96 : state.page,
97 };
98 continue;
99 }
100 if (beat.kind !== "browser") continue;
101 const local = frame - segment.from - segment.switchFrames;
102 const typingFrames = TIMING.urlPaste;
103 if (local < typingFrames) {
104 // Focus the address bar (selecting the old URL), then paste.
105 const pasted = local >= typingFrames / 2;
106 state = { ...state, address: pasted ? beat.url : "", focused: true, progress: undefined };
107 } else if (local < typingFrames + TIMING.pageLoad) {
108 state = {
109 ...state,
110 address: beat.url,
111 focused: false,
112 progress: (local - typingFrames) / TIMING.pageLoad,
113 };
114 } else {
115 const since = local - typingFrames - TIMING.pageLoad;
116 state = {
117 address: beat.url,
118 focused: false,
119 page: beat,
120 pointer: state.pointer,
121 progress: undefined,
122 reveal: interpolate(since, [0, 6], [0, 1], { extrapolateRight: "clamp" }),
123 };
124 }
125 }
126 return state;
127 };
128
129 const CHROME_BG = "#202124";
130 const TAB_BG = "#35363a";
131
132 const Icon = ({ d }: { d: string }) => (
133 <svg
134 width="22"
135 height="22"
136 viewBox="0 0 24 24"
137 fill="none"
138 stroke="#c4c7c5"
139 strokeWidth="2"
140 strokeLinecap="round"
141 strokeLinejoin="round"
142 >
143 <path d={d} />
144 </svg>
145 );
146
147 /** A macOS arrow pointer; `pressed` (0–1) shrinks it and shows a click ripple. */
148 const Pointer = ({ x, y, pressed }: { x: number; y: number; pressed: number }) => (
149 <div style={{ position: "absolute", left: x, top: y, pointerEvents: "none" }}>
150 {pressed > 0 ? (
151 <div
152 style={{
153 position: "absolute",
154 left: -22,
155 top: -22,
156 width: 44,
157 height: 44,
158 borderRadius: 22,
159 background: "rgba(163, 196, 115, 0.35)",
160 transform: `scale(${0.5 + pressed})`,
161 opacity: pressed,
162 }}
163 />
164 ) : null}
165 <svg
166 width="28"
167 height="34"
168 viewBox="0 0 28 34"
169 style={{
170 position: "absolute",
171 left: -3,
172 top: -2,
173 transform: `scale(${1 - pressed * 0.12})`,
174 filter: "drop-shadow(0 2px 3px rgba(0,0,0,0.5))",
175 }}
176 >
177 <path
178 d="M3 2 L3 26 L9.5 20 L14 31 L18.5 29 L14 18.5 L23 18.5 Z"
179 fill="#111"
180 stroke="#fff"
181 strokeWidth="2"
182 strokeLinejoin="round"
183 />
184 </svg>
185 </div>
186 );
187
188 export const Browser = ({
189 capture,
190 plan,
191 frame,
192 }: {
193 capture: SceneCapture;
194 plan: SceneSchedule;
195 frame: number;
196 }) => {
197 const state = browserState(capture, plan, frame);
198 const caretVisible = state.focused && frame % 30 < 18;
199 const bar = (
200 <div style={{ flex: "none", background: CHROME_BG, fontFamily: sans }}>
201 <div
202 style={{
203 height: 44,
204 display: "flex",
205 alignItems: "flex-end",
206 padding: "0 12px 0 18px",
207 gap: 18,
208 }}
209 >
210 <div style={{ alignSelf: "center" }}>
211 <TrafficLights />
212 </div>
213 <div
214 style={{
215 height: 36,
216 width: 260,
217 display: "flex",
218 alignItems: "center",
219 gap: 10,
220 padding: "0 14px",
221 borderRadius: "10px 10px 0 0",
222 background: TAB_BG,
223 color: "#e8eaed",
224 fontSize: 14,
225 }}
226 >
227 <div
228 style={{
229 width: 16,
230 height: 16,
231 borderRadius: 8,
232 background: state.page ? "#8ab4f8" : "#5f6368",
233 flex: "none",
234 }}
235 />
236 <span style={{ overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis" }}>
237 {state.progress !== undefined ? "Loading…" : (state.page?.title ?? "New Tab")}
238 </span>
239 </div>
240 <span style={{ alignSelf: "center", color: "#c4c7c5", fontSize: 22 }}>+</span>
241 </div>
242 <div
243 style={{
244 height: 48,
245 display: "flex",
246 alignItems: "center",
247 gap: 14,
248 padding: "0 16px",
249 background: TAB_BG,
250 position: "relative",
251 }}
252 >
253 <Icon d="M19 12H5M12 19l-7-7 7-7" />
254 <Icon d="M5 12h14M12 5l7 7-7 7" />
255 <Icon d="M21 12a9 9 0 1 1-3-6.7L21 8M21 3v5h-5" />
256 <div
257 style={{
258 flex: 1,
259 height: 34,
260 borderRadius: 17,
261 background: state.focused ? "#202124" : "#282a2d",
262 outline: state.focused ? "2px solid #8ab4f8" : undefined,
263 display: "flex",
264 alignItems: "center",
265 gap: 10,
266 padding: "0 16px",
267 color: "#e8eaed",
268 fontSize: 16,
269 }}
270 >
271 <Icon d="M7 11V8a5 5 0 0 1 10 0v3M5 11h14v10H5z" />
272 <span>
273 {state.address}
274 {caretVisible ? (
275 <span style={{ borderLeft: "2px solid #e8eaed", marginLeft: 1 }} />
276 ) : null}
277 </span>
278 </div>
279 {state.progress !== undefined ? (
280 <div
281 style={{
282 position: "absolute",
283 left: 0,
284 bottom: 0,
285 height: 3,
286 width: `${Math.round(state.progress * 100)}%`,
287 background: "#8ab4f8",
288 }}
289 />
290 ) : null}
291 </div>
292 </div>
293 );
294 return (
295 <Window background="#ffffff" bar={bar}>
296 <div style={{ position: "absolute", inset: 0, background: "#202124" }}>
297 {state.page ? (
298 <Img
299 src={staticFile(state.page.screenshot)}
300 style={{
301 width: BROWSER_VIEWPORT.width,
302 height: BROWSER_VIEWPORT.height,
303 display: "block",
304 opacity: state.reveal,
305 }}
306 />
307 ) : null}
308 {state.focusRing && state.focusRing.opacity > 0 ? (
309 <div
310 style={{
311 position: "absolute",
312 left: state.focusRing.x - 4,
313 top: state.focusRing.y - 4,
314 width: state.focusRing.width + 8,
315 height: state.focusRing.height + 8,
316 borderRadius: 16,
317 border: "3px solid rgba(163, 196, 115, 0.9)",
318 opacity: state.focusRing.opacity,
319 }}
320 />
321 ) : null}
322 {state.pointer ? <Pointer {...state.pointer} /> : null}
323 </div>
324 </Window>
325 );
326 };