Skip to content

Commit bd142d4

Browse files
committed
fix(snapshot): stop a SwiftUI toolbar host from covering the sheet it encloses
On iOS 27 a SwiftUI sheet's toolbar host spans the whole sheet. In snapshot -i pruning drops its same-frame wrappers and the list frame shrinks to its scroll-indicator band, so the sibling-sized exemption (#2996) no longer fires and every sheet ref is refused as covered. Exempt a toolbar that encloses a listed sibling bar or scroll region.
1 parent 9a245d0 commit bd142d4

2 files changed

Lines changed: 153 additions & 2 deletions

File tree

‎packages/capture-kit/src/snapshot-occlusion.ts‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import type { RawSnapshotNode, Rect } from '@agent-device/kernel/snapshot';
22
import { centerOfRect } from '@agent-device/kernel/snapshot';
33
import { areRectsApproximatelyEqual, normalizeRect } from '@agent-device/kernel/rect-center';
4-
import { containsPoint } from '@agent-device/kernel/rect';
5-
import { normalizeType, isViewportRootNode } from '@agent-device/contracts/snapshot';
4+
import { containsPoint, rectContains } from '@agent-device/kernel/rect';
5+
import {
6+
isScrollableNodeLike,
7+
normalizeType,
8+
isViewportRootNode,
9+
} from '@agent-device/contracts/snapshot';
610

