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}
117
118function restingComposerControlsWidth(
119 input: RestingComposerControlsMeasurement,
120 hiddenCount: number,
121 fixedWidth = input.naturalFixedWidth,
122): number {
123 const { blockWidths, gap } = input;
124 const visibleCount = blockWidths.length - hiddenCount;
125 return (
126 fixedWidth +
127 blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
128 (hiddenCount > 0 ? input.overflowWidth : 0) +
129 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
130 );
131}
132
133/**
134 * The width the resting controls take with nothing moved into overflow.
135 *
136 * The context strip reserves this much for the composer before deciding
137 * whether its own labels may expand. Judging against the currently visible
138 * controls instead lets the strip expand into space the composer just gave
139 * up, which shrinks the host, hides the controls again, and repeats.
140 */
141export function resolveRestingComposerControlsNaturalWidth(
142 input: RestingComposerControlsMeasurement,
143): number {
144 return restingComposerControlsWidth(input, 0);
145}
146
147/**
148 * Decide how many trailing resting control blocks move into the overflow
149 * menu, and whether the cluster can show at all, from natural widths.
150 *
151 * Trailing blocks hide before the model picker shrinks. Once they are all in
152 * the overflow menu, the picker may contract to its minimum readable width;
153 * below that the whole cluster hides rather than clipping.
154 */
155const RESTING_CONTROLS_SLACK_PX = 1;
156
157export function resolveRestingComposerControlsLayout(
158 input: RestingComposerControlsMeasurement & {
159 hostWidth: number;
160 previous?: { hiddenCount: number; visible: boolean };
161 },
162): { hiddenCount: number; visible: boolean } {
163 const { blockWidths, hostWidth, previous } = input;
164 let hiddenCount = 0;
165 while (
166 hiddenCount < blockWidths.length &&
167 restingComposerControlsWidth(input, hiddenCount) > hostWidth
168 ) {
169 hiddenCount += 1;
170 }
171 // Growing the overflow menu is unconditional, or the controls would clip.
172 // Shrinking it has to earn a pixel of slack first: the picker is flexible,
173 // so its natural width is recovered from a truncated label whose
174 // scrollWidth is integral while the rendered box is fractional. The
175 // composer re-measures on every render, so without that margin a host
176 // sitting exactly on a threshold flips a block in and out until React
177 // gives up with "Maximum update depth exceeded".
178 if (previous) {
179 const previousHiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
180 while (
181 hiddenCount < previousHiddenCount &&
182 restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX
183 ) {
184 hiddenCount += 1;
185 }
186 }
187 const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
188 const visible =
189 previous && !previous.visible
190 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
191 : minimumWidth <= hostWidth;
192 return { hiddenCount, visible };
193}
194
195export function resolveScrollToEndClearance(input: {
196 overlayHeight: number;
197 mainSurfaceTop: number;
198 button: { left: number; right: number };
199 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
200}): number {
201 let contentTop = input.mainSurfaceTop;
202 let top = contentTop;
203 for (const attachment of input.attachments) {
204 contentTop = Math.min(contentTop, attachment.top);
205 if (attachment.left < input.button.right && attachment.right > input.button.left) {
206 top = Math.min(top, attachment.top);
207 }
208 }
209 return Math.ceil(input.overlayHeight - (top - contentTop));
210}