Open History to see the last commit
1const COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX = 620;
2const 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 */
50const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
51
52/** Reserve expansion height because the timeline does not move when footer growth covers its last rows. */
53export function resolveComposerTimelineInset(input: {
54 currentInset: number;
55 overlayHeight: number;
56 isResting: boolean;
57 restingOnlyHeight?: number;
58 expandedNoticesHeight?: number;
59}): number {
60 // Expanded notices temporarily cover the timeline. Remove their growth before
61 // rounding so fractional measurements reserve exactly the collapsed height.
62 const overlayHeight = Math.ceil(input.overlayHeight - (input.expandedNoticesHeight ?? 0));
63 return input.isResting
64 ? Math.max(
65 input.currentInset,
66 overlayHeight +
67 (input.currentInset === 0
68 ? COMPOSER_RESTING_EXPANSION_MIN_PX - (input.restingOnlyHeight ?? 0)
69 : 0),
70 )
71 : 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/** Reserve natural composer width to prevent strip labels from repeatedly expanding into space vacated by
134 * hidden controls. */
135export function resolveRestingComposerControlsNaturalWidth(
136 input: RestingComposerControlsMeasurement,
137): number {
138 return restingComposerControlsWidth(input, 0);
139}
140
141const RESTING_CONTROLS_SLACK_PX = 1;
142
143export function resolveRestingComposerControlsLayout(
144 input: RestingComposerControlsMeasurement & {
145 hostWidth: number;
146 previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
147 },
148): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
149 const { blockWidths, hostWidth, previous } = input;
150 const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
151 const previousStep = previous
152 ? previous.hiddenCount > 0
153 ? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
154 : Math.min(previous.iconOnlyCount ?? 0, iconSteps)
155 : 0;
156 let step = 0;
157 const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
158 restingComposerControlsWidth(
159 input,
160 Math.max(0, candidate - iconSteps),
161 fixedWidth,
162 Math.min(candidate, iconSteps),
163 );
164 // Use 1px slack for natural-width jitter on promotion; demote immediately to avoid clipping or layout
165 // flapping.
166 while (
167 step < iconSteps + blockWidths.length &&
168 widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
169 ) {
170 step += 1;
171 }
172 const hiddenCount = Math.max(0, step - iconSteps);
173 const iconOnlyCount = Math.min(step, iconSteps);
174 const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
175 const visible =
176 previous && !previous.visible
177 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
178 : minimumWidth <= hostWidth;
179 return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
180}
181
182export function resolveScrollToEndClearance(input: {
183 overlayHeight: number;
184 mainSurfaceTop: number;
185 button: { left: number; right: number };
186 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
187}): number {
188 let contentTop = input.mainSurfaceTop;
189 let top = contentTop;
190 for (const attachment of input.attachments) {
191 contentTop = Math.min(contentTop, attachment.top);
192 if (attachment.left < input.button.right && attachment.right > input.button.left) {
193 top = Math.min(top, attachment.top);
194 }
195 }
196 return Math.ceil(input.overlayHeight - (top - contentTop));
197}