Skip to content

Commit 66185eb

Browse files
committed
refactor(snapshot): simplify presentation toolbar host classification
1 parent 1e9748c commit 66185eb

1 file changed

Lines changed: 19 additions & 40 deletions

File tree

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

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -325,53 +325,43 @@ function isSiblingSizedChromeContainer(
325325
if (typeof node.parentIndex !== 'number') return false;
326326
const rect = positiveRect(node.rect);
327327
if (!rect) return false;
328-
const listed = listedSiblings(node, neighbourhood);
329-
if (!listed) return false;
330-
const { siblings, position } = listed;
328+
const siblings = neighbourhood.childrenByParent.get(node.parentIndex) ?? [];
331329
return siblings.some(
332-
(sibling, siblingPosition) =>
333-
siblingPosition !== position && areRectsApproximatelyEqual(rect, sibling.rect),
330+
(sibling) => sibling.index !== node.index && areRectsApproximatelyEqual(rect, sibling.rect),
334331
);
335332
}
336333

337334
/**
338-
* Exempt a toolbar host that encloses only its own presentation's bars and scroll regions. SwiftUI's
339-
* toolbar host spans the presentation it belongs to while drawing only its descendants; once
340-
* projection pruning reshapes its siblings, the sibling-sized rule no longer sees it.
341-
*
342-
* Presentations are listed in z-order, each ending with its toolbar host, so the host's own
343-
* presentation is the run of siblings after the previous toolbar sibling. An enclosed bar or scroll
344-
* region outside that run belongs to a presentation stacked under or over the host, and the host
345-
* keeps covering.
335+
* SwiftUI toolbar hosts draw their descendants, not their enclosing frame. Exempt a host only
336+
* when it encloses bars or scroll containers from its own presentation: the siblings after the
337+
* previous toolbar and before this host. Enclosing another presentation's regions keeps it covering.
346338
*/
347339
function isOwnPresentationToolbarHost(
348340
node: RawSnapshotNode,
349341
neighbourhood: NeighbourhoodIndex,
350342
): boolean {
351343
if (!nodeKindIncludesAny(node, TOOLBAR_HOST_KIND_FRAGMENTS)) return false;
344+
if (typeof node.parentIndex !== 'number') return false;
352345
const rect = positiveRect(node.rect);
353346
if (!rect) return false;
354-
const listed = listedSiblings(node, neighbourhood);
355-
if (!listed) return false;
356-
const { siblings, position } = listed;
347+
const siblings = neighbourhood.childrenByParent.get(node.parentIndex)!;
348+
const position = siblings.findIndex((sibling) => sibling.index === node.index);
357349
const ownStart = ownPresentationStart(siblings, position);
358-
const enclosed = siblings.flatMap((sibling, siblingPosition) =>
359-
siblingPosition !== position && isEnclosedHostedRegion(rect, sibling) ? [siblingPosition] : [],
360-
);
361-
return (
362-
enclosed.length > 0 &&
363-
enclosed.every((siblingPosition) => siblingPosition >= ownStart && siblingPosition < position)
364-
);
350+
351+
let hasHostedRegion = false;
352+
for (const [siblingPosition, sibling] of siblings.entries()) {
353+
if (siblingPosition === position || !isEnclosedHostedRegion(rect, sibling)) continue;
354+
if (siblingPosition < ownStart || siblingPosition > position) return false;
355+
hasHostedRegion = true;
356+
}
357+
return hasHostedRegion;
365358
}
366359

367-
/** Where the presentation ending at `position` begins: just after the previous toolbar sibling. */
368360
function ownPresentationStart(siblings: readonly RawSnapshotNode[], position: number): number {
369-
let start = 0;
370-
for (const [siblingPosition, sibling] of siblings.entries()) {
371-
if (siblingPosition >= position) break;
372-
if (nodeKindIncludesAny(sibling, TOOLBAR_HOST_KIND_FRAGMENTS)) start = siblingPosition + 1;
361+
for (let previous = position - 1; previous >= 0; previous--) {
362+
if (nodeKindIncludesAny(siblings[previous]!, TOOLBAR_HOST_KIND_FRAGMENTS)) return previous + 1;
373363
}
374-
return start;
364+
return 0;
375365
}
376366

377367
/** A bar or a scroll container inside `rect`; a scroll indicator is not a container. */
@@ -383,17 +373,6 @@ function isEnclosedHostedRegion(rect: Rect, node: RawSnapshotNode): boolean {
383373
return Boolean(isHostedRegion && nodeRect && rectContains(rect, nodeRect));
384374
}
385375

386-
/** The siblings listed under the node's parent in document order and the node's position, if listed. */
387-
function listedSiblings(
388-
node: RawSnapshotNode,
389-
neighbourhood: NeighbourhoodIndex,
390-
): { siblings: readonly RawSnapshotNode[]; position: number } | undefined {
391-
if (typeof node.parentIndex !== 'number') return undefined;
392-
const siblings = neighbourhood.childrenByParent.get(node.parentIndex) ?? [];
393-
const position = siblings.findIndex((sibling) => sibling.index === node.index);
394-
return position < 0 ? undefined : { siblings, position };
395-
}
396-
397376
function isFullViewportChromeContainer(
398377
node: RawSnapshotNode,
399378
byIndex: Map<number, RawSnapshotNode>,

0 commit comments

Comments
 (0)