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 isFocused: boolean;
30 isScrollCollapsed: boolean;
31 hasExpandedChrome: boolean;
32 collapseOnBlur: boolean;
33 /** Whether the timeline has more content than fits above the composer. */
34 timelineOverflows: boolean;
35}): boolean {
36 // Passive draft content is deliberately absent here. Resting only clamps
37 // the prompt row and overlays its actions; non-image attachment and context
38 // rows keep their natural height above it while image previews move inline.
39 // Banners and the tasks badge dock above the surface, so they are absent
40 // too. Whether the context strip can host the relocated controls is
41 // deliberately absent here: resting reclaims vertical space at every
42 // desktop width, and where the strip is missing or too narrow the controls
43 // simply return when the composer is focused.
44 //
45 // A scroll collapse rests the composer regardless of the blur preference:
46 // the user asked for it with the gesture, and it lifts on the next
47 // composer interaction. With blur collapse off, losing focus alone never
48 // rests the composer.
49 //
50 // Resting exists to give reading space back to the timeline. A thread that
51 // fits above the composer has nothing to reclaim, so it stays expanded and
52 // never shows the collapsed row that a fresh thread would otherwise open on.
53 const collapsed = input.isScrollCollapsed || (input.collapseOnBlur && !input.isFocused);
54 return (
55 input.isExistingThread &&
56 !input.isMobileViewport &&
57 input.timelineOverflows &&
58 collapsed &&
59 !input.hasExpandedChrome
60 );
61}
62
63/**
64 * How much taller the empty expanded composer is than its resting row on
65 * desktop widths, from the layout classes in ChatComposer: the body loses
66 * 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
67 * the 48px footer leaves flow.
68 */
69export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
70
71/**
72 * The space the timeline reserves at its end for the composer overlay.
73 *
74 * The overlay is measured live, but a resting composer is much shorter than
75 * an expanded one. Reserving only the resting height lets a scroll to the end
76 * land flush against the short composer, and the expansion that follows then
77 * covers the last rows because the timeline never moves for footer growth.
78 * While resting, the reservation keeps the last expanded height, or at least
79 * the resting height plus the empty expansion, so expanding again changes
80 * nothing above the composer. An expanded measurement is authoritative and
81 * may shrink it.
82 */
83export function resolveComposerTimelineInset(input: {
84 currentInset: number;
85 overlayHeight: number;
86 isResting: boolean;
87}): number {
88 return input.isResting
89 ? Math.max(input.currentInset, input.overlayHeight + COMPOSER_RESTING_EXPANSION_MIN_PX)
90 : input.overlayHeight;
91}
92
93export function shouldAnimateComposerRestingTransition(input: {
94 hasCompletedInitialLayout: boolean;
95 stateChanged: boolean;
96 hasInterruptedAnimation: boolean;
97}): boolean {
98 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
99}
100
101export function shouldUseCompactComposerPrimaryActions(
102 width: number | null,
103 options?: { hasWideActions?: boolean },
104): boolean {
105 if (!options?.hasWideActions) {
106 return false;
107 }
108 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
109}
110
111export interface RestingComposerControlsMeasurement {
112 gap: number;
113 naturalFixedWidth: number;
114 minimumFixedWidth: number;
115 blockWidths: readonly number[];
116 overflowWidth: number;
117}
118
119function restingComposerControlsWidth(
120 input: RestingComposerControlsMeasurement,
121 hiddenCount: number,
122 fixedWidth = input.naturalFixedWidth,
123): number {
124 const { blockWidths, gap } = input;
125 const visibleCount = blockWidths.length - hiddenCount;
126 return (
127 fixedWidth +
128 blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
129 (hiddenCount > 0 ? input.overflowWidth : 0) +
130 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
131 );
132}
133
134/**
135 * The width the resting controls take with nothing moved into overflow.
136 *
137 * The context strip reserves this much for the composer before deciding
138 * whether its own labels may expand. Judging against the currently visible
139 * controls instead lets the strip expand into space the composer just gave
140 * up, which shrinks the host, hides the controls again, and repeats.
141 */
142export function resolveRestingComposerControlsNaturalWidth(
143 input: RestingComposerControlsMeasurement,
144): number {
145 return restingComposerControlsWidth(input, 0);
146}
147
148/**
149 * Decide how many trailing resting control blocks move into the overflow
150 * menu, and whether the cluster can show at all, from natural widths.
151 *
152 * Trailing blocks hide before the model picker shrinks. Once they are all in
153 * the overflow menu, the picker may contract to its minimum readable width;
154 * below that the whole cluster hides rather than clipping.
155 */
156const RESTING_CONTROLS_SLACK_PX = 1;
157
158export function resolveRestingComposerControlsLayout(
159 input: RestingComposerControlsMeasurement & {
160 hostWidth: number;
161 previous?: { hiddenCount: number; visible: boolean };
162 },
163): { hiddenCount: number; visible: boolean } {
164 const { blockWidths, hostWidth, previous } = input;
165 let hiddenCount = 0;
166 while (
167 hiddenCount < blockWidths.length &&
168 restingComposerControlsWidth(input, hiddenCount) > hostWidth
169 ) {
170 hiddenCount += 1;
171 }
172 // Growing the overflow menu is unconditional, or the controls would clip.
173 // Shrinking it has to earn a pixel of slack first: the picker is flexible,
174 // so its natural width is recovered from a truncated label whose
175 // scrollWidth is integral while the rendered box is fractional. The
176 // composer re-measures on every render, so without that margin a host
177 // sitting exactly on a threshold flips a block in and out until React
178 // gives up with "Maximum update depth exceeded".
179 if (previous) {
180 const previousHiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
181 while (
182 hiddenCount < previousHiddenCount &&
183 restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX
184 ) {
185 hiddenCount += 1;
186 }
187 }
188 const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
189 const visible =
190 previous && !previous.visible
191 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
192 : minimumWidth <= hostWidth;
193 return { hiddenCount, visible };
194}