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 AUXILIARY_PANE_MAX_WIDTH = 480;
58const AUXILIARY_PANE_DEFAULT_MAX_WIDTH = 320;
59const FILE_INSPECTOR_MIN_VIEWPORT_WIDTH = 820;
60const 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.
63const 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 */
69export type LayoutVariant = "compact" | "medium" | "expanded";
70
71export 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
80export 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
91export interface FileInspectorPaneLayout {
92 readonly supported: boolean;
93 readonly width: number | null;
94}
95
96export function deriveThreadFeedInitialContentInset(input: {
97 readonly platform: string;
98 readonly usesNativeAutomaticInsets: boolean;
99 readonly bottomContentInset: number;
100}): { readonly bottom: number } | undefined {
101 if (input.platform !== "android" || input.usesNativeAutomaticInsets) {
102 return undefined;
103 }
104
105 return { bottom: Math.max(0, input.bottomContentInset) };
106}
107
108export function deriveLayout(input: {
109 readonly width: number;
110 readonly height: number;
111 readonly nativeMetrics?: NativeLayoutMetrics | null;
112 readonly nativeWorkspace?: boolean;
113}): Layout {
114 const { width, height } = input;
115 const metrics = input.nativeMetrics;
116 const usableWidth = width - (metrics?.safeArea.left ?? 0) - (metrics?.safeArea.right ?? 0);
117 // UIKit retains split columns below the generic tablet breakpoint. Native
118 // columns follow its size class while reserving room for both column minimums.
119 // Unfolded in portrait, the inner display reports neither a side bar nor a
120 // fold, only its hinge.
121 const hasDuoGeometry =
122 metrics &&
123 (metrics.hasHinge === true ||
124 metrics.verticalBarEdge !== "none" ||
125 metrics.reservedRegions.some((region) => region.kind === "division"));
126 const variant: LayoutVariant =
127 metrics && (input.nativeWorkspace || hasDuoGeometry)
128 ? metrics.horizontalSizeClass === "regular" &&
129 usableWidth >= SPLIT_SIDEBAR_MIN_WIDTH + 320 &&
130 (input.nativeWorkspace || height >= 400)
131 ? // The native split floats its sidebar over an unfolded portrait Duo.
132 input.nativeWorkspace === true && metrics.hasHinge === true && width < height
133 ? "medium"
134 : "expanded"
135 : "compact"
136 : height < SPLIT_LAYOUT_MIN_HEIGHT || width < MEDIUM_LAYOUT_MIN_WIDTH
137 ? "compact"
138 : width >= EXPANDED_LAYOUT_MIN_WIDTH
139 ? "expanded"
140 : // Only the Android shell floats a sidebar over a page; iOS keeps its phone stack.
141 metrics
142 ? "compact"
143 : "medium";
144
145 if (variant === "compact") {
146 return {
147 variant,
148 usesSplitView: false,
149 listPaneWidth: null,
150 shellPadding: 0,
151 };
152 }
153
154 const division = metrics?.reservedRegions.find(
155 (region) =>
156 region.kind === "division" &&
157 region.height >= height / 2 &&
158 region.x - metrics.safeArea.left >= SPLIT_SIDEBAR_MIN_WIDTH &&
159 width - region.x - region.width - metrics.safeArea.right >= 320,
160 );
161 return {
162 variant,
163 usesSplitView: true,
164 listPaneWidth:
165 division?.x ??
166 clamp(Math.round(width * 0.32), SPLIT_SIDEBAR_MIN_WIDTH, SPLIT_SIDEBAR_DEFAULT_MAX_WIDTH),
167 ...(division ? { listPaneGap: division.width } : {}),
168 shellPadding: 0,
169 };
170}
171
172export interface VerticalHinge {
173 readonly x: number;
174 readonly width: number;
175}
176
177/**
178 * Moves the list/detail split onto a book-posture hinge so neither pane straddles the crease.
179 * `leadingWidth` is chrome left of the list pane, such as the Android navigation rail.
180 */
181export function alignListPaneToHinge(input: {
182 readonly layout: Layout;
183 readonly hinge: VerticalHinge | null;
184 readonly viewportWidth: number;
185 readonly leadingWidth: number;
186}): Layout {
187 const { layout, hinge } = input;
188 if (!hinge || !layout.usesSplitView) return layout;
189 const listPaneWidth = hinge.x - input.leadingWidth;
190 if (
191 listPaneWidth < SPLIT_SIDEBAR_MIN_WIDTH ||
192 input.viewportWidth - hinge.x - hinge.width < 320
193 ) {
194 return layout;
195 }
196 return { ...layout, listPaneWidth, listPaneGap: hinge.width };
197}
198
199/**
200 * Whether the workspace root must keep the primary sidebar open. Native split
201 * columns keep a sidebar toggle in the empty detail header, so only they may
202 * collapse it there. Compact layouts stay pinned so a reveal request (such as
203 * focusing search) cannot flip the saved preference for a later split view.
204 */
205/** Home's list, or the Settings list in its place, with no detail selected. */
206export function isWorkspaceListPath(pathname: string): boolean {
207 return pathname === "/" || pathname === "/settings";
208}
209
210export function isPrimarySidebarPinned(input: {
211 readonly pathname: string;
212 readonly nativeWorkspace: boolean;
213 readonly usesSplitView: boolean;
214}): boolean {
215 return isWorkspaceListPath(input.pathname) && !(input.nativeWorkspace && input.usesSplitView);
216}
217
218export function deriveWorkspacePaneLayout(input: {
219 readonly layout: Layout;
220 readonly viewportWidth: number;
221 readonly nativeWorkspace?: boolean;
222 /** Whether the sidebar floats over the content rather than taking its room; medium by default. */
223 readonly primarySidebarFloats?: boolean;
224 readonly primarySidebarPreferredVisible: boolean;
225 readonly auxiliaryPanePreferredVisible: boolean;
226 readonly auxiliaryPanePreferredWidth?: number;
227 /** The native inspector column's width; UIKit lays it out, not this layout. */
228 readonly nativeInspectorWidth?: number;
229}): WorkspacePaneLayout {
230 const viewportWidth = Math.max(0, input.viewportWidth);
231 const floats = input.primarySidebarFloats ?? input.layout.variant === "medium";
232 const preferredPrimarySidebarVisible =
233 input.layout.usesSplitView && input.primarySidebarPreferredVisible;
234 const preferredPrimarySidebarWidth =
235 preferredPrimarySidebarVisible && !floats
236 ? (input.layout.listPaneWidth ?? 0) + (input.layout.listPaneGap ?? 0)
237 : 0;
238 const fileInspector = deriveFileInspectorPaneLayout({
239 layout: input.layout,
240 viewportWidth,
241 preferredWidth: input.auxiliaryPanePreferredWidth,
242 reservedLeadingWidth: preferredPrimarySidebarWidth,
243 });
244 const auxiliaryPaneVisible = fileInspector.supported && input.auxiliaryPanePreferredVisible;
245 const inspectorWidth = input.nativeWorkspace
246 ? (input.nativeInspectorWidth ?? null)
247 : fileInspector.width;
248 const primarySidebarSuppressedByAuxiliary =
249 preferredPrimarySidebarVisible &&
250 !floats &&
251 auxiliaryPaneVisible &&
252 inspectorWidth !== null &&
253 input.layout.listPaneWidth !== null &&
254 viewportWidth - input.layout.listPaneWidth - (input.layout.listPaneGap ?? 0) - 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
271export function nativeInspectorColumnWidth(viewportWidth: number, fraction: number) {
272 return Math.max(260, Math.min(viewportWidth * 0.6, viewportWidth * fraction));
273}
274
275export function deriveFileInspectorPaneLayout(input: {
276 readonly layout: Layout;
277 readonly viewportWidth: number;
278 readonly preferredWidth?: number;
279 readonly reservedLeadingWidth?: number;
280}): FileInspectorPaneLayout {
281 const viewportWidth = Math.max(0, input.viewportWidth);
282 const reservedLeadingWidth = Number.isFinite(input.reservedLeadingWidth)
283 ? Math.max(0, input.reservedLeadingWidth ?? 0)
284 : 0;
285 const availableContentWidth = Math.max(0, viewportWidth - reservedLeadingWidth);
286 const supported =
287 input.layout.usesSplitView && viewportWidth >= FILE_INSPECTOR_MIN_VIEWPORT_WIDTH;
288
289 return {
290 supported,
291 width: supported
292 ? constrainAuxiliaryPaneWidth({
293 preferredWidth:
294 input.preferredWidth ??
295 clamp(
296 Math.round(availableContentWidth * 0.28),
297 AUXILIARY_PANE_MIN_WIDTH,
298 AUXILIARY_PANE_DEFAULT_MAX_WIDTH,
299 ),
300 availableWidth: availableContentWidth,
301 })
302 : null,
303 };
304}
305
306export function constrainAuxiliaryPaneWidth(input: {
307 readonly preferredWidth: number;
308 readonly availableWidth: number;
309}): number {
310 const safePreferredWidth = Number.isFinite(input.preferredWidth)
311 ? input.preferredWidth
312 : AUXILIARY_PANE_MIN_WIDTH;
313 const availableWidth = Number.isFinite(input.availableWidth)
314 ? Math.max(0, input.availableWidth)
315 : 0;
316 const maxWidth = Math.max(
317 AUXILIARY_PANE_MIN_WIDTH,
318 Math.min(AUXILIARY_PANE_MAX_WIDTH, availableWidth - FILE_INSPECTOR_MIN_MAIN_WIDTH),
319 );
320 return clamp(Math.round(safePreferredWidth), AUXILIARY_PANE_MIN_WIDTH, maxWidth);
321}
322
323export function deriveCenteredContentHorizontalPadding(input: {
324 readonly viewportWidth: number;
325 readonly maxContentWidth: number | null;
326 readonly minimumPadding: number;
327}): number {
328 const viewportWidth = Number.isFinite(input.viewportWidth) ? Math.max(0, input.viewportWidth) : 0;
329 const minimumPadding = Number.isFinite(input.minimumPadding)
330 ? Math.max(0, input.minimumPadding)
331 : 0;
332
333 if (
334 input.maxContentWidth === null ||
335 !Number.isFinite(input.maxContentWidth) ||
336 input.maxContentWidth <= 0
337 ) {
338 return minimumPadding;
339 }
340
341 return minimumPadding + Math.max(0, (viewportWidth - input.maxContentWidth) / 2);
342}