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

layout.ts

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