Skip to content
165 changes: 98 additions & 67 deletions apps/mobile/src/components/ComposerModelControl.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ModelSelection, ProviderDriverKind } from "@t3tools/contracts";
import { useState } from "react";
import { View } from "react-native";
import { useGT, useMessages } from "gt-react-native";
import {
buildTraitsTriggerDisplay,
Expand All @@ -20,6 +22,10 @@ export function ComposerModelControl(props: {
readonly disabled?: boolean;
readonly size?: "regular" | "large";
readonly iconOnly?: boolean;
/** Effort abbreviation instead of the full traits, for a row shared with the editor. */
readonly compact?: boolean;
/** Width the control may fill; falls back to the effort abbreviation plus speed when the full traits overflow it. */
readonly fitWidth?: number;
readonly onPress: () => void;
}) {
const gt = useGT();
Expand All @@ -29,7 +35,7 @@ export function ComposerModelControl(props: {
selections: props.selection?.options,
});
const promptTraits = getPromptControlledTraits(descriptors, props.prompt);
const summary = props.provider
const traits = props.provider
? buildTraitsTriggerDisplay(
{
descriptors,
Expand All @@ -39,8 +45,12 @@ export function ComposerModelControl(props: {
reportedModelSelection: props.reportedSelection,
},
m,
).label
: "";
)
: null;
const summary = traits?.label ?? "";
const [fullWidth, setFullWidth] = useState(0);
const overflows = props.fitWidth !== undefined && fullWidth > 0 && fullWidth > props.fitWidth + 1;
const compact = props.compact === true || overflows;
const badge = props.modelOption?.providerBadge;
const label = props.modelOption?.label ?? props.selection?.model ?? gt("Choose model");
const providerNameLabel = badge
Expand All @@ -52,70 +62,91 @@ export function ComposerModelControl(props: {
})
: null;
const shortLabel = props.modelOption?.shortName ?? label;
const visibleSummary = props.compact
? (traits?.compactEffortLabel ?? "")
: overflows
? [traits?.compactEffortLabel, traits?.speedLabel].filter(Boolean).join(" ")
: summary;
const renderControl = (full: boolean) => {
const controlSummary = full ? summary : visibleSummary;
const suffixLabel = providerNameLabel
? [shortLabel, controlSummary].filter(Boolean).join(" · ")
: controlSummary;
return (
<ComposerInlineControl
accessibilityLabel={[
gt("Model and reasoning settings"),
label,
badge?.showBadge ? badge.displayName : null,
summary,
]
.filter(Boolean)
.join(", ")}
size={props.size}
disabled={props.disabled}
emphasized
renderIcon={(size) =>
badge?.showBadge ? (
<ProviderInstanceIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
displayName={badge.displayName}
accentColor={badge.accentColor}
showBadge
surfaceColor={props.surfaceColor}
size={size}
/>
) : (
<ProviderIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
size={size}
/>
)
}
label={providerNameLabel ?? shortLabel}
labelEllipsizeMode={providerNameLabel ? "middle" : undefined}
iconOnly={props.iconOnly}
suffixWidth={providerNameLabel ? 96 : undefined}
// Only short abbreviations keep their width; an unrecognized effort name truncates.
suffixFixed={!full && compact && (traits?.compactEffortLabel?.length ?? 0) <= 4}
suffix={
suffixLabel ? (
<Text
className={
props.size === "large"
? "text-sm text-foreground-muted"
: "text-xs text-foreground-muted"
}
numberOfLines={1}
>
{suffixLabel}
</Text>
) : undefined
}
maxWidth={full ? 10_000 : "100%"}
onPress={props.onPress}
/>
);
};
if (props.fitWidth === undefined) return renderControl(false);
return (
<ComposerInlineControl
accessibilityLabel={[
gt("Model and reasoning settings"),
label,
badge?.showBadge ? badge.displayName : null,
summary,
]
.filter(Boolean)
.join(", ")}
size={props.size}
disabled={props.disabled}
emphasized
renderIcon={(size) =>
badge?.showBadge ? (
<ProviderInstanceIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
displayName={badge.displayName}
accentColor={badge.accentColor}
showBadge
surfaceColor={props.surfaceColor}
size={size}
/>
) : (
<ProviderIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
size={size}
/>
)
}
label={providerNameLabel ?? shortLabel}
labelEllipsizeMode={providerNameLabel ? "middle" : undefined}
iconOnly={props.iconOnly}
suffixWidth={providerNameLabel ? 96 : undefined}
suffix={
providerNameLabel ? (
<Text
className={
props.size === "large"
? "text-sm text-foreground-muted"
: "text-xs text-foreground-muted"
}
numberOfLines={1}
>
{shortLabel}
{summary ? ` · ${summary}` : ""}
</Text>
) : summary ? (
<Text
className={
props.size === "large"
? "text-sm text-foreground-muted"
: "text-xs text-foreground-muted"
}
numberOfLines={1}
>
{summary}
</Text>
) : undefined
}
maxWidth="100%"
onPress={props.onPress}
/>
<>
{/* Unconstrained copy measures whether the full labels fit. */}
<View
pointerEvents="none"
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
style={{ position: "absolute", left: 0, top: 0, width: 10_000, opacity: 0 }}
>
<View
className="self-start"
onLayout={(event) => setFullWidth(event.nativeEvent.layout.width)}
>
{renderControl(true)}
</View>
</View>
{renderControl(false)}
</>
);
}
14 changes: 10 additions & 4 deletions apps/mobile/src/components/ComposerSurface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ const EXPANDED_EDITOR_INSET = 14;
// Room under the expanded editor for the toolbar row.
const EXPANDED_EDITOR_BOTTOM = 52;

export function useCompactComposerEditor(text: ScaledTextRole, value: string, animate: boolean) {
export function useCompactComposerEditor(
text: ScaledTextRole,
value: string,
animate: boolean,
forceExpanded = false,
) {
const [contentSize, setContentSize] = useState({ width: 0, height: 0 });
const [compactWidth, setCompactWidth] = useState(0);
const contentHeight = contentSize.height;
Expand All @@ -61,9 +66,10 @@ export function useCompactComposerEditor(text: ScaledTextRole, value: string, an
// A line that fits the expanded editor may still overflow its compact slot.
// Compare its measured width too so widening cannot trigger a collapse loop.
const isExpanded =
value.length > 0 &&
(contentHeight > text.lineHeight * 1.5 ||
(compactWidth > 0 && contentSize.width > compactWidth));
forceExpanded ||
(value.length > 0 &&
(contentHeight > text.lineHeight * 1.5 ||
(compactWidth > 0 && contentSize.width > compactWidth)));
const compactRowHeight = Math.max(44, minimum);
const compactBottom = (compactRowHeight - minimum) / 2;
const isRTL = I18nManager.isRTL;
Expand Down
7 changes: 6 additions & 1 deletion apps/mobile/src/components/ComposerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function ComposerInlineControl(props: {
readonly labelEllipsizeMode?: ComponentProps<typeof Text>["ellipsizeMode"];
readonly suffix?: ReactNode;
readonly suffixWidth?: number;
/** A short suffix keeps its width while the label truncates. */
readonly suffixFixed?: boolean;
/** Show only the icon and chevron; the label still names the control for accessibility. */
readonly iconOnly?: boolean;
readonly maxWidth?: ViewStyle["maxWidth"];
Expand Down Expand Up @@ -115,7 +117,10 @@ export function ComposerInlineControl(props: {
)}
{!props.iconOnly && props.suffix ? (
<View
className={cn("min-w-0", props.suffixWidth === undefined ? "shrink" : "shrink-0")}
className={cn(
"min-w-0",
props.suffixFixed || props.suffixWidth !== undefined ? "shrink-0" : "shrink",
)}
style={{ width: props.suffixWidth }}
>
{props.suffix}
Expand Down
50 changes: 40 additions & 10 deletions apps/mobile/src/features/threads/ThreadComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ export interface ThreadComposerProps {
readonly onEditorFocusChange?: (focused: boolean) => void;
}

// Android types in the expanded editor, so the unfocused row only previews the draft.
const ANDROID_PREVIEW_EDITOR_MIN_WIDTH = "25%";

const COMPOSER_ATTACHMENT_ENTERING =
Platform.OS === "android"
? FadeIn.duration(160)
Expand Down Expand Up @@ -467,12 +470,14 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
const isExpanded = isFocused || settingsSheetPresentation.keepsComposerExpanded;
const { usesLargeControls, paddingVertical: surfacePaddingVertical } = useComposerChrome();
const [compactRowWidth, setCompactRowWidth] = useState(0);
const [modelToolbarWidth, setModelToolbarWidth] = useState(0);
const compactAttachmentLimit = compactRowWidth >= 500 ? 3 : 1;
// Animate only while typing; a thread switch or restored draft lands in place.
const compactEditor = useCompactComposerEditor(
bodyText,
draftMessage,
Platform.OS === "ios" && isFocused,
Platform.OS === "android" && isExpanded,
);
const showsCompactDictation = isVoiceInputPresented;
const isToolbarVisible = isVoiceInputPresented;
Expand Down Expand Up @@ -681,9 +686,27 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
</>
) : null;

const fitsModelToolbar = Platform.OS === "android" && compactEditor.isExpanded;
if (!fitsModelToolbar && modelToolbarWidth > 0) {
setModelToolbarWidth(0);
}
const modelControlIconOnly = Platform.OS !== "android" && !compactEditor.isExpanded;
const modelControl = (
// Only the expanded label can truncate; shrinking the icon-only control clips the icon.
<View className={compactEditor.isExpanded ? "min-w-0 shrink" : "shrink-0"}>
<View
className={
fitsModelToolbar
? "min-w-0 flex-1 flex-row justify-end"
: // Only a labelled control can truncate; shrinking the icon-only control clips the icon.
modelControlIconOnly
? "shrink-0"
: "min-w-0 shrink"
}
onLayout={
fitsModelToolbar
? (event) => setModelToolbarWidth(event.nativeEvent.layout.width)
: undefined
}
>
<ComposerModelControl
size={usesLargeControls ? "large" : "regular"}
modelOption={currentModelOption}
Expand All @@ -692,7 +715,9 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
reportedSelection={props.reportedModelSelection}
prompt={draftMessage}
surfaceColor={composerPanel}
iconOnly={!compactEditor.isExpanded}
iconOnly={modelControlIconOnly}
compact={Platform.OS === "android" && !isExpanded}
fitWidth={fitsModelToolbar && modelToolbarWidth > 0 ? modelToolbarWidth : undefined}
onPress={openSettings}
/>
</View>
Expand Down Expand Up @@ -845,12 +870,13 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
onLayout={compactEditor.onEditorLayout}
style={[
{
minWidth:
compactEditor.isExpanded ||
stripAttachments.length > 0 ||
compactControl !== null
? 0
: INLINE_EDITOR_MIN_WIDTH,
minWidth: compactEditor.isExpanded
? 0
: Platform.OS === "android"
? ANDROID_PREVIEW_EDITOR_MIN_WIDTH
: stripAttachments.length > 0 || compactControl !== null
? 0
: INLINE_EDITOR_MIN_WIDTH,
},
compactEditor.containerStyle,
compactEditor.isExpanded ? { flex: 0, width: 0 } : undefined,
Expand Down Expand Up @@ -1024,7 +1050,11 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
>
<Animated.View
key={compactEditor.isExpanded ? "expanded" : "compact"}
className="min-w-0 shrink flex-row items-center"
className={
fitsModelToolbar
? "min-w-0 flex-1 flex-row items-center"
: "min-w-0 shrink flex-row items-center"
}
entering={compactEditor.toolbarControlsEntering}
>
{compactControl}
Expand Down
Loading
Loading