@myrialabs/clopen 0.4.47 → 0.4.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (325) hide show
  1. package/README.md +11 -11
  2. package/backend/artifacts/project/sources.ts +1 -1
  3. package/backend/auth/auth-service.ts +52 -358
  4. package/backend/auth/onboarding.test.ts +2 -3
  5. package/backend/auth/permissions.test.ts +10 -20
  6. package/backend/auth/permissions.ts +4 -290
  7. package/backend/auth/rate-limiter.test.ts +8 -9
  8. package/backend/auth/rate-limiter.ts +0 -2
  9. package/backend/auth/tokens.ts +1 -8
  10. package/backend/bootstrap.ts +7 -0
  11. package/backend/chat/attachments.test.ts +88 -0
  12. package/backend/chat/attachments.ts +159 -0
  13. package/backend/chat/engine-handoff.test.ts +0 -2
  14. package/backend/chat/stream-manager.ts +62 -62
  15. package/backend/chat/turn-answer.test.ts +64 -0
  16. package/backend/chat/turn-answer.ts +125 -0
  17. package/backend/chat-apps/awaiting.test.ts +39 -0
  18. package/backend/chat-apps/awaiting.ts +49 -0
  19. package/backend/chat-apps/conversation.ts +746 -0
  20. package/backend/chat-apps/inbound.ts +324 -0
  21. package/backend/chat-apps/index.ts +309 -0
  22. package/backend/chat-apps/link-events.ts +20 -0
  23. package/backend/chat-apps/media.test.ts +62 -0
  24. package/backend/chat-apps/media.ts +208 -0
  25. package/backend/chat-apps/messages.test.ts +229 -0
  26. package/backend/chat-apps/messages.ts +365 -0
  27. package/backend/chat-apps/outbound.ts +243 -0
  28. package/backend/chat-apps/pairing.test.ts +45 -0
  29. package/backend/chat-apps/pairing.ts +74 -0
  30. package/backend/chat-apps/platforms/index.ts +20 -0
  31. package/backend/chat-apps/platforms/telegram.test.ts +35 -0
  32. package/backend/chat-apps/platforms/telegram.ts +191 -0
  33. package/backend/chat-apps/platforms/types.ts +114 -0
  34. package/backend/chat-apps/questions.ts +133 -0
  35. package/backend/chat-apps/runtime.ts +572 -0
  36. package/backend/chat-apps/storage.test.ts +201 -0
  37. package/backend/chat-apps/turns.test.ts +45 -0
  38. package/backend/chat-apps/turns.ts +208 -0
  39. package/backend/containers/pty.ts +3 -11
  40. package/backend/control/actions/accounts.ts +89 -0
  41. package/backend/control/actions/files.ts +116 -0
  42. package/backend/control/actions/models.ts +188 -0
  43. package/backend/control/actions/projects.ts +272 -0
  44. package/backend/control/actions/sessions.ts +280 -0
  45. package/backend/control/actions/skills.ts +111 -0
  46. package/backend/control/control.test.ts +169 -0
  47. package/backend/control/define.ts +20 -0
  48. package/backend/control/folders.test.ts +67 -0
  49. package/backend/control/index.ts +213 -0
  50. package/backend/control/invoke.ts +44 -0
  51. package/backend/control/links.ts +35 -0
  52. package/backend/control/paging.ts +26 -0
  53. package/backend/control/types.ts +117 -0
  54. package/backend/control/workspace.ts +127 -0
  55. package/backend/database/migrations/084_create_channel_links.ts +71 -0
  56. package/backend/database/migrations/085_add_home_projects.ts +36 -0
  57. package/backend/database/migrations/086_single_owner.test.ts +170 -0
  58. package/backend/database/migrations/086_single_owner.ts +418 -0
  59. package/backend/database/migrations/index.ts +21 -0
  60. package/backend/database/queries/auth-queries.ts +7 -85
  61. package/backend/database/queries/channel-queries.ts +270 -0
  62. package/backend/database/queries/db-client-connection-queries.ts +18 -56
  63. package/backend/database/queries/file-share-queries.ts +5 -23
  64. package/backend/database/queries/git-identity-queries.ts +31 -42
  65. package/backend/database/queries/graph-queries.ts +1 -1
  66. package/backend/database/queries/index.ts +13 -1
  67. package/backend/database/queries/message-queries.test.ts +146 -0
  68. package/backend/database/queries/message-queries.ts +42 -1
  69. package/backend/database/queries/permission-set-queries.ts +1 -1
  70. package/backend/database/queries/profile-queries.ts +1 -1
  71. package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
  72. package/backend/database/queries/project-queries.ts +50 -63
  73. package/backend/database/queries/session-queries.ts +28 -56
  74. package/backend/database/queries/ssh-connection-queries.ts +18 -41
  75. package/backend/db-client/integrations/projector.ts +0 -5
  76. package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
  77. package/backend/engine/adapters/claude/message-converter.ts +25 -2
  78. package/backend/engine/adapters/cline/message-converter.ts +0 -1
  79. package/backend/engine/adapters/cline/stream.ts +11 -15
  80. package/backend/engine/adapters/codex/message-converter.ts +0 -2
  81. package/backend/engine/adapters/copilot/message-converter.ts +1 -2
  82. package/backend/engine/adapters/cursor/message-converter.ts +0 -1
  83. package/backend/engine/adapters/opencode/message-converter.ts +2 -4
  84. package/backend/engine/adapters/opencode/stream.ts +11 -0
  85. package/backend/engine/adapters/parent-scope.test.ts +38 -0
  86. package/backend/engine/adapters/parent-scope.ts +44 -0
  87. package/backend/engine/adapters/pi/message-converter.ts +0 -1
  88. package/backend/engine/adapters/pi/stream.ts +4 -5
  89. package/backend/engine/adapters/qwen/message-converter.ts +0 -2
  90. package/backend/engine/bootstrap-default-engine.ts +2 -2
  91. package/backend/engine/docs/adapter-contract.md +8 -1
  92. package/backend/engine/install-runner.ts +8 -17
  93. package/backend/files/file-shares.ts +20 -44
  94. package/backend/files/file-size-limit.ts +3 -3
  95. package/backend/files/image-processing.ts +1 -1
  96. package/backend/files/os-clipboard.ts +2 -2
  97. package/backend/git/git-executor.ts +25 -5
  98. package/backend/git/git-service.ts +420 -19
  99. package/backend/git/identity/credential-helper.test.ts +2 -7
  100. package/backend/git/identity/import-local.test.ts +11 -16
  101. package/backend/git/identity/import-local.ts +12 -12
  102. package/backend/git/identity/index.ts +10 -24
  103. package/backend/git/identity/resolve.ts +13 -14
  104. package/backend/git/identity/scope-config.ts +33 -28
  105. package/backend/git/identity/service.ts +44 -47
  106. package/backend/git/op-lock.test.ts +111 -0
  107. package/backend/git/op-lock.ts +180 -0
  108. package/backend/git/remote-sync.test.ts +249 -0
  109. package/backend/host/metrics.ts +1 -1
  110. package/backend/http/bearer-auth.ts +1 -2
  111. package/backend/http/files-download.test.ts +8 -9
  112. package/backend/http/files-download.ts +2 -2
  113. package/backend/http/files-shared.test.ts +62 -89
  114. package/backend/http/files-shared.ts +2 -2
  115. package/backend/http/files-upload.test.ts +3 -7
  116. package/backend/http/files-upload.ts +3 -3
  117. package/backend/http/notes-images.test.ts +15 -38
  118. package/backend/http/notes-images.ts +6 -8
  119. package/backend/http/ssh-sftp.ts +5 -10
  120. package/backend/index.ts +21 -7
  121. package/backend/integrations/accounts.ts +61 -3
  122. package/backend/integrations/index.ts +2 -2
  123. package/backend/integrations/registry/define.ts +15 -0
  124. package/backend/integrations/registry/index.ts +2 -0
  125. package/backend/integrations/registry/providers/telegram.ts +54 -0
  126. package/backend/integrations/registry/types.ts +21 -1
  127. package/backend/integrations/webhooks/gateway.ts +7 -6
  128. package/backend/integrations/webhooks/verify.test.ts +30 -0
  129. package/backend/integrations/webhooks/verify.ts +4 -0
  130. package/backend/mcp/external/project.ts +1 -1
  131. package/backend/mcp/internal/remote-server.test.ts +1 -3
  132. package/backend/mcp/internal/remote-server.ts +4 -4
  133. package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
  134. package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
  135. package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
  136. package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
  137. package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
  138. package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
  139. package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
  140. package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
  141. package/backend/memory/redact.ts +1 -1
  142. package/backend/notes/access.test.ts +28 -29
  143. package/backend/notes/access.ts +6 -11
  144. package/backend/permissions/service.ts +1 -1
  145. package/backend/preview/browser/browser-clipboard.test.ts +46 -0
  146. package/backend/preview/browser/browser-clipboard.ts +67 -0
  147. package/backend/preview/browser/browser-dialog-handler.ts +95 -6
  148. package/backend/preview/browser/browser-host-bridge.ts +9 -0
  149. package/backend/preview/browser/browser-interaction-handler.ts +316 -86
  150. package/backend/preview/browser/browser-mcp-control.ts +34 -0
  151. package/backend/preview/browser/browser-pool.ts +67 -6
  152. package/backend/preview/browser/browser-preview-service.ts +361 -25
  153. package/backend/preview/browser/browser-tab-manager.ts +66 -23
  154. package/backend/preview/browser/browser-video-capture.ts +92 -5
  155. package/backend/preview/browser/element-locator.ts +190 -17
  156. package/backend/preview/browser/keyboard-chords.test.ts +68 -0
  157. package/backend/preview/browser/keyboard-chords.ts +132 -0
  158. package/backend/preview/browser/paste.ts +81 -0
  159. package/backend/preview/browser/process-usage.ts +142 -0
  160. package/backend/preview/browser/profile-lock.ts +209 -0
  161. package/backend/preview/browser/scripts/host-bridge.ts +59 -0
  162. package/backend/preview/browser/scripts/video-stream.ts +58 -0
  163. package/backend/preview/browser/types.ts +40 -2
  164. package/backend/preview/browser/viewport.test.ts +45 -0
  165. package/backend/preview/browser/viewport.ts +96 -0
  166. package/backend/profiles/service.ts +2 -2
  167. package/backend/project/status-manager.ts +43 -172
  168. package/backend/projects/home.ts +61 -0
  169. package/backend/projects/project-cleanup.integration.test.ts +2 -2
  170. package/backend/projects/project-cleanup.ts +0 -8
  171. package/backend/skills/service.ts +2 -3
  172. package/backend/snapshot/helpers.ts +0 -1
  173. package/backend/snapshot/restore.ts +252 -0
  174. package/backend/ssh/ptykit.ts +1 -2
  175. package/backend/terminal/ptykit.ts +5 -8
  176. package/backend/tunnel/public-origin.ts +31 -20
  177. package/backend/utils/user-helpers.ts +0 -55
  178. package/backend/utils/ws.ts +61 -154
  179. package/backend/worktrees/branching/engine.ts +3 -16
  180. package/backend/worktrees/branching/index.ts +1 -1
  181. package/backend/worktrees/branching/projector.ts +1 -1
  182. package/backend/worktrees/branching/service.ts +13 -25
  183. package/backend/ws/access.ts +12 -21
  184. package/backend/ws/artifacts/generate.ts +2 -2
  185. package/backend/ws/auth/device.ts +16 -71
  186. package/backend/ws/auth/index.ts +4 -22
  187. package/backend/ws/auth/login.test.ts +14 -60
  188. package/backend/ws/auth/login.ts +24 -124
  189. package/backend/ws/auth/status.ts +0 -1
  190. package/backend/ws/chat/composer.ts +118 -118
  191. package/backend/ws/chat/stream.ts +64 -47
  192. package/backend/ws/chat-apps/index.ts +117 -0
  193. package/backend/ws/containers/actions.ts +11 -25
  194. package/backend/ws/containers/index.ts +3 -4
  195. package/backend/ws/containers/list.ts +10 -36
  196. package/backend/ws/containers/logs.ts +2 -2
  197. package/backend/ws/containers/tunnel.ts +1 -2
  198. package/backend/ws/db-client/access.ts +2 -16
  199. package/backend/ws/db-client/accounts.ts +5 -15
  200. package/backend/ws/db-client/connections.ts +17 -25
  201. package/backend/ws/db-client/context.ts +1 -1
  202. package/backend/ws/db-client/env.ts +7 -10
  203. package/backend/ws/db-client/index.ts +2 -2
  204. package/backend/ws/db-client/io.ts +5 -5
  205. package/backend/ws/db-client/query-history-retention.test.ts +8 -8
  206. package/backend/ws/db-client/query.ts +18 -17
  207. package/backend/ws/db-client/schema.ts +13 -13
  208. package/backend/ws/db-client/structure.ts +35 -35
  209. package/backend/ws/db-client/supabase.ts +4 -21
  210. package/backend/ws/deployments/context.ts +1 -1
  211. package/backend/ws/deployments/crud.ts +0 -5
  212. package/backend/ws/engine/access.ts +7 -20
  213. package/backend/ws/engine/claude/accounts.ts +4 -4
  214. package/backend/ws/engine/cline/accounts.ts +5 -5
  215. package/backend/ws/engine/codex/accounts.ts +3 -3
  216. package/backend/ws/engine/pi/accounts.ts +5 -5
  217. package/backend/ws/files/clipboard.ts +8 -27
  218. package/backend/ws/files/image-edit.ts +6 -6
  219. package/backend/ws/files/index.ts +1 -1
  220. package/backend/ws/files/path-access.test.ts +32 -131
  221. package/backend/ws/files/path-access.ts +8 -87
  222. package/backend/ws/files/read.ts +15 -19
  223. package/backend/ws/files/reveal.ts +3 -3
  224. package/backend/ws/files/search.ts +7 -7
  225. package/backend/ws/files/share.ts +9 -13
  226. package/backend/ws/files/state.ts +9 -12
  227. package/backend/ws/files/watch.ts +3 -3
  228. package/backend/ws/files/write.ts +30 -31
  229. package/backend/ws/git/branch.ts +80 -59
  230. package/backend/ws/git/commit.ts +63 -44
  231. package/backend/ws/git/conflict.ts +16 -18
  232. package/backend/ws/git/identity-context.ts +8 -17
  233. package/backend/ws/git/identity.ts +17 -27
  234. package/backend/ws/git/index.ts +3 -1
  235. package/backend/ws/git/op-state.ts +139 -0
  236. package/backend/ws/git/remote.ts +241 -141
  237. package/backend/ws/git/repo-context.ts +67 -0
  238. package/backend/ws/git/staging.ts +18 -28
  239. package/backend/ws/git/status.ts +5 -2
  240. package/backend/ws/index.ts +5 -1
  241. package/backend/ws/instructions/crud.ts +1 -1
  242. package/backend/ws/integrations/crud.ts +1 -3
  243. package/backend/ws/mcp/crud.ts +3 -5
  244. package/backend/ws/mcp/tools.ts +1 -1
  245. package/backend/ws/memory/crud.ts +14 -46
  246. package/backend/ws/memory/index.ts +2 -2
  247. package/backend/ws/messages/crud.ts +29 -8
  248. package/backend/ws/notes/index.ts +33 -39
  249. package/backend/ws/permissions/crud.ts +3 -4
  250. package/backend/ws/ports/index.ts +1 -2
  251. package/backend/ws/ports/kill.ts +5 -10
  252. package/backend/ws/ports/list.ts +4 -10
  253. package/backend/ws/preview/access.ts +1 -1
  254. package/backend/ws/preview/browser/interact.ts +52 -79
  255. package/backend/ws/preview/browser/native-ui.ts +24 -0
  256. package/backend/ws/preview/browser/stats.ts +98 -1
  257. package/backend/ws/preview/browser/tab-info.ts +15 -0
  258. package/backend/ws/preview/browser/tab.ts +19 -4
  259. package/backend/ws/preview/index.ts +27 -0
  260. package/backend/ws/profiles/crud.ts +6 -8
  261. package/backend/ws/project-artifacts/index.ts +30 -31
  262. package/backend/ws/projects/crud.ts +33 -35
  263. package/backend/ws/projects/index.ts +3 -8
  264. package/backend/ws/projects/info.ts +3 -3
  265. package/backend/ws/projects/overview.ts +19 -30
  266. package/backend/ws/projects/status.ts +17 -57
  267. package/backend/ws/sessions/crud.ts +34 -45
  268. package/backend/ws/sessions/index.ts +1 -1
  269. package/backend/ws/settings/crud.ts +1 -1
  270. package/backend/ws/skills/crud.ts +3 -4
  271. package/backend/ws/skills/index.ts +1 -1
  272. package/backend/ws/snapshot/changes.ts +7 -7
  273. package/backend/ws/snapshot/restore.ts +11 -295
  274. package/backend/ws/snapshot/timeline.ts +2 -4
  275. package/backend/ws/ssh/access.ts +6 -21
  276. package/backend/ws/ssh/connections.ts +21 -31
  277. package/backend/ws/ssh/forwards.ts +12 -12
  278. package/backend/ws/ssh/sftp.ts +34 -34
  279. package/backend/ws/ssh/tunnel.ts +1 -2
  280. package/backend/ws/stack/install.ts +7 -17
  281. package/backend/ws/subagents/crud.ts +1 -1
  282. package/backend/ws/tunnel/share.ts +0 -2
  283. package/backend/ws/user/crud.ts +1 -59
  284. package/backend/ws/user/index.ts +1 -1
  285. package/backend/ws/work/context.ts +1 -1
  286. package/backend/ws/work/crud.ts +0 -5
  287. package/backend/ws/workspace/state.ts +10 -13
  288. package/backend/ws/worktrees/branching.ts +7 -36
  289. package/backend/ws/worktrees/crud.ts +2 -2
  290. package/bin/clopen.ts +10 -11
  291. package/dist/assets/{index-CwuKQelV.js → index-CONlwAXZ.js} +1 -1
  292. package/dist/assets/{index-BTcR1byF.js → index-DJzwt-hq.js} +1 -1
  293. package/dist/assets/main-BUiUL3eZ.css +1 -0
  294. package/dist/assets/main-nBOb9YOA.js +1240 -0
  295. package/dist/index.html +2 -2
  296. package/package.json +2 -1
  297. package/shared/constants/attachments.ts +14 -0
  298. package/shared/constants/engine-tools.ts +1 -1
  299. package/shared/constants/preview.test.ts +259 -0
  300. package/shared/constants/preview.ts +1018 -57
  301. package/shared/constants/tool-icons.ts +5 -0
  302. package/shared/types/containers/container.ts +1 -1
  303. package/shared/types/database/schema.ts +9 -6
  304. package/shared/types/git-identity.ts +1 -1
  305. package/shared/types/git.ts +116 -0
  306. package/shared/types/integrations.ts +7 -0
  307. package/shared/types/stores/settings.ts +1 -1
  308. package/shared/types/unified/index.ts +1 -1
  309. package/shared/types/unified/message.ts +0 -7
  310. package/shared/types/unified/stream.ts +2 -4
  311. package/shared/types/unified/tool.ts +15 -1
  312. package/shared/utils/ask-user-question.test.ts +319 -0
  313. package/shared/utils/ask-user-question.ts +154 -0
  314. package/shared/utils/home-project.ts +25 -0
  315. package/shared/utils/logger.ts +2 -0
  316. package/shared/utils/ws-server.ts +32 -7
  317. package/backend/auth/session-invalidation.test.ts +0 -112
  318. package/backend/auth/session-invalidation.ts +0 -77
  319. package/backend/project/status-manager.test.ts +0 -32
  320. package/backend/ws/auth/invites.ts +0 -94
  321. package/backend/ws/auth/users.ts +0 -112
  322. package/backend/ws/projects/presence.ts +0 -46
  323. package/backend/ws/stack/access.ts +0 -31
  324. package/dist/assets/main-BiDNMWb3.css +0 -1
  325. package/dist/assets/main-BlhKWz2B.js +0 -1241
