Import apps/android/src/screens/chat/expanded-composer.tsx

This commit is contained in:
QQ 2026-11-17 08:28:55 +08:00
parent cf8e99f09d
commit 50d12e45ae
1 changed files with 86 additions and 40 deletions

View File

@ -1,18 +1,19 @@
import { ArrowUp, CircleStop } from 'lucide-react-native'
import { useEffect, useMemo, useRef } from 'react'
import { ActivityIndicator, Animated, Easing, Pressable, Text, TextInput, View } from 'react-native'
import { useEffect, useMemo, useRef, useState } from 'react'
import { ActivityIndicator, Animated, Easing, Pressable, StyleSheet, Text, TextInput, View } from 'react-native'
import { strings } from '../../locales/i18n'
import type { RemoteSession } from '../../types'
import { CollapseCornerIcon } from '../../ui/composer-expand-icons'
import { triggerLightHaptic } from '../../ui/haptics'
import { splitDraftSegments } from '../../ui/mention-popover'
import { formatQuoteDraftSnippet } from '../../ui/quote-helpers'
import { QuoteMarkIcon } from '../../ui/quote-preview'
import { useTheme } from '../../ui/theme-context'
import { useThemedStyles } from '../../ui/use-themed-styles'
import { easingCurve, resolveMotion } from '../../ui/motion'
import { easingCurve } from '../../ui/motion'
import { useReduceMotion } from '../../ui/use-reduce-motion'
import { type ComposerInputBridge } from './composer'
import { createStyles } from './styles'
import type { useChatMentions } from './use-chat-mentions'
type BackendChatTextKeys = Record<
'generating' | 'messageLabel' | 'placeholder' | 'policyHint',
@ -30,16 +31,20 @@ const BACKEND_CHAT_TEXT_KEYS: Partial<Record<string, BackendChatTextKeys>> & { h
antigravity: { generating: 'antigravityGenerating', messageLabel: 'antigravityMessageLabel', placeholder: 'antigravityPlaceholder', policyHint: 'antigravityPolicyHint' },
}
type Props = {
export type ExpandedComposerProps = {
visible: boolean
onClose: () => void
backend: RemoteSession['backend']
backend?: RemoteSession['backend']
draft: string
quotes?: string[]
onRemoveQuote?: (index: number) => void
input: ReturnType<typeof useChatMentions>
input: Pick<ComposerInputBridge, 'selection' | 'onChangeText' | 'onSelectionChange'>
accessibilityLabel?: string
placeholder?: string
highlightMentionTokens?: boolean
replyActive: boolean
stopping: boolean
sending?: boolean
sendDisabled: boolean
onSubmit: () => void
onStop: () => void
@ -53,50 +58,81 @@ export function ExpandedComposer({
quotes,
onRemoveQuote,
input,
accessibilityLabel,
placeholder,
highlightMentionTokens = true,
replyActive,
stopping,
sending = false,
sendDisabled,
onSubmit,
onStop,
}: Props) {
}: ExpandedComposerProps) {
const { colors } = useTheme()
const styles = useThemedStyles(createStyles)
const { selection, onChangeText, onSelectionChange } = input
const draftSegments = useMemo(() => splitDraftSegments(draft), [draft])
const showTokenHighlight = highlightMentionTokens !== false
const draftSegments = useMemo(() => (showTokenHighlight ? splitDraftSegments(draft) : []), [draft, showTokenHighlight])
const backendTextKeys = BACKEND_CHAT_TEXT_KEYS[backend ?? 'harness'] ?? BACKEND_CHAT_TEXT_KEYS.harness
const resolvedLabel = accessibilityLabel ?? strings.chat[backendTextKeys.messageLabel]
const resolvedPlaceholder = placeholder ?? strings.chat[backendTextKeys.placeholder]
const reduceMotion = useReduceMotion()
// The composer expands in place; it is the same surface growing, not a new
// screen arriving from an edge.
const entrance = useRef(new Animated.Value(1)).current
const [mounted, setMounted] = useState(visible)
const entrance = useRef(new Animated.Value(visible ? 1 : 0)).current
useEffect(() => {
if (!visible) return
entrance.setValue(0)
if (visible) {
setMounted(true)
entrance.stopAnimation()
entrance.setValue(0)
const animation = Animated.timing(entrance, {
toValue: 1,
duration: reduceMotion ? 110 : 240,
easing: Easing.bezier(...easingCurve('expand')),
useNativeDriver: true,
})
animation.start()
return () => animation.stop()
}
if (!mounted) return
entrance.stopAnimation()
const animation = Animated.timing(entrance, {
toValue: 1,
duration: resolveMotion('fullscreen', reduceMotion).duration,
easing: Easing.bezier(...easingCurve('expand')),
toValue: 0,
duration: reduceMotion ? 90 : 170,
easing: Easing.bezier(...easingCurve('retreat')),
useNativeDriver: true,
})
animation.start()
animation.start(({ finished }) => {
if (finished) setMounted(false)
})
return () => animation.stop()
}, [visible, entrance, reduceMotion])
}, [visible, mounted, entrance, reduceMotion])
if (!visible) {
if (!mounted) {
return null
}
return (
<View style={styles.expandedComposerOverlay} pointerEvents="box-none">
<Pressable
accessibilityRole="button"
accessibilityLabel={strings.chat.collapseComposer}
onPress={onClose}
style={styles.expandedComposerBackdrop}
/>
<Animated.View style={[styles.expandedComposerBackdrop, { opacity: entrance }]}>
<Pressable
accessibilityRole="button"
accessibilityLabel={strings.chat.collapseComposer}
onPress={onClose}
style={StyleSheet.absoluteFill}
/>
</Animated.View>
<Animated.View
style={[styles.expandedComposerCard, {
opacity: entrance,
transform: [{ scale: entrance.interpolate({ inputRange: [0, 1], outputRange: [0.98, 1] }) }],
transform: [
{
translateY: entrance.interpolate({
inputRange: [0, 1],
outputRange: [reduceMotion ? 0 : 28, 0],
}),
},
],
}]}
>
<View style={styles.expandedComposerHeader}>
@ -132,25 +168,28 @@ export function ExpandedComposer({
<View style={styles.expandedInputContainer}>
<TextInput
accessibilityLabel={strings.chat[backendTextKeys.messageLabel]}
accessibilityLabel={resolvedLabel}
style={styles.expandedTextInput}
value={showTokenHighlight ? undefined : draft}
onChangeText={onChangeText}
onSelectionChange={onSelectionChange}
selection={selection}
placeholder={strings.chat[backendTextKeys.placeholder]}
placeholder={resolvedPlaceholder}
placeholderTextColor={colors.muted}
multiline
autoFocus
selectionColor={colors.accent}
cursorColor={colors.accent}
>
<Text>
{draftSegments.map((segment, index) => (
<Text key={index} style={{ color: segment.token ? colors.primary : colors.ink }}>
{segment.text}
</Text>
))}
</Text>
{showTokenHighlight ? (
<Text>
{draftSegments.map((segment, index) => (
<Text key={index} style={{ color: segment.token ? colors.primary : colors.ink }}>
{segment.text}
</Text>
))}
</Text>
) : undefined}
</TextInput>
</View>
@ -161,7 +200,10 @@ export function ExpandedComposer({
accessibilityLabel={strings.chat.stop}
accessibilityState={{ disabled: stopping, busy: stopping }}
disabled={stopping}
onPress={onStop}
onPress={() => {
triggerLightHaptic()
onStop()
}}
style={({ pressed }) => [styles.stopButton, pressed && !stopping && styles.stopPressed, stopping && styles.sendDisabled]}
>
{stopping ? (
@ -174,15 +216,19 @@ export function ExpandedComposer({
<Pressable
accessibilityRole="button"
accessibilityLabel={strings.chat.send}
accessibilityState={{ disabled: sendDisabled }}
accessibilityState={{ disabled: sendDisabled, busy: sending }}
disabled={sendDisabled}
onPress={() => {
onSubmit()
onClose()
}}
style={({ pressed }) => [styles.sendButton, pressed && styles.sendPressed, sendDisabled && styles.sendDisabled]}
style={({ pressed }) => [styles.sendButton, pressed && styles.sendPressed, !sending && sendDisabled && styles.sendDisabled]}
>
<ArrowUp size={20} color={sendDisabled ? colors.subtle : colors.white} strokeWidth={2.4} />
{sending ? (
<ActivityIndicator size="small" color={colors.onInverse ?? colors.white} />
) : (
<ArrowUp size={20} color={sendDisabled ? colors.subtle : (colors.onInverse ?? colors.white)} strokeWidth={2.4} />
)}
</Pressable>
)}
</View>