711
const COVERED_PRESENTATION_HINT = 'covered';
812
const OVERLAY_KIND_FRAGMENTS = [
@@ -18,6 +22,7 @@ const OVERLAY_KIND_FRAGMENTS = [
1822
'menu',
1923
];
2024
const VIEWPORT_CHROME_KIND_FRAGMENTS = ['tabbar', 'toolbar', 'navigationbar'];
25+
const TOOLBAR_HOST_KIND_FRAGMENTS = ['toolbar'];
2126
const SEMANTIC_TOUCH_KIND_FRAGMENTS = [
2227
'button',
2328
'link',
@@ -275,6 +280,7 @@ function isOverlayLikeNode(
275280
if (isViewportRootNode(node)) return false;
276281
if (isFullViewportChromeContainer(node, neighbourhood.byIndex)) return false;
277282
if (isSiblingSizedChromeContainer(node, neighbourhood)) return false;
283+
if (isSiblingEnclosingToolbarHost(node, neighbourhood)) return false;
278284
// This is a presentation-order heuristic: only known floating UI chrome should cover
279285
// later targets. Generic hittable containers can appear later without being visually on top.
280286
return (
@@ -318,6 +324,41 @@ function isSiblingSizedChromeContainer(
318324
);
319325
}
320326

327+
/**
328+
* Exempt a toolbar host that encloses a listed sibling bar or scroll region. A bar does not enclose
329+
* another bar or a list; SwiftUI's toolbar host does: it spans the presentation it belongs to (a
330+
* sheet, the navigation container), so its frame wraps the navigation bar and the list listed beside
331+
* it, while the bar it really draws is its own descendants.
332+
*
333+
* The sibling-sized rule cannot see this host once a projection reshapes the tree: pruning drops the
334+
* pass-through wrappers that share the host's frame, and the scroll-indicator viewport rewrites the
335+
* list's frame to its visible band, so no sibling keeps the host's frame. Captured in `snapshot -i`
336+
* on an iOS 27 simulator, this refused every control of a SwiftUI sheet, with or without a visible
337+
* navigation bar beside the list.
338+
*
339+
* Only the toolbar kind hosts this way: a navigation bar enclosing a same-kind sibling is a stacked
340+
* presentation drawn over the one beneath it. The enclosed sibling must be chrome or a scroll region,
341+
* since a host enclosing a plain content view cannot be told from a bar painted over that view.
342+
*/
343+
function isSiblingEnclosingToolbarHost(
344+
node: RawSnapshotNode,
345+
neighbourhood: NeighbourhoodIndex,
346+
): boolean {
347+
if (!nodeKindIncludesAny(node, TOOLBAR_HOST_KIND_FRAGMENTS)) return false;
348+
if (typeof node.parentIndex !== 'number') return false;
349+
const rect = positiveRect(node.rect);
350+
if (!rect) return false;
351+
const siblings = neighbourhood.childrenByParent.get(node.parentIndex) ?? [];
352+
return siblings.some((sibling) => {
353+
if (sibling.index === node.index) return false;
354+
const siblingRect = positiveRect(sibling.rect);
355+
if (!siblingRect || !rectContains(rect, siblingRect)) return false;
356+
return (
357+
nodeKindIncludesAny(sibling, VIEWPORT_CHROME_KIND_FRAGMENTS) || isScrollableNodeLike(sibling)
358+
);
359+
});
360+
}
361+
321362
function isFullViewportChromeContainer(
322363
node: RawSnapshotNode,
323364
byIndex: Map<number, RawSnapshotNode>,

‎packages/capture-kit/src/snapshot-sibling-sized-chrome.test.ts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,113 @@ test.for(CHROME_KIND_PUBLICATIONS)(
428428
);
429429
},
430430
);
431+
432+
type CapturedNode = [
433+
index: number,
434+
parentIndex: number | undefined,
435+
type: string,
436+
role: string,
437+
label: string | undefined,
438+
rect: [x: number, y: number, width: number, height: number],
439+
];
440+
441+
/** Builds a tree from rows captured with `snapshot -i --json`, every node hittable as published. */
442+
function capturedTree(rows: CapturedNode[]): RawSnapshotNode[] {
443+
const depthByIndex = new Map<number, number>();
444+
return rows.map(([index, parentIndex, type, role, label, [x, y, width, height]]) => {
445+
const depth = parentIndex === undefined ? 0 : (depthByIndex.get(parentIndex) ?? 0) + 1;
446+
depthByIndex.set(index, depth);
447+
return node({
448+
index,
449+
parentIndex,
450+
depth,
451+
type,
452+
role,
453+
label,
454+
rect: { x, y, width, height },
455+
hittable: true,
456+
});
457+
});
458+
}
459+
460+
/** Labels (or types, for unlabelled nodes) the occlusion pass marks covered, in tree order. */
461+
function coveredLabels(nodes: RawSnapshotNode[]): string[] {
462+
return annotateCoveredSnapshotNodes(nodes)
463+
.filter((candidate) => candidate.interactionBlocked === 'covered')
464+
.map((candidate) => candidate.label ?? candidate.type ?? '');
465+
}
466+
467+
test('the captured iOS 27 SwiftUI sheet toolbar host stops condemning the sheet in the interactive projection', () => {
468+
// `snapshot -i` of a `.medium` SwiftUI sheet on an iOS 27 simulator. Pruning dropped the wrappers
469+
// sharing the host's frame and the list's frame was rewritten to its scroll-indicator band, so no
470+
// sibling matches the host's 386x451 frame; it encloses the navigation bar and the list beside it.
471+
const nodes = capturedTree([
472+
[0, undefined, 'Other', 'SwiftUIApplication', 'SheetRepro', [0, 0, 402, 874]],
473+
[2, 0, 'Button', '_UIGrabber', 'Sheet Grabber', [152.99, 411.19, 96.02, 23.04]],
474+
[3, 0, 'NavigationBar', 'SwiftUI.UIKitNavigationBar', undefined, [8, 430.39, 386, 101.78]],
475+
[4, 3, 'Button', 'SwiftUI.AccessibilityNode', 'Close', [27.2, 434.23, 64.33, 34.57]],
476+
[7, 3, 'SearchField', 'UISearchBarTextField', 'Search', [23.36, 482.24, 355.27, 40.33]],
477+
[8, 0, 'CollectionView', 'SwiftUI.CollectionView', undefined, [8, 482.24, 386, 331.91]],
478+
[9, 8, 'Cell', 'SwiftUI.ListCollectionViewCell', undefined, [23.36, 532.17, 355.27, 49.93]],
479+
[
480+
10,
481+
9,
482+
'Button',
483+
'SwiftUI.AccessibilityNode',
484+
'Primary action',
485+
[23.36, 532.17, 355.27, 49.93],
486+
],
487+
[13, 8, 'Cell', 'SwiftUI.ListCollectionViewCell', undefined, [23.36, 632.03, 355.27, 49.93]],
488+
[14, 13, 'TextField', 'SwiftUI.UIKitTextField', 'Name', [38.73, 646.43, 324.55, 21.12]],
489+
[15, 8, 'Cell', 'SwiftUI.ListCollectionViewCell', undefined, [23.36, 681.96, 355.27, 132.19]],
490+
[16, 15, 'TextView', 'SwiftUI.TextEditorTextView', 'Notes', [38.73, 696.36, 324.55, 115.22]],
491+
[17, 0, 'Toolbar', '_UIFloatingBarContainerView', 'Toolbar', [8, 415.03, 386, 450.97]],
492+
[18, 17, 'Button', 'SwiftUI.AccessibilityNode', 'Bottom', [163.55, 798.79, 74.9, 34.57]],
493+
]);
494+
495+
assert.deepEqual(coveredLabels(nodes), []);
496+
});
497+
498+
test('the captured iOS 27 sheet toolbar host with a hidden navigation bar stops condemning the list', () => {
499+
// The same host beside nothing but the list: with `.toolbar(.hidden, for: .navigationBar)` the
500+
// enclosed scroll region is the only sibling evidence the interactive projection keeps.
501+
const nodes = capturedTree([
502+
[0, undefined, 'Other', 'SwiftUIApplication', 'SheetRepro', [0, 0, 402, 874]],
503+
[1, 0, 'CollectionView', 'SwiftUI.CollectionView', undefined, [0, 100, 402, 712]],
504+
[2, 1, 'Cell', 'SwiftUI.ListCollectionViewCell', undefined, [16, 100, 370, 49]],
505+
[3, 2, 'Button', 'SwiftUI.AccessibilityNode', 'Barless primary', [16, 100, 370, 49]],
506+
[16, 1, 'Cell', 'SwiftUI.ListCollectionViewCell', 'Barless row 12', [16, 773, 370, 39]],
507+
[17, 0, 'Toolbar', '_UIFloatingBarContainerView', 'Toolbar', [0, 62, 402, 812]],
508+
[18, 17, 'Button', 'SwiftUI.AccessibilityNode', 'Barless bottom', [132.67, 804, 137, 36]],
509+
]);
510+
511+
assert.deepEqual(coveredLabels(nodes), []);
512+
});
513+
514+
test('a navigation bar enclosing the navigation bar it was presented over still covers it', () => {
515+
// `snapshot -i` of a full-screen cover on an iOS 27 simulator: the cover's navigation bar encloses
516+
// the presenting screen's bar. A stacked bar is drawn over the one beneath, so only the toolbar
517+
// kind is read as a host.
518+
const nodes = capturedTree([
519+
[0, undefined, 'Other', 'SwiftUIApplication', 'SheetRepro', [0, 0, 402, 874]],
520+
[1, 0, 'NavigationBar', 'SwiftUI.UIKitNavigationBar', 'Home', [0, 62, 402, 106]],
521+
[2, 1, 'Button', 'SwiftUI.AccessibilityNode', 'Edit', [328.67, 66, 53.33, 36]],
522+
[19, 0, 'NavigationBar', 'SwiftUI.UIKitNavigationBar', 'Sheet cover', [0, 62, 402, 108]],
523+
[20, 19, 'Button', 'SwiftUI.AccessibilityNode', 'Close', [20, 66, 67, 36]],
524+
]);
525+
526+
assert.deepEqual(coveredLabels(nodes), ['Home', 'Edit']);
527+
});
528+
529+
test('a toolbar host enclosing a plain content sibling still covers it, so the enclosed kind is gated', () => {
530+
// Non-vacuity for the host rule: the same host frame beside a plain `Other` branch instead of the
531+
// list keeps covering, as the containment case above requires.
532+
const nodes = capturedTree([
533+
[0, undefined, 'Other', 'SwiftUIApplication', 'SheetRepro', [0, 0, 402, 874]],
534+
[1, 0, 'Other', 'UIView', undefined, [0, 100, 402, 712]],
535+
[3, 1, 'Button', 'SwiftUI.AccessibilityNode', 'Barless primary', [16, 100, 370, 49]],
536+
[17, 0, 'Toolbar', '_UIFloatingBarContainerView', 'Toolbar', [0, 62, 402, 812]],
537+
]);
538+
539+
assert.deepEqual(coveredLabels(nodes), ['Other', 'Barless primary']);
540+
});

0 commit comments

Comments
 (0)