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 /** Whether the timeline has more content than fits above the composer. */
33 timelineOverflows: boolean;
34}): boolean {
35 // Multiline drafts stay readable. Resting only clamps a single prompt
36 // line and overlays its actions; non-image attachment and context
37 // rows keep their natural height above it while image previews move inline.
38 // Banners and the tasks badge dock above the surface, so they are absent
39 // too. Whether the context strip can host the relocated controls is
40 // deliberately absent here: resting reclaims vertical space at every
41 // desktop width, and where the strip is missing or too narrow the controls
42 // simply return when the composer is focused.
43 //
44 // Only a timeline scroll rests the composer: the user asked for it with the
45 // gesture, and it lifts on the next composer interaction. Losing focus never
46 // rests it, so clicking a message, copying output, or selecting text for a
47 // citation leaves the composer where it was.
48 //
49 // Resting exists to give reading space back to the timeline. A thread that
50 // fits above the composer has nothing to reclaim, so it stays expanded and
51 // never shows the collapsed row that a fresh thread would otherwise open on.
52 return (
53 input.isExistingThread &&
54 !input.isMobileViewport &&
55 input.timelineOverflows &&
56 input.isScrollCollapsed &&
57 !input.hasMultilinePrompt &&
58 !input.hasExpandedChrome
59 );
60}
61
62/**
63 * How much taller the empty expanded composer is than its resting row on
64 * desktop widths, from the layout classes in ChatComposer: the body loses
65 * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
66 * the 48px footer leaves flow.
67 */
68export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
69
70/**
71 * The space the timeline reserves at its end for the composer overlay.
72 *
73 * The overlay is measured live, but a resting composer is much shorter than
74 * an expanded one. Reserving only the resting height lets a scroll to the end
75 * land flush against the short composer, and the expansion that follows then
76 * covers the last rows because the timeline never moves for footer growth.
77 * While resting, the reservation keeps the last expanded height, or at least
78 * the resting height plus the empty expansion, so expanding again changes
79 * nothing above the composer. An expanded measurement is authoritative and
80 * may shrink it.
81 */
82export function resolveComposerTimelineInset(input: {
83 currentInset: number;
84 overlayHeight: number;
85 isResting: boolean;
86}): number {
87 return input.isResting
88 ? Math.max(input.currentInset, input.overlayHeight + COMPOSER_RESTING_EXPANSION_MIN_PX)
89 : input.overlayHeight;
90}
91
92export function shouldAnimateComposerRestingTransition(input: {
93 hasCompletedInitialLayout: boolean;
94 stateChanged: boolean;
95 hasInterruptedAnimation: boolean;
96}): boolean {
97 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
98}
99
100export function shouldUseCompactComposerPrimaryActions(
101 width: number | null,
102 options?: { hasWideActions?: boolean },
103): boolean {
104 if (!options?.hasWideActions) {
105 return false;
106 }
107 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
108}
109
110export interface RestingComposerControlsMeasurement {
111 gap: number;
112 naturalFixedWidth: number;
113 minimumFixedWidth: number;
114 blockWidths: readonly number[];
115 overflowWidth: number;
116 iconOnlyBlockWidths?: readonly number[];
117}
118
119function restingComposerControlsWidth(
120 input: RestingComposerControlsMeasurement,
121 hiddenCount: number,
122 fixedWidth = input.naturalFixedWidth,
123 iconOnlyCount = 0,
124): number {
125 const { blockWidths, gap } = input;
126 const visibleCount = blockWidths.length - hiddenCount;
127 return (
128 fixedWidth +
129 blockWidths
130 .slice(0, visibleCount)
131 .reduce(
132 (sum, width, index) =>
133 sum +
134 (index >= blockWidths.length - iconOnlyCount
135 ? (input.iconOnlyBlockWidths?.[index] ?? width)
136 : width),
137 0,
138 ) +
139 (hiddenCount > 0 ? input.overflowWidth : 0) +
140 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
141 );
142}
143
144/**
145 * The width the resting controls take with nothing moved into overflow.
146 *
147 * The context strip reserves this much for the composer before deciding
148 * whether its own labels may expand. Judging against the currently visible
149 * controls instead lets the strip expand into space the composer just gave
150 * up, which shrinks the host, hides the controls again, and repeats.
151 */
152export function resolveRestingComposerControlsNaturalWidth(
153 input: RestingComposerControlsMeasurement,
154): number {
155 return restingComposerControlsWidth(input, 0);
156}
157
158/**
159 * Fit footer controls using natural widths: remove trailing labels first,
160 * then move trailing blocks into overflow. Resting and expanded share this
161 * decision, including the slack needed to safely restore controls.
162 *
163 * Trailing blocks compact before the model picker shrinks. Once they are all in
164 * the overflow menu, the picker may contract to its minimum readable width;
165 * below that the whole cluster hides rather than clipping.
166 */
167const RESTING_CONTROLS_SLACK_PX = 1;
168
169export function resolveRestingComposerControlsLayout(
170 input: RestingComposerControlsMeasurement & {
171 hostWidth: number;
172 previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
173 },
174): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
175 const { blockWidths, hostWidth, previous } = input;
176 const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
177 const previousStep = previous
178 ? previous.hiddenCount > 0
179 ? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
180 : Math.min(previous.iconOnlyCount ?? 0, iconSteps)
181 : 0;
182 let step = 0;
183 const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
184 restingComposerControlsWidth(
185 input,
186 Math.max(0, candidate - iconSteps),
187 fixedWidth,
188 Math.min(candidate, iconSteps),
189 );
190 // Promotions need a pixel of slack: recovering a flexible picker's natural
191 // width can jitter by a fraction of a pixel across renders. Demotions are
192 // immediate so a threshold cannot clip or flip React between layouts.
193 while (
194 step < iconSteps + blockWidths.length &&
195 widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
196 ) {
197 step += 1;
198 }
199 const hiddenCount = Math.max(0, step - iconSteps);
200 const iconOnlyCount = Math.min(step, iconSteps);
201 const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
202 const visible =
203 previous && !previous.visible
204 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
205 : minimumWidth <= hostWidth;
206 return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
207}
208
209export function resolveScrollToEndClearance(input: {
210 overlayHeight: number;
211 mainSurfaceTop: number;
212 button: { left: number; right: number };
213 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
214}): number {
215 let contentTop = input.mainSurfaceTop;
216 let top = contentTop;
217 for (const attachment of input.attachments) {
218 contentTop = Math.min(contentTop, attachment.top);
219 if (attachment.left < input.button.right && attachment.right > input.button.left) {
220 top = Math.min(top, attachment.top);
221 }
222 }
223 return Math.ceil(input.overlayHeight - (top - contentTop));
224}