Skip to content

Commit d39ce79

Browse files
fix(mobile): reduce thread feed scroll jank
- Disable layout slides while browsing history - Add fixed row sizing and bottom alignment for smoother scrolling
1 parent 49c0d96 commit d39ce79

2 files changed

Lines changed: 133 additions & 9 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
4848
import Animated, {
4949
FadeIn,
5050
FadeInUp,
51-
LinearTransition,
51+
useSharedValue,
52+
withTiming,
53+
type LayoutAnimationsValues,
5254
type SharedValue,
5355
} from "react-native-reanimated";
5456
import { useThemeColor } from "../../lib/useThemeColor";
@@ -91,7 +93,12 @@ import {
9193
type ThreadFeedLatestTurn,
9294
} from "../../lib/threadActivity";
9395
import type { ThreadContentPresentation } from "./threadContentPresentation";
94-
import { ThreadWorkGroupToggle, ThreadWorkLog } from "./thread-work-log";
96+
import {
97+
collapsedWorkLogHeight,
98+
ThreadWorkGroupToggle,
99+
ThreadWorkLog,
100+
WORK_GROUP_TOGGLE_HEIGHT,
101+
} from "./thread-work-log";
95102
import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
96103
import { useAssetUrl } from "../../state/assets";
97104
import { resolveWorkspaceRelativeFilePath } from "../files/filePath";
@@ -109,8 +116,19 @@ function formatMessageTime(input: string): string {
109116
}
110117

111118
// Rows shift when content above them grows (streaming text, work-log folds);
112-
// animating the container position turns those jumps into slides.
113-
const FEED_ITEM_LAYOUT_TRANSITION = LinearTransition.duration(180);
119+
// animating the container position turns those jumps into slides. Applied
120+
// conditionally — see the gated transition in ThreadFeed: while browsing
121+
// history the animation must NOT run, or every estimate→actual size
122+
// correction plays as a visible slide against the instant scroll-offset
123+
// compensation from maintainVisibleContentPosition.
124+
const FEED_ITEM_LAYOUT_DURATION_MS = 180;
125+
126+
// Pre-measurement heights for getFixedItemSize, mirroring renderFeedEntry's
127+
// classNames. Both rows hold a single text line shorter than the fixed parts,
128+
// so the height is deterministic; a drifted value costs one correction on
129+
// measure, not a persistent offset.
130+
const TURN_FOLD_HEIGHT = 56; // min-h-11 (44) + mb-3 (12)
131+
const WORKING_ROW_HEIGHT = 40; // py-1 (8) + text-xs line (16) + mb-4 (16)
114132

