Repository navigation
Expand file tree
/
Copy pathlayout.ts
More file actions
355 lines (328 loc) · 12.8 KB
/
Copy pathlayout.ts
File metadata and controls
355 lines (328 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { scaledTypographyLineHeight } from "./appearancePreferences";
import { MOBILE_TYPOGRAPHY } from "./typography";
import type { NativeLayoutMetrics } from "./reserved-regions";
function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}
const MEDIUM_LAYOUT_MIN_WIDTH = 600;
const EXPANDED_LAYOUT_MIN_WIDTH = 720;
const SPLIT_LAYOUT_MIN_HEIGHT = 600;
const SPLIT_SIDEBAR_MIN_WIDTH = 280;
const SPLIT_SIDEBAR_DEFAULT_MAX_WIDTH = 380;
export const CHAT_CONTENT_MAX_WIDTH = 960;
// Limit Android reading width for wide foldables and keyboard-open split views.
export const ANDROID_READING_MAX_WIDTH = 640;
// min-h-8 uses the 14px rem configured in metro.config.js.
export const THREAD_WORK_ROW_MIN_HEIGHT = 28;
export function deriveThreadArrangementSizing(input: {
readonly baseFontSize: number;
readonly fontScale: number;
}) {
const bodyLineHeight = scaledTypographyLineHeight(MOBILE_TYPOGRAPHY.body, input.baseFontSize);
const headerLineHeight = scaledTypographyLineHeight(
MOBILE_TYPOGRAPHY.footnote,
input.baseFontSize,
);
return {
rowHeight: Math.max(56, Math.ceil(2 * bodyLineHeight * input.fontScale) + 10),
headerHeight: Math.max(48, Math.ceil(headerLineHeight * input.fontScale) + 24),
};
}
export function deriveThreadWorkLogSizing(input: {
readonly baseFontSize: number;
readonly fontScale: number;
}) {
const lineHeight = scaledTypographyLineHeight(MOBILE_TYPOGRAPHY.footnote, input.baseFontSize);
return {
textSizeKey: `${input.baseFontSize}:${input.fontScale}`,
estimatedRowHeight: Math.max(
THREAD_WORK_ROW_MIN_HEIGHT,
Math.ceil(lineHeight * input.fontScale),
),
// Accessibility scaling can exceed authored line heights; leave those rows measured.
fixedRowHeight:
input.fontScale <= 1 && lineHeight <= THREAD_WORK_ROW_MIN_HEIGHT
? THREAD_WORK_ROW_MIN_HEIGHT
: undefined,
};
}
const AUXILIARY_PANE_MIN_WIDTH = 260;
const AUXILIARY_PANE_MAX_WIDTH = 480;
const AUXILIARY_PANE_DEFAULT_MAX_WIDTH = 320;
const FILE_INSPECTOR_MIN_VIEWPORT_WIDTH = 820;
const FILE_INSPECTOR_MIN_MAIN_WIDTH = 560;
// UIKit's inspector column keeps its own width, so the sidebar steps aside only when the page
// would get narrower than this: landscape 11" and 13" iPads keep three columns, portrait ones don't.
export const NATIVE_INSPECTOR_MIN_MAIN_WIDTH = 320;
/**
* The window's width class. Compact: the sidebar is the root and pages push over it. Medium: the
* sidebar floats over the content. Expanded: the sidebar sits beside the content.
*/
export type LayoutVariant = "compact" | "medium" | "expanded";
export interface Layout {
readonly variant: LayoutVariant;
/** Medium or expanded: pages replace the content beside the sidebar instead of pushing. */
readonly usesSplitView: boolean;
readonly listPaneWidth: number | null;
readonly listPaneGap?: number;
readonly shellPadding: number;
}
export interface WorkspacePaneLayout {
readonly primarySidebarVisible: boolean;
/** Whether the sidebar shows once a collapsed split expands; independent of the current width. */
readonly primarySidebarWhenExpanded: boolean;
readonly primarySidebarSuppressedByAuxiliary: boolean;
readonly contentPaneWidth: number;
readonly supportsAuxiliaryPane: boolean;
readonly auxiliaryPaneVisible: boolean;
readonly auxiliaryPaneWidth: number | null;
}
export interface FileInspectorPaneLayout {
readonly supported: boolean;
readonly width: number | null;
}
export function deriveLayout(input: {
readonly width: number;
readonly height: number;
readonly nativeMetrics?: NativeLayoutMetrics | null;
readonly nativeWorkspace?: boolean;
}): Layout {
const { width, height } = input;
const metrics = input.nativeMetrics;
const usableWidth = width - (metrics?.safeArea.left ?? 0) - (metrics?.safeArea.right ?? 0);
// UIKit retains split columns below the generic tablet breakpoint. Native
// columns follow its size class while reserving room for both column minimums.
// Unfolded in portrait, the inner display reports neither a side bar nor a
// fold, only its hinge.
const hasDuoGeometry =
metrics &&
(metrics.hasHinge === true ||
metrics.verticalBarEdge !== "none" ||
metrics.reservedRegions.some((region) => region.kind === "division"));
const variant: LayoutVariant =
metrics && (input.nativeWorkspace || hasDuoGeometry)
? metrics.horizontalSizeClass === "regular" &&
usableWidth >= SPLIT_SIDEBAR_MIN_WIDTH + 320 &&
(input.nativeWorkspace || height >= 400)
? // The native split floats its sidebar over an unfolded portrait Duo.
input.nativeWorkspace === true && metrics.hasHinge === true && width < height
? "medium"
: "expanded"
: "compact"
: height < SPLIT_LAYOUT_MIN_HEIGHT || width < MEDIUM_LAYOUT_MIN_WIDTH
? "compact"
: width >= EXPANDED_LAYOUT_MIN_WIDTH
? "expanded"
: // Only the Android shell floats a sidebar over a page; iOS keeps its phone stack.
metrics
? "compact"
: "medium";
if (variant === "compact") {
return {
variant,
usesSplitView: false,
listPaneWidth: null,
shellPadding: 0,
};
}
const division = metrics?.reservedRegions.find(
(region) =>
region.kind === "division" &&
region.height >= height / 2 &&
region.x - metrics.safeArea.left >= SPLIT_SIDEBAR_MIN_WIDTH &&
width - region.x - region.width - metrics.safeArea.right >= 320,
);
return {
variant,
usesSplitView: true,
listPaneWidth:
division?.x ??
(input.nativeWorkspace === true && variant === "medium"
? 350
: clamp(
Math.round(width * 0.32),
SPLIT_SIDEBAR_MIN_WIDTH,
SPLIT_SIDEBAR_DEFAULT_MAX_WIDTH,
)),
...(division ? { listPaneGap: division.width } : {}),
shellPadding: 0,
};
}
export interface VerticalHinge {
readonly x: number;
readonly width: number;
}
/**
* Moves the list/detail split onto a book-posture hinge so neither pane straddles the crease.
* `leadingWidth` is chrome left of the list pane, such as the Android navigation rail.
*/
export function alignListPaneToHinge(input: {
readonly layout: Layout;
readonly hinge: VerticalHinge | null;
readonly viewportWidth: number;
readonly leadingWidth: number;
}): Layout {
const { layout, hinge } = input;
if (!hinge || !layout.usesSplitView) return layout;
const listPaneWidth = hinge.x - input.leadingWidth;
if (
listPaneWidth < SPLIT_SIDEBAR_MIN_WIDTH ||
input.viewportWidth - hinge.x - hinge.width < 320
) {
return layout;
}
return { ...layout, listPaneWidth, listPaneGap: hinge.width };
}
/**
* Whether the workspace root must keep the primary sidebar open. Native split
* columns keep a sidebar toggle in the empty detail header, so only they may
* collapse it there. Compact layouts stay pinned so a reveal request (such as
* focusing search) cannot flip the saved preference for a later split view.
*/
/** Home's list, or the Settings list in its place, with no detail selected. */
export function isWorkspaceListPath(pathname: string): boolean {
return pathname === "/" || pathname === "/settings";
}
export function isPrimarySidebarPinned(input: {
readonly pathname: string;
readonly nativeWorkspace: boolean;
readonly usesSplitView: boolean;
}): boolean {
return isWorkspaceListPath(input.pathname) && !(input.nativeWorkspace && input.usesSplitView);
}
export function deriveWorkspacePaneLayout(input: {
readonly layout: Layout;
readonly viewportWidth: number;
/** Width UIKit keeps out of its columns, such as the Duo's system bar. */
readonly horizontalSafeArea?: number;
readonly nativeWorkspace?: boolean;
/** Whether the sidebar floats over the content rather than taking its room; medium by default. */
readonly primarySidebarFloats?: boolean;
readonly primarySidebarPreferredVisible: boolean;
readonly auxiliaryPanePreferredVisible: boolean;
readonly auxiliaryPanePreferredWidth?: number;
/** The native inspector column's width; UIKit lays it out, not this layout. */
readonly nativeInspectorWidth?: number;
}): WorkspacePaneLayout {
const viewportWidth = Math.max(0, input.viewportWidth);
const floats = input.primarySidebarFloats ?? input.layout.variant === "medium";
const preferredPrimarySidebarVisible =
input.layout.usesSplitView && input.primarySidebarPreferredVisible;
const preferredPrimarySidebarWidth =
preferredPrimarySidebarVisible && !floats
? (input.layout.listPaneWidth ?? 0) + (input.layout.listPaneGap ?? 0)
: 0;
const fileInspector = deriveFileInspectorPaneLayout({
layout: input.layout,
viewportWidth,
preferredWidth: input.auxiliaryPanePreferredWidth,
reservedLeadingWidth: preferredPrimarySidebarWidth,
});
const auxiliaryPaneVisible = fileInspector.supported && input.auxiliaryPanePreferredVisible;
const inspectorWidth = input.nativeWorkspace
? (input.nativeInspectorWidth ?? null)
: fileInspector.width;
const primarySidebarSuppressedByAuxiliary =
preferredPrimarySidebarVisible &&
!floats &&
auxiliaryPaneVisible &&
inspectorWidth !== null &&
input.layout.listPaneWidth !== null &&
viewportWidth -
(input.nativeWorkspace ? (input.horizontalSafeArea ?? 0) : 0) -
input.layout.listPaneWidth -
(input.layout.listPaneGap ?? 0) -
inspectorWidth <
(input.nativeWorkspace ? NATIVE_INSPECTOR_MIN_MAIN_WIDTH : FILE_INSPECTOR_MIN_MAIN_WIDTH);
const primarySidebarVisible =
preferredPrimarySidebarVisible && !primarySidebarSuppressedByAuxiliary;
const primarySidebarWidth = primarySidebarVisible ? preferredPrimarySidebarWidth : 0;
return {
primarySidebarVisible,
primarySidebarWhenExpanded: input.primarySidebarPreferredVisible,
primarySidebarSuppressedByAuxiliary,
contentPaneWidth: Math.max(0, viewportWidth - primarySidebarWidth),
supportsAuxiliaryPane: fileInspector.supported,
auxiliaryPaneVisible,
auxiliaryPaneWidth: fileInspector.width,
};
}
/**
* UIKit drops the inspector column, and refuses to show it again, once the page beside it would
* fall under its minimum inside the safe area (the Duo's system bar takes width from the window).
*/
export function nativeInspectorColumnWidth(input: {
readonly viewportWidth: number;
readonly horizontalSafeArea: number;
readonly fraction: number;
}) {
const { viewportWidth } = input;
const maxWidth = Math.min(
viewportWidth * 0.6,
viewportWidth - input.horizontalSafeArea - NATIVE_INSPECTOR_MIN_MAIN_WIDTH,
);
return Math.max(260, Math.min(maxWidth, viewportWidth * input.fraction));
}
export function deriveFileInspectorPaneLayout(input: {
readonly layout: Layout;
readonly viewportWidth: number;
readonly preferredWidth?: number;
readonly reservedLeadingWidth?: number;
}): FileInspectorPaneLayout {
const viewportWidth = Math.max(0, input.viewportWidth);
const reservedLeadingWidth = Number.isFinite(input.reservedLeadingWidth)
? Math.max(0, input.reservedLeadingWidth ?? 0)
: 0;
const availableContentWidth = Math.max(0, viewportWidth - reservedLeadingWidth);
const supported =
input.layout.usesSplitView && viewportWidth >= FILE_INSPECTOR_MIN_VIEWPORT_WIDTH;
return {
supported,
width: supported
? constrainAuxiliaryPaneWidth({
preferredWidth:
input.preferredWidth ??
clamp(
Math.round(availableContentWidth * 0.28),
AUXILIARY_PANE_MIN_WIDTH,
AUXILIARY_PANE_DEFAULT_MAX_WIDTH,
),
availableWidth: availableContentWidth,
})
: null,
};
}
export function constrainAuxiliaryPaneWidth(input: {
readonly preferredWidth: number;
readonly availableWidth: number;
}): number {
const safePreferredWidth = Number.isFinite(input.preferredWidth)
? input.preferredWidth
: AUXILIARY_PANE_MIN_WIDTH;
const availableWidth = Number.isFinite(input.availableWidth)
? Math.max(0, input.availableWidth)
: 0;
const maxWidth = Math.max(
AUXILIARY_PANE_MIN_WIDTH,
Math.min(AUXILIARY_PANE_MAX_WIDTH, availableWidth - FILE_INSPECTOR_MIN_MAIN_WIDTH),
);
return clamp(Math.round(safePreferredWidth), AUXILIARY_PANE_MIN_WIDTH, maxWidth);
}
export function deriveCenteredContentHorizontalPadding(input: {
readonly viewportWidth: number;
readonly maxContentWidth: number | null;
readonly minimumPadding: number;
}): number {
const viewportWidth = Number.isFinite(input.viewportWidth) ? Math.max(0, input.viewportWidth) : 0;
const minimumPadding = Number.isFinite(input.minimumPadding)
? Math.max(0, input.minimumPadding)
: 0;
if (
input.maxContentWidth === null ||
!Number.isFinite(input.maxContentWidth) ||
input.maxContentWidth <= 0
) {
return minimumPadding;
}
return minimumPadding + Math.max(0, (viewportWidth - input.maxContentWidth) / 2);
}