-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathCustomViewModifier.swift
301 lines (258 loc) · 9.83 KB
/
CustomViewModifier.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// CustomViewModifier.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete
// ID: 0EAFD5A78D9C0B607C3C0964CF3A3038 (SwiftUI)
// ID: 2BA0A33A15B7F322F46AFB9D0D1A262D (SwiftUICore)
import OpenGraphShims
// MARK: - _ViewModifier_Content
public struct _ViewModifier_Content<Modifier>: PrimitiveView where Modifier: ViewModifier {
nonisolated public static func _makeView(
view: _GraphValue<Self>,
inputs: _ViewInputs
) -> _ViewOutputs {
providerMakeView(view: view, inputs: inputs)
}
nonisolated public static func _makeViewList(
view: _GraphValue<Self>,
inputs: _ViewListInputs
) -> _ViewListOutputs {
providerMakeViewList(view: view, inputs: inputs)
}
nonisolated public static func _viewListCount(
inputs: _ViewListCountInputs,
body: (_ViewListCountInputs) -> Int?
) -> Int? {
nil
}
@_alwaysEmitIntoClient
nonisolated public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? {
_viewListCount(inputs: inputs) { _ in nil }
}
}
// MARK: - PlaceholderContentView
/// A placeholder used to construct an inline modifier, transition, or other
/// helper type.
///
/// You don't use this type directly. Instead OpenSwiftUI creates this type on
/// your behalf.
public struct PlaceholderContentView<Value>: View {
package init() {}
nonisolated public static func _makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
providerMakeView(view: view, inputs: inputs)
}
nonisolated public static func _makeViewList(view: _GraphValue<Self>, inputs: _ViewListInputs) -> _ViewListOutputs {
providerMakeViewList(view: view, inputs: inputs)
}
nonisolated public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? {
providerViewListCount(inputs: inputs)
}
public typealias Body = Never
}
@available(*, unavailable)
extension PlaceholderContentView: Sendable {}
extension _ViewInputs {
package mutating func pushModifierBody<Token>(_ type: Token.Type, body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs) {
append(BodyInputElement.view(body), to: BodyInput<Token>.self)
}
}
extension _ViewListInputs {
package mutating func pushModifierBody<Token>(_ type: Token.Type, body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs) {
base.append(BodyInputElement.list(body), to: BodyInput<Token>.self)
}
}
@available(*, unavailable)
extension _ViewModifier_Content: Sendable {}
// MARK: - ViewModifier + Extension
extension ViewModifier {
nonisolated public static func _makeView(
modifier: _GraphValue<Self>,
inputs: _ViewInputs,
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
) -> _ViewOutputs {
makeView(modifier: modifier, inputs: inputs, body: body)
}
nonisolated public static func _makeViewList(
modifier: _GraphValue<Self>,
inputs: _ViewListInputs,
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
) -> _ViewListOutputs {
makeViewList(modifier: modifier, inputs: inputs, body: body)
}
nonisolated public static func _viewListCount(
inputs: _ViewListCountInputs,
body: (_ViewListCountInputs) -> Int?
) -> Int? {
viewListCount(inputs: inputs, body: body)
}
nonisolated package static func makeView(
modifier: _GraphValue<Self>,
inputs: _ViewInputs,
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
) -> _ViewOutputs {
let fields = DynamicPropertyCache.fields(of: Self.self)
var inputs = inputs
let (view, buffer) = makeBody(modifier: modifier, inputs: &inputs.base, fields: fields)
inputs.append(.view(body), to: BodyInput<Content>.self)
let outputs = Body.makeDebuggableView(view: view, inputs: inputs)
if let buffer {
buffer.traceMountedProperties(to: modifier, fields: fields)
}
return outputs
}
nonisolated package static func makeViewList(
modifier: _GraphValue<Self>,
inputs: _ViewListInputs,
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
) -> _ViewListOutputs {
let fields = DynamicPropertyCache.fields(of: Self.self)
var inputs = inputs
let (view, buffer) = makeBody(modifier: modifier, inputs: &inputs.base, fields: fields)
inputs.base.append(.list(body), to: BodyInput<Content>.self)
let outputs = Body.makeDebuggableViewList(view: view, inputs: inputs)
if let buffer {
buffer.traceMountedProperties(to: modifier, fields: fields)
}
return outputs
}
nonisolated package static func viewListCount(
inputs: _ViewListCountInputs,
body: (_ViewListCountInputs) -> Int?
) -> Int? {
withoutActuallyEscaping(body) { escapingBody in
var inputs = inputs
inputs.append(escapingBody, to: BodyCountInput<Self>.self)
return Body._viewListCount(inputs: inputs)
}
}
nonisolated private static func makeBody(
modifier: _GraphValue<Self>,
inputs: inout _GraphInputs,
fields: DynamicPropertyCache.Fields
) -> (_GraphValue<Body>, _DynamicPropertyBuffer?) {
let kind = Metadata(Self.self).kind
switch kind {
case .struct, .enum, .optional, .tuple:
return ModifierBodyAccessor().makeBody(container: modifier, inputs: &inputs, fields: fields)
default:
preconditionFailure("view modifiers must be value types: \(Self.self)")
}
}
}
// MARK: - ViewModifierContentProvider
private protocol ViewModifierContentProvider: PrimitiveView {}
extension _ViewModifier_Content: ViewModifierContentProvider {}
extension PlaceholderContentView: ViewModifierContentProvider {}
extension ViewModifierContentProvider {
nonisolated fileprivate static func providerMakeView(
view: _GraphValue<Self>,
inputs: _ViewInputs
) -> _ViewOutputs {
let graphInputs = inputs.base
var inputs = inputs
guard let body = inputs.popLast(BodyInput<Self>.self) else {
return _ViewOutputs()
}
switch body {
case let .view(makeViewBody):
return makeViewBody(_Graph(), inputs)
case let .list(makeViewListBody):
return .multiView(inputs: inputs) {
makeViewListBody($0, .init($1.base, options: graphInputs.viewListOptions))
}
}
}
nonisolated fileprivate static func providerMakeViewList(
view: _GraphValue<Self>,
inputs: _ViewListInputs
) -> _ViewListOutputs {
var inputs = inputs
guard let body = inputs.base.popLast(BodyInput<Self>.self) else {
return .emptyViewList(inputs: inputs)
}
switch body {
case let .view(makeViewBody):
return .unaryViewList(
viewType: Self.self,
inputs: inputs
) {
makeViewBody(_Graph(), $0)
}
case let .list(makeViewListBody):
return makeViewListBody(_Graph(), inputs)
}
}
nonisolated fileprivate static func providerViewListCount(
inputs: _ViewListCountInputs
) -> Int? {
var inputs = inputs
guard let input = inputs.popLast(BodyCountInput<Self>.self) else {
return 0
}
guard !inputs.customModifierTypes.contains(ObjectIdentifier(Self.self)) else {
return nil
}
inputs.customModifierTypes.append(ObjectIdentifier(Self.self))
return input(inputs)
}
}
// MARK: - BodyInput
private struct BodyInput<V>: ViewInput {
static var defaultValue: Stack<BodyInputElement> { Stack() }
}
// MARK: - BodyInputElement
private enum BodyInputElement: GraphReusable, Equatable {
typealias MakeViewBody = (_Graph, _ViewInputs) -> _ViewOutputs
typealias MakeViewListBody = (_Graph, _ViewListInputs) -> _ViewListOutputs
case view(MakeViewBody)
case list(MakeViewListBody)
static var isTriviallyReusable: Bool {
_SemanticFeature_v5.isEnabled
}
func makeReusable(indirectMap: IndirectAttributeMap) {
return
}
func tryToReuse(by other: BodyInputElement, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool {
switch self {
case let .view(makeViewBody):
guard case let .view(otherMakeViewBody) = other else {
ReuseTrace.traceReuseInternalFailure()
return false
}
return Self.isTriviallyReusable || compareValues(makeViewBody, otherMakeViewBody)
case let .list(makeViewListBody):
guard case let .list(otherMakeViewListBody) = other else {
ReuseTrace.traceReuseInternalFailure()
return false
}
return Self.isTriviallyReusable || compareValues(makeViewListBody, otherMakeViewListBody)
}
}
static func == (lhs: BodyInputElement, rhs: BodyInputElement) -> Bool {
if case let .view(lhsBody) = lhs, case let .view(rhsBody) = rhs {
compareValues(lhsBody, rhsBody)
} else if case let .list(lhsBody) = lhs, case let .list(rhsBody) = rhs{
compareValues(lhsBody, rhsBody)
} else {
false
}
}
}
// MARK: - BodyCountInput
private struct BodyCountInput<V>: ViewInput {
static var defaultValue: Stack<(_ViewListCountInputs) -> Int?> { .init() }
}
// MARK: - ModifierBodyAccessor
private struct ModifierBodyAccessor<Container>: BodyAccessor where Container: ViewModifier {
typealias Body = Container.Body
func updateBody(of container: Container, changed: Bool) {
guard changed else {
return
}
setBody {
container.body(content: Container.Content())
}
}
}