115133
// Entering animations must only play for rows born just now — LegendList
116134
// remounts rows when they scroll back into view, and replaying an entrance for
@@ -1410,16 +1428,52 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
14101428
},
14111429
[props.onHeaderMaterialVisibilityChange],
14121430
);
1431+
// True while the viewport sits within ~one screen of the list end — the
1432+
// only region where layout shifts should animate. Starts true because the
1433+
// list opens pinned to the end.
1434+
const nearListEnd = useSharedValue(true);
1435+
14131436
const handleScroll = useCallback(
14141437
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
14151438
// anchorTopInset, not topContentInset: under automatic insets the list
14161439
// rests at contentOffset.y = -headerHeight (the inset lives only in
14171440
// UIKit's adjustedContentInset, so topContentInset is 0 here). Add the
14181441
// header height back or the material toggles a full header too late.
14191442
reportHeaderMaterialVisibility(event.nativeEvent.contentOffset.y + anchorTopInset > 6);
1443+
const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
1444+
nearListEnd.value =
1445+
contentSize.height - layoutMeasurement.height - contentOffset.y < layoutMeasurement.height;
14201446
},
1421-
[reportHeaderMaterialVisibility, anchorTopInset],
1447+
[reportHeaderMaterialVisibility, anchorTopInset, nearListEnd],
14221448
);
1449+
1450+
// Gated variant of the 180ms feed layout slide. Instant while browsing
1451+
// history: maintainVisibleContentPosition compensates the scroll offset in
1452+
// the same frame a row's measured size lands, so an instant reposition is
1453+
// invisible — animating it is exactly what made cold upward scrolls slide
1454+
// and jump. Near the end the slide stays on: streaming growth and sends
1455+
// shift rows at rest, where the animation is the thing preventing a hard
1456+
// visual snap.
1457+
const feedItemLayoutTransition = useMemo(() => {
1458+
return (values: LayoutAnimationsValues) => {
1459+
"worklet";
1460+
const duration = nearListEnd.value ? FEED_ITEM_LAYOUT_DURATION_MS : 0;
1461+
return {
1462+
initialValues: {
1463+
originX: values.currentOriginX,
1464+
originY: values.currentOriginY,
1465+
width: values.currentWidth,
1466+
height: values.currentHeight,
1467+
},
1468+
animations: {
1469+
originX: withTiming(values.targetOriginX, { duration }),
1470+
originY: withTiming(values.targetOriginY, { duration }),
1471+
width: withTiming(values.targetWidth, { duration }),
1472+
height: withTiming(values.targetHeight, { duration }),
1473+
},
1474+
};
1475+
};
1476+
}, [nearListEnd]);
14231477
const handleViewportLayout = useCallback((event: LayoutChangeEvent) => {
14241478
const nextWidth = Math.round(event.nativeEvent.layout.width);
14251479
const nextHeight = Math.round(event.nativeEvent.layout.height);
@@ -1636,6 +1690,34 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
16361690
setExpandedImage({ uri, headers });
16371691
}, []);
16381692

1693+
// Rows whose height is known before they ever render. Without this, every
1694+
// row above the viewport is assumed to be estimatedItemSize tall, and
1695+
// scrolling up through unmeasured content corrects each row's height as it
1696+
// mounts — the feed visibly jumps. Fixed sizes make the small chrome rows
1697+
// exact; message rows stay undefined and use LegendList's per-type running
1698+
// average once one of their type has been measured.
1699+
const getFixedItemSize = useCallback(
1700+
(entry: ThreadFeedEntry) => {
1701+
switch (entry.type) {
1702+
case "turn-fold":
1703+
return TURN_FOLD_HEIGHT;
1704+
case "work-toggle":
1705+
return WORK_GROUP_TOGGLE_HEIGHT;
1706+
case "working":
1707+
return WORKING_ROW_HEIGHT;
1708+
case "activity-group":
1709+
// Expanded rows append a variable detail block — fall back to
1710+
// measurement for those groups.
1711+
return entry.activities.some((activity) => expandedWorkRows[activity.id])
1712+
? undefined
1713+
: collapsedWorkLogHeight(entry.activities);
1714+
default:
1715+
return undefined;
1716+
}
1717+
},
1718+
[expandedWorkRows],
1719+
);
1720+
16391721
const renderItem = useCallback(
16401722
(info: { item: ThreadFeedEntry; index: number }) =>
16411723
renderFeedEntry(info, {
@@ -1725,7 +1807,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
17251807
}
17261808
: { scrollIndicatorInsets: { top: topContentInset, bottom: 0 } })}
17271809
{...(anchoredEndSpace ? { anchoredEndSpace } : {})}
1728-
itemLayoutAnimation={FEED_ITEM_LAYOUT_TRANSITION}
1810+
itemLayoutAnimation={feedItemLayoutTransition}
17291811
// Patched LegendList prop (patches/@[email protected]):
17301812
// lets its scroll math clamp programmatic scrolls to -headerInset
17311813
// instead of 0, so initialScrollAtEnd/maintainScrollAtEnd on short
@@ -1769,6 +1851,10 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
17691851
getItemType={(entry) =>
17701852
entry.type === "message" ? `message:${entry.message.role}` : entry.type
17711853
}
1854+
getFixedItemSize={getFixedItemSize}
1855+
// Measure rows well before they scroll into view so estimate→actual
1856+
// corrections land offscreen instead of under the user's finger.
1857+
drawDistance={500}
17721858
keyboardShouldPersistTaps="always"
17731859
keyboardDismissMode="none"
17741860
keyboardLiftBehavior="whenAtEnd"
@@ -1788,6 +1874,11 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
17881874
// user overscroll back to the adjusted rest position.
17891875
scrollToOverflowEnabled
17901876
estimatedItemSize={180}
1877+
// Chat-style bottom alignment: when a thread is shorter than the
1878+
// viewport, pad above the content so messages rest just above the
1879+
// composer instead of under the header. No effect on threads that
1880+
// overflow the viewport (the padding clamps to zero).
1881+
alignItemsAtEnd
17911882
initialScrollAtEnd
17921883
onScroll={handleScroll}
17931884
scrollEventThrottle={16}

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ function isFreshRow(createdAt: string): boolean {
7777
return Number.isFinite(timestamp) && Date.now() - timestamp < FRESH_ROW_WINDOW_MS;
7878
}
7979

