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