The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 2 | import { useGT, useMessages } from "gt-react-native"; |
| 3 | import { useState, type ReactNode } from "react"; |
| 4 | import { Linking, Platform, Pressable, useWindowDimensions, View } from "react-native"; |
| 5 | import Animated, { |
| 6 | Easing, |
| 7 | Extrapolation, |
| 8 | FadeIn, |
| 9 | interpolate, |
| 10 | ReduceMotion, |
| 11 | useAnimatedStyle, |
| 12 | useDerivedValue, |
| 13 | withTiming, |
| 14 | } from "react-native-reanimated"; |
| 15 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 16 | import { FullWindowOverlay } from "react-native-screens"; |
| 17 | |
| 18 | import { AppText as Text } from "../../components/ui/AppText"; |
| 19 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 20 | import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing"; |
| 21 | import { cn } from "../../lib/cn"; |
| 22 | import { CompactVoiceWaveform, DictationElapsedTime } from "./ComposerDictationControl"; |
| 23 | import { useGlobalVoiceInput } from "./VoiceInputProvider"; |
| 24 | import { resolveVoiceComposerPresentation } from "./voiceInputPresentation"; |
| 25 | |
| 26 | const PILL_HEIGHT = 36; |
| 27 | const COLLAPSED_WIDTH = 64; |
| 28 | const EXPANDED_MAX_WIDTH = 280; |
| 29 | const IOS_BOTTOM_CHROME_CLEARANCE = 64; |
| 30 | const MORPH_TIMING = { |
| 31 | duration: 260, |
| 32 | easing: Easing.out(Easing.cubic), |
| 33 | reduceMotion: ReduceMotion.System, |
| 34 | } as const; |
| 35 | const ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System); |
| 36 | |
| 37 | export 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 | |
| 70 | function 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("Draft", { $context: "noun, the draft being dictated into" }); |
| 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}, dictating into {destination}", { |
| 126 | status: presentation.statusLabel, |
| 127 | destination: label, |
| 128 | })} |
| 129 | accessibilityHint={gt("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={ |
| 165 | isError ? gt("Dismiss voice input error") : gt("Cancel dictation") |
| 166 | } |
| 167 | accessibilityRole="button" |
| 168 | className="size-[30px] items-center justify-center active:opacity-70" |
| 169 | onPress={voice.cancel} |
| 170 | > |
| 171 | <SymbolView |
| 172 | name="xmark" |
| 173 | size={13} |
| 174 | tintColorClassName="accent-icon-muted" |
| 175 | type="monochrome" |
| 176 | /> |
| 177 | </Pressable> |
| 178 | <Pressable |
| 179 | accessibilityLabel={isError ? (presentation.statusLabel ?? label) : label} |
| 180 | accessibilityHint={gt("Hides dictation controls")} |
| 181 | accessibilityRole="button" |
| 182 | className="min-w-0 flex-1 justify-center self-stretch active:opacity-70" |
| 183 | onPress={() => setExpanded(false)} |
| 184 | > |
| 185 | <Text |
| 186 | className={cn( |
| 187 | "text-xs", |
| 188 | isError ? "text-danger-foreground" : "font-t3-medium text-foreground", |
| 189 | )} |
| 190 | numberOfLines={1} |
| 191 | > |
| 192 | {isError ? presentation.statusLabel : label} |
| 193 | </Text> |
| 194 | </Pressable> |
| 195 | {phase === "recording" ? ( |
| 196 | <> |
| 197 | <CompactVoiceWaveform audioLevels={voice.audioLevels} /> |
| 198 | <DictationElapsedTime |
| 199 | className="text-foreground-muted" |
| 200 | seconds={voice.elapsedSeconds} |
| 201 | /> |
| 202 | <Pressable |
| 203 | accessibilityLabel={gt("Finish dictation")} |
| 204 | accessibilityRole="button" |
| 205 | className="size-[30px] items-center justify-center active:opacity-70" |
| 206 | onPress={voice.stop} |
| 207 | > |
| 208 | <View className="size-[24px] items-center justify-center rounded-full bg-primary"> |
| 209 | <SymbolView |
| 210 | name="checkmark" |
| 211 | size={12} |
| 212 | weight="semibold" |
| 213 | tintColorClassName="accent-primary-foreground" |
| 214 | type="monochrome" |
| 215 | /> |
| 216 | </View> |
| 217 | </Pressable> |
| 218 | </> |
| 219 | ) : isError ? ( |
| 220 | voice.isAvailable ? ( |
| 221 | <Pressable |
| 222 | accessibilityLabel={ |
| 223 | openSettings ? gt("Open microphone settings") : gt("Retry dictation") |
| 224 | } |
| 225 | accessibilityRole="button" |
| 226 | className="size-[30px] items-center justify-center active:opacity-70" |
| 227 | onPress={() => { |
| 228 | if (!openSettings) { |
| 229 | void voice.session.retry(); |
| 230 | return; |
| 231 | } |
| 232 | voice.cancel(); |
| 233 | void Linking.openSettings(); |
| 234 | }} |
| 235 | > |
| 236 | <SymbolView name="mic" size={16} tintColorClassName="accent-icon" /> |
| 237 | </Pressable> |
| 238 | ) : null |
| 239 | ) : ( |
| 240 | <View |
| 241 | accessibilityLabel={presentation.statusLabel ?? undefined} |
| 242 | className="size-[30px] items-center justify-center" |
| 243 | > |
| 244 | <AppActivityIndicator size="small" colorClassName="accent-icon-muted" /> |
| 245 | </View> |
| 246 | )} |
| 247 | </Animated.View> |
| 248 | </View> |
| 249 | </Animated.View> |
| 250 | </View> |
| 251 | ); |
| 252 | } |