The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { DevicePlatform, PreviewViewportSetting } from "@t3tools/contracts"; |
| 2 | |
| 3 | import type { BrowserSurfaceContentPresentation } from "~/browser/browserSurfaceStore"; |
| 4 | import { |
| 5 | resolveFittedBrowserViewport, |
| 6 | type BrowserViewportResizeDirection, |
| 7 | } from "~/browser/browserViewportLayout"; |
| 8 | import type { PreviewMiniPlayerPosition, PreviewMiniPlayerSize } from "~/previewMiniPlayerStore"; |
| 9 | |
| 10 | import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream"; |
| 11 | |
| 12 | export const PREVIEW_MINI_PLAYER_EDGE_GAP = 12; |
| 13 | export const PREVIEW_MINI_PLAYER_CORNER_RADIUS = 12; |
| 14 | // The mini-player shell straddles this webview at 47 and 49: above --z-sheet, under dialogs (50). |
| 15 | export const PREVIEW_MINI_PLAYER_WEBVIEW_Z_INDEX = 48; |
| 16 | const PREVIEW_MINI_PLAYER_DEFAULT_BOX = { width: 320, height: 320 } as const; |
| 17 | const PREVIEW_MINI_PLAYER_MIN_SIZE = { width: 240, height: 150 } as const; |
| 18 | |
| 19 | export interface PreviewMiniPlayerFrame extends PreviewMiniPlayerPosition, PreviewMiniPlayerSize {} |
| 20 | |
| 21 | export function resolvePreviewMiniPlayerSourceSize( |
| 22 | viewport: PreviewViewportSetting, |
| 23 | fittedSourceContent: BrowserSurfaceContentPresentation | null, |
| 24 | zoomFactor: number, |
| 25 | ): PreviewMiniPlayerSize { |
| 26 | const normalizedZoomFactor = Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1; |
| 27 | const fitted = resolveFittedBrowserViewport(viewport, fittedSourceContent, normalizedZoomFactor); |
| 28 | return { |
| 29 | width: fitted.width * normalizedZoomFactor, |
| 30 | height: fitted.height * normalizedZoomFactor, |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | export function resolveDeviceMiniPlayerSourceSize( |
| 35 | platform: DevicePlatform, |
| 36 | screen: DeviceScreenSize | null, |
| 37 | ): PreviewMiniPlayerSize { |
| 38 | if (!screen) { |
| 39 | const width = 1_000; |
| 40 | return { width, height: width / (platform === "ios" ? 9 / 19.5 : 9 / 20) }; |
| 41 | } |
| 42 | const landscape = |
| 43 | screen.orientation === "landscape_left" || screen.orientation === "landscape_right"; |
| 44 | const long = Math.max(screen.width, screen.height); |
| 45 | const short = Math.min(screen.width, screen.height); |
| 46 | return landscape ? { width: long, height: short } : { width: short, height: long }; |
| 47 | } |
| 48 | |
| 49 | /** Android frames include black corner wedges; clip at a matching radius. iOS streams rectangles and |
| 50 | * retains the frame radius. */ |
| 51 | export function resolveDeviceMiniPlayerCornerRadius( |
| 52 | platform: DevicePlatform, |
| 53 | player: PreviewMiniPlayerSize, |
| 54 | ): number { |
| 55 | if (platform !== "android") return PREVIEW_MINI_PLAYER_CORNER_RADIUS; |
| 56 | return Math.max( |
| 57 | PREVIEW_MINI_PLAYER_CORNER_RADIUS, |
| 58 | Math.round(Math.min(player.width, player.height) * 0.14), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | interface HorizontalSpan { |
| 63 | readonly left: number; |
| 64 | readonly right: number; |
| 65 | } |
| 66 | |
| 67 | export interface PreviewMiniPlayerObstacles { |
| 68 | readonly composer: (HorizontalSpan & { readonly height: number }) | null; |
| 69 | readonly detailsCard: (HorizontalSpan & { readonly bottom: number }) | null; |
| 70 | } |
| 71 | |
| 72 | const NO_PREVIEW_MINI_PLAYER_OBSTACLES: PreviewMiniPlayerObstacles = { |
| 73 | composer: null, |
| 74 | detailsCard: null, |
| 75 | }; |
| 76 | |
| 77 | const spanOf = (x: number, width: number): HorizontalSpan => ({ left: x, right: x + width }); |
| 78 | |
| 79 | const spansOverlap = (a: HorizontalSpan, b: HorizontalSpan) => a.left < b.right && a.right > b.left; |
| 80 | |
| 81 | function floorFor( |
| 82 | span: HorizontalSpan, |
| 83 | container: PreviewMiniPlayerSize, |
| 84 | obstacles: PreviewMiniPlayerObstacles, |
| 85 | ): number { |
| 86 | const { composer } = obstacles; |
| 87 | return composer && spansOverlap(span, composer) |
| 88 | ? container.height - Math.max(0, composer.height) |
| 89 | : container.height; |
| 90 | } |
| 91 | |
| 92 | function ceilingFor(span: HorizontalSpan, obstacles: PreviewMiniPlayerObstacles): number { |
| 93 | const { detailsCard } = obstacles; |
| 94 | return detailsCard && spansOverlap(span, detailsCard) ? Math.max(0, detailsCard.bottom) : 0; |
| 95 | } |
| 96 | |
| 97 | const availableArea = ( |
| 98 | container: PreviewMiniPlayerSize, |
| 99 | obstacles: PreviewMiniPlayerObstacles, |
| 100 | span: HorizontalSpan | null, |
| 101 | ): PreviewMiniPlayerSize => ({ |
| 102 | width: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2, |
| 103 | height: |
| 104 | (span |
| 105 | ? floorFor(span, container, obstacles) - ceilingFor(span, obstacles) |
| 106 | : container.height - Math.max(0, obstacles.composer?.height ?? 0)) - |
| 107 | PREVIEW_MINI_PLAYER_EDGE_GAP * 2, |
| 108 | }); |
| 109 | |
| 110 | const minimumPreviewMiniPlayerWidth = (source: PreviewMiniPlayerSize) => |
| 111 | Math.max( |
| 112 | PREVIEW_MINI_PLAYER_MIN_SIZE.width, |
| 113 | (PREVIEW_MINI_PLAYER_MIN_SIZE.height * source.width) / source.height, |
| 114 | ); |
| 115 | |
| 116 | function fitPreviewMiniPlayerWidth( |
| 117 | desiredWidth: number, |
| 118 | source: PreviewMiniPlayerSize, |
| 119 | max: PreviewMiniPlayerSize, |
| 120 | minimumWidth = minimumPreviewMiniPlayerWidth(source), |
| 121 | ): PreviewMiniPlayerSize { |
| 122 | const aspectRatio = source.width / source.height; |
| 123 | const width = Math.min( |
| 124 | Math.max(desiredWidth, minimumWidth), |
| 125 | Math.max(1, max.width), |
| 126 | Math.max(1, max.height * aspectRatio), |
| 127 | ); |
| 128 | return { width: Math.round(width), height: Math.round(width / aspectRatio) }; |
| 129 | } |
| 130 | |
| 131 | function defaultPreviewMiniPlayerWidth(source: PreviewMiniPlayerSize): number { |
| 132 | return Math.min( |
| 133 | PREVIEW_MINI_PLAYER_DEFAULT_BOX.width, |
| 134 | (PREVIEW_MINI_PLAYER_DEFAULT_BOX.height * source.width) / source.height, |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | const clampToContainer = ( |
| 139 | position: PreviewMiniPlayerPosition, |
| 140 | container: PreviewMiniPlayerSize, |
| 141 | player: PreviewMiniPlayerSize, |
| 142 | minimumX = PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 143 | ): PreviewMiniPlayerPosition => ({ |
| 144 | x: Math.min( |
| 145 | Math.max(position.x, minimumX, PREVIEW_MINI_PLAYER_EDGE_GAP), |
| 146 | Math.max( |
| 147 | PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 148 | container.width - player.width - PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 149 | ), |
| 150 | ), |
| 151 | y: Math.min( |
| 152 | Math.max(position.y, PREVIEW_MINI_PLAYER_EDGE_GAP), |
| 153 | Math.max( |
| 154 | PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 155 | container.height - player.height - PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 156 | ), |
| 157 | ), |
| 158 | }); |
| 159 | |
| 160 | const overlapsObstacle = ( |
| 161 | position: PreviewMiniPlayerPosition, |
| 162 | player: PreviewMiniPlayerSize, |
| 163 | container: PreviewMiniPlayerSize, |
| 164 | obstacles: PreviewMiniPlayerObstacles, |
| 165 | ): boolean => { |
| 166 | const span = spanOf(position.x, player.width); |
| 167 | return ( |
| 168 | position.y + player.height > floorFor(span, container, obstacles) || |
| 169 | position.y < ceilingFor(span, obstacles) |
| 170 | ); |
| 171 | }; |
| 172 | |
| 173 | export function clampPreviewMiniPlayerPosition( |
| 174 | position: PreviewMiniPlayerPosition, |
| 175 | container: PreviewMiniPlayerSize, |
| 176 | player: PreviewMiniPlayerSize, |
| 177 | obstacles: PreviewMiniPlayerObstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES, |
| 178 | minimumX = PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 179 | ): PreviewMiniPlayerPosition { |
| 180 | const inside = clampToContainer(position, container, player, minimumX); |
| 181 | if (!overlapsObstacle(inside, player, container, obstacles)) return inside; |
| 182 | const gap = PREVIEW_MINI_PLAYER_EDGE_GAP; |
| 183 | const fitRows = (x: number): PreviewMiniPlayerPosition => { |
| 184 | const span = spanOf(x, player.width); |
| 185 | return { |
| 186 | x, |
| 187 | y: Math.min( |
| 188 | container.height - gap - player.height, |
| 189 | Math.max( |
| 190 | Math.min(inside.y, floorFor(span, container, obstacles) - gap - player.height), |
| 191 | ceilingFor(span, obstacles) + gap, |
| 192 | gap, |
| 193 | ), |
| 194 | ), |
| 195 | }; |
| 196 | }; |
| 197 | const fallback = fitRows(inside.x); |
| 198 | const { composer, detailsCard } = obstacles; |
| 199 | const span = spanOf(inside.x, player.width); |
| 200 | const beside = [composer, detailsCard] |
| 201 | .filter( |
| 202 | (obstacle): obstacle is NonNullable<typeof obstacle> => |
| 203 | obstacle !== null && spansOverlap(span, obstacle), |
| 204 | ) |
| 205 | .flatMap((obstacle) => [obstacle.left - gap - player.width, obstacle.right + gap]); |
| 206 | let best = fallback; |
| 207 | let bestDistance = overlapsObstacle(fallback, player, container, obstacles) |
| 208 | ? Number.POSITIVE_INFINITY |
| 209 | : Math.abs(fallback.y - inside.y); |
| 210 | for (const x of beside) { |
| 211 | const candidate = fitRows(x); |
| 212 | if ( |
| 213 | clampToContainer(candidate, container, player, minimumX).x !== x || |
| 214 | overlapsObstacle(candidate, player, container, obstacles) |
| 215 | ) { |
| 216 | continue; |
| 217 | } |
| 218 | const distance = Math.abs(x - inside.x) + Math.abs(candidate.y - inside.y); |
| 219 | if (distance < bestDistance) { |
| 220 | best = candidate; |
| 221 | bestDistance = distance; |
| 222 | } |
| 223 | } |
| 224 | return best; |
| 225 | } |
| 226 | |
| 227 | /** Clamp per layout without persisting so narrow containers preserve the chosen width. */ |
| 228 | export function resolvePreviewMiniPlayerFrame(input: { |
| 229 | readonly width: number | null; |
| 230 | readonly position: PreviewMiniPlayerPosition | null; |
| 231 | readonly source: PreviewMiniPlayerSize; |
| 232 | readonly container: PreviewMiniPlayerSize; |
| 233 | readonly obstacles?: PreviewMiniPlayerObstacles; |
| 234 | }): PreviewMiniPlayerFrame { |
| 235 | const { |
| 236 | width, |
| 237 | position, |
| 238 | source, |
| 239 | container, |
| 240 | obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES, |
| 241 | } = input; |
| 242 | const size = fitPreviewMiniPlayerWidth( |
| 243 | width ?? defaultPreviewMiniPlayerWidth(source), |
| 244 | source, |
| 245 | availableArea( |
| 246 | container, |
| 247 | position && width ? { composer: obstacles.composer, detailsCard: null } : obstacles, |
| 248 | position && width ? spanOf(position.x, width) : null, |
| 249 | ), |
| 250 | width ?? undefined, |
| 251 | ); |
| 252 | const { detailsCard } = obstacles; |
| 253 | const anchored = |
| 254 | position ?? |
| 255 | (detailsCard |
| 256 | ? { x: detailsCard.right - size.width, y: detailsCard.bottom + PREVIEW_MINI_PLAYER_EDGE_GAP } |
| 257 | : { |
| 258 | x: container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - size.width, |
| 259 | y: PREVIEW_MINI_PLAYER_EDGE_GAP, |
| 260 | }); |
| 261 | return { ...clampPreviewMiniPlayerPosition(anchored, container, size, obstacles), ...size }; |
| 262 | } |
| 263 | |
| 264 | export function resizePreviewMiniPlayer(input: { |
| 265 | readonly start: PreviewMiniPlayerFrame; |
| 266 | readonly direction: BrowserViewportResizeDirection; |
| 267 | readonly delta: PreviewMiniPlayerPosition; |
| 268 | readonly source: PreviewMiniPlayerSize; |
| 269 | readonly container: PreviewMiniPlayerSize; |
| 270 | readonly obstacles?: PreviewMiniPlayerObstacles; |
| 271 | }): PreviewMiniPlayerFrame { |
| 272 | const { |
| 273 | start, |
| 274 | direction, |
| 275 | delta, |
| 276 | source, |
| 277 | container, |
| 278 | obstacles = NO_PREVIEW_MINI_PLAYER_OBSTACLES, |
| 279 | } = input; |
| 280 | const east = direction.includes("east"); |
| 281 | const west = direction.includes("west"); |
| 282 | const north = direction.includes("north"); |
| 283 | const south = direction.includes("south"); |
| 284 | const right = start.x + start.width; |
| 285 | const bottom = start.y + start.height; |
| 286 | const span = spanOf(start.x, start.width); |
| 287 | const floor = floorFor(span, container, obstacles); |
| 288 | const ceiling = ceilingFor(span, obstacles); |
| 289 | const max = { |
| 290 | width: west |
| 291 | ? right - PREVIEW_MINI_PLAYER_EDGE_GAP |
| 292 | : east |
| 293 | ? container.width - PREVIEW_MINI_PLAYER_EDGE_GAP - start.x |
| 294 | : container.width - PREVIEW_MINI_PLAYER_EDGE_GAP * 2, |
| 295 | height: north |
| 296 | ? bottom - ceiling - PREVIEW_MINI_PLAYER_EDGE_GAP |
| 297 | : south |
| 298 | ? floor - PREVIEW_MINI_PLAYER_EDGE_GAP - start.y |
| 299 | : floor - ceiling - PREVIEW_MINI_PLAYER_EDGE_GAP * 2, |
| 300 | }; |
| 301 | const desiredWidth = start.width + (east ? delta.x : west ? -delta.x : 0); |
| 302 | const desiredHeight = start.height + (south ? delta.y : north ? -delta.y : 0); |
| 303 | const horizontal = east || west; |
| 304 | const vertical = north || south; |
| 305 | const aspectRatio = source.width / source.height; |
| 306 | // Project onto the aspect-ratio diagonal; dominant-axis switching jumps when axes move in opposite |
| 307 | // directions. |
| 308 | const desired = |
| 309 | horizontal && vertical |
| 310 | ? (desiredWidth + desiredHeight / aspectRatio) / (1 + 1 / aspectRatio ** 2) |
| 311 | : horizontal |
| 312 | ? desiredWidth |
| 313 | : desiredHeight * aspectRatio; |
| 314 | const size = fitPreviewMiniPlayerWidth( |
| 315 | desired, |
| 316 | source, |
| 317 | max, |
| 318 | Math.min(start.width, minimumPreviewMiniPlayerWidth(source)), |
| 319 | ); |
| 320 | const position = clampPreviewMiniPlayerPosition( |
| 321 | { x: west ? right - size.width : start.x, y: north ? bottom - size.height : start.y }, |
| 322 | container, |
| 323 | size, |
| 324 | obstacles, |
| 325 | ); |
| 326 | return { ...position, ...size }; |
| 327 | } |