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
61const CONTROL_OVERLAY_OFFSET = CONTROL_HEIGHT + CONTROL_GAP - COMPOSER_CAPSULE_INSET;
62export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTROL_GAP;
63
64export function FloatingWorkingControl(props: {
65 readonly colorScheme: "light" | "dark";
66 readonly status: FloatingWorkingStatus | null;
67 readonly devicePreview: { readonly count: number; readonly onPress: () => void } | null;
68 readonly browserPreview: { readonly count: number; readonly onPress: () => void } | null;
69 readonly showScrollToEnd: boolean;
70 readonly onScrollToEnd: () => void;
71 readonly agents: SubagentPillSegment | null;
72 readonly onOpenAgents: () => void;
73 readonly queuedCount: number;
74 readonly onOpenQueue: () => void;
75 readonly lift?: SharedValue<number>;
76}) {
77 const { width: windowWidth } = useWindowDimensions();
78 const [overlayWidth, setOverlayWidth] = useState(windowWidth);
79 const [queueWidth, setQueueWidth] = useState(0);
80 const [agentsWidth, setAgentsWidth] = useState(0);
81 const hasQueue = props.queuedCount > 0;
82 const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
83 const [previewWidth, setPreviewWidth] = useState(0);
84 const agents = props.agents;
85 const hasAgents = agents !== null;
86 const labelWidth = Math.max(
87 0,
88 Math.min(overlayWidth, windowWidth) -
89 CONTROL_HEIGHT -
90 32 -
91 (hasQueue ? queueWidth : 0) -
92 (hasAgents ? agentsWidth : 0) -
93 (hasPreview ? previewWidth : 0),
94 );
95 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
96
97 useEffect(() => {
98 separationProgress.value = withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING);
99 }, [props.showScrollToEnd, separationProgress]);
100
101 const lift = props.lift;
102 const liftStyle = useAnimatedStyle(() => ({
103 transform: [{ translateY: -(lift?.value ?? 0) }],
104 }));
105 const arrowTransformStyle = useAnimatedStyle(() => ({
106 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
107 }));
108 const arrowContentStyle = useAnimatedStyle(() => ({
109 opacity: separationProgress.value,
110 }));
111
112 // Animate an in-flow sizer so native glass receives real layout updates.
113 // Measure labels in a separate, fixed-width host: measuring against the
114 // animated capsule constrains the incoming text to each intermediate width
115 // and repeatedly retargets the animation as it grows.
116 const capsuleWidth = useSharedValue<number | null>(null);
117 const measuredWidthRef = useRef<number | null>(null);
118 const handleLabelLayout = (event: LayoutChangeEvent) => {
119 const width = event.nativeEvent.layout.width;
120 if (width === measuredWidthRef.current) {
121 return;
122 }
123 const first = measuredWidthRef.current === null;
124 measuredWidthRef.current = width;
125 capsuleWidth.value = first ? width : withTiming(width, CONTROL_TIMING);
126 };
127 const hasStatus = props.status !== null;
128 const hasCapsule = hasStatus || hasQueue || hasAgents || hasPreview;
129 useEffect(() => {
130 if (!hasStatus) {
131 measuredWidthRef.current = null;
132 capsuleWidth.value = null;
133 }
134 }, [capsuleWidth, hasStatus]);
135 const capsuleStyle = useAnimatedStyle(() => ({
136 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
137 }));
138 const capsuleSizerStyle = useAnimatedStyle(() => ({
139 width: capsuleWidth.value ?? 0,
140 }));
141
142 if (!hasCapsule && !props.showScrollToEnd) {
143 return null;
144 }
145
146 const previewCompact =
147 hasStatus ||
148 hasAgents ||
149 hasQueue ||
150 (props.devicePreview !== null && props.browserPreview !== null);
151 const statusInteractive =
152 props.status?.kind === "connection" || props.status?.kind === "child-input";
153 const capsuleInteractive = statusInteractive || hasQueue || hasAgents || hasPreview;
154 const statusContent =
155 props.status !== null ? (
156 <View
157 pointerEvents={statusInteractive ? "box-none" : "none"}
158 className="h-11 items-center justify-center"
159 >
160 <Animated.View className="h-11" style={capsuleSizerStyle} />
161 <View
162 pointerEvents={statusInteractive ? "box-none" : "none"}
163 className="absolute h-11 items-center justify-center"
164 style={{ width: labelWidth }}
165 >
166 <FloatingStatusLabel
167 key={
168 props.status.kind === "working" || props.status.kind === "compacting"
169 ? props.status.kind
170 : `${props.status.kind}:${props.status.label}`
171 }
172 status={props.status}
173 onLayout={handleLabelLayout}
174 />
175 </View>
176 </View>
177 ) : null;
178
179 const capsuleContent = (
180 <View className="flex-row items-center">
181 {statusContent}
182 {hasPreview ? (
183 <View
184 className="h-11 flex-row items-center"
185 onLayout={(event) => setPreviewWidth(event.nativeEvent.layout.width)}
186 >
187 {hasStatus ? <View className="h-4 w-px bg-border" /> : null}
188 {props.devicePreview !== null ? (
189 <DevicePreviewButton {...props.devicePreview} compact={previewCompact} />
190 ) : null}
191 {props.browserPreview !== null ? (
192 <BrowserPreviewButton {...props.browserPreview} compact={previewCompact} />
193 ) : null}
194 </View>
195 ) : null}
196 {agents !== null ? (
197 <Pressable
198 accessibilityRole="button"
199 accessibilityLabel={`Open agents, ${agents.accessibilityLabel}`}
200 accessibilityHint="Opens this turn's subagents"
201 onPress={props.onOpenAgents}
202 onLayout={(event) => setAgentsWidth(event.nativeEvent.layout.width)}
203 className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
204 >
205 {hasStatus || hasPreview ? <View className="mr-1 h-4 w-px bg-border" /> : null}
206 <SymbolView name="person.2" size={13} tintColorClassName="accent-foreground-muted" />
207 <Text className="font-t3-medium text-xs tabular-nums" numberOfLines={1}>
208 {agents.label}
209 </Text>
210 </Pressable>
211 ) : null}
212 {hasQueue ? (
213 <Pressable
214 accessibilityRole="button"
215 accessibilityLabel={`Open queue, ${props.queuedCount} messages`}
216 accessibilityHint="Opens queued messages for reordering, steering, or removal"
217 onPress={props.onOpenQueue}
218 onLayout={(event) => setQueueWidth(event.nativeEvent.layout.width)}
219 style={{ maxWidth: Math.min(overlayWidth, windowWidth) * 0.45 }}
220 className="h-11 flex-row items-center gap-2 px-3 active:opacity-70"
221 >
222 {hasStatus || hasPreview || hasAgents ? (
223 <View className="mr-1 h-4 w-px bg-border" />
224 ) : null}
225 <SymbolView name="list.number" size={13} tintColorClassName="accent-foreground-muted" />
226 <Text className="shrink font-t3-medium text-xs tabular-nums" numberOfLines={1}>
227 {props.queuedCount} queued
228 </Text>
229 </Pressable>
230 ) : null}
231 </View>
232 );
233
234 return (
235 <Animated.View
236 pointerEvents="box-none"
237 className="absolute left-0 right-0 z-20 items-center"
238 style={[{ top: -CONTROL_OVERLAY_OFFSET }, liftStyle]}
239 onLayout={(event) => setOverlayWidth(event.nativeEvent.layout.width)}
240 entering={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_ENTERING}
241 exiting={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_EXITING}
242 >
243 {hasCapsule && NATIVE_LIQUID_GLASS_SUPPORTED ? (
244 <UniwindGlassContainer
245 spacing={GLASS_MERGE_SPACING}
246 pointerEvents="box-none"
247 className="flex-row items-center gap-4"
248 >
249 <AnimatedGlassView
250 colorScheme={props.colorScheme}
251 glassEffectStyle="regular"
252 isInteractive={capsuleInteractive}
253 pointerEvents={capsuleInteractive ? "box-none" : "none"}
254 className="h-11 items-center justify-center overflow-hidden rounded-full"
255 style={capsuleStyle}
256 >
257 {capsuleContent}
258 </AnimatedGlassView>
259
260 <AnimatedGlassView
261 colorScheme={props.colorScheme}
262 glassEffectStyle={{ style: props.showScrollToEnd ? "regular" : "none", animate: true }}
263 isInteractive
264 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
265 accessibilityElementsHidden={!props.showScrollToEnd}
266 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
267 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
268 style={arrowTransformStyle}
269 >
270 <Animated.View style={arrowContentStyle}>
271 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
272 </Animated.View>
273 </AnimatedGlassView>
274 </UniwindGlassContainer>
275 ) : hasCapsule ? (
276 <View pointerEvents="box-none" className="flex-row items-center gap-4">
277 <Animated.View
278 pointerEvents={capsuleInteractive ? "box-none" : "none"}
279 className="h-11 items-center justify-center overflow-hidden rounded-full border border-border bg-glass-fallback shadow-md shadow-black/10"
280 style={capsuleStyle}
281 >
282 {capsuleContent}
283 </Animated.View>
284
285 <Animated.View
286 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
287 accessibilityElementsHidden={!props.showScrollToEnd}
288 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
289 style={[arrowTransformStyle, arrowContentStyle]}
290 >
291 <ControlPill
292 accessibilityLabel="Scroll to end"
293 activateOnPressIn
294 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
295 disabled={!props.showScrollToEnd}
296 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
297 onPress={props.onScrollToEnd}
298 />
299 </Animated.View>
300 </View>
301 ) : NATIVE_LIQUID_GLASS_SUPPORTED ? (
302 <UniwindGlassView
303 colorScheme={props.colorScheme}
304 glassEffectStyle="regular"
305 isInteractive
306 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
307 >
308 <ScrollToEndButton onPress={props.onScrollToEnd} />
309 </UniwindGlassView>
310 ) : (
311 <ControlPill
312 accessibilityLabel="Scroll to end"
313 activateOnPressIn
314 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
315 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
316 onPress={props.onScrollToEnd}
317 />
318 )}
319 </Animated.View>
320 );
321}
322
323function CompactingLabel(props: { readonly onLayout: (event: LayoutChangeEvent) => void }) {
324 return (
325 <StatusLabelRow accessibilityLabel="Compacting" className="gap-1.5" onLayout={props.onLayout}>
326 <SymbolView
327 name="arrow.down.right.and.arrow.up.left"
328 size={13}
329 tintColorClassName="foreground"
330 type="monochrome"
331 />
332 <Text className="font-t3-medium text-xs text-foreground">Compacting…</Text>
333 </StatusLabelRow>
334 );
335}
336
337function FloatingStatusLabel(props: {
338 readonly status: FloatingWorkingStatus;
339 readonly onLayout: (event: LayoutChangeEvent) => void;
340}) {
341 if (props.status.kind === "syncing") {
342 return (
343 <StatusLabelRow
344 key="syncing"
345 accessibilityLabel={props.status.label}
346 className="gap-2"
347 onLayout={props.onLayout}
348 >
349 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
350 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
351 {props.status.label}
352 </Text>
353 </StatusLabelRow>
354 );
355 }
356 if (props.status.kind === "compacting") {
357 return <CompactingLabel key="compacting" onLayout={props.onLayout} />;
358 }
359 if (props.status.kind === "child-input") {
360 return (
361 <StatusLabelRow
362 key="child-input"
363 accessibilityLabel={props.status.accessibilityLabel}
364 accessibilityRole="button"
365 className="gap-2"
366 onLayout={props.onLayout}
367 onPress={props.status.onPress}
368 >
369 <SymbolView name="text.bubble" size={13} tintColorClassName="foreground" />
370 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
371 {props.status.label}
372 </Text>
373 </StatusLabelRow>
374 );
375 }
376 if (props.status.kind === "connection") {
377 return (
378 <StatusLabelRow
379 key="connection"
380 accessibilityLabel={props.status.label}
381 accessibilityRole="button"
382 className="gap-2"
383 onLayout={props.onLayout}
384 onPress={props.status.onPress}
385 >
386 {props.status.tone === "reconnecting" ? (
387 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
388 ) : (
389 <View className="h-2 w-2 rounded-full bg-red-500" />
390 )}
391 <Text
392 className="max-w-[260px] shrink font-t3-medium text-xs text-foreground"
393 numberOfLines={1}
394 >
395 {props.status.label}
396 </Text>
397 </StatusLabelRow>
398 );
399 }
400 if (props.status.kind === "background") {
401 return (
402 <StatusLabelRow
403 key="background"
404 accessibilityLabel={props.status.accessibilityLabel}
405 className="gap-2"
406 onLayout={props.onLayout}
407 >
408 <SymbolView
409 name={props.status.waiting ? { ios: "bolt", android: "bolt" } : "terminal"}
410 size={13}
411 tintColorClassName="foreground"
412 type="monochrome"
413 />
414 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
415 {props.status.label}
416 </Text>
417 </StatusLabelRow>
418 );
419 }
420 if (props.status.kind === "goal") {
421 return (
422 <StatusLabelRow
423 key="goal"
424 accessibilityLabel={props.status.accessibilityLabel}
425 className="gap-2"
426 onLayout={props.onLayout}
427 >
428 <SymbolView name="target" size={13} tintColorClassName="foreground" type="monochrome" />
429 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
430 {props.status.label}
431 </Text>
432 </StatusLabelRow>
433 );
434 }
435 if (props.status.kind === "preparing") {
436 return (
437 <StatusLabelRow
438 key="preparing"
439 accessibilityLabel={props.status.label}
440 className="gap-1.5"
441 onLayout={props.onLayout}
442 >
443 <SymbolView
444 name="arrow.triangle.branch"
445 size={13}
446 tintColorClassName="foreground"
447 type="monochrome"
448 />
449 <ShimmeringWorkContent
450 className="flex-none"
451 textClassName="font-t3-medium"
452 compact
453 icon="arrow.triangle.branch"
454 iconSubtleColor="transparent"
455 label={props.status.label}
456 showIcon={false}
457 />
458 </StatusLabelRow>
459 );
460 }
461 return (
462 <WorkingDuration key="working" startedAt={props.status.startedAt} onLayout={props.onLayout} />
463 );
464}
465
466function StatusLabelRow(props: {
467 readonly accessibilityLabel?: string;
468 readonly accessibilityRole?: "button";
469 readonly className?: string;
470 readonly children: ReactNode;
471 readonly onLayout: (event: LayoutChangeEvent) => void;
472 readonly onPress?: () => void;
473}) {
474 const rowClassName = `h-11 flex-row items-center px-4 ${props.className ?? ""}`;
475 return (
476 <Animated.View
477 className="absolute max-w-full"
478 entering={LABEL_ENTERING}
479 exiting={LABEL_EXITING}
480 onLayout={props.onLayout}
481 >
482 {props.onPress ? (
483 <Pressable
484 accessibilityLabel={props.accessibilityLabel}
485 accessibilityRole={props.accessibilityRole}
486 className={`${rowClassName} active:opacity-70`}
487 onPress={props.onPress}
488 >
489 {props.children}
490 </Pressable>
491 ) : (
492 <View accessible accessibilityLabel={props.accessibilityLabel} className={rowClassName}>
493 {props.children}
494 </View>
495 )}
496 </Animated.View>
497 );
498}
499
500function WorkingDuration(props: {
501 readonly startedAt: string;
502 readonly onLayout: (event: LayoutChangeEvent) => void;
503}) {
504 return (
505 <StatusLabelRow onLayout={props.onLayout}>
506 <WorkingTimer startedAt={props.startedAt} />
507 </StatusLabelRow>
508 );
509}
510
511export function WorkingTimer(props: { readonly startedAt: string }) {
512 const nowMs = useVisibleSecondClock(true);
513 return (
514 <SystemText
515 className="shrink text-xs text-foreground"
516 numberOfLines={1}
517 style={{ fontVariant: ["tabular-nums"], fontWeight: "500" }}
518 >
519 Working {formatWorkingDuration(props.startedAt, nowMs)}
520 </SystemText>
521 );
522}
523
524function formatWorkingDuration(startedAt: string, nowMs: number): string {
525 const startedAtMs = Date.parse(startedAt);
526 if (!Number.isFinite(startedAtMs) || nowMs <= startedAtMs) {
527 return "0s";
528 }
529
530 const totalSeconds = Math.floor((nowMs - startedAtMs) / 1_000);
531 if (totalSeconds < 60) {
532 return `${totalSeconds}s`;
533 }
534 if (totalSeconds >= 3_600) {
535 return formatDuration(totalSeconds * 1_000);
536 }
537
538 const minutes = Math.floor(totalSeconds / 60);
539 const seconds = String(totalSeconds % 60).padStart(2, "0");
540 return `${minutes}m ${seconds}s`;
541}
542
543function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
544 return (
545 <ControlPill
546 accessibilityLabel="Scroll to end"
547 activateOnPressIn
548 className="h-11 w-11 bg-transparent"
549 disabled={props.disabled}
550 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
551 onPress={props.onPress}
552 />
553 );
554}