The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ReactNode } from "react"; |
| 2 | import { interpolate } from "remotion"; |
| 3 | import { TITLE_BAR, WINDOW, type SceneCapture } from "../../shared/types.ts"; |
| 4 | import { mono, sans } from "../fonts.ts"; |
| 5 | import { vscode } from "../theme.ts"; |
| 6 | import { Window } from "./Desktop.tsx"; |
| 7 | import { languageLabel } from "./highlight.ts"; |
| 8 | import type { PatchView, Row } from "./patch.ts"; |
| 9 | import { TIMING, type SceneSchedule } from "./schedule.ts"; |
| 10 | |
| 11 | const CODE = { fontSize: 21, lineHeight: 32, charWidth: 21 * 0.6 } as const; |
| 12 | const UI = 15; |
| 13 | const ACTIVITY_BAR = 52; |
| 14 | const SIDEBAR = 270; |
| 15 | const TABS = 42; |
| 16 | const BREADCRUMBS = 28; |
| 17 | const STATUS = 28; |
| 18 | const GUTTER = 76; |
| 19 | |
| 20 | interface EditorState { |
| 21 | files: string[]; |
| 22 | tabs: string[]; |
| 23 | active: string | undefined; |
| 24 | view: PatchView | undefined; |
| 25 | /** Frames into the current patch (Infinity: settled). */ |
| 26 | local: number; |
| 27 | /** Scroll position (in rows) to animate from, and to. */ |
| 28 | scrollFrom: number; |
| 29 | scrollTo: number; |
| 30 | } |
| 31 | |
| 32 | const VISIBLE_ROWS = |
| 33 | Math.floor((WINDOW.height - TITLE_BAR - TABS - BREADCRUMBS - STATUS) / CODE.lineHeight) - 1; |
| 34 | |
| 35 | /** Keep the change in view: its top a few rows down, its end on screen when it fits. */ |
| 36 | const scrollFor = (view: PatchView, kind: "patch" | "open") => { |
| 37 | if (kind === "open") return 0; |
| 38 | const max = Math.max(0, view.rows.length - VISIBLE_ROWS); |
| 39 | // The whole edit if it fits; otherwise its biggest hunk (e.g. the new code, not the imports). |
| 40 | const [first, last] = |
| 41 | view.last - view.first <= VISIBLE_ROWS - 6 ? [view.first, view.last] : view.main; |
| 42 | let top = first - 4; |
| 43 | if (last - top > VISIBLE_ROWS - 3) top = Math.min(first - 2, last - VISIBLE_ROWS + 3); |
| 44 | return Math.max(0, Math.min(top, max)); |
| 45 | }; |
| 46 | |
| 47 | const editorState = (capture: SceneCapture, plan: SceneSchedule, frame: number): EditorState => { |
| 48 | const files = new Set(capture.start.files); |
| 49 | // Start each scene with the few most recent tabs, like closing old ones between chapters. |
| 50 | const tabs = capture.start.tabs.map((t) => t.file).slice(-4); |
| 51 | let active = capture.start.active; |
| 52 | let view = active ? plan.initialViews[active] : undefined; |
| 53 | let local = Infinity; |
| 54 | const scrolls = new Map<string, number>(); |
| 55 | let scrollFrom = 0; |
| 56 | let scrollTo = 0; |
| 57 | for (const segment of plan.segments) { |
| 58 | if (segment.from > frame) break; |
| 59 | const { beat } = segment; |
| 60 | if (beat.kind === "editor.delete") { |
| 61 | files.delete(beat.file); |
| 62 | const at = tabs.indexOf(beat.file); |
| 63 | if (at >= 0) tabs.splice(at, 1); |
| 64 | if (active === beat.file) { |
| 65 | active = tabs.at(-1); |
| 66 | view = undefined; |
| 67 | } |
| 68 | local = Infinity; |
| 69 | continue; |
| 70 | } |
| 71 | if (beat.kind !== "editor.open" && beat.kind !== "editor.edit" && beat.kind !== "editor.patch") |
| 72 | continue; |
| 73 | if (!tabs.includes(beat.file)) tabs.push(beat.file); |
| 74 | active = beat.file; |
| 75 | files.add(beat.file); |
| 76 | view = segment.view; |
| 77 | local = beat.kind === "editor.open" ? Infinity : frame - segment.from - segment.switchFrames; |
| 78 | scrollFrom = scrolls.get(beat.file) ?? 0; |
| 79 | scrollTo = view ? scrollFor(view, beat.kind === "editor.open" ? "open" : "patch") : 0; |
| 80 | scrolls.set(beat.file, scrollTo); |
| 81 | } |
| 82 | return { files: [...files].sort(), tabs, active, view, local, scrollFrom, scrollTo }; |
| 83 | }; |
| 84 | |
| 85 | interface TreeNode { |
| 86 | name: string; |
| 87 | path: string; |
| 88 | children: TreeNode[]; |
| 89 | } |
| 90 | |
| 91 | const buildTree = (files: string[]): TreeNode[] => { |
| 92 | const root: TreeNode = { name: "", path: "", children: [] }; |
| 93 | for (const file of files) { |
| 94 | let node = root; |
| 95 | const parts = file.split("/"); |
| 96 | parts.forEach((name, i) => { |
| 97 | const nodePath = parts.slice(0, i + 1).join("/"); |
| 98 | let child = node.children.find((c) => c.name === name); |
| 99 | if (!child) { |
| 100 | child = { name, path: nodePath, children: [] }; |
| 101 | node.children.push(child); |
| 102 | } |
| 103 | node = child; |
| 104 | }); |
| 105 | } |
| 106 | const sort = (nodes: TreeNode[]): TreeNode[] => |
| 107 | nodes |
| 108 | .sort((a, b) => |
| 109 | a.children.length > 0 === b.children.length > 0 |
| 110 | ? a.name.localeCompare(b.name) |
| 111 | : a.children.length > 0 |
| 112 | ? -1 |
| 113 | : 1, |
| 114 | ) |
| 115 | .map((n) => ({ ...n, children: sort(n.children) })); |
| 116 | return sort(root.children); |
| 117 | }; |
| 118 | |
| 119 | const FileIcon = ({ name }: { name: string }) => { |
| 120 | const ext = name.split(".").pop(); |
| 121 | const [label, color] = |
| 122 | ext === "ts" |
| 123 | ? ["TS", "#3178c6"] |
| 124 | : ext === "tsx" |
| 125 | ? ["⚛", "#4fc1ff"] |
| 126 | : ext === "json" |
| 127 | ? ["{}", "#cbcb41"] |
| 128 | : ext === "css" |
| 129 | ? ["#", "#56b6f7"] |
| 130 | : ext === "html" |
| 131 | ? ["<>", "#e37933"] |
| 132 | : ["≡", "#9d9d9d"]; |
| 133 | return ( |
| 134 | <span |
| 135 | style={{ |
| 136 | width: 22, |
| 137 | display: "inline-block", |
| 138 | textAlign: "center", |
| 139 | color, |
| 140 | fontFamily: mono, |
| 141 | fontSize: label.length > 1 ? 11 : 14, |
| 142 | fontWeight: 700, |
| 143 | }} |
| 144 | > |
| 145 | {label} |
| 146 | </span> |
| 147 | ); |
| 148 | }; |
| 149 | |
| 150 | const Chevron = ({ open = true }: { open?: boolean }) => ( |
| 151 | <svg width="16" height="16" viewBox="0 0 16 16" style={{ flex: "none" }}> |
| 152 | <path |
| 153 | d={open ? "M4 6 L8 10 L12 6" : "M6 4 L10 8 L6 12"} |
| 154 | stroke={vscode.fgMuted} |
| 155 | strokeWidth="1.4" |
| 156 | fill="none" |
| 157 | /> |
| 158 | </svg> |
| 159 | ); |
| 160 | |
| 161 | const Tree = ({ nodes, depth, active }: { nodes: TreeNode[]; depth: number; active?: string }) => ( |
| 162 | <> |
| 163 | {nodes.map((node) => ( |
| 164 | <div key={node.path}> |
| 165 | <div |
| 166 | style={{ |
| 167 | height: 26, |
| 168 | display: "flex", |
| 169 | alignItems: "center", |
| 170 | gap: 4, |
| 171 | paddingLeft: 12 + depth * 14, |
| 172 | background: node.path === active ? vscode.listActive : undefined, |
| 173 | outline: node.path === active ? `1px solid ${vscode.accent}` : undefined, |
| 174 | outlineOffset: -1, |
| 175 | color: vscode.fg, |
| 176 | }} |
| 177 | > |
| 178 | {node.children.length > 0 ? <Chevron /> : <span style={{ width: 16 }} />} |
| 179 | {node.children.length > 0 ? null : <FileIcon name={node.name} />} |
| 180 | <span>{node.name}</span> |
| 181 | </div> |
| 182 | {node.children.length > 0 ? ( |
| 183 | <Tree nodes={node.children} depth={depth + 1} active={active} /> |
| 184 | ) : null} |
| 185 | </div> |
| 186 | ))} |
| 187 | </> |
| 188 | ); |
| 189 | |
| 190 | const ActivityIcon = ({ children, active }: { children: ReactNode; active?: boolean }) => ( |
| 191 | <div |
| 192 | style={{ |
| 193 | height: 56, |
| 194 | display: "grid", |
| 195 | placeItems: "center", |
| 196 | borderLeft: `2px solid ${active ? vscode.fg : "transparent"}`, |
| 197 | opacity: active ? 1 : 0.5, |
| 198 | }} |
| 199 | > |
| 200 | <svg |
| 201 | width="26" |
| 202 | height="26" |
| 203 | viewBox="0 0 24 24" |
| 204 | fill="none" |
| 205 | stroke={vscode.fg} |
| 206 | strokeWidth="1.5" |
| 207 | > |
| 208 | {children} |
| 209 | </svg> |
| 210 | </div> |
| 211 | ); |
| 212 | |
| 213 | const ActivityBar = () => ( |
| 214 | <div |
| 215 | style={{ |
| 216 | width: ACTIVITY_BAR, |
| 217 | background: vscode.chromeBg, |
| 218 | borderRight: `1px solid ${vscode.border}`, |
| 219 | }} |
| 220 | > |
| 221 | <ActivityIcon active> |
| 222 | <path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z" /> |
| 223 | <path d="M14 3v5h5" /> |
| 224 | </ActivityIcon> |
| 225 | <ActivityIcon> |
| 226 | <circle cx="11" cy="11" r="6" /> |
| 227 | <path d="M20 20l-4.5-4.5" /> |
| 228 | </ActivityIcon> |
| 229 | <ActivityIcon> |
| 230 | <circle cx="6" cy="6" r="2" /> |
| 231 | <circle cx="6" cy="18" r="2" /> |
| 232 | <circle cx="18" cy="8" r="2" /> |
| 233 | <path d="M6 8v8M18 10c0 4-6 3-11 6" /> |
| 234 | </ActivityIcon> |
| 235 | <ActivityIcon> |
| 236 | <rect x="4" y="4" width="7" height="7" /> |
| 237 | <rect x="13" y="4" width="7" height="7" /> |
| 238 | <rect x="4" y="13" width="7" height="7" /> |
| 239 | <rect x="13" y="13" width="7" height="7" /> |
| 240 | </ActivityIcon> |
| 241 | </div> |
| 242 | ); |
| 243 | |
| 244 | /** Merge neighbouring characters that share a colour into spans. */ |
| 245 | const Line = ({ row }: { row: Row }) => { |
| 246 | const spans: { text: string; color: string }[] = []; |
| 247 | for (let i = 0; i < row.text.length; i++) { |
| 248 | const color = row.colors[i] || vscode.fg; |
| 249 | const last = spans.at(-1); |
| 250 | if (last && last.color === color) last.text += row.text[i]; |
| 251 | else spans.push({ text: row.text[i]!, color }); |
| 252 | } |
| 253 | return ( |
| 254 | <> |
| 255 | {spans.map((span, i) => ( |
| 256 | <span key={i} style={{ color: span.color }}> |
| 257 | {span.text} |
| 258 | </span> |
| 259 | ))} |
| 260 | </> |
| 261 | ); |
| 262 | }; |
| 263 | |
| 264 | const ADD_BG = "rgba(46, 160, 67, 0.22)"; |
| 265 | const ADD_BAR = "#2ea043"; |
| 266 | const DEL_BG = "rgba(248, 81, 73, 0.2)"; |
| 267 | const DEL_BAR = "#f85149"; |
| 268 | |
| 269 | const Code = ({ state }: { state: EditorState }) => { |
| 270 | const view = state.view!; |
| 271 | const { show } = TIMING.patch; |
| 272 | const t = state.local; |
| 273 | const ease = (x: number) => 1 - (1 - x) ** 3; |
| 274 | const clamp = { extrapolateLeft: "clamp", extrapolateRight: "clamp" } as const; |
| 275 | const hasDel = view.rows.some((r) => r.kind === "del"); |
| 276 | const addedTotal = view.rows.filter((r) => r.kind === "add").length; |
| 277 | // Removed lines collapse first. |
| 278 | const p = |
| 279 | t === Infinity || !hasDel |
| 280 | ? 1 |
| 281 | : ease(interpolate(t, [show, show + TIMING.patchDelete], [0, 1], clamp)); |
| 282 | // Then added lines stream in one at a time, each at full size the moment it appears. |
| 283 | const streamFrom = show + (hasDel ? TIMING.patchDelete : 0); |
| 284 | const perLine = addedTotal |
| 285 | ? Math.min(TIMING.patchPerLine, TIMING.patchStreamMax / addedTotal) |
| 286 | : 0; |
| 287 | const addIndex = new Map<Row, number>(); |
| 288 | view.rows.forEach((r) => { |
| 289 | if (r.kind === "add") addIndex.set(r, addIndex.size); |
| 290 | }); |
| 291 | const shown = (row: Row) => |
| 292 | t === Infinity || t >= streamFrom + (addIndex.get(row) ?? 0) * perLine + perLine ? 1 : 0; |
| 293 | const scroll = |
| 294 | t === Infinity |
| 295 | ? state.scrollTo |
| 296 | : interpolate(t, [0, show], [state.scrollFrom, state.scrollTo], { |
| 297 | extrapolateLeft: "clamp", |
| 298 | extrapolateRight: "clamp", |
| 299 | easing: ease, |
| 300 | }); |
| 301 | const heightOf = (row: Row) => (row.kind === "del" ? 1 - p : row.kind === "add" ? shown(row) : 1); |
| 302 | // Pixel offset of the fractional scroll row, using the rows' current heights. |
| 303 | let offset = 0; |
| 304 | for (let i = 0; i < Math.floor(scroll) && i < view.rows.length; i++) |
| 305 | offset += heightOf(view.rows[i]!); |
| 306 | offset += (scroll % 1) * heightOf(view.rows[Math.floor(scroll)] ?? view.rows[0]!); |
| 307 | const settled = t === Infinity; |
| 308 | return ( |
| 309 | <div |
| 310 | style={{ |
| 311 | position: "absolute", |
| 312 | inset: 0, |
| 313 | fontFamily: mono, |
| 314 | fontSize: CODE.fontSize, |
| 315 | lineHeight: `${CODE.lineHeight}px`, |
| 316 | whiteSpace: "pre", |
| 317 | paddingTop: 6, |
| 318 | overflow: "hidden", |
| 319 | }} |
| 320 | > |
| 321 | <div style={{ transform: `translateY(${-offset * CODE.lineHeight}px)` }}> |
| 322 | {view.rows.map((row, i) => { |
| 323 | const h = heightOf(row); |
| 324 | if (h <= 0.001) return null; |
| 325 | const added = row.kind === "add" && !settled; |
| 326 | const removed = row.kind === "del"; |
| 327 | return ( |
| 328 | <div |
| 329 | key={i} |
| 330 | style={{ |
| 331 | height: CODE.lineHeight * h, |
| 332 | overflow: "hidden", |
| 333 | display: "flex", |
| 334 | position: "relative", |
| 335 | background: added ? ADD_BG : removed ? DEL_BG : undefined, |
| 336 | boxShadow: added |
| 337 | ? `inset 3px 0 ${ADD_BAR}` |
| 338 | : removed |
| 339 | ? `inset 3px 0 ${DEL_BAR}` |
| 340 | : undefined, |
| 341 | // Removed lines fade as they collapse; added lines appear whole. |
| 342 | opacity: row.kind === "del" ? (1 - p) ** 2 : 1, |
| 343 | }} |
| 344 | > |
| 345 | <span |
| 346 | style={{ |
| 347 | width: GUTTER, |
| 348 | paddingRight: 24, |
| 349 | textAlign: "right", |
| 350 | color: added ? "#7ee787" : removed ? "#ffa198" : vscode.lineNumber, |
| 351 | flex: "none", |
| 352 | }} |
| 353 | > |
| 354 | {row.kind === "del" ? "−" : row.number} |
| 355 | </span> |
| 356 | <span |
| 357 | style={{ |
| 358 | textDecoration: removed && p > 0 ? "line-through" : undefined, |
| 359 | textDecorationColor: DEL_BAR, |
| 360 | }} |
| 361 | > |
| 362 | <Line row={row} /> |
| 363 | </span> |
| 364 | </div> |
| 365 | ); |
| 366 | })} |
| 367 | </div> |
| 368 | </div> |
| 369 | ); |
| 370 | }; |
| 371 | |
| 372 | export const Editor = ({ |
| 373 | capture, |
| 374 | plan, |
| 375 | frame, |
| 376 | }: { |
| 377 | capture: SceneCapture; |
| 378 | plan: SceneSchedule; |
| 379 | frame: number; |
| 380 | }) => { |
| 381 | const state = editorState(capture, plan, frame); |
| 382 | const title = state.active |
| 383 | ? `${state.active.split("/").pop()} — ${capture.project}` |
| 384 | : capture.project; |
| 385 | return ( |
| 386 | <Window title={title} background={vscode.editorBg}> |
| 387 | <div |
| 388 | style={{ |
| 389 | position: "absolute", |
| 390 | inset: 0, |
| 391 | display: "flex", |
| 392 | flexDirection: "column", |
| 393 | fontFamily: sans, |
| 394 | fontSize: UI, |
| 395 | }} |
| 396 | > |
| 397 | <div style={{ flex: 1, display: "flex", minHeight: 0 }}> |
| 398 | <ActivityBar /> |
| 399 | <div |
| 400 | style={{ |
| 401 | width: SIDEBAR, |
| 402 | background: vscode.chromeBg, |
| 403 | borderRight: `1px solid ${vscode.border}`, |
| 404 | color: vscode.fg, |
| 405 | }} |
| 406 | > |
| 407 | <div |
| 408 | style={{ |
| 409 | height: 40, |
| 410 | display: "flex", |
| 411 | alignItems: "center", |
| 412 | padding: "0 20px", |
| 413 | fontSize: 13, |
| 414 | letterSpacing: 0.6, |
| 415 | color: vscode.fgMuted, |
| 416 | }} |
| 417 | > |
| 418 | EXPLORER |
| 419 | </div> |
| 420 | <div |
| 421 | style={{ |
| 422 | height: 28, |
| 423 | display: "flex", |
| 424 | alignItems: "center", |
| 425 | gap: 4, |
| 426 | padding: "0 6px", |
| 427 | fontWeight: 700, |
| 428 | fontSize: 13, |
| 429 | letterSpacing: 0.4, |
| 430 | }} |
| 431 | > |
| 432 | <Chevron /> |
| 433 | {capture.project.toUpperCase()} |
| 434 | </div> |
| 435 | <Tree nodes={buildTree(state.files)} depth={0} active={state.active} /> |
| 436 | </div> |
| 437 | <div style={{ flex: 1, display: "flex", flexDirection: "column", minWidth: 0 }}> |
| 438 | <div |
| 439 | style={{ |
| 440 | height: TABS, |
| 441 | display: "flex", |
| 442 | background: vscode.chromeBg, |
| 443 | borderBottom: `1px solid ${vscode.border}`, |
| 444 | }} |
| 445 | > |
| 446 | {state.tabs.map((tab) => { |
| 447 | const active = tab === state.active; |
| 448 | return ( |
| 449 | <div |
| 450 | key={tab} |
| 451 | style={{ |
| 452 | display: "flex", |
| 453 | alignItems: "center", |
| 454 | gap: 6, |
| 455 | padding: "0 18px 0 12px", |
| 456 | background: active ? vscode.editorBg : vscode.chromeBg, |
| 457 | borderTop: `2px solid ${active ? vscode.tabActiveBorder : "transparent"}`, |
| 458 | borderRight: `1px solid ${vscode.border}`, |
| 459 | color: active ? "#ffffff" : vscode.fgMuted, |
| 460 | marginBottom: active ? -1 : 0, |
| 461 | }} |
| 462 | > |
| 463 | <FileIcon name={tab} /> |
| 464 | {tab.split("/").pop()} |
| 465 | </div> |
| 466 | ); |
| 467 | })} |
| 468 | </div> |
| 469 | <div |
| 470 | style={{ |
| 471 | height: BREADCRUMBS, |
| 472 | display: "flex", |
| 473 | alignItems: "center", |
| 474 | gap: 8, |
| 475 | padding: "0 20px", |
| 476 | color: vscode.fgMuted, |
| 477 | fontSize: 14, |
| 478 | }} |
| 479 | > |
| 480 | {(state.active ?? "").split("/").map((part, i, all) => ( |
| 481 | <span key={i} style={{ display: "flex", alignItems: "center", gap: 8 }}> |
| 482 | {i === all.length - 1 ? <FileIcon name={part} /> : null} |
| 483 | {part} |
| 484 | {i < all.length - 1 ? <span>›</span> : null} |
| 485 | </span> |
| 486 | ))} |
| 487 | </div> |
| 488 | <div style={{ flex: 1, position: "relative", overflow: "hidden" }}> |
| 489 | {state.view ? <Code state={state} /> : null} |
| 490 | </div> |
| 491 | </div> |
| 492 | </div> |
| 493 | <div |
| 494 | style={{ |
| 495 | height: STATUS, |
| 496 | display: "flex", |
| 497 | alignItems: "center", |
| 498 | gap: 22, |
| 499 | padding: "0 14px", |
| 500 | background: vscode.chromeBg, |
| 501 | borderTop: `1px solid ${vscode.border}`, |
| 502 | color: vscode.fgMuted, |
| 503 | fontSize: 13, |
| 504 | }} |
| 505 | > |
| 506 | <span>⎇ main</span> |
| 507 | <span>⊗ 0 ⚠ 0</span> |
| 508 | <span style={{ marginLeft: "auto" }}>Spaces: 2</span> |
| 509 | <span>UTF-8</span> |
| 510 | <span>{state.active ? languageLabel(state.active) : ""}</span> |
| 511 | </div> |
| 512 | </div> |
| 513 | </Window> |
| 514 | ); |
| 515 | }; |