Open History to see the last commit
1import type { RestingComposerControlsMeasurement } from "../composerFooterLayout";
2
3function elementOuterWidth(element: HTMLElement): number {
4 const width = element.getBoundingClientRect().width;
5 if (width === 0) return 0;
6 const style = getComputedStyle(element);
7 return (
8 width +
9 (Number.parseFloat(style.marginInlineStart) || 0) +
10 (Number.parseFloat(style.marginInlineEnd) || 0)
11 );
12}
13
14function elementInlineMarginWidth(element: HTMLElement): number {
15 const style = getComputedStyle(element);
16 return (
17 (Number.parseFloat(style.marginInlineStart) || 0) +
18 (Number.parseFloat(style.marginInlineEnd) || 0)
19 );
20}
21
22function providerModelPickerNaturalWidth(picker: HTMLElement): number {
23 const renderedWidth = picker.getBoundingClientRect().width;
24 if (renderedWidth === 0) return 0;
25 const style = getComputedStyle(picker);
26 const label = picker.querySelector<HTMLElement>('[data-chat-provider-model-picker-label="true"]');
27 const labelIsCollapsed = label?.clientWidth === 0 && getComputedStyle(label).flexGrow === "0";
28 const hiddenLabelWidth =
29 label && !labelIsCollapsed ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
30 const maxWidth = Number.parseFloat(style.maxWidth);
31 const naturalWidth = Math.min(
32 renderedWidth + hiddenLabelWidth,
33 Number.isFinite(maxWidth) ? maxWidth : Number.POSITIVE_INFINITY,
34 );
35 return naturalWidth + elementInlineMarginWidth(picker);
36}
37
38function providerModelPickerMinimumWidth(picker: HTMLElement): number {
39 const minWidth = Number.parseFloat(getComputedStyle(picker).minWidth) || 0;
40 return minWidth + elementInlineMarginWidth(picker);
41}
42
43function controlBlockWidths(block: HTMLElement): { natural: number; iconOnly: number } {
44 const compact = block.dataset.composerBlockIconOnly === "true";
45 let natural = elementOuterWidth(block);
46 let iconOnly = natural;
47 for (const label of block.querySelectorAll<HTMLElement>("[data-composer-control-label]")) {
48 const labelStyle = getComputedStyle(label);
49 const inFlow = labelStyle.position !== "absolute";
50 // Phone sr-only clipping survives compact styles overriding the label width.
51 if (!inFlow && (!compact || labelStyle.clip !== "auto")) continue;
52 const labelWidth = label.scrollWidth;
53 const renderedWidth = inFlow ? label.getBoundingClientRect().width : 0;
54 const gap = Number.parseFloat(getComputedStyle(label.parentElement!).columnGap) || 0;
55 natural += labelWidth - renderedWidth + (inFlow ? 0 : gap);
56 iconOnly -= renderedWidth + (inFlow ? gap : 0);
57 }
58 for (const icon of block.querySelectorAll<HTMLElement>("[data-composer-control-compact-icon]")) {
59 const width = elementOuterWidth(icon);
60 const gap = Number.parseFloat(getComputedStyle(icon.parentElement!).columnGap) || 0;
61 natural -= compact ? width + gap : 0;
62 iconOnly += compact ? 0 : width + gap;
63 }
64 return { natural, iconOnly: Math.min(natural, iconOnly) };
65}
66
67/** Measure independently of controls hidden by the previous render. */
68export function measureRestingComposerControls(
69 controls: HTMLElement,
70): RestingComposerControlsMeasurement | null {
71 const gap = Number.parseFloat(getComputedStyle(controls).columnGap) || 0;
72 const picker = controls.querySelector<HTMLElement>("[data-chat-provider-model-picker]");
73 const leadingControl =
74 picker ?? controls.querySelector<HTMLElement>('[data-chat-provider-unavailable="true"]');
75 if (!leadingControl) return null;
76 const separator = controls.querySelector<HTMLElement>("[data-resting-controls-separator]");
77 const separatorWidth = separator ? elementOuterWidth(separator) : 0;
78 const overflow = controls.querySelector<HTMLElement>("[data-resting-controls-overflow]");
79 const separatorAndGapWidth = separatorWidth > 0 ? separatorWidth + gap : 0;
80 const blocks = Array.from(controls.querySelectorAll<HTMLElement>("[data-resting-block]"));
81 const widths = blocks.map(controlBlockWidths);
82 return {
83 gap,
84 naturalFixedWidth:
85 (picker ? providerModelPickerNaturalWidth(picker) : elementOuterWidth(leadingControl)) +
86 separatorAndGapWidth,
87 minimumFixedWidth:
88 (picker ? providerModelPickerMinimumWidth(picker) : elementOuterWidth(leadingControl)) +
89 separatorAndGapWidth,
90 blockWidths: widths.map((width) => width.natural),
91 iconOnlyBlockWidths: widths.map((width) => width.iconOnly),
92 overflowWidth: overflow ? elementOuterWidth(overflow) : 0,
93 };
94}