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