@@ -2,88 +2,1049 @@
2
2
  // Centralized configuration for preview components
3
3
 
4
4
  /**
5
- * Device viewport dimensions
6
- * These are the physical pixel dimensions for each device type.
7
- * Desktop and laptop are naturally landscape (width > height).
8
- * Tablet and mobile are naturally portrait (height > width).
9
- */
10
- export const DEVICE_VIEWPORTS = {
11
- desktop: { width: 1920, height: 1080 }, // FHD — still the most common desktop monitor
12
- laptop: { width: 1440, height: 900 }, // Modern 14" MacBook Air / 16:10 laptop
13
- tablet: { width: 834, height: 1194 }, // iPad Pro 11" M4 class
14
- mobile: { width: 402, height: 874 } // iPhone 16 / modern flagship phone
15
- } as const;
16
-
17
- // Type exports
18
- export type DeviceSize = keyof typeof DEVICE_VIEWPORTS;
5
+ * What the page is shown on.
6
+ *
7
+ * Four of these are device classes, each with a set of device models to pick
8
+ * from and a mockup drawn around the page. `responsive` is the fifth: no
9
+ * mockup and no fixed size — the viewport is whatever the user drags it to,
10
+ * the way a desktop browser window is.
11
+ */
12
+ export const DEVICE_SIZES = ['desktop', 'laptop', 'tablet', 'mobile', 'responsive'] as const;
13
+
14
+ export type DeviceSize = (typeof DEVICE_SIZES)[number];
19
15
  export type Rotation = 'portrait' | 'landscape';
