The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { scaledTypographyLineHeight } from "./appearancePreferences"; |
| 2 | import { MOBILE_TYPOGRAPHY } from "./typography"; |
| 3 | import type { NativeLayoutMetrics } from "./reserved-regions"; |
| 4 | |
| 5 | function clamp(value: number, min: number, max: number): number { |
| 6 | return Math.min(Math.max(value, min), max); |
| 7 | } |
| 8 | |
| 9 | const MEDIUM_LAYOUT_MIN_WIDTH = 600; |
| 10 | const EXPANDED_LAYOUT_MIN_WIDTH = 720; |
| 11 | const SPLIT_LAYOUT_MIN_HEIGHT = 600; |
| 12 | |
| 13 | const SPLIT_SIDEBAR_MIN_WIDTH = 280; |
| 14 | const SPLIT_SIDEBAR_DEFAULT_MAX_WIDTH = 380; |
| 15 | |
| 16 | export const CHAT_CONTENT_MAX_WIDTH = 960; |
| 17 | // Limit Android reading width for wide foldables and keyboard-open split views. |
| 18 | export const ANDROID_READING_MAX_WIDTH = 640; |
| 19 | // min-h-8 uses the 14px rem configured in metro.config.js. |
| 20 | export const THREAD_WORK_ROW_MIN_HEIGHT = 28; |
| 21 | |
| 22 | export function deriveThreadArrangementSizing(input: { |
| 23 | readonly baseFontSize: number; |
| 24 | readonly fontScale: number; |
| 25 | }) { |
| 26 | const bodyLineHeight = scaledTypographyLineHeight(MOBILE_TYPOGRAPHY.body, input.baseFontSize); |
| 27 | const headerLineHeight = scaledTypographyLineHeight( |
| 28 | MOBILE_TYPOGRAPHY.footnote, |
| 29 | input.baseFontSize, |
| 30 | ); |
| 31 | return { |
| 32 | rowHeight: Math.max(56, Math.ceil(2 * bodyLineHeight * input.fontScale) + 10), |
| 33 | headerHeight: Math.max(48, Math.ceil(headerLineHeight * input.fontScale) + 24), |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | export function deriveThreadWorkLogSizing(input: { |
| 38 | readonly baseFontSize: number; |
| 39 | readonly fontScale: number; |
| 40 | }) { |
| 41 | const lineHeight = scaledTypographyLineHeight(MOBILE_TYPOGRAPHY.footnote, input.baseFontSize); |
| 42 | return { |
| 43 | textSizeKey: `${input.baseFontSize}:${input.fontScale}`, |
| 44 | estimatedRowHeight: Math.max( |
| 45 | THREAD_WORK_ROW_MIN_HEIGHT, |
| 46 | Math.ceil(lineHeight * input.fontScale), |
| 47 | ), |
| 48 | // Accessibility scaling can exceed authored line heights; leave those rows measured. |
| 49 | fixedRowHeight: |
| 50 | input.fontScale <= 1 && lineHeight <= THREAD_WORK_ROW_MIN_HEIGHT |
| 51 | ? THREAD_WORK_ROW_MIN_HEIGHT |
| 52 | : undefined, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | const AUXILIARY_PANE_MIN_WIDTH = 260; |
| 57 | const AUXILIARY_PANE_MAX_WIDTH = 480; |
| 58 | const AUXILIARY_PANE_DEFAULT_MAX_WIDTH = 320; |
| 59 | const FILE_INSPECTOR_MIN_VIEWPORT_WIDTH = 820; |
| 60 | const FILE_INSPECTOR_MIN_MAIN_WIDTH = 560; |
| 61 | // UIKit's inspector column keeps its own width, so the sidebar steps aside only when the page |
| 62 | // would get narrower than this: landscape 11" and 13" iPads keep three columns, portrait ones don't. |
| 63 | export const NATIVE_INSPECTOR_MIN_MAIN_WIDTH = 320; |
| 64 | |
| 65 | /** |
| 66 | * The window's width class. Compact: the sidebar is the root and pages push over it. Medium: the |
| 67 | * sidebar floats over the content. Expanded: the sidebar sits beside the content. |
| 68 | */ |
| 69 | export type LayoutVariant = "compact" | "medium" | "expanded"; |
| 70 | |
| 71 | export interface Layout { |
| 72 | readonly variant: LayoutVariant; |
| 73 | /** Medium or expanded: pages replace the content beside the sidebar instead of pushing. */ |
| 74 | readonly usesSplitView: boolean; |
| 75 | readonly listPaneWidth: number | null; |
| 76 | readonly listPaneGap?: number; |
| 77 | readonly shellPadding: number; |
| 78 | } |
| 79 | |
| 80 | export interface WorkspacePaneLayout { |
| 81 | readonly primarySidebarVisible: boolean; |
| 82 | /** Whether the sidebar shows once a collapsed split expands; independent of the current width. */ |
| 83 | readonly primarySidebarWhenExpanded: boolean; |
| 84 | readonly primarySidebarSuppressedByAuxiliary: boolean; |
| 85 | readonly contentPaneWidth: number; |
| 86 | readonly supportsAuxiliaryPane: boolean; |
| 87 | readonly auxiliaryPaneVisible: boolean; |
| 88 | readonly auxiliaryPaneWidth: number | null; |
| 89 | } |
| 90 | |
| 91 | export interface FileInspectorPaneLayout { |
| 92 | readonly supported: boolean; |
| 93 | readonly width: number | null; |
| 94 | } |
| 95 | |
| 96 | export function deriveLayout(input: { |
| 97 | readonly width: number; |
| 98 | readonly height: number; |
| 99 | readonly nativeMetrics?: NativeLayoutMetrics | null; |
| 100 | readonly nativeWorkspace?: boolean; |
| 101 | }): Layout { |
| 102 | const { width, height } = input; |
| 103 | const metrics = input.nativeMetrics; |
| 104 | const usableWidth = width - (metrics?.safeArea.left ?? 0) - (metrics?.safeArea.right ?? 0); |
| 105 | // UIKit retains split columns below the generic tablet breakpoint. Native |
| 106 | // columns follow its size class while reserving room for both column minimums. |
| 107 | // Unfolded in portrait, the inner display reports neither a side bar nor a |
| 108 | // fold, only its hinge. |
| 109 | const hasDuoGeometry = |
| 110 | metrics && |
| 111 | (metrics.hasHinge === true || |
| 112 | metrics.verticalBarEdge !== "none" || |
| 113 | metrics.reservedRegions.some((region) => region.kind === "division")); |
| 114 | const variant: LayoutVariant = |
| 115 | metrics && (input.nativeWorkspace || hasDuoGeometry) |
| 116 | ? metrics.horizontalSizeClass === "regular" && |
| 117 | usableWidth >= SPLIT_SIDEBAR_MIN_WIDTH + 320 && |
| 118 | (input.nativeWorkspace || height >= 400) |
| 119 | ? // The native split floats its sidebar over an unfolded portrait Duo. |
| 120 | input.nativeWorkspace === true && metrics.hasHinge === true && width < height |
| 121 | ? "medium" |
| 122 | : "expanded" |
| 123 | : "compact" |
| 124 | : height < SPLIT_LAYOUT_MIN_HEIGHT || width < MEDIUM_LAYOUT_MIN_WIDTH |
| 125 | ? "compact" |
| 126 | : width >= EXPANDED_LAYOUT_MIN_WIDTH |
| 127 | ? "expanded" |
| 128 | : // Only the Android shell floats a sidebar over a page; iOS keeps its phone stack. |
| 129 | metrics |
| 130 | ? "compact" |
| 131 | : "medium"; |
| 132 | |
| 133 | if (variant === "compact") { |
| 134 | return { |
| 135 | variant, |
| 136 | usesSplitView: false, |
| 137 | listPaneWidth: null, |
| 138 | shellPadding: 0, |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | const division = metrics?.reservedRegions.find( |
| 143 | (region) => |
| 144 | region.kind === "division" && |
| 145 | region.height >= height / 2 && |
| 146 | region.x - metrics.safeArea.left >= SPLIT_SIDEBAR_MIN_WIDTH && |
| 147 | width - region.x - region.width - metrics.safeArea.right >= 320, |
| 148 | ); |
| 149 | return { |
| 150 | variant, |
| 151 | usesSplitView: true, |
| 152 | listPaneWidth: |
| 153 | division?.x ?? |
| 154 | (input.nativeWorkspace === true && variant === "medium" |
| 155 | ? 350 |
| 156 | : clamp( |
| 157 | Math.round(width * 0.32), |
| 158 | SPLIT_SIDEBAR_MIN_WIDTH, |
| 159 | SPLIT_SIDEBAR_DEFAULT_MAX_WIDTH, |
| 160 | )), |
| 161 | ...(division ? { listPaneGap: division.width } : {}), |
| 162 | shellPadding: 0, |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | export interface VerticalHinge { |
| 167 | readonly x: number; |
| 168 | readonly width: number; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Moves the list/detail split onto a book-posture hinge so neither pane straddles the crease. |
| 173 | * `leadingWidth` is chrome left of the list pane, such as the Android navigation rail. |
| 174 | */ |
| 175 | export function alignListPaneToHinge(input: { |
| 176 | readonly layout: Layout; |
| 177 | readonly hinge: VerticalHinge | null; |
| 178 | readonly viewportWidth: number; |
| 179 | readonly leadingWidth: number; |
| 180 | }): Layout { |
| 181 | const { layout, hinge } = input; |
| 182 | if (!hinge || !layout.usesSplitView) return layout; |
| 183 | const listPaneWidth = hinge.x - input.leadingWidth; |
| 184 | if ( |
| 185 | listPaneWidth < SPLIT_SIDEBAR_MIN_WIDTH || |
| 186 | input.viewportWidth - hinge.x - hinge.width < 320 |
| 187 | ) { |
| 188 | return layout; |
| 189 | } |
| 190 | return { ...layout, listPaneWidth, listPaneGap: hinge.width }; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Whether the workspace root must keep the primary sidebar open. Native split |
| 195 | * columns keep a sidebar toggle in the empty detail header, so only they may |
| 196 | * collapse it there. Compact layouts stay pinned so a reveal request (such as |
| 197 | * focusing search) cannot flip the saved preference for a later split view. |
| 198 | */ |
| 199 | /** Home's list, or the Settings list in its place, with no detail selected. */ |
| 200 | export function isWorkspaceListPath(pathname: string): boolean { |
| 201 | return pathname === "/" || pathname === "/settings"; |
| 202 | } |
| 203 | |
| 204 | export function isPrimarySidebarPinned(input: { |
| 205 | readonly pathname: string; |
| 206 | readonly nativeWorkspace: boolean; |
| 207 | readonly usesSplitView: boolean; |
| 208 | }): boolean { |
| 209 | return isWorkspaceListPath(input.pathname) && !(input.nativeWorkspace && input.usesSplitView); |
| 210 | } |
| 211 | |
| 212 | export function deriveWorkspacePaneLayout(input: { |
| 213 | readonly layout: Layout; |
| 214 | readonly viewportWidth: number; |
| 215 | /** Width UIKit keeps out of its columns, such as the Duo's system bar. */ |
| 216 | readonly horizontalSafeArea?: number; |
| 217 | readonly nativeWorkspace?: boolean; |
| 218 | /** Whether the sidebar floats over the content rather than taking its room; medium by default. */ |
| 219 | readonly primarySidebarFloats?: boolean; |
| 220 | readonly primarySidebarPreferredVisible: boolean; |
| 221 | readonly auxiliaryPanePreferredVisible: boolean; |
| 222 | readonly auxiliaryPanePreferredWidth?: number; |
| 223 | /** The native inspector column's width; UIKit lays it out, not this layout. */ |
| 224 | readonly nativeInspectorWidth?: number; |
| 225 | }): WorkspacePaneLayout { |
| 226 | const viewportWidth = Math.max(0, input.viewportWidth); |
| 227 | const floats = input.primarySidebarFloats ?? input.layout.variant === "medium"; |
| 228 | const preferredPrimarySidebarVisible = |
| 229 | input.layout.usesSplitView && input.primarySidebarPreferredVisible; |
| 230 | const preferredPrimarySidebarWidth = |
| 231 | preferredPrimarySidebarVisible && !floats |
| 232 | ? (input.layout.listPaneWidth ?? 0) + (input.layout.listPaneGap ?? 0) |
| 233 | : 0; |
| 234 | const fileInspector = deriveFileInspectorPaneLayout({ |
| 235 | layout: input.layout, |
| 236 | viewportWidth, |
| 237 | preferredWidth: input.auxiliaryPanePreferredWidth, |
| 238 | reservedLeadingWidth: preferredPrimarySidebarWidth, |
| 239 | }); |
| 240 | const auxiliaryPaneVisible = fileInspector.supported && input.auxiliaryPanePreferredVisible; |
| 241 | const inspectorWidth = input.nativeWorkspace |
| 242 | ? (input.nativeInspectorWidth ?? null) |
| 243 | : fileInspector.width; |
| 244 | const primarySidebarSuppressedByAuxiliary = |
| 245 | preferredPrimarySidebarVisible && |
| 246 | !floats && |
| 247 | auxiliaryPaneVisible && |
| 248 | inspectorWidth !== null && |
| 249 | input.layout.listPaneWidth !== null && |
| 250 | viewportWidth - |
| 251 | (input.nativeWorkspace ? (input.horizontalSafeArea ?? 0) : 0) - |
| 252 | input.layout.listPaneWidth - |
| 253 | (input.layout.listPaneGap ?? 0) - |
| 254 | inspectorWidth < |
| 255 | (input.nativeWorkspace ? NATIVE_INSPECTOR_MIN_MAIN_WIDTH : FILE_INSPECTOR_MIN_MAIN_WIDTH); |
| 256 | const primarySidebarVisible = |
| 257 | preferredPrimarySidebarVisible && !primarySidebarSuppressedByAuxiliary; |
| 258 | const primarySidebarWidth = primarySidebarVisible ? preferredPrimarySidebarWidth : 0; |
| 259 | |
| 260 | return { |
| 261 | primarySidebarVisible, |
| 262 | primarySidebarWhenExpanded: input.primarySidebarPreferredVisible, |
| 263 | primarySidebarSuppressedByAuxiliary, |
| 264 | contentPaneWidth: Math.max(0, viewportWidth - primarySidebarWidth), |
| 265 | supportsAuxiliaryPane: fileInspector.supported, |
| 266 | auxiliaryPaneVisible, |
| 267 | auxiliaryPaneWidth: fileInspector.width, |
| 268 | }; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * UIKit drops the inspector column, and refuses to show it again, once the page beside it would |
| 273 | * fall under its minimum inside the safe area (the Duo's system bar takes width from the window). |
| 274 | */ |
| 275 | export function nativeInspectorColumnWidth(input: { |
| 276 | readonly viewportWidth: number; |
| 277 | readonly horizontalSafeArea: number; |
| 278 | readonly fraction: number; |
| 279 | }) { |
| 280 | const { viewportWidth } = input; |
| 281 | const maxWidth = Math.min( |
| 282 | viewportWidth * 0.6, |
| 283 | viewportWidth - input.horizontalSafeArea - NATIVE_INSPECTOR_MIN_MAIN_WIDTH, |
| 284 | ); |
| 285 | return Math.max(260, Math.min(maxWidth, viewportWidth * input.fraction)); |
| 286 | } |
| 287 | |
| 288 | export function deriveFileInspectorPaneLayout(input: { |
| 289 | readonly layout: Layout; |
| 290 | readonly viewportWidth: number; |
| 291 | readonly preferredWidth?: number; |
| 292 | readonly reservedLeadingWidth?: number; |
| 293 | }): FileInspectorPaneLayout { |
| 294 | const viewportWidth = Math.max(0, input.viewportWidth); |
| 295 | const reservedLeadingWidth = Number.isFinite(input.reservedLeadingWidth) |
| 296 | ? Math.max(0, input.reservedLeadingWidth ?? 0) |
| 297 | : 0; |
| 298 | const availableContentWidth = Math.max(0, viewportWidth - reservedLeadingWidth); |
| 299 | const supported = |
| 300 | input.layout.usesSplitView && viewportWidth >= FILE_INSPECTOR_MIN_VIEWPORT_WIDTH; |
| 301 | |
| 302 | return { |
| 303 | supported, |
| 304 | width: supported |
| 305 | ? constrainAuxiliaryPaneWidth({ |
| 306 | preferredWidth: |
| 307 | input.preferredWidth ?? |
| 308 | clamp( |
| 309 | Math.round(availableContentWidth * 0.28), |
| 310 | AUXILIARY_PANE_MIN_WIDTH, |
| 311 | AUXILIARY_PANE_DEFAULT_MAX_WIDTH, |
| 312 | ), |
| 313 | availableWidth: availableContentWidth, |
| 314 | }) |
| 315 | : null, |
| 316 | }; |
| 317 | } |
| 318 | |
| 319 | export function constrainAuxiliaryPaneWidth(input: { |
| 320 | readonly preferredWidth: number; |
| 321 | readonly availableWidth: number; |
| 322 | }): number { |
| 323 | const safePreferredWidth = Number.isFinite(input.preferredWidth) |
| 324 | ? input.preferredWidth |
| 325 | : AUXILIARY_PANE_MIN_WIDTH; |
| 326 | const availableWidth = Number.isFinite(input.availableWidth) |
| 327 | ? Math.max(0, input.availableWidth) |
| 328 | : 0; |
| 329 | const maxWidth = Math.max( |
| 330 | AUXILIARY_PANE_MIN_WIDTH, |
| 331 | Math.min(AUXILIARY_PANE_MAX_WIDTH, availableWidth - FILE_INSPECTOR_MIN_MAIN_WIDTH), |
| 332 | ); |
| 333 | return clamp(Math.round(safePreferredWidth), AUXILIARY_PANE_MIN_WIDTH, maxWidth); |
| 334 | } |
| 335 | |
| 336 | export function deriveCenteredContentHorizontalPadding(input: { |
| 337 | readonly viewportWidth: number; |
| 338 | readonly maxContentWidth: number | null; |
| 339 | readonly minimumPadding: number; |
| 340 | }): number { |
| 341 | const viewportWidth = Number.isFinite(input.viewportWidth) ? Math.max(0, input.viewportWidth) : 0; |
| 342 | const minimumPadding = Number.isFinite(input.minimumPadding) |
| 343 | ? Math.max(0, input.minimumPadding) |
| 344 | : 0; |
| 345 | |
| 346 | if ( |
| 347 | input.maxContentWidth === null || |
| 348 | !Number.isFinite(input.maxContentWidth) || |
| 349 | input.maxContentWidth <= 0 |
| 350 | ) { |
| 351 | return minimumPadding; |
| 352 | } |
| 353 | |
| 354 | return minimumPadding + Math.max(0, (viewportWidth - input.maxContentWidth) / 2); |
| 355 | } |