|
| 1 | +import ExpoModulesCore |
| 2 | +import UIKit |
| 3 | + |
| 4 | +/// Wraps a screen's header scroll view. Where a column pads its content away from a side safe |
| 5 | +/// area (the iPhone Duo's vertical bar), this widens the scroll view under that band. UIKit draws |
| 6 | +/// the header's scroll-edge effect inside the scroll view, so the blur reaches the bar like in the |
| 7 | +/// system apps while the content stays in the safe area. Everything happens in UIKit layout, so |
| 8 | +/// folds move the blur and the content together. |
| 9 | +final class T3ScrollEdgeBleedView: ExpoView { |
| 10 | + // Fabric recycles scroll views, so everything changed here is put back before this view leaves |
| 11 | + // the window, which unmounting does before any recycling. |
| 12 | + private var scrollView: UIScrollView? |
| 13 | + private var container: UIView? |
| 14 | + private var bleed = UIEdgeInsets.zero |
| 15 | + /// React Native's clipping, saved while bleeding. React Native only rewrites it when the |
| 16 | + /// `overflow` style changes, so it stays off while bleeding. |
| 17 | + private var containerClipped: Bool? |
| 18 | + |
| 19 | + deinit { |
| 20 | + MainActor.assumeIsolated { detach() } |
| 21 | + } |
| 22 | + |
| 23 | + override func didMoveToWindow() { |
| 24 | + super.didMoveToWindow() |
| 25 | + if window == nil { detach() } else { setNeedsLayout() } |
| 26 | + } |
| 27 | + |
| 28 | + override func didAddSubview(_ subview: UIView) { |
| 29 | + super.didAddSubview(subview) |
| 30 | + setNeedsLayout() |
| 31 | + } |
| 32 | + |
| 33 | + override func willRemoveSubview(_ subview: UIView) { |
| 34 | + if let scrollView, scrollView.isDescendant(of: subview) { detach() } |
| 35 | + super.willRemoveSubview(subview) |
| 36 | + } |
| 37 | + |
| 38 | + override func layoutSubviews() { |
| 39 | + super.layoutSubviews() |
| 40 | + guard window != nil, let found = findScrollView(), let parent = found.superview else { return } |
| 41 | + if found !== scrollView { |
| 42 | + detach() |
| 43 | + scrollView = found |
| 44 | + container = parent |
| 45 | + } |
| 46 | + bleed = flushSafeAreaBand() |
| 47 | + apply() |
| 48 | + } |
| 49 | + |
| 50 | + /// The side safe areas this view sits flush against: an ancestor padded its content away from |
| 51 | + /// them. Only those bands are bled into, so a screen with its own margins is left alone. |
| 52 | + private func flushSafeAreaBand() -> UIEdgeInsets { |
| 53 | + var ancestor = superview |
| 54 | + while let view = ancestor { |
| 55 | + let insets = view.safeAreaInsets |
| 56 | + if insets.left > 0 || insets.right > 0 { |
| 57 | + let frame = convert(bounds, to: view) |
| 58 | + let left = insets.left > 0 && abs(frame.minX - insets.left) < 0.5 ? insets.left : 0 |
| 59 | + let right = insets.right > 0 && abs(view.bounds.maxX - insets.right - frame.maxX) < 0.5 |
| 60 | + ? insets.right : 0 |
| 61 | + return UIEdgeInsets(top: 0, left: left, bottom: 0, right: right) |
| 62 | + } |
| 63 | + ancestor = view.superview |
| 64 | + } |
| 65 | + return .zero |
| 66 | + } |
| 67 | + |
| 68 | + /// Widening to the right leaves the content in place. Widening to the left moves it with the |
| 69 | + /// frame, so a left inset and offset put it back. |
| 70 | + private func apply() { |
| 71 | + guard let scrollView, let container else { return } |
| 72 | + // React Native sizes the scroll view's component view; the UIScrollView inside follows it by |
| 73 | + // autoresizing, so the widened frame survives React Native's own layout updates. |
| 74 | + let frame = container.bounds.inset(by: UIEdgeInsets(top: 0, left: -bleed.left, bottom: 0, right: -bleed.right)) |
| 75 | + if scrollView.frame != frame { scrollView.frame = frame } |
| 76 | + if bleed == .zero { |
| 77 | + if let clipped = containerClipped { |
| 78 | + containerClipped = nil |
| 79 | + container.layer.masksToBounds = clipped |
| 80 | + } |
| 81 | + } else if container.layer.masksToBounds { |
| 82 | + // The component view clips to its own bounds, which would cut the widened scroll view back. |
| 83 | + // The scroll view itself still clips its content. |
| 84 | + containerClipped = containerClipped ?? true |
| 85 | + container.layer.masksToBounds = false |
| 86 | + } |
| 87 | + var inset = scrollView.contentInset |
| 88 | + if inset.left != bleed.left { |
| 89 | + inset.left = bleed.left |
| 90 | + scrollView.contentInset = inset |
| 91 | + } |
| 92 | + var indicators = scrollView.verticalScrollIndicatorInsets |
| 93 | + if indicators.right != bleed.right { |
| 94 | + indicators.right = bleed.right |
| 95 | + scrollView.verticalScrollIndicatorInsets = indicators |
| 96 | + } |
| 97 | + let x = -scrollView.adjustedContentInset.left |
| 98 | + if scrollView.contentOffset.x != x { |
| 99 | + scrollView.contentOffset = CGPoint(x: x, y: scrollView.contentOffset.y) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private func detach() { |
| 104 | + guard scrollView != nil else { return } |
| 105 | + bleed = .zero |
| 106 | + apply() |
| 107 | + scrollView = nil |
| 108 | + container = nil |
| 109 | + } |
| 110 | + |
| 111 | + /// The scroll view sits a fixed few levels down (marker, React Native's scroll view component); |
| 112 | + /// never walk into a scroll view's content. |
| 113 | + private func findScrollView() -> UIScrollView? { |
| 114 | + var level: [UIView] = subviews |
| 115 | + for _ in 0..<4 { |
| 116 | + var next: [UIView] = [] |
| 117 | + for view in level { |
| 118 | + if let scrollView = view as? UIScrollView { return scrollView } |
| 119 | + next.append(contentsOf: view.subviews) |
| 120 | + } |
| 121 | + level = next |
| 122 | + } |
| 123 | + return nil |
| 124 | + } |
| 125 | +} |
0 commit comments