Open History to see the last commit
1import type { SubagentPillSegment } from "@t3tools/client-runtime/state/thread-subagents";
2import { GlassContainer, GlassView } from "expo-glass-effect";
3import { useGT } from "gt-react-native";
4import { type ReactNode, useEffect, useRef, useState } from "react";
5import { type LayoutChangeEvent, Pressable, useWindowDimensions, View } from "react-native";
6import Animated, {
7 Easing,
8 FadeIn,
9 FadeOut,
10 ReduceMotion,
11 type SharedValue,
12 useAnimatedStyle,
13 useReducedMotion,
14 useSharedValue,
15 withTiming,
16} from "react-native-reanimated";
17import { withUniwind } from "uniwind";
18
19import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
20import { AppText as Text } from "../../components/ui/AppText";
21import { SymbolView } from "../../components/ui/AppSymbol";
22import { ControlPill } from "../../components/ui/ControlPill";
23import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
24import { BrowserPreviewButton } from "../browser/browser-preview-button";
25import { DevicePreviewButton } from "../devices/device-preview-button";
26import type { FloatingWorkingStatus } from "./floating-working-status";
27import { ShimmeringWorkContent } from "./thread-work-log";
28
29const CONTROL_HEIGHT = 38.5;
30const CONTROL_GAP = 8;
31const COMPOSER_CAPSULE_INSET = 6;
32const GLASS_MERGE_SPACING = 12;
33const CONTROL_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
34const CONTROL_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
35const CONTROL_TIMING = {
36 duration: 240,
37 easing: Easing.out(Easing.cubic),
38 reduceMotion: ReduceMotion.System,
39} as const;
40const CONTROL_ROW_GAP = 14;
41const CONTROL_SEPARATION = (CONTROL_ROW_GAP + CONTROL_HEIGHT) / 2;
42const LABEL_ENTERING = FadeIn.duration(160).delay(80).reduceMotion(ReduceMotion.System);
43const LABEL_EXITING = FadeOut.duration(100).reduceMotion(ReduceMotion.System);
44
45// Expo reapplies glass after layout or reattachment, restoring material UIKit can lose behind visible labels.
46const UniwindGlassView = withUniwind(GlassView, {
47 style: { fromClassName: "className" },
48});
49const UniwindGlassContainer = withUniwind(GlassContainer, {
50 style: { fromClassName: "className" },
51});
52const AnimatedGlassView = Animated.createAnimatedComponent(UniwindGlassView);
53
54// Segment widths feed back into labelWidth; ignore sub-point changes to avoid rounding-driven ellipsis loops.
55function settleWidth(next: number) {
56 return (current: number) => (Math.abs(current - next) < 1 ? current : next);
57}
58
59const CONTROL_OVERLAY_OFFSET = CONTROL_HEIGHT + CONTROL_GAP - COMPOSER_CAPSULE_INSET;
60export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTROL_GAP;
61
62export function FloatingWorkingControl(props: {
63 readonly colorScheme: "light" | "dark";
64 readonly status: FloatingWorkingStatus | null;
65 readonly extensionStatus?: { readonly label: string; readonly onPress: () => void } | null;
66 readonly devicePreview: { readonly count: number; readonly onPress: () => void } | null;
67 readonly browserPreview: { readonly count: number; readonly onPress: () => void } | null;
68 readonly showScrollToEnd: boolean;
69 readonly onScrollToEnd: () => void;
70 readonly agents: SubagentPillSegment | null;
71 readonly onOpenAgents: () => void;
72 readonly queuedCount: number;
73 readonly onOpenQueue: () => void;
74 readonly lift?: SharedValue<number>;
75}) {
76 const gt = useGT();
77 const { width: windowWidth } = useWindowDimensions();
78 const reducedMotion = useReducedMotion();
79 const [overlayWidth, setOverlayWidth] = useState(windowWidth);
80 const [queueWidth, setQueueWidth] = useState(0);
81 const [actionWidth, setActionWidth] = useState(0);
82 const [extensionWidth, setExtensionWidth] = useState(0);
83 const extensionStatus = props.extensionStatus ?? null;
84 const extensionOnly = props.status?.kind === "extension";
85 const useGlass = NATIVE_LIQUID_GLASS_SUPPORTED && !extensionOnly;
86 const action = props.status?.kind === "stop" ? props.status.action : null;
87 const [agentsWidth, setAgentsWidth] = useState(0);
88 const hasQueue = props.queuedCount > 0;
89 const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
90 const [previewWidth, setPreviewWidth] = useState(0);
91 const agents = props.agents;
92 const hasAgents = agents !== null;
93 // Only reserve the scroll button's slot while it is visible.
94 const reservedWidth = props.showScrollToEnd ? CONTROL_HEIGHT + 32 : 16;
95 const availableLabelWidth = Math.max(
96 0,
97 Math.min(overlayWidth, windowWidth) -
98 reservedWidth -
99 (action !== null ? actionWidth : 0) -
100 (hasQueue ? queueWidth : 0) -
101 (hasAgents ? agentsWidth : 0) -
102 (hasPreview ? previewWidth : 0),
103 );
104 const labelWidth = Math.max(
105 0,
106 availableLabelWidth - (extensionStatus === null ? 0 : extensionWidth),
107 );
108 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
109
110 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated separation value when scroll-to-end visibility changes
111 useEffect(() => {
112 separationProgress.set(withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING));
113 }, [props.showScrollToEnd, separationProgress]);
114
115 const lift = props.lift;
116 const liftStyle = useAnimatedStyle(() => ({
117 transform: [{ translateY: -(lift?.value ?? 0) }],
118 }));
119 const arrowTransformStyle = useAnimatedStyle(() => ({
120 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
121 }));
122 const arrowContentStyle = useAnimatedStyle(() => ({
123 opacity: separationProgress.value,
124 }));
125
126 // Animate an in-flow sizer for native glass layout. Measure labels at fixed width to avoid repeated animation retargeting.
127 const capsuleWidth = useSharedValue<number | null>(null);
128 const measuredWidthRef = useRef<number | null>(null);
129 const handleLabelLayout = (event: LayoutChangeEvent) => {
130 const width = event.nativeEvent.layout.width;
131 if (width === measuredWidthRef.current) {
132 return;
133 }
134 const first = measuredWidthRef.current === null;
135 measuredWidthRef.current = width;
136 capsuleWidth.set(first ? width : withTiming(width, CONTROL_TIMING));
137 };
138 const hasStatus = props.status !== null;
139 const hasCapsule = hasStatus || hasQueue || hasAgents || hasPreview;
140 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the Reanimated capsule width value and measured width when the status goes away
141 useEffect(() => {
142 if (!hasStatus) {
143 measuredWidthRef.current = null;
144 capsuleWidth.set(null);
145 }
146 }, [capsuleWidth, hasStatus]);
147 const capsuleStyle = useAnimatedStyle(() => ({
148 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
149 }));
150 const capsuleSizerStyle = useAnimatedStyle(() => ({
151 width: capsuleWidth.value ?? 0,
152 }));
153
154 if (!hasCapsule && !props.showScrollToEnd) {
155 return null;
156 }
157
158 const previewCompact =
159 hasStatus ||
160 hasAgents ||
161 hasQueue ||
162 (props.devicePreview !== null && props.browserPreview !== null);
163 const statusInteractive =
164 props.status?.kind === "connection" ||
165 props.status?.kind === "extension" ||
166 props.status?.kind === "child-input";
167 const capsuleInteractive =
168 statusInteractive ||
169 extensionStatus !== null ||
170 action !== null ||
171 hasQueue ||
172 hasAgents ||
173 hasPreview;
174 const statusContent =
175 props.status !== null ? (
176 <View
177 pointerEvents={statusInteractive ? "box-none" : "none"}
178 className="h-11 items-center justify-center overflow-hidden"
179 >
180 <Animated.View className="h-11" style={capsuleSizerStyle} />
181 <View
182 pointerEvents={statusInteractive ? "box-none" : "none"}
183 className="absolute h-11 items-center justify-center"
184 style={{ width: labelWidth }}
185 >
186 <FloatingStatusLabel
187 key={
188 props.status.kind === "extension"
189 ? props.status.kind
190 : `${props.status.kind}:${props.status.label}`
191 }
192 status={props.status}
193 onLayout={handleLabelLayout}
194 />
195 </View>
196 </View>
197 ) : null;
198
199 const capsuleContent = (
200 <View className="flex-row items-center">
201 {statusContent}
202 {extensionStatus !== null ? (
203 <Pressable
204 accessibilityRole="button"
205 accessibilityLabel={extensionStatus.label}
206 accessibilityHint={gt("Shows the full Pi status")}
207 onPress={extensionStatus.onPress}
208 onLayout={(event) => setExtensionWidth(settleWidth(event.nativeEvent.layout.width))}
209 style={{ maxWidth: availableLabelWidth * 0.5 }}
210 className="h-11 flex-row items-center px-3 active:opacity-70"
211 >
212 {hasStatus ? <View className="absolute left-0 h-4 w-px bg-border" /> : null}
213 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
214 {extensionStatus.label}
215 </Text>
216 </Pressable>
217 ) : null}
218 {action !== null ? (
219 <View
220 onLayout={(event) => setActionWidth(settleWidth(event.nativeEvent.layout.width))}
221 className="h-11 flex-row items-center"
222 >
223 <View className="h-4 w-px bg-border" />
224 <Pressable
225 accessibilityRole="button"
226 accessibilityLabel={action.accessibilityLabel}
227 accessibilityState={{ disabled: action.disabled }}
228 disabled={action.disabled}
229 onPress={action.onPress}
230 className={`h-11 items-center justify-center px-3 active:opacity-70 ${action.disabled ? "opacity-50" : ""}`}
231 >
232 <Text className="font-t3-medium text-xs text-foreground" numberOfLines={1}>
233 {action.label}
234 </Text>
235 </Pressable>
236 </View>
237 ) : null}
238 {hasPreview ? (
239 <View
240 className="h-11 flex-row items-center"
241 onLayout={(event) => setPreviewWidth(settleWidth(event.nativeEvent.layout.width))}
242 >
243 {hasStatus ? <View className="h-4 w-px bg-border" /> : null}
244 {props.devicePreview !== null ? (
245 <DevicePreviewButton {...props.devicePreview} compact={previewCompact} />
246 ) : null}
247 {props.browserPreview !== null ? (
248 <BrowserPreviewButton {...props.browserPreview} compact={previewCompact} />
249 ) : null}
250 </View>
251 ) : null}
252 {agents !== null ? (
253 <Pressable
254 accessibilityRole="button"
255 accessibilityLabel={gt("Open agents, {agents}", { agents: agents.accessibilityLabel })}
256 accessibilityHint={gt("Opens this turn's subagents")}
257 onPress={props.onOpenAgents}
258 onLayout={(event) => setAgentsWidth(settleWidth(event.nativeEvent.layout.width))}
259 className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
260 >
261 {hasStatus || hasPreview ? <View className="absolute left-0 h-4 w-px bg-border" /> : null}
262 <SymbolView name="person.2" size={13} tintColorClassName="accent-foreground" />
263 <Text className="font-t3-medium text-xs tabular-nums" numberOfLines={1}>
264 {agents.label}
265 </Text>
266 </Pressable>
267 ) : null}
268 {hasQueue ? (
269 <Pressable
270 accessibilityRole="button"
271 accessibilityLabel={gt("{count, plural, other {Open queue, {count} messages}}", {
272 count: props.queuedCount,
273 })}
274 accessibilityHint={gt("Opens queued messages to send now, edit, or remove")}
275 onPress={props.onOpenQueue}
276 onLayout={(event) => setQueueWidth(settleWidth(event.nativeEvent.layout.width))}
277 style={{ maxWidth: Math.min(overlayWidth, windowWidth) * 0.45 }}
278 className="h-11 flex-row items-center gap-2 px-3 active:opacity-70"
279 >
280 {hasStatus || hasPreview || hasAgents ? (
281 <View className="absolute left-0 h-4 w-px bg-border" />
282 ) : null}
283 <SymbolView name="list.number" size={13} tintColorClassName="accent-foreground" />
284 <Text className="shrink font-t3-medium text-xs tabular-nums" numberOfLines={1}>
285 {gt("{count, plural, other {{count} queued}}", { count: props.queuedCount })}
286 </Text>
287 </Pressable>
288 ) : null}
289 </View>
290 );
291
292 return (
293 <Animated.View
294 pointerEvents="box-none"
295 className="absolute left-0 right-0 z-20 items-center"
296 style={[{ top: -CONTROL_OVERLAY_OFFSET }, liftStyle]}
297 onLayout={(event) => setOverlayWidth(event.nativeEvent.layout.width)}
298 entering={useGlass || extensionOnly || reducedMotion ? undefined : CONTROL_ENTERING}
299 exiting={useGlass || extensionOnly || reducedMotion ? undefined : CONTROL_EXITING}
300 >
301 {hasCapsule && useGlass ? (
302 <UniwindGlassContainer
303 spacing={GLASS_MERGE_SPACING}
304 pointerEvents="box-none"
305 className="flex-row items-center gap-4"
306 >
307 <AnimatedGlassView
308 colorScheme={props.colorScheme}
309 glassEffectStyle="regular"
310 isInteractive={capsuleInteractive}
311 pointerEvents={capsuleInteractive ? "box-none" : "none"}
312 className="h-11 items-center justify-center overflow-hidden rounded-full"
313 style={capsuleStyle}
314 >
315 {capsuleContent}
316 </AnimatedGlassView>
317
318 <AnimatedGlassView
319 colorScheme={props.colorScheme}
320 glassEffectStyle={{ style: props.showScrollToEnd ? "regular" : "none", animate: true }}
321 isInteractive
322 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
323 accessibilityElementsHidden={!props.showScrollToEnd}
324 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
325 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
326 style={arrowTransformStyle}
327 >
328 <Animated.View style={arrowContentStyle}>
329 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
330 </Animated.View>
331 </AnimatedGlassView>
332 </UniwindGlassContainer>
333 ) : hasCapsule ? (
334 <View pointerEvents="box-none" className="flex-row items-center gap-4">
335 <Animated.View
336 pointerEvents={capsuleInteractive ? "box-none" : "none"}
337 className="h-11 items-center justify-center overflow-hidden rounded-full border border-border bg-glass-fallback shadow-md shadow-black/10"
338 style={capsuleStyle}
339 >
340 {capsuleContent}
341 </Animated.View>
342
343 <Animated.View
344 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
345 accessibilityElementsHidden={!props.showScrollToEnd}
346 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
347 style={[arrowTransformStyle, arrowContentStyle]}
348 >
349 <ControlPill
350 accessibilityLabel={gt("Scroll to end")}
351 activateOnPressIn
352 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
353 disabled={!props.showScrollToEnd}
354 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
355 onPress={props.onScrollToEnd}
356 />
357 </Animated.View>
358 </View>
359 ) : NATIVE_LIQUID_GLASS_SUPPORTED ? (
360 <UniwindGlassView
361 colorScheme={props.colorScheme}
362 glassEffectStyle="regular"
363 isInteractive
364 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
365 >
366 <ScrollToEndButton onPress={props.onScrollToEnd} />
367 </UniwindGlassView>
368 ) : (
369 <ControlPill
370 accessibilityLabel={gt("Scroll to end")}
371 activateOnPressIn
372 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
373 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
374 onPress={props.onScrollToEnd}
375 />
376 )}
377 </Animated.View>
378 );
379}
380
381function FloatingStatusLabel(props: {
382 readonly status: FloatingWorkingStatus;
383 readonly onLayout: (event: LayoutChangeEvent) => void;
384}) {
385 if (props.status.kind === "extension") {
386 return (
387 <StatusLabelRow
388 accessibilityLabel={props.status.label}
389 accessibilityRole="button"
390 onPress={props.status.onPress}
391 onLayout={props.onLayout}
392 className="gap-1.5"
393 animated={false}
394 >
395 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
396 {props.status.label}
397 </Text>
398 <SymbolView name="chevron.down" size={10} tintColorClassName="accent-foreground" />
399 </StatusLabelRow>
400 );
401 }
402 if (props.status.kind === "child-input") {
403 return (
404 <StatusLabelRow
405 key="child-input"
406 accessibilityLabel={props.status.accessibilityLabel}
407 accessibilityRole="button"
408 className="gap-2"
409 onLayout={props.onLayout}
410 onPress={props.status.onPress}
411 >
412 <SymbolView name="text.bubble" size={13} tintColorClassName="accent-foreground" />
413 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
414 {props.status.label}
415 </Text>
416 </StatusLabelRow>
417 );
418 }
419 if (props.status.kind === "connection") {
420 return (
421 <StatusLabelRow
422 key="connection"
423 accessibilityLabel={props.status.label}
424 accessibilityRole="button"
425 className="gap-2"
426 onLayout={props.onLayout}
427 onPress={props.status.onPress}
428 >
429 <View className="h-2 w-2 rounded-full bg-red-500" />
430 <Text
431 className="max-w-[260px] shrink font-t3-medium text-xs text-foreground"
432 numberOfLines={1}
433 >
434 {props.status.label}
435 </Text>
436 </StatusLabelRow>
437 );
438 }
439 if (props.status.kind === "stop") {
440 return (
441 <StatusLabelRow
442 key="stop"
443 accessibilityLabel={props.status.label}
444 className="gap-2"
445 onLayout={props.onLayout}
446 >
447 {props.status.unresponsive ? (
448 <SymbolView
449 name="exclamationmark.triangle"
450 size={13}
451 tintColorClassName="accent-warning-foreground"
452 type="monochrome"
453 />
454 ) : (
455 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
456 )}
457 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
458 {props.status.label}
459 </Text>
460 </StatusLabelRow>
461 );
462 }
463 if (props.status.kind === "goal") {
464 return (
465 <StatusLabelRow
466 key="goal"
467 accessibilityLabel={props.status.accessibilityLabel}
468 className="gap-2"
469 onLayout={props.onLayout}
470 >
471 <SymbolView
472 name="target"
473 size={13}
474 tintColorClassName="accent-foreground"
475 type="monochrome"
476 />
477 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
478 {props.status.label}
479 </Text>
480 </StatusLabelRow>
481 );
482 }
483 return (
484 <StatusLabelRow
485 key="preparing"
486 accessibilityLabel={props.status.label}
487 className="gap-1.5"
488 onLayout={props.onLayout}
489 >
490 <SymbolView
491 name="arrow.triangle.branch"
492 size={13}
493 tintColorClassName="accent-foreground"
494 type="monochrome"
495 />
496 <ShimmeringWorkContent
497 className="flex-none"
498 textClassName="font-t3-medium"
499 compact
500 icon="arrow.triangle.branch"
501 iconSubtleColor="transparent"
502 label={props.status.label}
503 showIcon={false}
504 />
505 </StatusLabelRow>
506 );
507}
508
509function StatusLabelRow(props: {
510 readonly animated?: boolean;
511 readonly accessibilityLabel?: string;
512 readonly accessibilityRole?: "button";
513 readonly className?: string;
514 readonly children: ReactNode;
515 readonly onLayout: (event: LayoutChangeEvent) => void;
516 readonly onPress?: () => void;
517}) {
518 const reducedMotion = useReducedMotion();
519 const rowClassName = `h-11 flex-row items-center px-4 ${props.className ?? ""}`;
520 return (
521 <Animated.View
522 className="absolute max-w-full"
523 entering={props.animated === false || reducedMotion ? undefined : LABEL_ENTERING}
524 exiting={props.animated === false || reducedMotion ? undefined : LABEL_EXITING}
525 onLayout={props.onLayout}
526 >
527 {props.onPress ? (
528 <Pressable
529 accessibilityLabel={props.accessibilityLabel}
530 accessibilityRole={props.accessibilityRole}
531 className={`${rowClassName} active:opacity-70`}
532 onPress={props.onPress}
533 >
534 {props.children}
535 </Pressable>
536 ) : (
537 <View accessible accessibilityLabel={props.accessibilityLabel} className={rowClassName}>
538 {props.children}
539 </View>
540 )}
541 </Animated.View>
542 );
543}
544
545function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
546 const gt = useGT();
547 return (
548 <ControlPill
549 accessibilityLabel={gt("Scroll to end")}
550 activateOnPressIn
551 className="h-11 w-11 bg-transparent"
552 disabled={props.disabled}
553 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
554 onPress={props.onPress}
555 />
556 );
557}