80+
// Tool-like activities with a neutral status carry no signal worth a row.
81+
export function visibleWorkLogActivities(
82+
activities: ReadonlyArray<ThreadFeedActivity>,
83+
): ReadonlyArray<ThreadFeedActivity> {
84+
return activities.filter((activity) => !(activity.toolLike && activity.status === "neutral"));
85+
}
86+
87+
// Pre-measurement heights for the feed's getFixedItemSize. Collapsed work-log
88+
// rows are single-line (numberOfLines={1}) inside a min-height taller than the
89+
// text, so their height is deterministic. Values mirror the classNames below —
90+
// keep them in sync; a mismatch only costs a one-time correction on measure.
91+
const WORK_ROW_HEIGHT = 32; // min-h-8
92+
const WORK_ROW_GAP = 1; // gap-px
93+
const WORK_LOG_HEADER_HEIGHT = 18; // "work log" label: text-2xs line (16) + pb-0.5 (2)
94+
const WORK_LOG_BOTTOM_MARGIN = 4; // mb-1
95+
96+
export const WORK_GROUP_TOGGLE_HEIGHT = 36; // min-h-8 (32) + mb-1 (4)
97+
98+
export function collapsedWorkLogHeight(activities: ReadonlyArray<ThreadFeedActivity>): number {
99+
const rows = visibleWorkLogActivities(activities);
100+
if (rows.length === 0) {
101+
return 0;
102+
}
103+
const onlyToolRows = rows.every((row) => row.toolLike);
104+
return (
105+
WORK_LOG_BOTTOM_MARGIN +
106+
(onlyToolRows ? 0 : WORK_LOG_HEADER_HEIGHT) +
107+
rows.length * WORK_ROW_HEIGHT +
108+
(rows.length - 1) * WORK_ROW_GAP
109+
);
110+
}
111+
80112
export function ThreadWorkLog(props: {
81113
readonly activities: ReadonlyArray<ThreadFeedActivity>;
82114
readonly copiedRowId: string | null;
@@ -87,9 +119,10 @@ export function ThreadWorkLog(props: {
87119
}) {
88120
const colorScheme = useColorScheme();
89121
const pressedBackground = colorScheme === "dark" ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.035)";
90-
const rows = props.activities
91-
.filter((activity) => !(activity.toolLike && activity.status === "neutral"))
92-
.map((activity) => ({ ...activity, detail: compactActivityDetail(activity.detail) }));
122+
const rows = visibleWorkLogActivities(props.activities).map((activity) => ({
123+
...activity,
124+
detail: compactActivityDetail(activity.detail),
125+
}));
93126

94127
if (rows.length === 0) {
95128
return null;

0 commit comments

Comments
 (0)