Skip to content

Commit 04de140

Browse files
shivamhwpclaude
andcommitted
fix(mobile): wrapped file viewer reaches far requested lines
Without getItemLayout, VirtualizedList ends its content at the highest measured row, so the native scroll clamped each estimated jump there and retries crawled one render batch at a time. Reserve the estimated offset as bottom padding while the jump is pending, re-aim when the list lays out, and release the padding once the jump settles or the user drags. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5b995ee commit 04de140

3 files changed

Lines changed: 86 additions & 15 deletions

File tree

‎apps/mobile/src/features/files/SourceFileSurface.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
248248
const { normalizedContents, lines, status, targetIndex, tokens } = useSourceFileModel(props);
249249
const selectableTokens = props.selectable ? boundedSelectableSourceTokens(tokens) : null;
250250
const listRef = useRef<FlatList<string>>(null);
251-
const [lineScroll] = useState(createSourceLineScroll);
251+
const [tailReserve, setTailReserve] = useState(0);
252+
const [lineScroll] = useState(() => createSourceLineScroll(setTailReserve));
252253
const { isPullRefreshing, handlePullToRefresh } = useSourceFileRefresh(props.onRefresh);
253254
const refreshControl = props.onRefresh ? (
254255
<RefreshControl refreshing={isPullRefreshing} onRefresh={() => void handlePullToRefresh()} />
@@ -377,10 +378,11 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
377378
})}
378379
contentContainerStyle={{
379380
minWidth: codeWordBreak ? undefined : "100%",
380-
paddingBottom: codeSurface.rowHeight,
381+
paddingBottom: codeSurface.rowHeight + tailReserve,
381382
paddingTop: 8,
382383
}}
383384
renderItem={renderLine}
385+
onContentSizeChange={lineScroll.onContentSizeChange}
384386
onScrollToIndexFailed={lineScroll.onScrollToIndexFailed}
385387
onScrollBeginDrag={lineScroll.cancel}
386388
/>

‎apps/mobile/src/features/files/source-file-scroll.test.ts‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import { createSourceLineScroll } from "./source-file-scroll";
44

55
const ROW_HEIGHT = 24;
66

