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

layout.ts

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