Skip to content

Commit 273e341

Browse files
airoweclaude
andcommitted
fix(scroll): select the same-rect ancestor so scoped recaptures keep it
Scoping the ancestor's hints to the verdict alone let selection pick the nested RecyclerView at the top; the loop then scoped its recapture to that subtree, lost the ancestor, and fell back to the stale hint (scroll_edge_no_progress on a second `scroll top`). Scroll edge selection now stands a same-rect scrollable ancestor in for the scroller nested in it, so the chosen container, its scope and its verdict all come from the ancestor. The visited-index guard stays. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4e9bcc8 commit 273e341

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

‎packages/capture-kit/src/snapshot/__tests__/scroll-edge-state-same-rect-ancestor.test.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import assert from 'node:assert/strict';
33
import { runScrollEdgePasses } from '../scroll-edge-state.ts';
44
import { capture, scrollNode, windowRoot } from './scroll-edge-state-fixtures.ts';
55
import { settingsHomeTree } from './scroll-edge-state-android-settings-fixture.ts';
6+
import type { SnapshotNode } from '@agent-device/kernel/snapshot';
7+
8+
function subtreeFor(nodes: readonly SnapshotNode[], scope: string | undefined): SnapshotNode[] {
9+
if (scope === undefined) return [...nodes];
10+
const root = nodes.find((node) => node.identifier === scope);
11+
if (!root) return [];
12+
const inScope = new Set([root.index]);
13+
for (const node of nodes) {
14+
if (node.parentIndex !== undefined && inScope.has(node.parentIndex)) inScope.add(node.index);
15+
}
16+
return nodes.filter((node) => inScope.has(node.index));
17+
}
618

719
// ---------------------------------------------------------------------------
820
// A scroller nested in a scrollable ancestor with the same rect takes its edge hints from that
@@ -15,9 +27,17 @@ test('scroll top edge: the Android Settings list at the top reports no hidden co
1527
assert.equal(state.canScroll, false);
1628
});
1729

18-
test('scroll top edge: the ancestor answers only the verdict, so selection still names the nested list', async () => {
19-
const state = await capture(settingsHomeTree('at-top'), 'top');
20-
assert.equal(state.scope, 'com.android.settings:id/recycler_view');
30+
test('scroll top on Android Settings already at the top costs no pass under scoped recapture', async () => {
31+
// Scoped captures return only the scope root's subtree, as an Android scoped snapshot does.
32+
const tree = settingsHomeTree('at-top');
33+
const outcome = await runScrollEdgePasses({
34+
edge: 'top',
35+
captureState: async (scope) => await capture(subtreeFor(tree, scope), 'top'),
36+
scroll: async () => {
37+
throw new Error('scroll top dispatched a gesture at the top');
38+
},
39+
});
40+
assert.equal(outcome.passes, 0);
2141
});
2242

2343
test('scroll top edge: a cyclic chain of same-rect scrollables ends the ancestor walk', async () => {
@@ -29,7 +49,7 @@ test('scroll top edge: a cyclic chain of same-rect scrollables ends the ancestor
2949
];
3050
const state = await capture(nodes, 'top');
3151
assert.equal(state.scope, 'first');
32-
assert.equal(state.canScroll, false);
52+
assert.equal(state.canScroll, true);
3353
});
3454

3555
test('scroll top edge: the Android Settings list scrolled down still reports hidden content above', async () => {

‎packages/capture-kit/src/snapshot/scroll-edge-state/selection.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ export function analyzeScrollEdgeState(
3535
};
3636
}
3737

38-
// A scroller nested in a scrollable ancestor with the same rect answers the edge verdict with
39-
// that ancestor's hints: Android's Settings RecyclerView offers scroll-backward even at the top,
40-
// while the ScrollView framing the same pixels reports the real position (#3737). Selection and
41-
// the snapshot's own per-node hints are unchanged.
42-
const verdictNode = sameRectScrollableAncestor(container, nodes) ?? container;
43-
const canScroll = hasHiddenContentAtEdge(verdictNode, hiddenHints.get(verdictNode.index), edge);
38+
const canScroll = hasHiddenContentAtEdge(container, hiddenHints.get(container.index), edge);
4439
return {
4540
canScroll,
4641
emptySnapshot: false,
@@ -69,12 +64,20 @@ function selectScrollContainer(
6964
target: ScrollEdgeTarget,
7065
): SnapshotNode | null {
7166
const visibility = createSnapshotVisibility(nodes);
72-
const scrollables = nodes.filter((node) => isScrollableNodeLike(node) && isUsableRect(node.rect));
67+
const edgeContainer = (node: SnapshotNode): SnapshotNode =>
68+
sameRectScrollableAncestor(node, visibility.nodeByIndex) ?? node;
69+
const scrollables = [
70+
...new Set(
71+
nodes
72+
.filter((node) => isScrollableNodeLike(node) && isUsableRect(node.rect))
73+
.map(edgeContainer),
74+
),
75+
];
7376
if (scrollables.length === 0) return null;
7477

7578
const targetAncestor = findNearestScrollableAncestor(target.nodeIndex, visibility.nodeByIndex);
7679
if (targetAncestor) {
77-
return targetAncestor;
80+
return edgeContainer(targetAncestor);
7881
}
7982

8083
const targetPoint = target.point;
@@ -163,15 +166,17 @@ function hasHiddenContentAtEdge(
163166
}
164167

165168
/**
166-
* The outermost scrollable in an unbroken chain of same-rect scrollable ancestors, or undefined
167-
* when the nearest scrollable ancestor frames different pixels. Visited indexes stop a cyclic
168-
* `parentIndex` chain in a malformed tree.
169+
* The outermost scrollable in an unbroken chain of same-rect scrollable ancestors, or undefined when
170+
* the nearest scrollable ancestor frames different pixels. Scroll edge selection stands that
171+
* ancestor in for the nested scroller: Android's Settings RecyclerView offers scroll-backward even at
172+
* the top, while the ScrollView framing the same pixels reports the real position (#3737). Choosing
173+
* the ancestor also keeps the nested scroller inside the scoped recapture. Visited indexes stop a
174+
* cyclic `parentIndex` chain in a malformed tree.
169175
*/
170176
function sameRectScrollableAncestor(
171177
node: SnapshotNode,
172-
nodes: readonly SnapshotNode[],
178+
byIndex: ReadonlyMap<number, SnapshotNode>,
173179
): SnapshotNode | undefined {
174-
const byIndex = new Map(nodes.map((candidate) => [candidate.index, candidate] as const));
175180
const visited = new Set<number>([node.index]);
176181
let owner: SnapshotNode | undefined;
177182
let current = node;

0 commit comments

Comments
 (0)