forked from siteline/swiftui-introspect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrospectionView.swift
246 lines (209 loc) · 7.39 KB
/
IntrospectionView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#if !os(watchOS)
import SwiftUI
typealias IntrospectionViewID = UUID
fileprivate enum IntrospectionStore {
@MainActor static var shared: [IntrospectionViewID: Pair] = [:]
struct Pair {
weak var controller: IntrospectionPlatformViewController?
weak var anchor: IntrospectionAnchorPlatformViewController?
}
}
extension PlatformEntity {
@MainActor var introspectionAnchorEntity: Base? {
if let introspectionController = self as? IntrospectionPlatformViewController {
return IntrospectionStore.shared[introspectionController.id]?.anchor~
}
if
let view = self as? PlatformView,
let introspectionController = view.introspectionController
{
return IntrospectionStore.shared[introspectionController.id]?.anchor?.view~
}
return nil
}
}
/// ⚓️
struct IntrospectionAnchorView: PlatformViewControllerRepresentable {
#if canImport(UIKit)
typealias UIViewControllerType = IntrospectionAnchorPlatformViewController
#elseif canImport(AppKit)
typealias NSViewControllerType = IntrospectionAnchorPlatformViewController
#endif
@Binding
private var observed: Void // workaround for state changes not triggering view updates
let id: IntrospectionViewID
init(id: IntrospectionViewID) {
self._observed = .constant(())
self.id = id
}
func makePlatformViewController(context: Context) -> IntrospectionAnchorPlatformViewController {
IntrospectionAnchorPlatformViewController(id: id)
}
func updatePlatformViewController(_ controller: IntrospectionAnchorPlatformViewController, context: Context) {}
static func dismantlePlatformViewController(_ controller: IntrospectionAnchorPlatformViewController, coordinator: Coordinator) {}
}
final class IntrospectionAnchorPlatformViewController: PlatformViewController {
init(id: IntrospectionViewID) {
super.init(nibName: nil, bundle: nil)
self.isIntrospectionPlatformEntity = true
IntrospectionStore.shared[id, default: .init()].anchor = self
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#if canImport(UIKit)
override func viewDidLoad() {
super.viewDidLoad()
view.isIntrospectionPlatformEntity = true
}
#elseif canImport(AppKit)
override func loadView() {
view = NSView()
view.isIntrospectionPlatformEntity = true
}
#endif
}
struct IntrospectionView<Target: PlatformEntity>: PlatformViewControllerRepresentable {
#if canImport(UIKit)
typealias UIViewControllerType = IntrospectionPlatformViewController
#elseif canImport(AppKit)
typealias NSViewControllerType = IntrospectionPlatformViewController
#endif
final class TargetCache {
weak var target: Target?
}
@Binding
private var observed: Void // workaround for state changes not triggering view updates
private let id: IntrospectionViewID
private let selector: (IntrospectionPlatformViewController) -> Target?
private let customize: (Target) -> Void
init(
id: IntrospectionViewID,
selector: @escaping (IntrospectionPlatformViewController) -> Target?,
customize: @escaping (Target) -> Void
) {
self._observed = .constant(())
self.id = id
self.selector = selector
self.customize = customize
}
func makeCoordinator() -> TargetCache {
TargetCache()
}
func makePlatformViewController(context: Context) -> IntrospectionPlatformViewController {
let controller = IntrospectionPlatformViewController(id: id) { controller in
guard let target = selector(controller) else {
return
}
context.coordinator.target = target
customize(target)
controller.handler = nil
}
// - Workaround -
// iOS/tvOS 13 sometimes need a nudge on the next run loop.
if #available(iOS 14, tvOS 14, *) {} else {
DispatchQueue.main.async { [weak controller] in
controller?.handler?()
}
}
return controller
}
func updatePlatformViewController(_ controller: IntrospectionPlatformViewController, context: Context) {
guard let target = context.coordinator.target ?? selector(controller) else {
return
}
customize(target)
}
static func dismantlePlatformViewController(_ controller: IntrospectionPlatformViewController, coordinator: Coordinator) {
controller.handler = nil
}
}
final class IntrospectionPlatformViewController: PlatformViewController {
let id: IntrospectionViewID
var handler: (() -> Void)? = nil
fileprivate init(
id: IntrospectionViewID,
handler: ((IntrospectionPlatformViewController) -> Void)?
) {
self.id = id
super.init(nibName: nil, bundle: nil)
self.handler = { [weak self] in
guard let self else {
return
}
handler?(self)
}
self.isIntrospectionPlatformEntity = true
IntrospectionStore.shared[id, default: .init()].controller = self
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#if canImport(UIKit)
#if os(iOS)
override var preferredStatusBarStyle: UIStatusBarStyle {
parent?.preferredStatusBarStyle ?? super.preferredStatusBarStyle
}
#endif
override func viewDidLoad() {
super.viewDidLoad()
view.introspectionController = self
view.isIntrospectionPlatformEntity = true
handler?()
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
handler?()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
handler?()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
handler?()
}
#elseif canImport(AppKit)
override func loadView() {
view = NSView()
view.introspectionController = self
view.isIntrospectionPlatformEntity = true
}
override func viewDidLoad() {
super.viewDidLoad()
handler?()
}
override func viewDidAppear() {
super.viewDidAppear()
handler?()
}
#endif
}
import ObjectiveC
extension PlatformView {
fileprivate var introspectionController: IntrospectionPlatformViewController? {
get {
let key = unsafeBitCast(Selector(#function), to: UnsafeRawPointer.self)
return objc_getAssociatedObject(self, key) as? IntrospectionPlatformViewController
}
set {
let key = unsafeBitCast(Selector(#function), to: UnsafeRawPointer.self)
objc_setAssociatedObject(self, key, newValue, .OBJC_ASSOCIATION_ASSIGN)
}
}
}
extension PlatformEntity {
var isIntrospectionPlatformEntity: Bool {
get {
let key = unsafeBitCast(Selector(#function), to: UnsafeRawPointer.self)
return objc_getAssociatedObject(self, key) as? Bool ?? false
}
set {
let key = unsafeBitCast(Selector(#function), to: UnsafeRawPointer.self)
objc_setAssociatedObject(self, key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
#endif