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}): number {
59 const overlayHeight = Math.ceil(input.overlayHeight);
60 return input.isResting
61 ? Math.max(
62 input.currentInset,
63 overlayHeight +
64 (input.currentInset === 0
65 ? COMPOSER_RESTING_EXPANSION_MIN_PX - (input.restingOnlyHeight ?? 0)
66 : 0),
67 )
68 : overlayHeight;
69}
70
71export function overlayComposerIsResting(input: {
72 composerMounted: boolean;
73 composerReportedResting: boolean;
74}): boolean {
75 return input.composerMounted && input.composerReportedResting;
76}
77
78export function shouldAnimateComposerRestingTransition(input: {
79 hasCompletedInitialLayout: boolean;
80 stateChanged: boolean;
81 hasInterruptedAnimation: boolean;
82}): boolean {
83 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
84}
85
86export function shouldUseCompactComposerPrimaryActions(
87 width: number | null,
88 options?: { hasWideActions?: boolean },
89): boolean {
90 if (!options?.hasWideActions) {
91 return false;
92 }
93 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
94}
95
96export interface RestingComposerControlsMeasurement {
97 gap: number;
98 naturalFixedWidth: number;
99 minimumFixedWidth: number;
100 blockWidths: readonly number[];
101 overflowWidth: number;
102 iconOnlyBlockWidths?: readonly number[];
103}
104
105function restingComposerControlsWidth(
106 input: RestingComposerControlsMeasurement,
107 hiddenCount: number,
108 fixedWidth = input.naturalFixedWidth,
109 iconOnlyCount = 0,
110): number {
111 const { blockWidths, gap } = input;
112 const visibleCount = blockWidths.length - hiddenCount;
113 return (
114 fixedWidth +
115 blockWidths
116 .slice(0, visibleCount)
117 .reduce(
118 (sum, width, index) =>
119 sum +
120 (index >= blockWidths.length - iconOnlyCount
121 ? (input.iconOnlyBlockWidths?.[index] ?? width)
122 : width),
123 0,
124 ) +
125 (hiddenCount > 0 ? input.overflowWidth : 0) +
126 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
127 );
128}
129
130/** Reserve natural composer width to prevent strip labels from repeatedly expanding into space vacated by
131 * hidden controls. */
132export function resolveRestingComposerControlsNaturalWidth(
133 input: RestingComposerControlsMeasurement,
134): number {
135 return restingComposerControlsWidth(input, 0);
136}
137
138const RESTING_CONTROLS_SLACK_PX = 1;
139
140export function resolveRestingComposerControlsLayout(
141 input: RestingComposerControlsMeasurement & {
142 hostWidth: number;
143 previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
144 },
145): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
146 const { blockWidths, hostWidth, previous } = input;
147 const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
148 const previousStep = previous
149 ? previous.hiddenCount > 0
150 ? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
151 : Math.min(previous.iconOnlyCount ?? 0, iconSteps)
152 : 0;
153 let step = 0;
154 const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
155 restingComposerControlsWidth(
156 input,
157 Math.max(0, candidate - iconSteps),
158 fixedWidth,
159 Math.min(candidate, iconSteps),
160 );
161 // Use 1px slack for natural-width jitter on promotion; demote immediately to avoid clipping or layout
162 // flapping.
163 while (
164 step < iconSteps + blockWidths.length &&
165 widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
166 ) {
167 step += 1;
168 }
169 const hiddenCount = Math.max(0, step - iconSteps);
170 const iconOnlyCount = Math.min(step, iconSteps);
171 const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
172 const visible =
173 previous && !previous.visible
174 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
175 : minimumWidth <= hostWidth;
176 return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
177}
178
179export function resolveScrollToEndClearance(input: {
180 overlayHeight: number;
181 mainSurfaceTop: number;
182 button: { left: number; right: number };
183 attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
184}): number {
185 let contentTop = input.mainSurfaceTop;
186 let top = contentTop;
187 for (const attachment of input.attachments) {
188 contentTop = Math.min(contentTop, attachment.top);
189 if (attachment.left < input.button.right && attachment.right > input.button.left) {
190 top = Math.min(top, attachment.top);
191 }
192 }
193 return Math.ceil(input.overlayHeight - (top - contentTop));
194}