Open History to see the last commit
1export const COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX = 620;
2export const COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX = 780;
3const RESTING_COMPOSER_IMAGE_THUMBNAIL_LIMIT = 3;
4
5export function getRestingComposerImagePreviewCounts(imageCount: number): {
6 visibleCount: number;
7 overflowCount: number;
8} {
9 const visibleCount = Math.min(imageCount, RESTING_COMPOSER_IMAGE_THUMBNAIL_LIMIT);
10 return {
11 visibleCount,
12 overflowCount: Math.max(0, imageCount - visibleCount),
13 };
14}
15
16export function shouldUseCompactComposerFooter(
17 width: number | null,
18 options?: { hasWideActions?: boolean },
19): boolean {
20 const breakpoint = options?.hasWideActions
21 ? COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX
22 : COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX;
23 return width !== null && width < breakpoint;
24}
25
26export function shouldUseRestingComposerLayout(input: {
27 isExistingThread: boolean;
28 isMobileViewport: boolean;
29 isScrollCollapsed: boolean;
30 hasExpandedChrome: boolean;
31 hasMultilinePrompt: boolean;
32 timelineOverflows: boolean;
33}): boolean {
34 return (
35 input.isExistingThread &&
36 !input.isMobileViewport &&
37 input.timelineOverflows &&
38 input.isScrollCollapsed &&
39 !input.hasMultilinePrompt &&
40 !input.hasExpandedChrome
41 );
42}
43
44/**
45 * How much taller the empty expanded composer is than its resting row on
46 * desktop widths, from the layout classes in ChatComposer: the body loses
47 * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
48 * the 48px footer leaves flow.
49 */
50export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
51
52/**
53 * Reserving only the resting height lets a scroll to the end land flush against the short
54 * composer, and the expansion that follows then covers the last rows because the timeline never
55 * moves for footer growth.
56 */
57export function resolveComposerTimelineInset(input: {
58 currentInset: number;
59 overlayHeight: number;
60 isResting: boolean;
61 restingOnlyHeight?: number;
62}): number {
63 return input.isResting
64 ? Math.max(
65 input.currentInset,
66 input.overlayHeight +
67 (input.currentInset === 0
68 ? COMPOSER_RESTING_EXPANSION_MIN_PX - (input.restingOnlyHeight ?? 0)
69 : 0),
70 )
71 : input.overlayHeight;
72}
73
74export function overlayComposerIsResting(input: {
75 composerMounted: boolean;
76 composerReportedResting: boolean;
77}): boolean {
78 return input.composerMounted && input.composerReportedResting;
79}
80
81export function shouldAnimateComposerRestingTransition(input: {
82 hasCompletedInitialLayout: boolean;
83 stateChanged: boolean;
84 hasInterruptedAnimation: boolean;
85}): boolean {
86 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
87}
88
89export function shouldUseCompactComposerPrimaryActions(
90 width: number | null,
91 options?: { hasWideActions?: boolean },
92): boolean {
93 if (!options?.hasWideActions) {
94 return false;
95 }
96 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
97}
98
99export interface RestingComposerControlsMeasurement {
100 gap: number;
101 naturalFixedWidth: number;
102 minimumFixedWidth: number;
103 blockWidths: readonly number[];
104 overflowWidth: number;
105 iconOnlyBlockWidths?: readonly number[];
106}
107
108function restingComposerControlsWidth(
109 input: RestingComposerControlsMeasurement,
110 hiddenCount: number,
111 fixedWidth = input.naturalFixedWidth,
112 iconOnlyCount = 0,
113): number {
114 const { blockWidths, gap } = input;
115 const visibleCount = blockWidths.length - hiddenCount;
116 return (
117 fixedWidth +
118 blockWidths
119 .slice(0, visibleCount)
120 .reduce(
121 (sum, width, index) =>
122 sum +
123 (index >= blockWidths.length - iconOnlyCount
124 ? (input.iconOnlyBlockWidths?.[index] ?? width)
125 : width),
126 0,
127 ) +
128 (hiddenCount > 0 ? input.overflowWidth : 0) +
129 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
130 );
131}
132
133/**
134 * The context strip reserves this much for the composer before deciding whether its own labels
135 * may expand. Judging against the currently visible controls instead lets the strip expand into
136 * space the composer just gave up, which shrinks the host, hides the controls again, and
137 * repeats.
138 */
139export function resolveRestingComposerControlsNaturalWidth(
140 input: RestingComposerControlsMeasurement,
141): number {
142 return restingComposerControlsWidth(input, 0);
143}
144
145const RESTING_CONTROLS_SLACK_PX = 1;
146
147export function resolveRestingComposerControlsLayout(
148 input: RestingComposerControlsMeasurement & {
149 hostWidth: number;
150 previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
151 },
152): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
153 const { blockWidths, hostWidth, previous } = input;
154 const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
155 const previousStep = previous
156 ? previous.hiddenCount > 0
157 ? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
158 : Math.min(previous.iconOnlyCount ?? 0, iconSteps)
159 : 0;
160 let step = 0;
161 const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
162 restingComposerControlsWidth(
163 input,
164 Math.max(0, candidate - iconSteps),
165 fixedWidth,
166 Math.min(candidate, iconSteps),
167 );
168 // Promotions need a pixel of slack: recovering a flexible picker's natural
169 // width can jitter by a fraction of a pixel across renders. Demotions are
170 // immediate so a threshold cannot clip or flip React between layouts.
171 while (
172 step < iconSteps + blockWidths.length &&
173 widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
174 ) {
175 step += 1;
176 }
177 const hiddenCount = Math.max(0, step - iconSteps);
178 const iconOnlyCount = Math.min(step, iconSteps);
179 const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
180 const visible =
181 previous && !previous.visible
182 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
183 : minimumWidth <= hostWidth;
184 return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
185}
186
187export function resolveScrollToEndClearance(input: {
188 overlayHeight: number;
189 mainSurfaceTop: number;
190 button: { left: number; right: number };
191 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
192}): number {
193 let contentTop = input.mainSurfaceTop;
194 let top = contentTop;
195 for (const attachment of input.attachments) {
196 contentTop = Math.min(contentTop, attachment.top);
197 if (attachment.left < input.button.right && attachment.right > input.button.left) {
198 top = Math.min(top, attachment.top);
199 }
200 }
201 return Math.ceil(input.overlayHeight - (top - contentTop));
202}