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