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