20
16
 
17
+ export interface ViewportSize {
18
+ width: number;
19
+ height: number;
20
+ }
21
+
22
+ /**
23
+ * The default size of each device class, in CSS pixels.
24
+ *
25
+ * Desktop and laptop are naturally landscape (width > height); tablet and
26
+ * mobile are naturally portrait. Picking a class without a model lands here.
27
+ */
28
+ export const DEVICE_VIEWPORTS: Record<DeviceSize, ViewportSize> = {
29
+ desktop: { width: 1920, height: 1080 }, // FHD — still the most common desktop monitor
30
+ laptop: { width: 1440, height: 900 }, // Modern 13" MacBook Air / 16:10 laptop
31
+ tablet: { width: 834, height: 1194 }, // iPad Pro 11" M4 class
32
+ mobile: { width: 402, height: 874 }, // iPhone 16 / modern flagship phone
33
+ responsive: { width: 1280, height: 800 }
34
+ };
35
+
36
+ /**
37
+ * The range a viewport may be set to.
38
+ *
39
+ * The floor is below any real phone so narrow breakpoints can still be
40
+ * reached; the ceiling is 4K, past which the capture has nothing more to give.
41
+ */
42
+ export const VIEWPORT_LIMITS = { min: 200, max: 3840 } as const;
43
+
44
+ /** The system whose UI is drawn around the page — status bar, menu bar, taskbar. */
45
+ export type DeviceOs = 'ios' | 'ipados' | 'android' | 'macos' | 'windows';
46
+
47
+ export interface DevicePreset {
48
+ /** Stable id; also how a tab names the device it is shown on. */
49
+ id: string;
50
+ label: string;
51
+ deviceSize: Exclude<DeviceSize, 'responsive'>;
52
+ os: DeviceOs;
53
+ /** In the class's natural orientation. */
54
+ width: number;
55
+ height: number;
56
+ }
57
+
21
58
  /**
22
- * Get viewport dimensions based on device size and rotation
59
+ * Device models, grouped by class.
60
+ *
61
+ * Each is its own piece of hardware with its own mockup — an iPhone 16 and a
62
+ * Galaxy S24 are both phones, but they do not look alike and the preview
63
+ * should not pretend they do. The first model of each class is that class's
64
+ * default and must match `DEVICE_VIEWPORTS`.
65
+ */
66
+ export const DEVICE_PRESETS: readonly DevicePreset[] = [
67
+ { id: 'monitor-fhd', label: 'Monitor 24" FHD', deviceSize: 'desktop', os: 'windows', width: 1920, height: 1080 },
68
+ { id: 'studio-display', label: 'Studio Display', deviceSize: 'desktop', os: 'macos', width: 2560, height: 1440 },
69
+ { id: 'monitor-hd', label: 'Monitor HD', deviceSize: 'desktop', os: 'windows', width: 1280, height: 720 },
70
+
71
+ { id: 'macbook-air-13', label: 'MacBook Air 13"', deviceSize: 'laptop', os: 'macos', width: 1440, height: 900 },
72
+ { id: 'macbook-pro-16', label: 'MacBook Pro 16"', deviceSize: 'laptop', os: 'macos', width: 1728, height: 1117 },
73
+ { id: 'windows-laptop', label: 'Windows Laptop', deviceSize: 'laptop', os: 'windows', width: 1366, height: 768 },
74
+
75
+ { id: 'ipad-pro-11', label: 'iPad Pro 11"', deviceSize: 'tablet', os: 'ipados', width: 834, height: 1194 },
76
+ { id: 'ipad-pro-13', label: 'iPad Pro 13"', deviceSize: 'tablet', os: 'ipados', width: 1032, height: 1376 },
77
+ { id: 'ipad-mini', label: 'iPad mini', deviceSize: 'tablet', os: 'ipados', width: 744, height: 1133 },
78
+ { id: 'galaxy-tab-s9', label: 'Galaxy Tab S9', deviceSize: 'tablet', os: 'android', width: 800, height: 1280 },
79
+
80
+ { id: 'iphone-16', label: 'iPhone 16', deviceSize: 'mobile', os: 'ios', width: 402, height: 874 },
81
+ { id: 'iphone-16-pro-max', label: 'iPhone 16 Pro Max', deviceSize: 'mobile', os: 'ios', width: 440, height: 956 },
82
+ { id: 'iphone-se', label: 'iPhone SE', deviceSize: 'mobile', os: 'ios', width: 375, height: 667 },
83
+ { id: 'pixel-9', label: 'Pixel 9', deviceSize: 'mobile', os: 'android', width: 412, height: 923 },
84
+ { id: 'galaxy-s24', label: 'Galaxy S24', deviceSize: 'mobile', os: 'android', width: 360, height: 780 }
85
+ ];
86
+
87
+ export const DEVICE_MODEL_IDS = DEVICE_PRESETS.map((preset) => preset.id) as [string, ...string[]];
88
+
89
+ export function isDeviceSize(value: unknown): value is DeviceSize {
90
+ return typeof value === 'string' && (DEVICE_SIZES as readonly string[]).includes(value);
91
+ }
92
+
93
+ export function getDevicePreset(id: string | null | undefined): DevicePreset | undefined {
94
+ return id ? DEVICE_PRESETS.find((preset) => preset.id === id) : undefined;
95
+ }
96
+
97
+ /**
98
+ * The model a tab is shown on: the one it names, when that model belongs to
99
+ * its class; otherwise the class default. Responsive has none.
100
+ */
101
+ export function resolveDevicePreset(deviceSize: DeviceSize, deviceModel?: string | null): DevicePreset | undefined {
102
+ if (deviceSize === 'responsive') return undefined;
103
+ const named = getDevicePreset(deviceModel);
104
+ if (named && named.deviceSize === deviceSize) return named;
105
+ return DEVICE_PRESETS.find((preset) => preset.deviceSize === deviceSize);
106
+ }
107
+
108
+ /** Held devices stand up; ones you sit in front of lie down. */
109
+ export function defaultRotation(deviceSize: DeviceSize): Rotation {
110
+ return deviceSize === 'tablet' || deviceSize === 'mobile' ? 'portrait' : 'landscape';
111
+ }
112
+
113
+ /**
114
+ * Whether a class can be turned. Phones and tablets turn in the hand and a
115
+ * monitor pivots on its stand, but a laptop's screen is hinged to its
116
+ * keyboard — a portrait laptop is not a thing anyone has seen.
117
+ */
118
+ export function canRotate(deviceSize: DeviceSize): boolean {
119
+ return deviceSize !== 'laptop';
120
+ }
121
+
122
+ /** A square counts as portrait, which is what the frames are drawn for. */
123
+ export function orientationOf(size: ViewportSize): Rotation {
124
+ return size.width > size.height ? 'landscape' : 'portrait';
125
+ }
126
+
127
+ /** The same size turned to face the given way. */
128
+ export function orient(size: ViewportSize, rotation: Rotation): ViewportSize {
129
+ const larger = Math.max(size.width, size.height);
130
+ const smaller = Math.min(size.width, size.height);
131
+ return rotation === 'portrait'
132
+ ? { width: smaller, height: larger }
133
+ : { width: larger, height: smaller };
134
+ }
135
+
136
+ export function clampViewport(size: ViewportSize): ViewportSize {
137
+ const clamp = (value: number) =>
138
+ Math.round(Math.min(VIEWPORT_LIMITS.max, Math.max(VIEWPORT_LIMITS.min, Number(value) || 0)));
139
+ return { width: clamp(size.width), height: clamp(size.height) };
140
+ }
141
+
142
+ /**
143
+ * Get viewport dimensions based on device size and rotation — the class
144
+ * default, ignoring any size the tab was set to.
23
145
  *
24
146
  * SIMPLE RULE:
25
147
  * - Portrait: height > width (vertical orientation)
26
148
  * - Landscape: width > height (horizontal orientation)
27
149
  */
