Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

merge: preserve viewport-gated previews with smooth group scrolling

Chrise3989810d84b
diff --cc apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx
index 1207ff4ded,931715a5f1..545698c765
--- a/apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx
+++ b/apps/mobile/src/__tests__/BotGroupChatScreen.test.tsx
@@@ -40,32 -34,11 +42,33 @@@ const h = vi.hoisted(() => (
vi.mock('expo-web-browser', () => ({ openBrowserAsync: vi.fn(async () => ({})) }));
vi.mock('@/components/ChatReplyIcon', () => ({ ChatReplyIcon: () => null }));
+// Native animation is covered by useSmoothMessageFollow/AnimatedContentHeight.
+// This screen test exercises ownership, visible range, and read receipts.
+vi.mock('react-native-reanimated', async () => ({ default: { ScrollView: (await import('react-native')).ScrollView } }));
+vi.mock('@/session/NativeChatViewport', () => ({
+ get nativeChatViewportAvailable() { return h.nativeChatViewportAvailable; },
+ get nativeAndroidKeyboardViewportAvailable() { return h.nativeChatViewportAvailable && h.platform.OS === 'android'; },
+ NativeChatViewport: ({ children, ...props }: any) => { h.nativeChatViewport = props; return children; },
+ NativeAndroidComposerAnchor: ({ children, followKeyboard }: any) => el('div', { 'data-native-keyboard-follow': String(!!followKeyboard) }, children),
+}));
+vi.mock('@/session/useSmoothMessageFollow', () => ({ useSmoothMessageFollow: () => ({
+ scrollRef: useRef(null), nativeScrollRef: useRef({ scrollTo: h.scrollTo }), move: h.moveTail, cancel: h.cancelTail,
+}) }));
+vi.mock('@/session/AnimatedContentHeight', () => ({ AnimatedContentHeight: ({ open = true, children }: any) => open ? children : null }));
+vi.mock('@/session/ComposerDockKeyboardFollow', () => ({ DockKeyboardLift: ({ children, ...props }: any) => {
+ h.keyboardLift = props; return el('div', { 'data-keyboard-lift': String(props.active) }, children);
+} }));
+vi.mock('@/session/NativeKeyboardViewport', () => ({
+ get nativeKeyboardViewportAvailable() { return h.nativeViewportAvailable && h.platform.OS === 'ios'; },
+ NativeKeyboardViewport: ({ children, ...props }: any) => { h.nativeViewport = props; return children; },
+}));
vi.mock('react-native', () => {
- const box = (tag: string) => ({ children, testID, accessibilityLabel, style, onLayout }: any) =>
- el(tag, { 'data-testid': testID, 'aria-label': accessibilityLabel,
+ const box = (tag: string) => ({ children, testID, accessibilityLabel, style, onLayout }: any) => {
+ if (testID === 'botGroup.flowHeader') h.flowHeaderLayout = onLayout;
+ return el(tag, { 'data-testid': testID, 'aria-label': accessibilityLabel,
+ ref: (node: any) => { if (node) node.nativeLayout = onLayout; },
style: { opacity: [style].flat().filter(Boolean).findLast((item: any) => item.opacity !== undefined)?.opacity } }, children);
+ };
return {
View: box('div'), ScrollView: ({ ref, children, ...props }: any) => {
h.scroll = props;
@@@ -314,9 -289,8 +319,10 @@@ beforeEach(() =>
h.management = null;
vi.clearAllMocks();
h.uuid = 0; h.inputs = {}; h.teammateQuery = ''; h.teammates = { rows: [], loading: false, failed: false };
+ h.appState = 'active'; h.appStateListeners.clear();
h.platform.OS = 'ios'; h.focused = true; h.keyboardAvoidingView = null; h.headerBlur = []; h.headerFade = [];
+ h.nativeViewportAvailable = false; h.nativeViewport = null; h.keyboardLift = null;
+ h.nativeChatViewportAvailable = false; h.nativeChatViewport = null;
h.chat = { reload: vi.fn(), act: vi.fn(async () => ({ effects: [] })), online: true };
h.discarded = []; h.deleted = []; h.leaveGuard = []; h.leaveDecision = async () => false;
h.local = {
@@@ -1499,12 -1380,56 +1505,87 @@@ describe('direct server group presentat
expect(byId('botGroup.settings.delete')?.disabled).toBe(true);
expect(byId('botGroup.settings.replyMode.mentioned')?.disabled).toBe(true);
});
- it('reauthorizes a server attachment on tap and opens the existing image viewer', async () => {
+ // PR #7145 r4371434691: a pushed task leaves the group mounted. Unit,
+ // real page + native layout/focus/AppState events; old tests only scrolled rows.
+ it.each(['ios', 'android'])('suspends previews on blur/background and restores the viewport (%s)', async platform => {
+ h.platform.OS = platform;
+ const attachment = (id: string) => ({ id, name: `${id}.png`, category: 'file' as const, mimeType: '', size: 0, url: null, path: null });
+ const media = vi.fn(async (id: string) => ({ ...attachment(id), category: 'image' as const, url: `https://media.example.invalid/${id}` }));
+ h.chat = { ...h.chat, server: true, media };
+ const data = (count: number) => group({ messages: Array.from({ length: count }, (_, n) => message(`m${n}`, n + 1,
+ { authorKind: 'user', isSelf: false, attachments: [attachment(`image-${n}`)] })), plans: [], openPlan: null });
+ const show = async (count: number) => {
+ await render(data(count));
+ await act(async () => {
+ h.scroll.onLayout({ nativeEvent: { layout: { height: 500 } } });
+ for (const strip of node.querySelectorAll('[data-testid="attachmentStrip"]')) {
+ let row = strip as HTMLElement & { nativeLayout?: (event: unknown) => void };
+ while (!row.nativeLayout) row = row.parentElement as typeof row;
+ row.nativeLayout!({ nativeEvent: { layout: { y: 0, height: 100 } } });
+ }
+ });
+ };
+ await show(1); expect(media).toHaveBeenCalledTimes(1);
+ h.focused = false;
+ await show(2); expect(media).toHaveBeenCalledTimes(1);
+ h.focused = true;
+ await show(2); expect(media).toHaveBeenCalledTimes(2);
+ await act(async () => { h.appState = 'background'; h.appStateListeners.forEach(listener => listener(h.appState)); });
+ await show(3); expect(media).toHaveBeenCalledTimes(2);
+ await act(async () => { h.appState = 'active'; h.appStateListeners.forEach(listener => listener(h.appState)); });
+ expect(media).toHaveBeenCalledTimes(3);
+ expect(byId('attachment.image')?.getAttribute('data-uri')).toBe('https://media.example.invalid/image-0');
+ });
+
++ // Merge #7123/#7145: native IME changes occupancy without shrinking ScrollView.
++ // Unit, real screen + preview resolver: an obscured image must stay unloaded
++ // until keyboard dismissal exposes it, even if Android reports zero inset.
++ it.each(['ios', 'android'])('authorizes previews using the native keyboard viewport (%s)', async platform => {
++ h.platform.OS = platform;
++ h.nativeViewportAvailable = platform === 'ios';
++ h.nativeChatViewportAvailable = platform === 'android';
++ const attachment = { id: 'media', name: 'picture.png', mimeType: 'image/png', size: 10,
++ category: 'image' as const, url: 'https://media.example.invalid/one-use', path: null };
++ h.chat = { ...h.chat, server: true, media: vi.fn(async () => attachment) };
++ await render(group({ messages: [message('picture', 1, { authorKind: 'user', isSelf: false,
++ attachments: [{ ...attachment, url: null, category: 'file' }] })], plans: [], openPlan: null }));
++ const keyboard = (bottom: number) => {
++ if (platform === 'ios') h.nativeViewport.onKeyboardTransition({ nativeEvent: { active: false, bottom } });
++ else h.nativeChatViewport.onViewportInsetChange({ nativeEvent: { bottom, viewportHeight: 1000 - bottom, offsetY: 0 } });
++ };
++ let row = byId('attachmentStrip') as HTMLElement & { nativeLayout?: (event: unknown) => void };
++ while (!row.nativeLayout) row = row.parentElement as typeof row;
++ await act(async () => {
++ h.scroll.onLayout({ nativeEvent: { layout: { height: 1000 } } });
++ keyboard(300);
++ row.nativeLayout!({ nativeEvent: { layout: { y: 800, height: 100 } } });
++ h.scroll.onScroll({ nativeEvent: { contentOffset: { y: 0 }, contentSize: { height: 1200 },
++ layoutMeasurement: { height: 1000 }, contentInset: { bottom: platform === 'ios' ? 300 : 0 } } });
++ });
++ expect(h.chat.media).not.toHaveBeenCalled();
++ await act(async () => keyboard(0));
++ expect(h.chat.media).toHaveBeenCalledExactlyOnceWith('media', expect.any(AbortSignal));
++ expect(byId('attachment.image')?.getAttribute('data-uri')).toBe(attachment.url);
++ });
++
+ it.each([undefined, 'thread-root'])('gates server previews by the viewport and reauthorizes on tap (thread=%s)', async (threadRootId) => {
const attachment = { id: 'media', name: 'picture.png', mimeType: 'image/png', size: 10, category: 'image' as const, url: 'https://media.example.invalid/one-use', path: null };
h.chat = { ...h.chat, server: true, media: vi.fn(async () => attachment) };
- await render(group({ messages: [message('picture', 1, { authorKind: 'user', isSelf: false, attachments: [{ ...attachment, url: null, category: 'file' }] })], plans: [], openPlan: null }));
+ await render(group({ messages: [message('picture', 1, { authorKind: 'user', isSelf: false, attachments: [{ ...attachment, url: null, category: 'file' }] })], plans: [], openPlan: null }), 'ready', threadRootId);
+ expect(h.chat.media).not.toHaveBeenCalled();
+ let row = byId('attachmentStrip') as HTMLElement & { nativeLayout?: (event: unknown) => void };
+ while (!row.nativeLayout) row = row.parentElement as typeof row;
+ await act(async () => {
+ row.nativeLayout!({ nativeEvent: { layout: { y: 800, height: 200 } } });
+ h.scroll.onLayout({ nativeEvent: { layout: { height: 500 } } });
+ });
expect(h.chat.media).not.toHaveBeenCalled();
- await click('attachment.file'); expect(h.chat.media).toHaveBeenCalledExactlyOnceWith('media');
+ await act(async () => h.scroll.onScroll({ nativeEvent: {
+ contentOffset: { y: 700 }, contentSize: { height: 1000 }, layoutMeasurement: { height: 500 },
+ } }));
+ expect(h.chat.media).toHaveBeenCalledExactlyOnceWith('media', expect.any(AbortSignal));
+ expect(byId('attachment.image')?.getAttribute('data-uri')).toBe(attachment.url);
+ await click('attachment.image'); expect(h.chat.media).toHaveBeenCalledTimes(2);
expect(byId('lightbox')?.getAttribute('data-url')).toBe(attachment.url);
});
});
diff --cc apps/mobile/src/session/BotGroupChatScreen.tsx
index 624f97efc5,dff980bf66..b7d6d892c3
--- a/apps/mobile/src/session/BotGroupChatScreen.tsx
+++ b/apps/mobile/src/session/BotGroupChatScreen.tsx
@@@ -81,9 -74,10 +81,10 @@@ import
} from './BotGroupAvatars';
import { CHAT_HEADER_MARK_SIZE, CHAT_HEADER_BLUR_FADE_INSIDE, ChatIdentityHeader } from './ChatIdentityHeader';
import { SessionHeaderNativeBlur } from './SessionHeaderNativeControls';
-import { CompanionEntering, CompanionFadeIn } from './CompanionEntering';
+import { CompanionEntering, CompanionFadeIn, useMessageEntrances } from './CompanionEntering';
import { BotGroupComposer, type BotGroupSendInput } from './BotGroupComposer';
import { BotGroupMessageAttachments } from './BotGroupMessageAttachments';
+ import { MessageListVisibilityContext, useScrollViewMessageVisibility } from './messageListVisibility';
import { BotGroupTaskCard } from './BotGroupTaskCard';
import { BotGroupMarkdownText, BotGroupUserText } from './BotGroupMessageText';
import { BotGroupQuote } from './BotGroupQuote';
@@@ -178,6 -148,15 +179,12 @@@ export function BotGroupChatScreen({ de
const [highlightId, setHighlightId] = useState('');
const jumpAttempts = useRef(0);
const messagePositions = useRef(new Map<string, number>());
- const group = chat.state.kind === 'ready' ? chat.state.group : null;
- const focused = useIsFocused();
+ const [foreground, setForeground] = useState(() => AppState.currentState === 'active');
+ const previewVisibility = useScrollViewMessageVisibility(
+ `${accountGeneration}:${deviceId}:${groupId}:${threadRootId ?? ''}`,
+ useMemo(() => group?.messages.map(message => message.id) ?? [], [group?.messages]),
+ focused && foreground,
+ );
- const viewportOwner = `${user?.id ?? ''}:${accountGeneration}:${deviceId}:${groupId}:${threadRootId ?? 'main'}`;
const [seen, setSeen] = useState<{ owner: string; sequence: string } | null>(null);
const latestSequence = group?.messages.reduce((latest, message) => {
const position = message.serverSequence ?? String(message.sequence);
@@@ -393,19 -361,13 +403,20 @@@
};
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
- const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
- previewVisibility.setViewport(contentOffset.y, layoutMeasurement.height);
- if (stickToBottom.current) setSeen(previous => previous?.owner === viewportOwner && previous.sequence === latestSequence
+ const metrics = mobileNativeScrollMetrics(androidComposerSurfaceId ? {
+ ...event.nativeEvent,
+ contentInset: { ...event.nativeEvent.contentInset, bottom: keyboardInset.current },
+ } : event.nativeEvent);
+ rawViewportHeight.current = event.nativeEvent.layoutMeasurement.height;
+ if (!androidComposerSurfaceId) keyboardInset.current = event.nativeEvent.contentInset?.bottom ?? 0;
+ Object.assign(viewport.current, { measuredGroup: group, ...metrics });
++ previewVisibility.setViewport(metrics.offsetY, metrics.viewportHeight);
+ const nearEnd = metrics.contentHeight - metrics.offsetY - metrics.viewportHeight < STICK_TO_BOTTOM_PX;
+ // A size-change scroll event can arrive before followTail. Only a user's
+ // gesture can give up follow intent; programmatic correction cannot.
+ if (userScrolling.current || nearEnd) setFollowIntent(nearEnd);
+ if (nearEnd) setSeen(previous => previous?.owner === viewportOwner && previous.sequence === latestSequence
? previous : { owner: viewportOwner, sequence: latestSequence });
- Object.assign(viewport.current, { measuredGroup: group, contentHeight: contentSize.height,
- offsetY: contentOffset.y, viewportHeight: layoutMeasurement.height });
- stickToBottom.current = contentSize.height - contentOffset.y - layoutMeasurement.height < STICK_TO_BOTTOM_PX;
acknowledge();
};
@@@ -461,55 -422,23 +472,59 @@@
</MainWindowEmptyState>
</View>;
} else {
- body = <KeyboardAvoidingView
+ body = <NativeKeyboardViewport style={styles.flex} enabled={nativeKeyboardViewportAvailable && focused}
+ followLatest={followingLatest}
+ onKeyboardTransition={({ nativeEvent }) => {
+ keyboardTransition.current = nativeEvent.active;
+ cancelTail();
+ if (!nativeEvent.active) {
+ keyboardInset.current = nativeEvent.bottom;
+ viewport.current.viewportHeight = Math.max(0, rawViewportHeight.current - nativeEvent.bottom);
++ previewVisibility.setViewport(viewport.current.offsetY, viewport.current.viewportHeight);
+ followTail();
+ acknowledge();
+ }
+ }}>
+ <NativeChatViewport style={styles.flex} active={focused} followLatest={followingLatest}
+ composerSurfaceId={androidComposerSurfaceId}
+ onViewportInsetChange={({ nativeEvent }) => {
+ keyboardInset.current = nativeEvent.bottom;
+ Object.assign(viewport.current, { viewportHeight: nativeEvent.viewportHeight, offsetY: nativeEvent.offsetY });
++ previewVisibility.setViewport(nativeEvent.offsetY, nativeEvent.viewportHeight);
+ acknowledge();
+ }}>
+ <KeyboardAvoidingView
style={styles.flex}
- enabled={Platform.OS === 'ios' || Platform.OS === 'android'}
+ enabled={!nativeKeyboardViewportAvailable && !androidComposerSurfaceId && (Platform.OS === 'ios' || Platform.OS === 'android')}
+ // The nested viewport starts below Android's in-flow header. KAV's
+ // local frame starts at zero, but keyboard coordinates are screen-based.
+ keyboardVerticalOffset={floatingHeader ? 0 : insets.top + headerHeight}
behavior={keyboardAvoidingBehaviorForPlatform(
Platform.OS === 'ios' ? 'ios' : Platform.OS === 'android' ? 'android' : 'web',
)}
>
{floatingHeader ? null : offlineNote}
{group.dissolvedAt ? <Text style={styles.offline}>{t('groupChat.lifecycle.dissolved')}</Text> : null}
- <View style={styles.flex}>
+ <View style={[styles.flex, { overflow: 'hidden' }]}>
+ <MessageListVisibilityContext.Provider value={previewVisibility}>
- <ScrollView ref={scrollRef} style={styles.flex} contentContainerStyle={[styles.timeline, floatingHeader && { paddingTop: headerHeight + spacing.md }]}
+ <Reanimated.ScrollView ref={scrollRef} style={styles.flex} contentContainerStyle={[styles.timeline, floatingHeader && { paddingTop: headerHeight + spacing.md }]}
scrollIndicatorInsets={floatingHeader ? { top: headerHeight } : undefined} keyboardShouldPersistTaps="handled"
keyboardDismissMode="interactive"
- maintainVisibleContentPosition={chat.server ? { minIndexForVisible: group.hasMoreBefore ? 1 : 0 } : undefined}
- onScroll={onScroll} scrollEventThrottle={64}
- onLayout={event => { viewport.current.viewportHeight = event.nativeEvent.layout.height; previewVisibility.setHeight(event.nativeEvent.layout.height); acknowledge(); }}
+ maintainVisibleContentPosition={chat.server && !stickToBottom.current ? { minIndexForVisible: group.hasMoreBefore ? 1 : 0 } : undefined}
+ onTouchStart={() => { touchingTimeline.current = true; cancelTail(); }}
+ onTouchEnd={() => { touchingTimeline.current = false; followTail(); }}
+ onTouchCancel={() => { touchingTimeline.current = false; }}
+ onScrollBeginDrag={() => { userScrolling.current = true; setFollowIntent(false); cancelTail(); }}
+ onScrollEndDrag={event => { onScroll(event); userScrolling.current = false; followTail(); }}
+ onMomentumScrollBegin={() => { userScrolling.current = true; }}
+ onMomentumScrollEnd={event => { onScroll(event); userScrolling.current = false; followTail(); }}
+ onScroll={onScroll} scrollEventThrottle={16}
+ onLayout={event => {
+ rawViewportHeight.current = event.nativeEvent.layout.height;
+ viewport.current.viewportHeight = Math.max(0, rawViewportHeight.current - keyboardInset.current);
++ previewVisibility.setViewport(viewport.current.offsetY, viewport.current.viewportHeight);
+ followTail(); acknowledge();
+ }}
onContentSizeChange={(_width, height) => {
viewport.current.contentHeight = height;
viewport.current.measuredGroup = group;
@@@ -531,12 -461,9 +547,13 @@@
onPlanAction={(action, planId) => void runPlanAction(action, planId)}
onEditStep={(planId, step, action, botId) => void editPlanStep(planId, step, action, botId)}
onInteractionError={(message) => { if (message) Alert.alert(message); }} />
- </ScrollView>
+ </Reanimated.ScrollView>
+ </MessageListVisibilityContext.Provider>
- {newMessageCount > 0 ? <View style={styles.newMessages} accessibilityLiveRegion="polite">
+ {newMessageCount > 0 ? <NativeAndroidComposerAnchor surfaceId="" followKeyboard={!!androidComposerSurfaceId}
+ pointerEvents="box-none" style={StyleSheet.absoluteFill}>
+ <DockKeyboardLift active={nativeKeyboardViewportAvailable} restingBottom={insets.bottom} keyboardGap={0}
+ pointerEvents="box-none" style={StyleSheet.absoluteFill}>
+ <View style={styles.newMessages} accessibilityLiveRegion="polite">
<CompanionFadeIn play distance={spacing.xs}>
<MainWindowActionButton density="compact" style={styles.newMessagesButton} action={{
label: t('groupChat.timeline.newMessages', { count: newMessageCount }),
@@@ -650,11 -564,10 +667,11 @@@ export function BotGroupTimeline(
// G2: the same teammate speaking again inside one time group keeps its avatar and name once.
const continued = groupTime === undefined && message.kind === 'message' && message.authorKind === 'bot'
&& previous?.kind === 'message' && previous.authorKind === 'bot' && previous.authorBotId === message.authorBotId;
- return <View key={message.id} style={[styles.item, highlightId === message.id && styles.locatedMessage]}
+ return <View key={message.id} style={highlightId === message.id && styles.locatedMessage}
- onLayout={event => onMessageLayout?.(message.id, event.nativeEvent.layout.y)}>
+ onLayout={event => onMessageLayout?.(message.id, event.nativeEvent.layout.y, event.nativeEvent.layout.height)}>
+ <CompanionEntering id={message.id} arrivedAt={enteringMessageKeys.get(message.id)}>
+ <View style={[styles.item, (index > 0 || group.hasMoreBefore) && { paddingTop: spacing.lg }]}>
{groupTime !== undefined ? <Text style={styles.time}>{formatBotMessageGroupTime(groupTime, i18n.language)}</Text> : null}
- <CompanionEntering id={message.id} createdAt={message.createdAt} kind={message.authorKind === 'user' ? 'send' : 'reply'}>
<BotGroupTimelineItem message={message} continued={continued} member={message.authorBotId ? memberById.get(message.authorBotId) : undefined}
quoteDisplay={message.quote ? resolveBotGroupQuote(message.quote, messages) : undefined}
onLocateQuote={message.quote && onLocateMessage ? () => onLocateMessage(message.quote!.messageId) : undefined}