Skip to content

Commit

Permalink
♻️ Ensure target elements have center point visible, and the only the…
Browse files Browse the repository at this point in the history
… region within the safe area is highlighted
  • Loading branch information
iujames committed Dec 6, 2023
1 parent 9cc1e6c commit dfcb17c
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,22 @@ internal extension UIView {
}

func asViewElement() -> AppcuesViewElement? {
return self.asViewElement(in: self.bounds, autoTag: nil)
return self.asViewElement(in: self.bounds, safeAreaInsets: self.safeAreaInsets, autoTag: nil)
}

private func asViewElement(in bounds: CGRect, autoTag: String?) -> AppcuesViewElement? {
private func asViewElement(in bounds: CGRect, safeAreaInsets: UIEdgeInsets, autoTag: String?) -> AppcuesViewElement? {
let absolutePosition = self.convert(self.bounds, to: nil)

// discard views that are not visible in the screenshot image
guard absolutePosition.intersects(bounds) else { return nil }

let childInsets = UIEdgeInsets(
top: max(safeAreaInsets.top, self.safeAreaInsets.top),
left: max(safeAreaInsets.left, self.safeAreaInsets.left),
bottom: max(safeAreaInsets.bottom, self.safeAreaInsets.bottom),
right: max(safeAreaInsets.right, self.safeAreaInsets.right)
)

var tabCount = 0

let children: [AppcuesViewElement] = self.subviews.compactMap { subview -> AppcuesViewElement? in
Expand All @@ -182,16 +189,21 @@ internal extension UIView {
childTabIndex = tabCount - 1
}
let childAutoTag = getAutoTag(tabIndex: childTabIndex)
return subview.asViewElement(in: bounds, autoTag: childAutoTag)
return subview.asViewElement(in: bounds, safeAreaInsets: childInsets, autoTag: childAutoTag)
}

let selector = getAppcuesSelector(autoTag: autoTag)
// only create a selector for elements that have at least the center point
// visible in the current screen bounds, inset by any safe area adjustments
let safeBounds = bounds.inset(by: safeAreaInsets)
let centerPointVisible = safeBounds.contains(CGPoint(x: absolutePosition.midX, y: absolutePosition.midY))
let visibleRect = safeBounds.intersection(absolutePosition)
let selector = centerPointVisible ? getAppcuesSelector(autoTag: autoTag) : nil

return AppcuesViewElement(
x: absolutePosition.origin.x,
y: absolutePosition.origin.y,
width: absolutePosition.width,
height: absolutePosition.height,
x: visibleRect.origin.x,
y: visibleRect.origin.y,
width: visibleRect.width,
height: visibleRect.height,
type: displayType,
selector: selector,
children: children.isEmpty ? nil : children,
Expand Down

0 comments on commit dfcb17c

Please sign in to comment.