28
- export function getViewportDimensions(
150
+ export function getViewportDimensions(deviceSize: DeviceSize, rotation: Rotation): ViewportSize {
151
+ return orient(DEVICE_VIEWPORTS[deviceSize] ?? DEVICE_VIEWPORTS.laptop, rotation);
152
+ }
153
+
154
+ /**
155
+ * The size a tab's page is laid out at.
156
+ *
157
+ * A tab carries its own size once anyone has picked one — a model, a typed
158
+ * W×H, a drag. Before that, and for anything persisted before tabs carried a
159
+ * size at all, the class default stands in.
160
+ */
161
+ export function resolveViewport(
29
162
  deviceSize: DeviceSize,
30
- rotation: Rotation
31
- ): { width: number; height: number } {
32
- const viewport = DEVICE_VIEWPORTS[deviceSize];
163
+ rotation: Rotation,
164
+ viewport?: Partial<ViewportSize> | null
165
+ ): ViewportSize {
166
+ if (viewport && Number(viewport.width) > 0 && Number(viewport.height) > 0) {
167
+ return clampViewport(viewport as ViewportSize);
168
+ }
169
+ return getViewportDimensions(deviceSize, rotation);
170
+ }
171
+
172
+ /** A model's own size, turned to face the given way. */
173
+ export function presetViewport(preset: DevicePreset, rotation: Rotation): ViewportSize {
174
+ return orient(preset, rotation);
175
+ }
176
+
177
+ /** What a tab is shown on. */
178
+ export interface DeviceState {
179
+ deviceSize: DeviceSize;
180
+ rotation: Rotation;
181
+ viewport: ViewportSize;
182
+ /** Null for responsive. */
183
+ deviceModel: string | null;
184
+ }
185
+
186
+ /**
187
+ * A change of device. Anything left out is carried over from the current
188
+ * state where that still makes sense, and derived where it does not.
189
+ */
190
+ export interface DeviceRequest {
191
+ deviceSize: DeviceSize;
192
+ rotation?: Rotation;
193
+ /** An exact size. Decides the orientation itself. */
194
+ size?: ViewportSize | null;
195
+ /** A model of `deviceSize`; one of another class falls back to the class default. */
196
+ deviceModel?: string | null;
197
+ }
198
+
199
+ /**
200
+ * Where a device change lands.
201
+ *
202
+ * The one rule for every way a device gets picked — a class, a model, a typed
203
+ * or dragged size, a rotation, an agent's `set_viewport` — shared by the panel
204
+ * and the server so the two cannot disagree:
205
+ *
206
+ * - an exact size wins, and its shape decides the orientation;
207
+ * - a new class or model starts from that model's own size, turned to the
208
+ * requested orientation (a new class: its natural one);
209
+ * - otherwise the current size is kept, turned to the requested orientation —
210
+ * so rotating a custom size keeps its dimensions;
211
+ * - responsive has no size of its own and keeps whatever the page had.
212
+ */
213
+ export function planDevice(current: DeviceState | null, request: DeviceRequest): DeviceState {
214
+ const deviceSize = request.deviceSize;
215
+ const classChanged = !current || current.deviceSize !== deviceSize;
216
+
217
+ const requestedModel =
218
+ request.deviceModel !== undefined ? request.deviceModel : classChanged ? null : current.deviceModel;
219
+ const preset = resolveDevicePreset(deviceSize, requestedModel);
220
+ const deviceModel = preset?.id ?? null;
221
+ const modelChanged = classChanged || deviceModel !== current.deviceModel;
33
222
 
34
- // Get the larger and smaller dimension
35
- const larger = Math.max(viewport.width, viewport.height);
36
- const smaller = Math.min(viewport.width, viewport.height);
223
+ let rotation: Rotation =
224
+ request.rotation ??
225
+ (!classChanged
226
+ ? current.rotation
227
+ : deviceSize === 'responsive' && current
228
+ ? orientationOf(current.viewport)
229
+ : defaultRotation(deviceSize));
37
230
 
38
- if (rotation === 'portrait') {
39
- // Portrait: height > width (vertical)
40
- return { width: smaller, height: larger };
231
+ // A laptop only lies one way; anything that asked otherwise without an
232
+ // exact size to insist on gets the way it actually lies.
233
+ if (!canRotate(deviceSize) && !request.size) rotation = 'landscape';
234
+
235
+ let viewport: ViewportSize;
236
+ if (request.size) {
237
+ viewport = clampViewport(request.size);
238
+ // A square has no orientation of its own, so the requested one stands.
239
+ if (viewport.width !== viewport.height) rotation = orientationOf(viewport);
240
+ } else if (preset && modelChanged) {
241
+ viewport = orient(preset, rotation);
242
+ } else if (current) {
243
+ viewport = orient(current.viewport, rotation);
41
244
  } else {
42
- // Landscape: width > height (horizontal)
43
- return { width: larger, height: smaller };
245
+ viewport = getViewportDimensions(deviceSize, rotation);
44
246
  }
247
+
248
+ return { deviceSize, rotation, viewport, deviceModel };
45
249
  }
46
250
 
47
251
  /**
48
- * Device frame bezel metrics (unscaled pixels).
252
+ * The colours a page asks to be framed in, read from the page itself the way
253
+ * a mobile browser reads them — never sampled from pixels.
254
+ */
255
+ export interface PageColors {
256
+ /** The status bar: the page's `theme-color` when it has one, else its background. */
257
+ statusBar: string;
258
+ /** Everything else around the page: the root or body background. */
259
+ background: string;
260
+ }
261
+
262
+ // ============================================================================
263
+ // Emulation
264
+ // ============================================================================
265
+
266
+ export type ColorSchemeOverride = 'light' | 'dark';
267
+ export type ReducedMotionOverride = 'reduce' | 'no-preference';
268
+
269
+ /**
270
+ * Device settings an agent (or a future control) can set on a tab beyond the
271
+ * model it is shown on. Absent means "what the device would do".
272
+ */
273
+ export interface DeviceEmulationOverrides {
274
+ /** Touch input and `'ontouchstart' in window`. Default: on for phones and tablets. */
275
+ touch?: boolean;
276
+ /** Mobile viewport: `<meta viewport>` honoured, overlay scrollbars. Default: on for phones and tablets. */
277
+ mobile?: boolean;
278
+ /** `prefers-color-scheme`. Default: whatever the browser reports. */
279
+ colorScheme?: ColorSchemeOverride;
280
+ /** `prefers-reduced-motion`. Default: whatever the browser reports. */
281
+ reducedMotion?: ReducedMotionOverride;
282
+ }
283
+
284
+ /**
285
+ * How the page should believe it is being viewed.
286
+ *
287
+ * Phones and tablets are emulated as such: `<meta name="viewport">` is honoured,
288
+ * `(hover: none)` and `(pointer: coarse)` match, and `'ontouchstart' in window`
289
+ * is true from the first script. Without it the Mobile class was a desktop
290
+ * page in a narrow window, which is not what a phone shows. Overrides win over
291
+ * the class, so a touch laptop or a desktop-mode phone can be tested too.
49
292
  *
50
- * These describe the decorative frame drawn around the canvas for each device
51
- * type and rotation. The canvas renders at native viewport dimensions; the
52
- * frame wraps it and the whole device (canvas + frame) is scaled together.
293
+ * The scale factor stays at 1 on purpose: the capture is sized from the
294
+ * viewer's own display, not from what the page thinks its density is.
295
+ */
296
+ export function getDeviceEmulation(
297
+ deviceSize: DeviceSize,
298
+ overrides?: DeviceEmulationOverrides | null
299
+ ): { isMobile: boolean; hasTouch: boolean } {
300
+ const handheld = deviceSize === 'mobile' || deviceSize === 'tablet';
301
+ return {
302
+ isMobile: overrides?.mobile ?? handheld,
303
+ hasTouch: overrides?.touch ?? handheld
304
+ };
305
+ }
306
+
307
+ /**
308
+ * How large the preview is drawn. `fit` scales it to the panel, never past
309
+ * 100%; a number is a fixed zoom and the panel scrolls when it does not fit.
310
+ * Capped at 100% because the capture is never sharper than the page itself.
53
311
  */
54
- export interface DeviceFrameMetrics {
312
+ export type PreviewZoom = 'fit' | number;
313
+
314
+ export const PREVIEW_ZOOM_LEVELS = [0.25, 0.5, 0.75, 1] as const;
315
+
316
+ export function isPreviewZoom(value: unknown): value is PreviewZoom {
317
+ return value === 'fit' || (PREVIEW_ZOOM_LEVELS as readonly unknown[]).includes(value);
318
+ }
319
+
320
+ // ============================================================================
321
+ // Device frames
322
+ // ============================================================================
323
+
324
+ export interface Rect {
325
+ x: number;
326
+ y: number;
327
+ width: number;
328
+ height: number;
329
+ }
330
+
331
+ interface Edges {
55
332
  top: number;
56
333
  right: number;
57
334
  bottom: number;
58
335
  left: number;
59
- bodyRadius: number;
60
- screenRadius: number;
61
- statusBarHeight: number;
62
- notch?: 'top' | 'left';
63
- homeBar?: 'bottom' | 'right';
64
- cameraDot?: 'top' | 'left';
65
- stand?: boolean;
66
- }
67
-
68
- const FRAME_METRICS: Record<DeviceSize, Record<Rotation, DeviceFrameMetrics>> = {
69
- desktop: {
70
- landscape: { top: 14, right: 14, bottom: 20, left: 14, bodyRadius: 14, screenRadius: 6, statusBarHeight: 22, stand: true },
71
- portrait: { top: 14, right: 14, bottom: 20, left: 14, bodyRadius: 14, screenRadius: 6, statusBarHeight: 22, stand: true }
336
+ }
337
+
338
+ /** Corner radii, clockwise from top-left — the order CSS `border-radius` takes. */
339
+ export type Corners = [number, number, number, number];
340
+
341
+ export type FrameKind = 'phone' | 'tablet' | 'laptop' | 'monitor' | 'none';
342
+
343
+ /** What the body is made of, which decides its colour. */
344
+ export type DeviceFinish = 'aluminum' | 'titanium' | 'space-black' | 'plastic';
345
+
346
+ type ButtonEdge = 'top' | 'right' | 'bottom' | 'left';
347
+
348
+ export interface FrameButton extends Rect {
349
+ edge: ButtonEdge;
350
+ }
351
+
352
+ /**
353
+ * Where everything in a device mockup sits, in unscaled pixels from the
354
+ * top-left of its footprint.
355
+ *
356
+ * The layers nest from the outside in: the `body`, the black `glass` set into
357
+ * it, the lit `display` behind the glass, and inside the display the `screen`
358
+ * — the page's own viewport — with the system UI (status bar, menu bar,
359
+ * taskbar, home indicator) in the strips around it. The page is never covered
360
+ * by any of that; the strips sit beside it, as they do on the real device.
361
+ *
362
+ * Both the mockup and every overlay drawn over the page read from here, so
363
+ * the two cannot disagree about where the page is.
364
+ */
365
+ export interface FrameLayout {
366
+ kind: FrameKind;
367
+ /** The model drawn, or null when there is no mockup. */
368
+ model: string | null;
369
+ os: DeviceOs | null;
370
+ finish: DeviceFinish | null;
371
+ orientation: Rotation;
372
+ /** Everything, accessories included: the box the preview has to fit. */
373
+ footprint: ViewportSize;
374
+ body: Rect;
375
+ bodyRadius: Corners;
376
+ glass: Rect;
377
+ glassRadius: Corners;
378
+ display: Rect;
379
+ displayRadius: Corners;
380
+ /** The page viewport. */
381
+ screen: Rect;
382
+ /** System UI strip above the page: status bar or menu bar. */
383
+ statusBar: Rect | null;
384
+ /** System UI strip below the page: home indicator area, nav bar or taskbar. */
385
+ bottomBar: Rect | null;
386
+ /** iOS / iPadOS home indicator. */
387
+ homeIndicator: Rect | null;
388
+ /**
389
+ * Android three-button navigation (back, home, recents): a horizontal bar
390
+ * along the bottom, or a vertical one down the right of a turned phone.
391
+ */
392
+ navBar: (Rect & { direction: 'horizontal' | 'vertical' }) | null;
393
+ island: Rect | null;
394
+ punchHole: Rect | null;
395
+ notch: Rect | null;
396
+ /** A lens in the bezel — the islands, holes and notches carry their own. */
397
+ camera: { x: number; y: number; size: number } | null;
398
+ earpiece: Rect | null;
399
+ homeButton: Rect | null;
400
+ buttons: FrameButton[];
401
+ /** Laptop deck, seen edge-on below the lid, and what is cut into or under it. */
402
+ base: Rect | null;
403
+ baseStyle: 'macbook' | 'notebook' | null;
404
+ scoop: Rect | null;
405
+ feet: Rect[];
406
+ /** Monitor stand. */
407
+ neck: Rect | null;
408
+ foot: Rect | null;
409
+ standStyle: 'studio' | 'pedestal' | null;
410
+ /**
411
+ * The page's own corner rounding — whatever of the display's rounding the
412
+ * system bands do not already absorb. A portrait phone's thin home strip
413
+ * leaves the page's bottom corners partly under the curve of the glass.
414
+ */
415
+ screenRadius: Corners;
416
+ }
417
+
418
+ interface ButtonSpec {
419
+ edge: ButtonEdge;
420
+ /** Where along the edge it starts and how long it runs, as fractions of that edge. */
421
+ start: number;
422
+ length: number;
423
+ }
424
+
425
+ /**
426
+ * One device's hardware.
427
+ *
428
+ * Bezels and buttons are drawn for the device held its natural way — portrait
429
+ * for phones and tablets — and turned for the other orientation; the system UI
430
+ * strips are given per orientation, because systems lay them out differently
431
+ * when turned (an iPhone drops its status bar, an Android phone keeps it).
432
+ */
433
+ interface DeviceModelSpec {
434
+ kind: Exclude<FrameKind, 'none'>;
435
+ finish: DeviceFinish;
436
+ /** Body edge → glass edge. */
437
+ rim: number;
438
+ /** Black border: glass edge → lit display, in the natural orientation. */
439
+ border: Edges;
440
+ /**
441
+ * System UI bands per orientation, beside the page and never over it: the
442
+ * status bar and the camera cutout above or to the side, the home
443
+ * indicator below, the menu bar or taskbar on a computer. This is where
444
+ * Safari and Chrome keep a page that has not opted into running under them
445
+ * (`viewport-fit=auto`), painted in the page's own colours.
446
+ */
447
+ chrome: Record<Rotation, Edges>;
448
+ bodyRadius: Corners;
449
+ glassRadius: Corners;
450
+ displayRadius: Corners;
451
+ cutout?: { kind: 'island'; long: number; short: number } | { kind: 'punch-hole'; size: number } | { kind: 'notch' };
452
+ /**
453
+ * A lens in the bezel: centred on the natural top edge, or on the edge that
454
+ * is on top when the device lies landscape.
455
+ */
456
+ camera?: 'border-top' | 'border-long';
457
+ earpiece?: boolean;
458
+ homeButton?: boolean;
459
+ buttons?: ButtonSpec[];
460
+ base?: 'macbook' | 'macbook-pro' | 'notebook';
461
+ stand?: 'studio' | 'pedestal';
462
+ }
463
+
464
+ const all = (n: number): Edges => ({ top: n, right: n, bottom: n, left: n });
465
+ const corners = (top: number, bottom = top): Corners => [top, top, bottom, bottom];
466
+ const edges = (top: number, right: number, bottom: number, left: number): Edges => ({ top, right, bottom, left });
467
+
468
+ const NO_CHROME = all(0);
469
+
470
+ /**
471
+ * Proportions after the real hardware, in the same units the page is laid out
472
+ * in — which for an iPhone or an iPad are the points its own UI is drawn in,
473
+ * so a 50pt status bar here is a 50pt status bar there.
474
+ */
475
+ const DEVICE_MODELS: Record<string, DeviceModelSpec> = {
476
+ // ── Phones ────────────────────────────────────────────────────────────
477
+ 'iphone-16': {
478
+ kind: 'phone',
479
+ finish: 'aluminum',
480
+ rim: 4,
481
+ border: all(9),
482
+ chrome: { portrait: edges(50, 0, 22, 0), landscape: edges(0, 50, 20, 50) },
483
+ bodyRadius: corners(66),
484
+ glassRadius: corners(62),
485
+ displayRadius: corners(53),
486
+ cutout: { kind: 'island', long: 120, short: 35 },
487
+ buttons: [
488
+ { edge: 'left', start: 0.15, length: 0.036 }, // Action button
489
+ { edge: 'left', start: 0.22, length: 0.07 }, // Volume up
490
+ { edge: 'left', start: 0.31, length: 0.07 }, // Volume down
491
+ { edge: 'right', start: 0.25, length: 0.11 }, // Side button
492
+ { edge: 'right', start: 0.6, length: 0.065 } // Camera Control
493
+ ]
494
+ },
495
+ 'iphone-16-pro-max': {
496
+ kind: 'phone',
497
+ finish: 'titanium',
498
+ rim: 3.5,
499
+ border: all(6.5),
500
+ chrome: { portrait: edges(54, 0, 22, 0), landscape: edges(0, 54, 20, 54) },
501
+ bodyRadius: corners(70),
502
+ glassRadius: corners(66.5),
503
+ displayRadius: corners(60),
504
+ cutout: { kind: 'island', long: 126, short: 37 },
505
+ buttons: [
506
+ { edge: 'left', start: 0.16, length: 0.034 },
507
+ { edge: 'left', start: 0.225, length: 0.065 },
508
+ { edge: 'left', start: 0.305, length: 0.065 },
509
+ { edge: 'right', start: 0.26, length: 0.1 },
510
+ { edge: 'right', start: 0.62, length: 0.06 }
511
+ ]
512
+ },
513
+ 'iphone-se': {
514
+ kind: 'phone',
515
+ finish: 'aluminum',
516
+ rim: 4,
517
+ border: edges(100, 20, 100, 20),
518
+ // iOS on a home-button iPhone: a 20pt status bar, hidden in landscape.
519
+ chrome: { portrait: edges(20, 0, 0, 0), landscape: NO_CHROME },
520
+ bodyRadius: corners(58),
521
+ glassRadius: corners(54),
522
+ displayRadius: corners(0),
523
+ camera: 'border-top',
524
+ earpiece: true,
525
+ homeButton: true,
526
+ buttons: [
527
+ { edge: 'left', start: 0.13, length: 0.03 }, // Ring/Silent switch
528
+ { edge: 'left', start: 0.2, length: 0.06 },
529
+ { edge: 'left', start: 0.28, length: 0.06 },
530
+ { edge: 'right', start: 0.19, length: 0.08 }
531
+ ]
532
+ },
533
+ 'pixel-9': {
534
+ kind: 'phone',
535
+ finish: 'aluminum',
536
+ rim: 4,
537
+ border: all(11),
538
+ // Three-button navigation: along the bottom when upright, down the right
539
+ // edge when turned — the bottom is then free and the page reaches it.
540
+ // The status bar stays on top, with the camera hole on the left.
541
+ chrome: { portrait: edges(36, 0, 48, 0), landscape: edges(24, 48, 0, 36) },
542
+ bodyRadius: corners(56),
543
+ glassRadius: corners(52),
544
+ displayRadius: corners(42),
545
+ cutout: { kind: 'punch-hole', size: 22 },
546
+ buttons: [
547
+ { edge: 'right', start: 0.22, length: 0.07 }, // Power
548
+ { edge: 'right', start: 0.32, length: 0.13 } // Volume rocker
549
+ ]
550
+ },
551
+ 'galaxy-s24': {
552
+ kind: 'phone',
553
+ finish: 'aluminum',
554
+ rim: 3,
555
+ border: all(7),
556
+ chrome: { portrait: edges(30, 0, 48, 0), landscape: edges(22, 48, 0, 30) },
557
+ bodyRadius: corners(42),
558
+ glassRadius: corners(39),
559
+ displayRadius: corners(33),
560
+ cutout: { kind: 'punch-hole', size: 18 },
561
+ buttons: [
562
+ { edge: 'right', start: 0.2, length: 0.12 }, // Volume rocker
563
+ { edge: 'right', start: 0.36, length: 0.065 } // Side key
564
+ ]
72
565
  },
73
- laptop: {
74
- landscape: { top: 14, right: 14, bottom: 22, left: 14, bodyRadius: 14, screenRadius: 6, statusBarHeight: 22 },
75
- portrait: { top: 14, right: 14, bottom: 22, left: 14, bodyRadius: 14, screenRadius: 6, statusBarHeight: 22 }
566
+
567
+ // ── Tablets ───────────────────────────────────────────────────────────
568
+ 'ipad-pro-11': {
569
+ kind: 'tablet',
570
+ finish: 'aluminum',
571
+ rim: 4,
572
+ border: all(24),
573
+ chrome: { portrait: edges(24, 0, 18, 0), landscape: edges(24, 0, 18, 0) },
574
+ bodyRadius: corners(48),
575
+ glassRadius: corners(44),
576
+ displayRadius: corners(18),
577
+ camera: 'border-long',
578
+ buttons: [
579
+ { edge: 'top', start: 0.8, length: 0.065 }, // Top button
580
+ { edge: 'right', start: 0.07, length: 0.045 },
581
+ { edge: 'right', start: 0.125, length: 0.045 }
582
+ ]
583
+ },
584
+ 'ipad-pro-13': {
585
+ kind: 'tablet',
586
+ finish: 'aluminum',
587
+ rim: 4,
588
+ border: all(26),
589
+ chrome: { portrait: edges(24, 0, 18, 0), landscape: edges(24, 0, 18, 0) },
590
+ bodyRadius: corners(52),
591
+ glassRadius: corners(48),
592
+ displayRadius: corners(20),
593
+ camera: 'border-long',
594
+ buttons: [
595
+ { edge: 'top', start: 0.82, length: 0.055 },
596
+ { edge: 'right', start: 0.06, length: 0.04 },
597
+ { edge: 'right', start: 0.11, length: 0.04 }
598
+ ]
599
+ },
600
+ 'ipad-mini': {
601
+ kind: 'tablet',
602
+ finish: 'aluminum',
603
+ rim: 4,
604
+ border: all(30),
605
+ chrome: { portrait: edges(24, 0, 18, 0), landscape: edges(24, 0, 18, 0) },
606
+ bodyRadius: corners(46),
607
+ glassRadius: corners(42),
608
+ displayRadius: corners(21),
609
+ // The mini keeps its camera on the short edge, above the display.
610
+ camera: 'border-top',
611
+ buttons: [
612
+ { edge: 'top', start: 0.56, length: 0.07 }, // Volume up
613
+ { edge: 'top', start: 0.645, length: 0.07 }, // Volume down
614
+ { edge: 'top', start: 0.79, length: 0.1 } // Top button with Touch ID
615
+ ]
616
+ },
617
+ 'galaxy-tab-s9': {
618
+ kind: 'tablet',
619
+ finish: 'aluminum',
620
+ rim: 3,
621
+ border: all(22),
622
+ // A tablet keeps its navigation buttons along the bottom either way up.
623
+ chrome: { portrait: edges(24, 0, 48, 0), landscape: edges(24, 0, 48, 0) },
624
+ bodyRadius: corners(34),
625
+ glassRadius: corners(31),
626
+ displayRadius: corners(12),
627
+ camera: 'border-long',
628
+ buttons: [
629
+ { edge: 'right', start: 0.08, length: 0.05 }, // Side key
630
+ { edge: 'right', start: 0.15, length: 0.08 } // Volume
631
+ ]
632
+ },
633
+
634
+ // ── Laptops ───────────────────────────────────────────────────────────
635
+ 'macbook-air-13': {
636
+ kind: 'laptop',
637
+ finish: 'aluminum',
638
+ rim: 3,
639
+ border: edges(14, 14, 24, 14),
640
+ chrome: { portrait: edges(30, 0, 0, 0), landscape: edges(30, 0, 0, 0) },
641
+ bodyRadius: corners(20, 6),
642
+ glassRadius: corners(17, 4),
643
+ displayRadius: corners(10, 0),
644
+ cutout: { kind: 'notch' },
645
+ base: 'macbook'
646
+ },
647
+ 'macbook-pro-16': {
648
+ kind: 'laptop',
649
+ finish: 'space-black',
650
+ rim: 3,
651
+ border: edges(12, 12, 20, 12),
652
+ chrome: { portrait: edges(32, 0, 0, 0), landscape: edges(32, 0, 0, 0) },
653
+ bodyRadius: corners(22, 6),
654
+ glassRadius: corners(19, 4),
655
+ displayRadius: corners(11, 0),
656
+ cutout: { kind: 'notch' },
657
+ base: 'macbook-pro'
658
+ },
659
+ 'windows-laptop': {
660
+ kind: 'laptop',
661
+ finish: 'plastic',
662
+ rim: 2,
663
+ border: edges(22, 16, 30, 16),
664
+ // Windows puts its system UI at the bottom: the taskbar.
665
+ chrome: { portrait: edges(0, 0, 40, 0), landscape: edges(0, 0, 40, 0) },
666
+ bodyRadius: corners(10, 4),
667
+ glassRadius: corners(8, 2),
668
+ displayRadius: corners(0),
669
+ camera: 'border-top',
670
+ base: 'notebook'
671
+ },
672
+
673
+ // ── Monitors ──────────────────────────────────────────────────────────
674
+ 'monitor-fhd': {
675
+ kind: 'monitor',
676
+ finish: 'plastic',
677
+ rim: 2,
678
+ border: edges(8, 8, 30, 8),
679
+ chrome: { portrait: edges(0, 0, 44, 0), landscape: edges(0, 0, 44, 0) },
680
+ bodyRadius: corners(8),
681
+ glassRadius: corners(6),
682
+ displayRadius: corners(0),
683
+ stand: 'pedestal'
76
684
  },
77
- tablet: {
78
- portrait: { top: 22, right: 26, bottom: 26, left: 26, bodyRadius: 36, screenRadius: 16, statusBarHeight: 26, cameraDot: 'top' },
79
- landscape: { top: 22, right: 26, bottom: 26, left: 26, bodyRadius: 36, screenRadius: 16, statusBarHeight: 26, cameraDot: 'left' }
685
+ 'studio-display': {
686
+ kind: 'monitor',
687
+ finish: 'aluminum',
688
+ rim: 6,
689
+ border: all(26),
690
+ chrome: { portrait: edges(26, 0, 0, 0), landscape: edges(26, 0, 0, 0) },
691
+ bodyRadius: corners(18),
692
+ glassRadius: corners(12),
693
+ displayRadius: corners(3),
694
+ camera: 'border-top',
695
+ stand: 'studio'
80
696
  },
81
- mobile: {
82
- portrait: { top: 18, right: 14, bottom: 22, left: 14, bodyRadius: 48, screenRadius: 32, statusBarHeight: 26, notch: 'top' },
83
- landscape: { top: 14, right: 22, bottom: 14, left: 18, bodyRadius: 48, screenRadius: 32, statusBarHeight: 26, notch: 'left' }
697
+ 'monitor-hd': {
698
+ kind: 'monitor',
699
+ finish: 'plastic',
700
+ rim: 2,
701
+ border: edges(10, 10, 26, 10),
702
+ chrome: { portrait: edges(0, 0, 40, 0), landscape: edges(0, 0, 40, 0) },
703
+ bodyRadius: corners(8),
704
+ glassRadius: corners(6),
705
+ displayRadius: corners(0),
706
+ stand: 'pedestal'
84
707
  }
85
708
  };
86
709
 
87
- export function getFrameMetrics(deviceSize: DeviceSize, rotation: Rotation): DeviceFrameMetrics {
88
- return FRAME_METRICS[deviceSize][rotation];
89
- }
710
+ /** Quarter turn counter-clockwise: portrait right edge ends up on top. */
711
+ function turnEdges(e: Edges): Edges {
712
+ return { top: e.right, right: e.bottom, bottom: e.left, left: e.top };
713
+ }
714
+
715
+ function turnButton(button: ButtonSpec): ButtonSpec {
716
+ switch (button.edge) {
717
+ case 'right':
718
+ return { edge: 'top', start: button.start, length: button.length };
719
+ case 'top':
720
+ return { edge: 'left', start: 1 - button.start - button.length, length: button.length };
721
+ case 'left':
722
+ return { edge: 'bottom', start: button.start, length: button.length };
723
+ case 'bottom':
724
+ return { edge: 'right', start: 1 - button.start - button.length, length: button.length };
725
+ }
726
+ }
727
+
728
+ const BUTTON_DEPTH = 3;
729
+
730
+ const clampTo = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
731
+
732
+ function flatLayout(viewport: ViewportSize, orientation: Rotation): FrameLayout {
733
+ const page: Rect = { x: 0, y: 0, width: viewport.width, height: viewport.height };
734
+ const flat: Corners = [0, 0, 0, 0];
735
+ return {
736
+ kind: 'none',
737
+ model: null,
738
+ os: null,
739
+ finish: null,
740
+ orientation,
741
+ footprint: { width: viewport.width, height: viewport.height },
742
+ body: page,
743
+ bodyRadius: flat,
744
+ glass: page,
745
+ glassRadius: flat,
746
+ display: page,
747
+ displayRadius: flat,
748
+ screen: page,
749
+ statusBar: null,
750
+ bottomBar: null,
751
+ homeIndicator: null,
752
+ navBar: null,
753
+ island: null,
754
+ punchHole: null,
755
+ notch: null,
756
+ camera: null,
757
+ earpiece: null,
758
+ homeButton: null,
759
+ buttons: [],
760
+ base: null,
761
+ baseStyle: null,
762
+ scoop: null,
763
+ feet: [],
764
+ neck: null,
765
+ foot: null,
766
+ standStyle: null,
767
+ screenRadius: flat
768
+ };
769
+ }
770
+
771
+ /**
772
+ * Lay out a device mockup around a viewport of the given size.
773
+ *
774
+ * The model decides the hardware; the orientation is read off the viewport's
775
+ * own shape rather than taken as an argument, so the frame cannot be drawn for
776
+ * one orientation around a page laid out in the other. With `showFrame` off,
777
+ * or for `responsive`, there is no mockup: every layer collapses onto the page.
778
+ */
779
+ export function getFrameLayout(
780
+ deviceSize: DeviceSize,
781
+ viewport: ViewportSize,
782
+ showFrame = true,
783
+ deviceModel?: string | null
784
+ ): FrameLayout {
785
+ const orientation = orientationOf(viewport);
786
+ const preset = resolveDevicePreset(deviceSize, deviceModel);
787
+ const spec = preset ? DEVICE_MODELS[preset.id] : undefined;
788
+ if (!preset || !spec || !showFrame) return flatLayout(viewport, orientation);
789
+
790
+ const vw = viewport.width;
791
+ const vh = viewport.height;
792
+ const handheld = spec.kind === 'phone' || spec.kind === 'tablet';
793
+ const turned = handheld && orientation === 'landscape';
794
+
795
+ const rim = all(spec.rim);
796
+ const border = turned ? turnEdges(spec.border) : spec.border;
797
+ const chrome = spec.chrome[orientation];
798
+
799
+ // Sizes first, from the page outwards.
800
+ const displayW = chrome.left + vw + chrome.right;
801
+ const displayH = chrome.top + vh + chrome.bottom;
802
+ const glassW = border.left + displayW + border.right;
803
+ const glassH = border.top + displayH + border.bottom;
804
+ const bodyW = rim.left + glassW + rim.right;
805
+ const bodyH = rim.top + glassH + rim.bottom;
806
+
807
+ // Accessories, which decide how far the footprint reaches past the body.
808
+ const buttonSpecs = (spec.buttons ?? []).map((button) => (turned ? turnButton(button) : button));
809
+ const buttonEdges = new Set(buttonSpecs.map((b) => b.edge));
810
+
811
+ let baseW = 0;
812
+ let baseH = 0;
813
+ let feetH = 0;
814
+ let neckW = 0;
815
+ let neckH = 0;
816
+ let footW = 0;
817
+ let footH = 0;
818
+ if (spec.base === 'macbook') {
819
+ baseW = Math.round(bodyW * 1.12);
820
+ baseH = Math.round(clampTo(bodyW * 0.012, 12, 22));
821
+ } else if (spec.base === 'macbook-pro') {
822
+ baseW = Math.round(bodyW * 1.1);
823
+ baseH = Math.round(clampTo(bodyW * 0.016, 16, 30));
824
+ feetH = 4;
825
+ } else if (spec.base === 'notebook') {
826
+ baseW = Math.round(bodyW * 1.06);
827
+ baseH = Math.round(clampTo(bodyW * 0.018, 14, 26));
828
+ feetH = 3;
829
+ }
830
+ if (spec.stand === 'studio') {
831
+ neckW = Math.round(clampTo(bodyW * 0.17, 110, 380));
832
+ neckH = Math.round(clampTo(bodyH * 0.14, 60, 180));
833
+ footW = Math.round(neckW * 1.35);
834
+ footH = Math.round(clampTo(bodyW * 0.007, 8, 14));
835
+ } else if (spec.stand === 'pedestal') {
836
+ neckW = Math.round(clampTo(bodyW * 0.045, 36, 90));
837
+ neckH = Math.round(clampTo(bodyH * 0.16, 70, 190));
838
+ footW = Math.round(clampTo(bodyW * 0.34, 220, 700));
839
+ footH = Math.round(clampTo(bodyW * 0.014, 12, 24));
840
+ }
841
+
842
+ const sideReach = Math.max(baseW, footW) > bodyW ? (Math.max(baseW, footW) - bodyW) / 2 : 0;
843
+ const outset: Edges = {
844
+ top: buttonEdges.has('top') ? BUTTON_DEPTH : 0,
845
+ right: Math.max(sideReach, buttonEdges.has('right') ? BUTTON_DEPTH : 0),
846
+ bottom: Math.max(baseH + feetH, neckH + footH, buttonEdges.has('bottom') ? BUTTON_DEPTH : 0),
847
+ left: Math.max(sideReach, buttonEdges.has('left') ? BUTTON_DEPTH : 0)
848
+ };
849
+
850
+ // Then positions, from the body inwards.
851
+ const body: Rect = { x: outset.left, y: outset.top, width: bodyW, height: bodyH };
852
+ const glass: Rect = { x: body.x + rim.left, y: body.y + rim.top, width: glassW, height: glassH };
853
+ const display: Rect = { x: glass.x + border.left, y: glass.y + border.top, width: displayW, height: displayH };
854
+ const screen: Rect = { x: display.x + chrome.left, y: display.y + chrome.top, width: vw, height: vh };
855
+ const [tl, tr, br, bl] = spec.displayRadius;
856
+ const screenRadius: Corners = [
857
+ Math.max(0, tl - Math.max(chrome.left, chrome.top)),
858
+ Math.max(0, tr - Math.max(chrome.right, chrome.top)),
859
+ Math.max(0, br - Math.max(chrome.right, chrome.bottom)),
860
+ Math.max(0, bl - Math.max(chrome.left, chrome.bottom))
861
+ ];
862
+
863
+ const statusBar: Rect | null =
864
+ chrome.top > 0 ? { x: display.x, y: display.y, width: displayW, height: chrome.top } : null;
865
+ const bottomBar: Rect | null =
866
+ chrome.bottom > 0
867
+ ? { x: display.x, y: display.y + displayH - chrome.bottom, width: displayW, height: chrome.bottom }
868
+ : null;
869
+
870
+ // iOS home indicator.
871
+ let homeIndicator: Rect | null = null;
872
+ if (bottomBar && (preset.os === 'ios' || preset.os === 'ipados')) {
873
+ const barW =
874
+ orientation === 'portrait' || spec.kind === 'tablet'
875
+ ? clampTo(displayW * (spec.kind === 'tablet' ? 0.24 : 0.34), 100, 300)
876
+ : clampTo(displayW * 0.24, 140, 220);
877
+ homeIndicator = {
878
+ x: display.x + (displayW - barW) / 2,
879
+ y: bottomBar.y + bottomBar.height / 2 - 2.5,
880
+ width: barW,
881
+ height: 5
882
+ };
883
+ }
884
+
885
+ // Android navigation buttons: the bottom band, or the right one when a
886
+ // turned phone moved them there.
887
+ let navBar: FrameLayout['navBar'] = null;
888
+ if (preset.os === 'android') {
889
+ if (bottomBar) {
890
+ navBar = { ...bottomBar, direction: 'horizontal' };
891
+ } else if (chrome.right > 0) {
892
+ navBar = {
893
+ x: screen.x + vw,
894
+ y: screen.y,
895
+ width: chrome.right,
896
+ height: vh,
897
+ direction: 'vertical'
898
+ };
899
+ }
900
+ }
901
+
902
+ let island: Rect | null = null;
903
+ let punchHole: Rect | null = null;
904
+ let notch: Rect | null = null;
905
+ let camera: FrameLayout['camera'] = null;
906
+ let earpiece: Rect | null = null;
907
+ let homeButton: Rect | null = null;
908
+
909
+ const cutout = spec.cutout;
910
+ if (cutout?.kind === 'island') {
911
+ island =
912
+ orientation === 'portrait'
913
+ ? { x: display.x + (displayW - cutout.long) / 2, y: display.y + 11, width: cutout.long, height: cutout.short }
914
+ : { x: display.x + 11, y: display.y + (displayH - cutout.long) / 2, width: cutout.short, height: cutout.long };
915
+ camera = {
916
+ x: orientation === 'portrait' ? island.x + island.width - cutout.short / 2 : island.x + cutout.short / 2,
917
+ y: orientation === 'portrait' ? island.y + cutout.short / 2 : island.y + island.height - cutout.short / 2,
918
+ size: Math.round(cutout.short * 0.32)
919
+ };
920
+ } else if (cutout?.kind === 'punch-hole') {
921
+ const size = cutout.size;
922
+ punchHole =
923
+ orientation === 'portrait'
924
+ ? { x: display.x + (displayW - size) / 2, y: display.y + (chrome.top - size) / 2, width: size, height: size }
925
+ : { x: display.x + (chrome.left - size) / 2, y: display.y + (displayH - size) / 2, width: size, height: size };
926
+ } else if (cutout?.kind === 'notch') {
927
+ const notchW = Math.round(clampTo(displayW * 0.13, 120, 240));
928
+ notch = { x: display.x + (displayW - notchW) / 2, y: display.y, width: notchW, height: chrome.top };
929
+ camera = { x: notch.x + notchW / 2, y: notch.y + chrome.top / 2, size: 9 };
930
+ }
931
+
932
+ // The natural top edge: on top for a laptop, a monitor or an upright
933
+ // handheld; on the left for a handheld turned counter-clockwise.
934
+ const naturalTopCentre = turned
935
+ ? { x: glass.x + border.left / 2, y: glass.y + glassH / 2 }
936
+ : { x: glass.x + glassW / 2, y: glass.y + border.top / 2 };
937
+ const naturalBottomCentre = turned
938
+ ? { x: glass.x + glassW - border.right / 2, y: glass.y + glassH / 2 }
939
+ : { x: glass.x + glassW / 2, y: glass.y + glassH - border.bottom / 2 };
940
+
941
+ if (spec.camera === 'border-top') {
942
+ const size = spec.kind === 'phone' ? 10 : 8;
943
+ camera = spec.earpiece
944
+ ? // Beside the earpiece, not on it.
945
+ turned
946
+ ? { x: naturalTopCentre.x, y: naturalTopCentre.y + 46, size }
947
+ : { x: naturalTopCentre.x - 46, y: naturalTopCentre.y, size }
948
+ : { ...naturalTopCentre, size };
949
+ } else if (spec.camera === 'border-long') {
950
+ // The edge that is on top in landscape — on the right when upright.
951
+ camera =
952
+ orientation === 'landscape'
953
+ ? { x: glass.x + glassW / 2, y: glass.y + border.top / 2, size: 9 }
954
+ : { x: glass.x + glassW - border.right / 2, y: glass.y + glassH / 2, size: 9 };
955
+ }
956
+
957
+ if (spec.earpiece) {
958
+ const long = 58;
959
+ const short = 6;
960
+ earpiece = turned
961
+ ? { x: naturalTopCentre.x - short / 2, y: naturalTopCentre.y - long / 2, width: short, height: long }
962
+ : { x: naturalTopCentre.x - long / 2, y: naturalTopCentre.y - short / 2, width: long, height: short };
963
+ }
964
+
965
+ if (spec.homeButton) {
966
+ const size = Math.round(clampTo((turned ? border.right : border.bottom) * 0.64, 40, 72));
967
+ homeButton = { x: naturalBottomCentre.x - size / 2, y: naturalBottomCentre.y - size / 2, width: size, height: size };
968
+ }
969
+
970
+ const buttons: FrameButton[] = buttonSpecs.map((button) => {
971
+ const horizontal = button.edge === 'top' || button.edge === 'bottom';
972
+ const along = horizontal ? bodyW : bodyH;
973
+ const start = (horizontal ? body.x : body.y) + along * button.start;
974
+ const length = along * button.length;
975
+ switch (button.edge) {
976
+ case 'top':
977
+ return { edge: 'top', x: start, y: body.y - BUTTON_DEPTH, width: length, height: BUTTON_DEPTH + 1 };
978
+ case 'bottom':
979
+ return { edge: 'bottom', x: start, y: body.y + bodyH - 1, width: length, height: BUTTON_DEPTH + 1 };
980
+ case 'left':
981
+ return { edge: 'left', x: body.x - BUTTON_DEPTH, y: start, width: BUTTON_DEPTH + 1, height: length };
982
+ case 'right':
983
+ return { edge: 'right', x: body.x + bodyW - 1, y: start, width: BUTTON_DEPTH + 1, height: length };
984
+ }
985
+ });
986
+
987
+ const centreX = body.x + bodyW / 2;
988
+ const base: Rect | null =
989
+ baseW > 0 ? { x: centreX - baseW / 2, y: body.y + bodyH, width: baseW, height: baseH } : null;
990
+ const scoop: Rect | null =
991
+ base && spec.base !== 'notebook'
992
+ ? { x: centreX - bodyW * 0.08, y: base.y, width: bodyW * 0.16, height: Math.max(4, baseH * 0.3) }
993
+ : null;
994
+ const feet: Rect[] =
995
+ base && feetH > 0
996
+ ? [0.08, 0.92].map((at) => ({
997
+ x: base.x + base.width * at - base.width * 0.04,
998
+ y: base.y + base.height,
999
+ width: base.width * 0.08,
1000
+ height: feetH
1001
+ }))
1002
+ : [];
1003
+ const neck: Rect | null =
1004
+ neckW > 0 ? { x: centreX - neckW / 2, y: body.y + bodyH, width: neckW, height: neckH } : null;
1005
+ const foot: Rect | null =
1006
+ footW > 0 ? { x: centreX - footW / 2, y: body.y + bodyH + neckH, width: footW, height: footH } : null;
1007
+
1008
+ return {
1009
+ kind: spec.kind,
1010
+ model: preset.id,
1011
+ os: preset.os,
1012
+ finish: spec.finish,
1013
+ orientation,
1014
+ footprint: {
1015
+ width: outset.left + bodyW + outset.right,
1016
+ height: outset.top + bodyH + outset.bottom
1017
+ },
1018
+ body,
1019
+ bodyRadius: spec.bodyRadius,
1020
+ glass,
1021
+ glassRadius: spec.glassRadius,
1022
+ display,
1023
+ displayRadius: spec.displayRadius,
1024
+ screen,
1025
+ statusBar,
1026
+ bottomBar,
1027
+ homeIndicator,
1028
+ navBar,
1029
+ island,
1030
+ punchHole,
1031
+ notch,
1032
+ camera,
1033
+ earpiece,
1034
+ homeButton,
1035
+ buttons,
1036
+ base,
1037
+ baseStyle: spec.base === 'notebook' ? 'notebook' : spec.base ? 'macbook' : null,
1038
+ scoop,
1039
+ feet,
1040
+ neck,
1041
+ foot,
1042
+ standStyle: spec.stand ?? null,
1043
+ screenRadius
1044
+ };
1045
+ }
1046
+
1047
+ /** `border-radius` for a set of corners, optionally scaled. */
1048
+ export function cornersToCss(radius: Corners, scale = 1): string {
1049
+ return radius.map((r) => `${r * scale}px`).join(' ');
1050
+ }