7-
// Mirrors VirtualizedList.scrollToIndex without getItemLayout: indices past the highest
8-
// measured cell fail, and unmeasured indices below it scroll to an averaged estimate.
7+
// Mirrors VirtualizedList without getItemLayout: indices past the highest measured cell fail,
8+
// unmeasured indices below it scroll to an averaged estimate, and content ends at the highest
9+
// measured cell plus reserved padding, so the native scroll clamps there. Layout lands later
10+
// than the retry delay.
911
function setup({
1012
lineCount = 1_203,
1113
measuredThrough = 79,
1214
rowHeight = (_index: number): number => ROW_HEIGHT,
15+
visibleLength = 20 * ROW_HEIGHT,
16+
fillRows = 0,
1317
} = {}) {
1418
vi.useFakeTimers();
1519
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
@@ -20,9 +24,11 @@ function setup({
2024
const state = {
2125
lineCount,
2226
offset: 0,
27+
reserve: 0,
2328
measure(from: number, to: number) {
2429
for (let index = from; index <= to; index += 1) measured.add(index);
2530
},
31+
isMeasured: (index: number) => measured.has(index),
2632
};
2733
state.measure(0, measuredThrough);
2834
const offsetOf = (index: number) => {
@@ -52,8 +58,37 @@ function setup({
5258
? offsetOf(params.index)
5359
: averageItemLength() * params.index;
5460
});
61+
// One render pass: reserved padding applies, rows around the viewport lay out, and the
62+
// content size changes.
63+
let pendingReserve = 0;
64+
let layoutPending = false;
65+
const scheduleLayout = () => {
66+
if (layoutPending) return;
67+
layoutPending = true;
68+
setTimeout(() => {
69+
layoutPending = false;
70+
state.reserve = pendingReserve;
71+
if (fillRows > 0) {
72+
const index = Math.floor(state.offset / averageItemLength());
73+
state.measure(
74+
Math.max(0, index - fillRows / 2),
75+
Math.min(state.lineCount - 1, index + fillRows),
76+
);
77+
}
78+
scroll.onContentSizeChange();
79+
}, 250);
80+
};
81+
const reserveTail = (length: number) => {
82+
if (length === pendingReserve) return;
83+
pendingReserve = length;
84+
scheduleLayout();
85+
};
5586
const scrollToOffset = vi.fn((params: { offset: number }) => {
56-
state.offset = params.offset;
87+
const contentLength = offsetOf(Math.max(...measured) + 1) + state.reserve;
88+
const offset = Math.max(0, Math.min(params.offset, contentLength - visibleLength));
89+
if (offset === state.offset) return;
90+
state.offset = offset;
91+
scheduleLayout();
5792
});
5893
const list = {
5994
get props() {
@@ -62,7 +97,7 @@ function setup({
6297
scrollToIndex,
6398
scrollToOffset,
6499
};
65-
const scroll = createSourceLineScroll();
100+
const scroll = createSourceLineScroll(reserveTail);
66101
return {
67102
scroll,
68103
list,
@@ -154,3 +189,21 @@ it("a drag before the first frame cancels the jump", () => {
154189

155190
expect(t.scrollToIndex).not.toHaveBeenCalled();
156191
});
192+
193+
it("lands on a far wrapped line beyond the measured content", () => {
194+
using t = setup({
195+
lineCount: 16_000,
196+
rowHeight: () => ROW_HEIGHT * 2,
197+
visibleLength: 20 * ROW_HEIGHT * 2,
198+
fillRows: 120,
199+
});
200+
201+
t.scroll.scrollToLine(t.list, 4_999);
202+
vi.runAllTimers();
203+
204+
expect(t.state.offset).toBe(t.offsetOf(4_999));
205+
// Each render batch is slow on device, so the jump must skip the rows in between.
206+
expect(t.state.isMeasured(2_500)).toBe(false);
207+
expect(t.state.reserve).toBe(0);
208+
expect(vi.getTimerCount()).toBe(0);
209+
});

‎apps/mobile/src/features/files/source-file-scroll.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@ interface SourceLineList {
77
const RETRY_DELAY_MS = 100;
88
const MAX_RETRIES = 5;
99

10-
// Wrapped rows have no getItemLayout. VirtualizedList fails a jump past its highest measured
11-
// row and silently estimates an unmeasured row below it, so re-aim until the budget runs out.
12-
export function createSourceLineScroll() {
10+
// Wrapped rows have no getItemLayout. VirtualizedList fails a jump past its highest measured row
11+
// and ends its content there, so the native scroll clamps an estimated jump to the measured rows.
12+
// Reserve the estimate below them, re-aim when the list lays out, and refine once it accepts.
13+
export function createSourceLineScroll(reserveTail: (length: number) => void) {
1314
let list: SourceLineList | null = null;
1415
let target: number | null = null;
1516
let retries = 0;
17+
let highestMeasured = -1;
18+
let awaitingLayout = false;
1619
let cancelPending: (() => void) | null = null;
1720

1821
const cancel = () => {
1922
cancelPending?.();
2023
cancelPending = null;
24+
awaitingLayout = false;
2125
target = null;
26+
reserveTail(0);
2227
};
2328

2429
const retry = () => {
2530
if (retries >= MAX_RETRIES) {
26-
target = null;
31+
cancel();
2732
return;
2833
}
2934
retries += 1;
@@ -33,13 +38,13 @@ export function createSourceLineScroll() {
3338

3439
const attempt = () => {
3540
cancelPending = null;
41+
awaitingLayout = false;
3642
if (target === null || !list || target >= (list.props.data?.length ?? 0)) {
37-
target = null;
43+
cancel();
3844
return;
3945
}
4046
list.scrollToIndex({ index: target, animated: false, viewPosition: 0.3 });
41-
// A failed jump has already scheduled its retry.
42-
if (cancelPending === null) retry();
47+
if (!awaitingLayout) retry();
4348
};
4449

4550
return {
@@ -49,6 +54,7 @@ export function createSourceLineScroll() {
4954
if (!Number.isInteger(index) || index < 0) return;
5055
target = index;
5156
retries = 0;
57+
highestMeasured = -1;
5258
const frame = requestAnimationFrame(attempt);
5359
cancelPending = () => cancelAnimationFrame(frame);
5460
},
@@ -58,8 +64,18 @@ export function createSourceLineScroll() {
5864
highestMeasuredFrameIndex: number;
5965
}) {
6066
if (info.index !== target || !list) return;
61-
list.scrollToOffset({ offset: info.averageItemLength * info.index, animated: false });
62-
retry();
67+
// A failure that measured no new rows spends the budget; leaving awaitingLayout unset ends it.
68+
const progressed = info.highestMeasuredFrameIndex > highestMeasured;
69+
highestMeasured = info.highestMeasuredFrameIndex;
70+
if (!progressed && retries >= MAX_RETRIES) return;
71+
if (!progressed) retries += 1;
72+
const offset = info.averageItemLength * info.index;
73+
reserveTail(offset);
74+
list.scrollToOffset({ offset, animated: false });
75+
awaitingLayout = true;
76+
},
77+
onContentSizeChange() {
78+
if (awaitingLayout) attempt();
6379
},
6480
cancel,
6581
};

0 commit comments

Comments
 (0)