Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

layout.ts

361 lines13.0 KB

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 };
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 };
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.
63export 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 */
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;
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;
90​
91export interface FileInspectorPaneLayout {
92 readonly supported: boolean;
93 readonly width: number | null;
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) };
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 };
171​
172export interface VerticalHinge {
173 readonly x: number;
174 readonly width: number;
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 };
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";
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);
217​
218export function deriveWorkspacePaneLayout(input: {
219 readonly layout: Layout;
220 readonly viewportWidth: number;
221 /** Width UIKit keeps out of its columns, such as the Duo's system bar. */
222 readonly horizontalSafeArea?: number;
223 readonly nativeWorkspace?: boolean;
224 /** Whether the sidebar floats over the content rather than taking its room; medium by default. */
225 readonly primarySidebarFloats?: boolean;
226 readonly primarySidebarPreferredVisible: boolean;
227 readonly auxiliaryPanePreferredVisible: boolean;
228 readonly auxiliaryPanePreferredWidth?: number;
229 /** The native inspector column's width; UIKit lays it out, not this layout. */
230 readonly nativeInspectorWidth?: number;
231}): WorkspacePaneLayout {
232 const viewportWidth = Math.max(0, input.viewportWidth);
233 const floats = input.primarySidebarFloats ?? input.layout.variant === "medium";
234 const preferredPrimarySidebarVisible =
235 input.layout.usesSplitView && input.primarySidebarPreferredVisible;
236 const preferredPrimarySidebarWidth =
237 preferredPrimarySidebarVisible && !floats
238 ? (input.layout.listPaneWidth ?? 0) + (input.layout.listPaneGap ?? 0)
239 : 0;
240 const fileInspector = deriveFileInspectorPaneLayout({
241 layout: input.layout,
242 viewportWidth,
243 preferredWidth: input.auxiliaryPanePreferredWidth,
244 reservedLeadingWidth: preferredPrimarySidebarWidth,
245 });
246 const auxiliaryPaneVisible = fileInspector.supported && input.auxiliaryPanePreferredVisible;
247 const inspectorWidth = input.nativeWorkspace
248 ? (input.nativeInspectorWidth ?? null)
249 : fileInspector.width;
250 const primarySidebarSuppressedByAuxiliary =
251 preferredPrimarySidebarVisible &&
252 !floats &&
253 auxiliaryPaneVisible &&
254 inspectorWidth !== null &&
255 input.layout.listPaneWidth !== null &&
256 viewportWidth -
257 (input.nativeWorkspace ? (input.horizontalSafeArea ?? 0) : 0) -
258 input.layout.listPaneWidth -
259 (input.layout.listPaneGap ?? 0) -
260 inspectorWidth <
261 (input.nativeWorkspace ? NATIVE_INSPECTOR_MIN_MAIN_WIDTH : FILE_INSPECTOR_MIN_MAIN_WIDTH);
262 const primarySidebarVisible =
263 preferredPrimarySidebarVisible && !primarySidebarSuppressedByAuxiliary;
264 const primarySidebarWidth = primarySidebarVisible ? preferredPrimarySidebarWidth : 0;
265​
266 return {
267 primarySidebarVisible,
268 primarySidebarWhenExpanded: input.primarySidebarPreferredVisible,
269 primarySidebarSuppressedByAuxiliary,
270 contentPaneWidth: Math.max(0, viewportWidth - primarySidebarWidth),
271 supportsAuxiliaryPane: fileInspector.supported,
272 auxiliaryPaneVisible,
273 auxiliaryPaneWidth: fileInspector.width,
274 };
276​
277/**
278 * UIKit drops the inspector column, and refuses to show it again, once the page beside it would
279 * fall under its minimum inside the safe area (the Duo's system bar takes width from the window).
280 */
281export function nativeInspectorColumnWidth(input: {
282 readonly viewportWidth: number;
283 readonly horizontalSafeArea: number;
284 readonly fraction: number;
285}) {
286 const { viewportWidth } = input;
287 const maxWidth = Math.min(
288 viewportWidth * 0.6,
289 viewportWidth - input.horizontalSafeArea - NATIVE_INSPECTOR_MIN_MAIN_WIDTH,
290 );
291 return Math.max(260, Math.min(maxWidth, viewportWidth * input.fraction));
293​
294export function deriveFileInspectorPaneLayout(input: {
295 readonly layout: Layout;
296 readonly viewportWidth: number;
297 readonly preferredWidth?: number;
298 readonly reservedLeadingWidth?: number;
299}): FileInspectorPaneLayout {
300 const viewportWidth = Math.max(0, input.viewportWidth);
301 const reservedLeadingWidth = Number.isFinite(input.reservedLeadingWidth)
302 ? Math.max(0, input.reservedLeadingWidth ?? 0)
303 : 0;
304 const availableContentWidth = Math.max(0, viewportWidth - reservedLeadingWidth);
305 const supported =
306 input.layout.usesSplitView && viewportWidth >= FILE_INSPECTOR_MIN_VIEWPORT_WIDTH;
307​
308 return {
309 supported,
310 width: supported
311 ? constrainAuxiliaryPaneWidth({
312 preferredWidth:
313 input.preferredWidth ??
314 clamp(
315 Math.round(availableContentWidth * 0.28),
316 AUXILIARY_PANE_MIN_WIDTH,
317 AUXILIARY_PANE_DEFAULT_MAX_WIDTH,
318 ),
319 availableWidth: availableContentWidth,
320 })
321 : null,
322 };
324​
325export function constrainAuxiliaryPaneWidth(input: {
326 readonly preferredWidth: number;
327 readonly availableWidth: number;
328}): number {
329 const safePreferredWidth = Number.isFinite(input.preferredWidth)
330 ? input.preferredWidth
331 : AUXILIARY_PANE_MIN_WIDTH;
332 const availableWidth = Number.isFinite(input.availableWidth)
333 ? Math.max(0, input.availableWidth)
334 : 0;
335 const maxWidth = Math.max(
336 AUXILIARY_PANE_MIN_WIDTH,
337 Math.min(AUXILIARY_PANE_MAX_WIDTH, availableWidth - FILE_INSPECTOR_MIN_MAIN_WIDTH),
338 );
339 return clamp(Math.round(safePreferredWidth), AUXILIARY_PANE_MIN_WIDTH, maxWidth);
341​
342export function deriveCenteredContentHorizontalPadding(input: {
343 readonly viewportWidth: number;
344 readonly maxContentWidth: number | null;
345 readonly minimumPadding: number;
346}): number {
347 const viewportWidth = Number.isFinite(input.viewportWidth) ? Math.max(0, input.viewportWidth) : 0;
348 const minimumPadding = Number.isFinite(input.minimumPadding)
349 ? Math.max(0, input.minimumPadding)
350 : 0;
351​
352 if (
353 input.maxContentWidth === null ||
354 !Number.isFinite(input.maxContentWidth) ||
355 input.maxContentWidth <= 0
356 ) {
357 return minimumPadding;
358 }
359​
360 return minimumPadding + Math.max(0, (viewportWidth - input.maxContentWidth) / 2);