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;
3export const 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}): boolean {
34 // Passive draft content is deliberately absent here. Resting only clamps
35 // the prompt row and overlays its actions; non-image attachment and context
36 // rows keep their natural height above it while image previews move inline.
37 // Banners and the tasks badge dock above the surface, so they are absent
38 // too. Whether the context strip can host the relocated controls is
39 // deliberately absent here: resting reclaims vertical space at every
40 // desktop width, and where the strip is missing or too narrow the controls
41 // simply return when the composer is focused.
42 //
43 // A scroll collapse rests the composer regardless of the blur preference:
44 // the user asked for it with the gesture, and it lifts on the next
45 // composer interaction. With blur collapse off, losing focus alone never
46 // rests the composer.
47 const collapsed = input.isScrollCollapsed || (input.collapseOnBlur && !input.isFocused);
48 return input.isExistingThread && !input.isMobileViewport && collapsed && !input.hasExpandedChrome;
49}
50
51export function shouldAnimateComposerRestingTransition(input: {
52 hasCompletedInitialLayout: boolean;
53 stateChanged: boolean;
54 hasInterruptedAnimation: boolean;
55}): boolean {
56 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
57}
58
59export function shouldUseCompactComposerPrimaryActions(
60 width: number | null,
61 options?: { hasWideActions?: boolean },
62): boolean {
63 if (!options?.hasWideActions) {
64 return false;
65 }
66 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
67}
68
69export interface RestingComposerControlsMeasurement {
70 gap: number;
71 naturalFixedWidth: number;
72 minimumFixedWidth: number;
73 blockWidths: readonly number[];
74 overflowWidth: number;
75}
76
77function restingComposerControlsWidth(
78 input: RestingComposerControlsMeasurement,
79 hiddenCount: number,
80 fixedWidth = input.naturalFixedWidth,
81): number {
82 const { blockWidths, gap } = input;
83 const visibleCount = blockWidths.length - hiddenCount;
84 return (
85 fixedWidth +
86 blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
87 (hiddenCount > 0 ? input.overflowWidth : 0) +
88 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
89 );
90}
91
92/**
93 * The width the resting controls take with nothing moved into overflow.
94 *
95 * The context strip reserves this much for the composer before deciding
96 * whether its own labels may expand. Judging against the currently visible
97 * controls instead lets the strip expand into space the composer just gave
98 * up, which shrinks the host, hides the controls again, and repeats.
99 */
100export function resolveRestingComposerControlsNaturalWidth(
101 input: RestingComposerControlsMeasurement,
102): number {
103 return restingComposerControlsWidth(input, 0);
104}
105
106/**
107 * Decide how many trailing resting control blocks move into the overflow
108 * menu, and whether the cluster can show at all, from natural widths.
109 *
110 * Trailing blocks hide before the model picker shrinks. Once they are all in
111 * the overflow menu, the picker may contract to its minimum readable width;
112 * below that the whole cluster hides rather than clipping.
113 */
114const RESTING_CONTROLS_SLACK_PX = 1;
115
116export function resolveRestingComposerControlsLayout(
117 input: RestingComposerControlsMeasurement & {
118 hostWidth: number;
119 previous?: { hiddenCount: number; visible: boolean };
120 },
121): { hiddenCount: number; visible: boolean } {
122 const { blockWidths, hostWidth, previous } = input;
123 let hiddenCount = 0;
124 while (
125 hiddenCount < blockWidths.length &&
126 restingComposerControlsWidth(input, hiddenCount) > hostWidth
127 ) {
128 hiddenCount += 1;
129 }
130 // Growing the overflow menu is unconditional, or the controls would clip.
131 // Shrinking it has to earn a pixel of slack first: the picker is flexible,
132 // so its natural width is recovered from a truncated label whose
133 // scrollWidth is integral while the rendered box is fractional. The
134 // composer re-measures on every render, so without that margin a host
135 // sitting exactly on a threshold flips a block in and out until React
136 // gives up with "Maximum update depth exceeded".
137 if (previous) {
138 const previousHiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
139 while (
140 hiddenCount < previousHiddenCount &&
141 restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX
142 ) {
143 hiddenCount += 1;
144 }
145 }
146 const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
147 const visible =
148 previous && !previous.visible
149 ? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
150 : minimumWidth <= hostWidth;
151 return { hiddenCount, visible };
152}