Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerFooterLayout.ts

125 lines4.3 KB

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 };
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;
25​
26export function shouldUseRestingComposerLayout(input: {
27 isExistingThread: boolean;
28 isMobileViewport: boolean;
29 isFocused: boolean;
30 hasExpandedChrome: boolean;
31}): boolean {
32 // Passive draft content is deliberately absent here. Resting only clamps
33 // the prompt row and overlays its actions; non-image attachment and context
34 // rows keep their natural height above it while image previews move inline.
35 // Banners and the tasks badge dock above the surface, so they are absent
36 // too. Whether the context strip can host the relocated controls is
37 // deliberately absent here: resting reclaims vertical space at every
38 // desktop width, and where the strip is missing or too narrow the controls
39 // simply return when the composer is focused.
40 return (
41 input.isExistingThread &&
42 !input.isMobileViewport &&
43 !input.isFocused &&
44 !input.hasExpandedChrome
45 );
47​
48export function shouldAnimateComposerRestingTransition(input: {
49 hasCompletedInitialLayout: boolean;
50 stateChanged: boolean;
51 hasInterruptedAnimation: boolean;
52}): boolean {
53 return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
55​
56export function shouldUseCompactComposerPrimaryActions(
57 width: number | null,
58 options?: { hasWideActions?: boolean },
59): boolean {
60 if (!options?.hasWideActions) {
61 return false;
62 }
63 return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
65​
66export interface RestingComposerControlsMeasurement {
67 gap: number;
68 naturalFixedWidth: number;
69 minimumFixedWidth: number;
70 blockWidths: readonly number[];
71 overflowWidth: number;
73​
74function restingComposerControlsWidth(
75 input: RestingComposerControlsMeasurement,
76 hiddenCount: number,
77 fixedWidth = input.naturalFixedWidth,
78): number {
79 const { blockWidths, gap } = input;
80 const visibleCount = blockWidths.length - hiddenCount;
81 return (
82 fixedWidth +
83 blockWidths.slice(0, visibleCount).reduce((sum, width) => sum + width, 0) +
84 (hiddenCount > 0 ? input.overflowWidth : 0) +
85 gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
86 );
88​
89/**
90 * The width the resting controls take with nothing moved into overflow.
91 *
92 * The context strip reserves this much for the composer before deciding
93 * whether its own labels may expand. Judging against the currently visible
94 * controls instead lets the strip expand into space the composer just gave
95 * up, which shrinks the host, hides the controls again, and repeats.
96 */
97export function resolveRestingComposerControlsNaturalWidth(
98 input: RestingComposerControlsMeasurement,
99): number {
100 return restingComposerControlsWidth(input, 0);
102​
103/**
104 * Decide how many trailing resting control blocks move into the overflow
105 * menu, and whether the cluster can show at all, from natural widths.
106 *
107 * Trailing blocks hide before the model picker shrinks. Once they are all in
108 * the overflow menu, the picker may contract to its minimum readable width;
109 * below that the whole cluster hides rather than clipping.
110 */
111export function resolveRestingComposerControlsLayout(
112 input: RestingComposerControlsMeasurement & { hostWidth: number },
113): { hiddenCount: number; visible: boolean } {
114 const { blockWidths, hostWidth } = input;
115 let hiddenCount = 0;
116 while (
117 hiddenCount < blockWidths.length &&
118 restingComposerControlsWidth(input, hiddenCount) > hostWidth
119 ) {
120 hiddenCount += 1;
121 }
122 const visible =
123 restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth) <= hostWidth;
124 return { hiddenCount, visible };