|
1 | 1 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
2 | | -import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react"; |
| 2 | +import { useState, type ReactNode } from "react"; |
3 | 3 | import { Linking, Platform, Pressable, useWindowDimensions, View } from "react-native"; |
4 | 4 | import Animated, { |
5 | 5 | Easing, |
@@ -8,7 +8,7 @@ import Animated, { |
8 | 8 | interpolate, |
9 | 9 | ReduceMotion, |
10 | 10 | useAnimatedStyle, |
11 | | - useSharedValue, |
| 11 | + useDerivedValue, |
12 | 12 | withTiming, |
13 | 13 | } from "react-native-reanimated"; |
14 | 14 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
@@ -35,18 +35,22 @@ const ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System); |
35 | 35 |
|
36 | 36 | export function GlobalVoiceInputControl(props: { readonly children: ReactNode }) { |
37 | 37 | const voice = useGlobalVoiceInput(); |
38 | | - const collapseRef = useRef<(() => void) | null>(null); |
| 38 | + const [expanded, setExpanded] = useState(false); |
39 | 39 | const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds); |
40 | 40 | const visible = |
41 | 41 | presentation.statusLabel !== null && |
42 | 42 | !(voice.ownerKey && voice.focusedOwners.has(voice.ownerKey)); |
43 | | - const pill = visible ? <EdgeDictationPill collapseRef={collapseRef} /> : null; |
| 43 | + // A pill that reappears starts collapsed. |
| 44 | + if (!visible && expanded) setExpanded(false); |
| 45 | + const pill = visible ? ( |
| 46 | + <EdgeDictationPill expanded={expanded} onExpandedChange={setExpanded} /> |
| 47 | + ) : null; |
44 | 48 | return ( |
45 | 49 | <> |
46 | 50 | <View |
47 | 51 | className="flex-1" |
48 | 52 | onStartShouldSetResponderCapture={() => { |
49 | | - collapseRef.current?.(); |
| 53 | + if (expanded) setExpanded(false); |
50 | 54 | return false; |
51 | 55 | }} |
52 | 56 | > |
@@ -57,24 +61,16 @@ export function GlobalVoiceInputControl(props: { readonly children: ReactNode }) |
57 | 61 | ); |
58 | 62 | } |
59 | 63 |
|
60 | | -function EdgeDictationPill(props: { readonly collapseRef: RefObject<(() => void) | null> }) { |
| 64 | +function EdgeDictationPill(props: { |
| 65 | + readonly expanded: boolean; |
| 66 | + readonly onExpandedChange: (expanded: boolean) => void; |
| 67 | +}) { |
61 | 68 | const voice = useGlobalVoiceInput(); |
62 | 69 | const insets = useSafeAreaInsets(); |
63 | 70 | const { width } = useWindowDimensions(); |
64 | 71 | const { fabSize } = useAndroidControlSizing(); |
65 | | - const [expanded, setExpanded] = useState(false); |
66 | | - const progress = useSharedValue(0); |
67 | | - useEffect(() => { |
68 | | - progress.set(withTiming(expanded ? 1 : 0, MORPH_TIMING)); |
69 | | - }, [expanded, progress]); |
70 | | - const { collapseRef } = props; |
71 | | - useEffect(() => { |
72 | | - if (!expanded) return; |
73 | | - collapseRef.current = () => setExpanded(false); |
74 | | - return () => { |
75 | | - collapseRef.current = null; |
76 | | - }; |
77 | | - }, [collapseRef, expanded]); |
| 72 | + const { expanded, onExpandedChange: setExpanded } = props; |
| 73 | + const progress = useDerivedValue(() => withTiming(expanded ? 1 : 0, MORPH_TIMING)); |
78 | 74 |
|
79 | 75 | const expandedWidth = Math.min(width - insets.left - insets.right - 32, EXPANDED_MAX_WIDTH); |
80 | 76 | const pillStyle = useAnimatedStyle(() => ({ |
|