225 lines
8.3 KiB
TypeScript
225 lines
8.3 KiB
TypeScript
export const COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX = 620;
|
|
export const COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX = 780;
|
|
const RESTING_COMPOSER_IMAGE_THUMBNAIL_LIMIT = 3;
|
|
|
|
export function getRestingComposerImagePreviewCounts(imageCount: number): {
|
|
visibleCount: number;
|
|
overflowCount: number;
|
|
} {
|
|
const visibleCount = Math.min(imageCount, RESTING_COMPOSER_IMAGE_THUMBNAIL_LIMIT);
|
|
return {
|
|
visibleCount,
|
|
overflowCount: Math.max(0, imageCount - visibleCount),
|
|
};
|
|
}
|
|
|
|
export function shouldUseCompactComposerFooter(
|
|
width: number | null,
|
|
options?: { hasWideActions?: boolean },
|
|
): boolean {
|
|
const breakpoint = options?.hasWideActions
|
|
? COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX
|
|
: COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX;
|
|
return width !== null && width < breakpoint;
|
|
}
|
|
|
|
export function shouldUseRestingComposerLayout(input: {
|
|
isExistingThread: boolean;
|
|
isMobileViewport: boolean;
|
|
isScrollCollapsed: boolean;
|
|
hasExpandedChrome: boolean;
|
|
hasMultilinePrompt: boolean;
|
|
/** Whether the timeline has more content than fits above the composer. */
|
|
timelineOverflows: boolean;
|
|
}): boolean {
|
|
// Multiline drafts stay readable. Resting only clamps a single prompt
|
|
// line and overlays its actions; non-image attachment and context
|
|
// rows keep their natural height above it while image previews move inline.
|
|
// Banners and the tasks badge dock above the surface, so they are absent
|
|
// too. Whether the context strip can host the relocated controls is
|
|
// deliberately absent here: resting reclaims vertical space at every
|
|
// desktop width, and where the strip is missing or too narrow the controls
|
|
// simply return when the composer is focused.
|
|
//
|
|
// Only a timeline scroll rests the composer: the user asked for it with the
|
|
// gesture, and it lifts on the next composer interaction. Losing focus never
|
|
// rests it, so clicking a message, copying output, or selecting text for a
|
|
// citation leaves the composer where it was.
|
|
//
|
|
// Resting exists to give reading space back to the timeline. A thread that
|
|
// fits above the composer has nothing to reclaim, so it stays expanded and
|
|
// never shows the collapsed row that a fresh thread would otherwise open on.
|
|
return (
|
|
input.isExistingThread &&
|
|
!input.isMobileViewport &&
|
|
input.timelineOverflows &&
|
|
input.isScrollCollapsed &&
|
|
!input.hasMultilinePrompt &&
|
|
!input.hasExpandedChrome
|
|
);
|
|
}
|
|
|
|
/**
|
|
* How much taller the empty expanded composer is than its resting row on
|
|
* desktop widths, from the layout classes in ChatComposer: the body loses
|
|
* 8px of top padding, the prompt clamps from min-h-17.5 (70px) to 32px, and
|
|
* the 48px footer leaves flow.
|
|
*/
|
|
export const COMPOSER_RESTING_EXPANSION_MIN_PX = 94;
|
|
|
|
/**
|
|
* The space the timeline reserves at its end for the composer overlay.
|
|
*
|
|
* The overlay is measured live, but a resting composer is much shorter than
|
|
* an expanded one. Reserving only the resting height lets a scroll to the end
|
|
* land flush against the short composer, and the expansion that follows then
|
|
* covers the last rows because the timeline never moves for footer growth.
|
|
* While resting, the reservation keeps the last expanded height, or at least
|
|
* the resting height plus the empty expansion, so expanding again changes
|
|
* nothing above the composer. An expanded measurement is authoritative and
|
|
* may shrink it.
|
|
*/
|
|
export function resolveComposerTimelineInset(input: {
|
|
currentInset: number;
|
|
overlayHeight: number;
|
|
isResting: boolean;
|
|
}): number {
|
|
return input.isResting
|
|
? Math.max(input.currentInset, input.overlayHeight + COMPOSER_RESTING_EXPANSION_MIN_PX)
|
|
: input.overlayHeight;
|
|
}
|
|
|
|
export function shouldAnimateComposerRestingTransition(input: {
|
|
hasCompletedInitialLayout: boolean;
|
|
stateChanged: boolean;
|
|
hasInterruptedAnimation: boolean;
|
|
}): boolean {
|
|
return input.hasCompletedInitialLayout && (input.stateChanged || input.hasInterruptedAnimation);
|
|
}
|
|
|
|
export function shouldUseCompactComposerPrimaryActions(
|
|
width: number | null,
|
|
options?: { hasWideActions?: boolean },
|
|
): boolean {
|
|
if (!options?.hasWideActions) {
|
|
return false;
|
|
}
|
|
return width !== null && width < COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX;
|
|
}
|
|
|
|
export interface RestingComposerControlsMeasurement {
|
|
gap: number;
|
|
naturalFixedWidth: number;
|
|
minimumFixedWidth: number;
|
|
blockWidths: readonly number[];
|
|
overflowWidth: number;
|
|
iconOnlyBlockWidths?: readonly number[];
|
|
}
|
|
|
|
function restingComposerControlsWidth(
|
|
input: RestingComposerControlsMeasurement,
|
|
hiddenCount: number,
|
|
fixedWidth = input.naturalFixedWidth,
|
|
iconOnlyCount = 0,
|
|
): number {
|
|
const { blockWidths, gap } = input;
|
|
const visibleCount = blockWidths.length - hiddenCount;
|
|
return (
|
|
fixedWidth +
|
|
blockWidths
|
|
.slice(0, visibleCount)
|
|
.reduce(
|
|
(sum, width, index) =>
|
|
sum +
|
|
(index >= blockWidths.length - iconOnlyCount
|
|
? (input.iconOnlyBlockWidths?.[index] ?? width)
|
|
: width),
|
|
0,
|
|
) +
|
|
(hiddenCount > 0 ? input.overflowWidth : 0) +
|
|
gap * (visibleCount + (hiddenCount > 0 ? 1 : 0))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The width the resting controls take with nothing moved into overflow.
|
|
*
|
|
* The context strip reserves this much for the composer before deciding
|
|
* whether its own labels may expand. Judging against the currently visible
|
|
* controls instead lets the strip expand into space the composer just gave
|
|
* up, which shrinks the host, hides the controls again, and repeats.
|
|
*/
|
|
export function resolveRestingComposerControlsNaturalWidth(
|
|
input: RestingComposerControlsMeasurement,
|
|
): number {
|
|
return restingComposerControlsWidth(input, 0);
|
|
}
|
|
|
|
/**
|
|
* Fit footer controls using natural widths: remove trailing labels first,
|
|
* then move trailing blocks into overflow. Resting and expanded share this
|
|
* decision, including the slack needed to safely restore controls.
|
|
*
|
|
* Trailing blocks compact before the model picker shrinks. Once they are all in
|
|
* the overflow menu, the picker may contract to its minimum readable width;
|
|
* below that the whole cluster hides rather than clipping.
|
|
*/
|
|
const RESTING_CONTROLS_SLACK_PX = 1;
|
|
|
|
export function resolveRestingComposerControlsLayout(
|
|
input: RestingComposerControlsMeasurement & {
|
|
hostWidth: number;
|
|
previous?: { hiddenCount: number; iconOnlyCount?: number; visible: boolean };
|
|
},
|
|
): { hiddenCount: number; iconOnlyCount?: number; visible: boolean } {
|
|
const { blockWidths, hostWidth, previous } = input;
|
|
const iconSteps = input.iconOnlyBlockWidths ? blockWidths.length : 0;
|
|
const previousStep = previous
|
|
? previous.hiddenCount > 0
|
|
? iconSteps + Math.min(previous.hiddenCount, blockWidths.length)
|
|
: Math.min(previous.iconOnlyCount ?? 0, iconSteps)
|
|
: 0;
|
|
let step = 0;
|
|
const widthAtStep = (candidate: number, fixedWidth = input.naturalFixedWidth) =>
|
|
restingComposerControlsWidth(
|
|
input,
|
|
Math.max(0, candidate - iconSteps),
|
|
fixedWidth,
|
|
Math.min(candidate, iconSteps),
|
|
);
|
|
// Promotions need a pixel of slack: recovering a flexible picker's natural
|
|
// width can jitter by a fraction of a pixel across renders. Demotions are
|
|
// immediate so a threshold cannot clip or flip React between layouts.
|
|
while (
|
|
step < iconSteps + blockWidths.length &&
|
|
widthAtStep(step) > hostWidth - (step < previousStep ? RESTING_CONTROLS_SLACK_PX : 0)
|
|
) {
|
|
step += 1;
|
|
}
|
|
const hiddenCount = Math.max(0, step - iconSteps);
|
|
const iconOnlyCount = Math.min(step, iconSteps);
|
|
const minimumWidth = widthAtStep(step, input.minimumFixedWidth);
|
|
const visible =
|
|
previous && !previous.visible
|
|
? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
|
|
: minimumWidth <= hostWidth;
|
|
return { hiddenCount, ...(input.iconOnlyBlockWidths ? { iconOnlyCount } : {}), visible };
|
|
}
|
|
|
|
export function resolveScrollToEndClearance(input: {
|
|
overlayHeight: number;
|
|
mainSurfaceTop: number;
|
|
button: { left: number; right: number };
|
|
attachments: ReadonlyArray<{ top: number; left: number; right: number }>;
|
|
}): number {
|
|
let contentTop = input.mainSurfaceTop;
|
|
let top = contentTop;
|
|
for (const attachment of input.attachments) {
|
|
contentTop = Math.min(contentTop, attachment.top);
|
|
if (attachment.left < input.button.right && attachment.right > input.button.left) {
|
|
top = Math.min(top, attachment.top);
|
|
}
|
|
}
|
|
return Math.ceil(input.overlayHeight - (top - contentTop));
|
|
}
|