The best way to control Claude Code, Codex and more from any computer or phone

main
swift 194 lines 7.53 KB
Raw
1 import ExpoModulesCore
2 import UIKit
3
4 /// Wraps a screen's header scroll view. Where a column pads its content away from the iPhone Duo's
5 /// vertical bar, this widens the scroll view under the bar, like the system apps' lists. UIKit
6 /// draws the header's scroll-edge effect inside the scroll view, so the blur reaches the bar while
7 /// the content stays in the safe area, and the scroll indicator runs along the screen edge past the
8 /// bar, as in Messages. Everything happens in UIKit layout, so folds move the blur and the content
9 /// together.
10 final class T3ScrollEdgeBleedView: ExpoView {
11 // Fabric recycles scroll views, so everything changed here is put back before this view leaves
12 // the window, which unmounting does before any recycling.
13 private var scrollView: UIScrollView?
14 private var container: UIView?
15 /// React Native's content view, which every row lives in.
16 private var content: UIView?
17 private var bleed = UIEdgeInsets.zero
18 /// Whether the scroll view currently carries this view's changes.
19 private var applied = false
20 /// React Native's clipping, saved while bleeding. React Native only rewrites it when the
21 /// `overflow` style changes, so it stays off while bleeding.
22 private var containerClipped: Bool?
23 /// The bar's side is read from the root view controller, whose traits can settle after this
24 /// view's own during a fold; with no size change after that, nothing else would recheck.
25 private weak var observedWindow: UIWindow?
26 private var windowTraitRegistration: (any UITraitChangeRegistration)?
27
28 required init(appContext: AppContext? = nil) {
29 super.init(appContext: appContext)
30 #if canImport(UIKit, _version: 9127.0.85)
31 if #available(iOS 27.1, *) {
32 registerForTraitChanges(UITraitCollection.systemTraitsAffectingVerticalBarEdge) { (view: T3ScrollEdgeBleedView, _: UITraitCollection) in
33 view.setNeedsLayout()
34 }
35 }
36 #endif
37 }
38
39 deinit {
40 MainActor.assumeIsolated { detach() }
41 }
42
43 override func didMoveToWindow() {
44 super.didMoveToWindow()
45 observeWindowTraits()
46 if window == nil { detach() } else { setNeedsLayout() }
47 }
48
49 override func safeAreaInsetsDidChange() {
50 super.safeAreaInsetsDidChange()
51 setNeedsLayout()
52 }
53
54 private func observeWindowTraits() {
55 guard observedWindow !== window else { return }
56 if let observedWindow, let windowTraitRegistration {
57 observedWindow.unregisterForTraitChanges(windowTraitRegistration)
58 }
59 observedWindow = window
60 windowTraitRegistration = nil
61 #if canImport(UIKit, _version: 9127.0.85)
62 if #available(iOS 27.1, *), let window {
63 windowTraitRegistration = window.registerForTraitChanges(
64 UITraitCollection.systemTraitsAffectingVerticalBarEdge
65 ) { [weak self] (_: UIWindow, _: UITraitCollection) in
66 self?.setNeedsLayout()
67 }
68 }
69 #endif
70 }
71
72 override func didAddSubview(_ subview: UIView) {
73 super.didAddSubview(subview)
74 setNeedsLayout()
75 }
76
77 override func willRemoveSubview(_ subview: UIView) {
78 if let scrollView, scrollView.isDescendant(of: subview) { detach() }
79 super.willRemoveSubview(subview)
80 }
81
82 override func layoutSubviews() {
83 super.layoutSubviews()
84 guard window != nil, let found = findScrollView(), let parent = found.superview else { return }
85 if found !== scrollView {
86 detach()
87 scrollView = found
88 container = parent
89 // React Native adds one plain UIView to its scroll view and mounts every child into it.
90 content = found.subviews.first { type(of: $0) == UIView.self }
91 }
92 bleed = verticalBarBleed()
93 // Without the content view to shift back, bleeding left would move the rows under the bar.
94 if content == nil { bleed.left = 0 }
95 apply()
96 }
97
98 /// How far the vertical bar's safe area reaches past this view's edge, when this view sits flush
99 /// against the padding its column added for it. Nothing else is bled into: not a sidebar column
100 /// overlapping this one, and not a notch.
101 private func verticalBarBleed() -> UIEdgeInsets {
102 guard let side = verticalBarSide() else { return .zero }
103 var ancestor = superview
104 while let view = ancestor {
105 let inset = side == .left ? view.safeAreaInsets.left : view.safeAreaInsets.right
106 if inset > 0 {
107 let frame = convert(bounds, to: view)
108 let flush = side == .left
109 ? abs(frame.minX - (view.bounds.minX + inset)) < 0.5
110 : abs(view.bounds.maxX - inset - frame.maxX) < 0.5
111 guard flush else { return .zero }
112 return side == .left
113 ? UIEdgeInsets(top: 0, left: inset, bottom: 0, right: 0)
114 : UIEdgeInsets(top: 0, left: 0, bottom: 0, right: inset)
115 }
116 ancestor = view.superview
117 }
118 return .zero
119 }
120
121 private enum Side { case left, right }
122
123 private func verticalBarSide() -> Side? {
124 // The vertical-bar API ships in the iOS 27.1 SDK (UIKit 9127.0.85); see T3LayoutMetricsView.
125 #if canImport(UIKit, _version: 9127.0.85)
126 if #available(iOS 27.1, *) {
127 // UIKit leaves the trait unset in a split view's inspector column, though the bar overlaps
128 // it; the flush check above still decides whether this view sits at the bar.
129 let traits = window?.rootViewController?.traitCollection ?? traitCollection
130 let rtl = effectiveUserInterfaceLayoutDirection == .rightToLeft
131 switch traits.verticalBarEdge {
132 case .leading: return rtl ? .right : .left
133 case .trailing: return rtl ? .left : .right
134 default: return nil
135 }
136 }
137 #endif
138 return nil
139 }
140
141 /// Widening to the left would move the rows with it. Instead of a horizontal content offset,
142 /// which React Native rewrites on its own layout passes, the content view's bounds origin shifts
143 /// them back: React Native only ever sets that view's frame, which leaves its bounds origin.
144 /// Nothing at all is written unless this view is bleeding or undoing its bleed.
145 private func apply() {
146 guard let scrollView, let container else { return }
147 let bleeding = bleed != .zero
148 guard bleeding || applied else { return }
149 // React Native sizes the scroll view's component view; the UIScrollView inside follows it by
150 // autoresizing, so the widened frame survives React Native's own layout updates.
151 let frame = container.bounds.inset(
152 by: UIEdgeInsets(top: 0, left: -bleed.left, bottom: 0, right: -bleed.right))
153 if scrollView.frame != frame { scrollView.frame = frame }
154 if let content, content.bounds.origin.x != -bleed.left {
155 content.bounds.origin.x = -bleed.left
156 }
157 if !bleeding {
158 if let clipped = containerClipped {
159 containerClipped = nil
160 container.layer.masksToBounds = clipped
161 }
162 } else if container.layer.masksToBounds {
163 // The component view clips to its own bounds, which would cut the widened scroll view back.
164 // The scroll view itself still clips its content.
165 containerClipped = containerClipped ?? true
166 container.layer.masksToBounds = false
167 }
168 applied = bleeding
169 }
170
171 private func detach() {
172 guard scrollView != nil else { return }
173 bleed = .zero
174 apply()
175 scrollView = nil
176 container = nil
177 content = nil
178 }
179
180 /// The scroll view sits a fixed few levels down (marker, React Native's scroll view component);
181 /// never walk into a scroll view's content.
182 private func findScrollView() -> UIScrollView? {
183 var level: [UIView] = subviews
184 for _ in 0..<4 {
185 var next: [UIView] = []
186 for view in level {
187 if let scrollView = view as? UIScrollView { return scrollView }
188 next.append(contentsOf: view.subviews)
189 }
190 level = next
191 }
192 return nil
193 }
194 }