Open History to see the last commit
1import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
2import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react";
3import { Linking, Platform, Pressable, useWindowDimensions, View } from "react-native";
4import Animated, {
5 Easing,
6 Extrapolation,
7 FadeIn,
8 interpolate,
9 ReduceMotion,
10 useAnimatedStyle,
11 useSharedValue,
12 withTiming,
13} from "react-native-reanimated";
14import { useSafeAreaInsets } from "react-native-safe-area-context";
15import { FullWindowOverlay } from "react-native-screens";
16
17import { AppText as Text } from "../../components/ui/AppText";
18import { SymbolView } from "../../components/ui/AppSymbol";
19import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
20import { cn } from "../../lib/cn";
21import { CompactVoiceWaveform, DictationElapsedTime } from "./ComposerDictationControl";
22import { useGlobalVoiceInput } from "./VoiceInputProvider";
23import { resolveVoiceComposerPresentation } from "./voiceInputPresentation";
24
25const PILL_HEIGHT = 36;
26const COLLAPSED_WIDTH = 64;
27const EXPANDED_MAX_WIDTH = 280;
28// Clears Home's floating bottom toolbar (56) and a collapsed thread composer (60).
29const IOS_BOTTOM_CHROME_CLEARANCE = 64;
30const MORPH_TIMING = {
31 duration: 260,
32 easing: Easing.out(Easing.cubic),
33 reduceMotion: ReduceMotion.System,
34} as const;
35const ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
36
37/**
38 * Keeps an off-screen dictation reachable as a pill on the screen's trailing
39 * edge. Wraps the app content so any touch that starts outside the pill,
40 * including a scroll, collapses it without claiming the touch.
41 */
42export function GlobalVoiceInputControl(props: { readonly children: ReactNode }) {
43 const voice = useGlobalVoiceInput();
44 const collapseRef = useRef<(() => void) | null>(null);
45 const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds);
46 const visible =
47 presentation.statusLabel !== null &&
48 !(voice.ownerKey && voice.focusedOwners.has(voice.ownerKey));
49 // Mounted only while visible, so each dictation starts collapsed.
50 const pill = visible ? <EdgeDictationPill collapseRef={collapseRef} /> : null;
51 return (
52 <>
53 <View
54 className="flex-1"
55 onStartShouldSetResponderCapture={() => {
56 collapseRef.current?.();
57 return false;
58 }}
59 >
60 {props.children}
61 </View>
62 {pill && Platform.OS === "ios" ? <FullWindowOverlay>{pill}</FullWindowOverlay> : pill}
63 </>
64 );
65}
66
67function EdgeDictationPill(props: { readonly collapseRef: RefObject<(() => void) | null> }) {
68 const voice = useGlobalVoiceInput();
69 const insets = useSafeAreaInsets();
70 const { width } = useWindowDimensions();
71 const { fabSize } = useAndroidControlSizing();
72 const [expanded, setExpanded] = useState(false);
73 const progress = useSharedValue(0);
74 useEffect(() => {
75 progress.value = withTiming(expanded ? 1 : 0, MORPH_TIMING);
76 }, [expanded, progress]);
77 const { collapseRef } = props;
78 useEffect(() => {
79 if (!expanded) return;
80 collapseRef.current = () => setExpanded(false);
81 return () => {
82 collapseRef.current = null;
83 };
84 }, [collapseRef, expanded]);
85
86 const expandedWidth = Math.min(width - insets.left - insets.right - 32, EXPANDED_MAX_WIDTH);
87 // Each face keeps its final width and stays pinned to the edge, so the
88 // morphing pill reveals the expanded row instead of reflowing it.
89 const pillStyle = useAnimatedStyle(() => ({
90 width: interpolate(progress.value, [0, 1], [COLLAPSED_WIDTH, expandedWidth]),
91 }));
92 const collapsedStyle = useAnimatedStyle(() => ({
93 opacity: interpolate(progress.value, [0, 0.4], [1, 0], Extrapolation.CLAMP),
94 }));
95 const expandedStyle = useAnimatedStyle(() => ({
96 opacity: interpolate(progress.value, [0.3, 1], [0, 1], Extrapolation.CLAMP),
97 }));
98
99 // Sit just above the bottom chrome: the toolbar or composer on iOS, the
100 // new-thread FAB on Android.
101 const bottom =
102 Platform.OS === "android"
103 ? Math.max(insets.bottom, 16) + 16 + fabSize + 12
104 : Math.max(insets.bottom, 12) + IOS_BOTTOM_CHROME_CLEARANCE;
105
106 const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds);
107 const phase = voice.state.phase;
108 const isError = phase === "error";
109 const openSettings = isError && voice.state.errorAction === "settings";
110 const label = voice.label ?? "Draft";
111
112 return (
113 <View pointerEvents="box-none" className="absolute inset-0">
114 <Animated.View
115 entering={ENTERING}
116 className="absolute rounded-l-full border border-r-0 border-border bg-card shadow-md shadow-black/10"
117 style={[{ bottom, right: insets.right, height: PILL_HEIGHT }, pillStyle]}
118 >
119 <View className="flex-1 overflow-hidden rounded-l-full">
120 <Animated.View
121 accessibilityElementsHidden={expanded}
122 importantForAccessibility={expanded ? "no-hide-descendants" : "auto"}
123 pointerEvents={expanded ? "none" : "auto"}
124 className="absolute bottom-0 right-0 top-0"
125 style={[{ width: COLLAPSED_WIDTH }, collapsedStyle]}
126 >
127 <Pressable
128 accessibilityLabel={`${presentation.statusLabel}, dictating into ${label}`}
129 accessibilityHint="Shows dictation controls"
130 accessibilityRole="button"
131 className="flex-1 flex-row items-center justify-center gap-1.5 active:opacity-70"
132 hitSlop={{ top: 6, bottom: 6, left: 6 }}
133 onPress={() => setExpanded(true)}
134 >
135 {isError ? (
136 <SymbolView
137 name="exclamationmark.circle"
138 size={16}
139 tintColorClassName="accent-danger-foreground"
140 type="monochrome"
141 />
142 ) : phase === "recording" ? (
143 <>
144 <View className="size-2 rounded-full bg-danger-foreground" />
145 <DictationElapsedTime
146 className="text-foreground"
147 seconds={voice.elapsedSeconds}
148 />
149 </>
150 ) : (
151 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
152 )}
153 </Pressable>
154 </Animated.View>
155
156 <Animated.View
157 accessibilityElementsHidden={!expanded}
158 importantForAccessibility={expanded ? "auto" : "no-hide-descendants"}
159 pointerEvents={expanded ? "auto" : "none"}
160 className="absolute bottom-0 right-0 top-0 flex-row items-center gap-1.5 pl-1 pr-1.5"
161 style={[{ width: expandedWidth }, expandedStyle]}
162 >
163 <Pressable
164 accessibilityLabel={isError ? "Dismiss voice input error" : "Cancel dictation"}
165 accessibilityRole="button"
166 className="size-[30px] items-center justify-center active:opacity-70"
167 onPress={voice.cancel}
168 >
169 <SymbolView
170 name="xmark"
171 size={13}
172 tintColorClassName="accent-icon-muted"
173 type="monochrome"
174 />
175 </Pressable>
176 <Pressable
177 accessibilityLabel={isError ? (presentation.statusLabel ?? label) : label}
178 accessibilityHint="Hides dictation controls"
179 accessibilityRole="button"
180 className="min-w-0 flex-1 justify-center self-stretch active:opacity-70"
181 onPress={() => setExpanded(false)}
182 >
183 <Text
184 className={cn(
185 "text-xs",
186 isError ? "text-danger-foreground" : "font-t3-medium text-foreground",
187 )}
188 numberOfLines={1}
189 >
190 {isError ? presentation.statusLabel : label}
191 </Text>
192 </Pressable>
193 {phase === "recording" ? (
194 <>
195 <CompactVoiceWaveform audioLevels={voice.audioLevels} />
196 <DictationElapsedTime
197 className="text-foreground-muted"
198 seconds={voice.elapsedSeconds}
199 />
200 <Pressable
201 accessibilityLabel="Finish dictation"
202 accessibilityRole="button"
203 className="size-[30px] items-center justify-center active:opacity-70"
204 onPress={voice.stop}
205 >
206 <View className="size-[24px] items-center justify-center rounded-full bg-primary">
207 <SymbolView
208 name="checkmark"
209 size={12}
210 weight="semibold"
211 tintColorClassName="accent-primary-foreground"
212 type="monochrome"
213 />
214 </View>
215 </Pressable>
216 </>
217 ) : isError ? (
218 voice.isAvailable ? (
219 <Pressable
220 accessibilityLabel={openSettings ? "Open microphone settings" : "Retry dictation"}
221 accessibilityRole="button"
222 className="size-[30px] items-center justify-center active:opacity-70"
223 onPress={() => {
224 if (!openSettings) {
225 void voice.session.retry();
226 return;
227 }
228 voice.cancel();
229 void Linking.openSettings();
230 }}
231 >
232 <SymbolView name="mic" size={16} tintColorClassName="accent-icon" />
233 </Pressable>
234 ) : null
235 ) : (
236 <View
237 accessibilityLabel={presentation.statusLabel ?? undefined}
238 className="size-[30px] items-center justify-center"
239 >
240 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
241 </View>
242 )}
243 </Animated.View>
244 </View>
245 </Animated.View>
246 </View>
247 );
248}