Skip to content

Commit d8c5824

Browse files
committed
Update AnyView and ViewDebug.makeView
1 parent 0cb73bb commit d8c5824

File tree

8 files changed

+171
-28
lines changed

8 files changed

+171
-28
lines changed

Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public struct _GraphInputs {
66
var cachedEnvironment: MutableBox<CachedEnvironment>
77
var phase: Attribute<_GraphInputs.Phase>
88
var transaction: Attribute<Transaction>
9-
var changedDebugProperties: _ViewDebug.Properties
9+
private var changedDebugProperties: _ViewDebug.Properties
1010
private var options: _GraphInputs.Options
1111
#if canImport(Darwin) // FIXME: See #39
1212
var mergedInputs: Set<OGAttribute>
@@ -55,14 +55,31 @@ public struct _GraphInputs {
5555
set { customInputs[type] = newValue }
5656
}
5757

58+
// MARK: - cachedEnvironment
59+
5860
@inline(__always)
5961
func detechedEnvironmentInputs() -> Self {
6062
var newInputs = self
6163
newInputs.cachedEnvironment = MutableBox(cachedEnvironment.wrappedValue)
6264
return newInputs
6365
}
6466

65-
// MARK: Options
67+
@inline(__always)
68+
mutating func updateCachedEnvironment(_ box: MutableBox<CachedEnvironment>) {
69+
cachedEnvironment = box
70+
changedDebugProperties.insert(.environment)
71+
}
72+
73+
// MARK: - changedDebugProperties
74+
75+
@inline(__always)
76+
func withEmptyChangedDebugPropertiesInputs<R>(_ body: (_GraphInputs) -> R) -> R {
77+
var inputs = self
78+
inputs.changedDebugProperties = []
79+
return body(inputs)
80+
}
81+
82+
// MARK: - options
6683

6784
@inline(__always)
6885
var enableLayout: Bool {

Sources/OpenSwiftUI/Core/View/CustomView.swift

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ internal import OpenGraphShims
1010

1111
extension View {
1212
static func makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
13-
var inputs = inputs
14-
inputs.base.changedDebugProperties = []
1513
let fields = DynamicPropertyCache.fields(of: Self.self)
16-
let (body, buffer) = makeBody(view: view, inputs: &inputs.base, fields: fields)
17-
OGSubgraph.beginTreeElement(value: body.value, flags: 0)
18-
var outputs = Body._makeView(view: body, inputs: inputs)
19-
if OGSubgraph.shouldRecordTree {
20-
_ViewDebug.reallyWrap(&outputs, value: body, inputs: &inputs) // FIXME
21-
OGSubgraph.endTreeElement(value: body.value)
14+
var inputs = inputs
15+
let (body, buffer) = inputs.withMutateGraphInputs { inputs in
16+
makeBody(view: view, inputs: &inputs, fields: fields)
17+
}
18+
let outputs = _ViewDebug.makeView(
19+
view: body,
20+
inputs: inputs
21+
) { view, inputs in
22+
Body._makeView(view: body, inputs: inputs)
2223
}
2324
if let buffer {
2425
buffer.traceMountedProperties(to: body, fields: fields)

Sources/OpenSwiftUI/Core/View/ViewGraph.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ final class ViewGraph: GraphHost {
215215
as: RootGeometry.self,
216216
invalidating: true
217217
) { rootGeometry in
218-
// FIXME
219-
rootGeometry.$layoutDirection = inputs.base.cachedEnvironment.wrappedValue.attribute(keyPath: \.layoutDirection)
218+
inputs.withMutableCachedEnviroment {
219+
rootGeometry.$layoutDirection = $0.attribute(keyPath: \.layoutDirection)
220+
}
220221
}
221222
// TOOD
222223
return makeRootView(rootView, inputs)

Sources/OpenSwiftUI/Core/View/ViewInputs.swift

+67-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
internal import OpenGraphShims
22

33
public struct _ViewInputs {
4-
var base: _GraphInputs
4+
private var base: _GraphInputs
55
var preferences: PreferencesInputs
66
var transform: Attribute<ViewTransform>
77
var position: Attribute<ViewOrigin>
88
var containerPosition: Attribute<ViewOrigin>
99
var size: Attribute<ViewSize>
1010
var safeAreaInsets: OptionalAttribute<SafeAreaInsets>
1111

12-
@inline(__always)
13-
func detechedEnvironmentInputs() -> Self {
14-
var newInputs = self
15-
newInputs.base = self.base.detechedEnvironmentInputs()
16-
return newInputs
12+
init(
13+
base: _GraphInputs,
14+
preferences: PreferencesInputs,
15+
transform: Attribute<ViewTransform>,
16+
position: Attribute<ViewOrigin>,
17+
containerPosition: Attribute<ViewOrigin>,
18+
size: Attribute<ViewSize>,
19+
safeAreaInsets: OptionalAttribute<SafeAreaInsets>
20+
) {
21+
self.base = base
22+
self.preferences = preferences
23+
self.transform = transform
24+
self.position = position
25+
self.containerPosition = containerPosition
26+
self.size = size
27+
self.safeAreaInsets = safeAreaInsets
1728
}
1829

1930
func makeIndirectOutputs() -> _ViewOutputs {
@@ -36,7 +47,56 @@ public struct _ViewInputs {
3647
}
3748
return outputs
3849
}
39-
50+
51+
// MARK: - base
52+
53+
@inline(__always)
54+
mutating func withMutateGraphInputs<R>(_ body: (inout _GraphInputs) -> R) -> R {
55+
body(&base)
56+
}
57+
58+
// MARK: - base.customInputs
59+
60+
@inline(__always)
61+
func withCustomInputs<R>(_ body: (PropertyList) -> R) -> R {
62+
body(base.customInputs)
63+
}
64+
65+
@inline(__always)
66+
mutating func withMutableCustomInputs<R>(_ body: (inout PropertyList) -> R) -> R {
67+
body(&base.customInputs)
68+
}
69+
70+
// MARK: - base.cachedEnvironment
71+
72+
@inline(__always)
73+
func withCachedEnviroment<R>(_ body: (CachedEnvironment) -> R) -> R {
74+
body(base.cachedEnvironment.wrappedValue)
75+
}
76+
77+
@inline(__always)
78+
func withMutableCachedEnviroment<R>(_ body: (inout CachedEnvironment) -> R) -> R {
79+
body(&base.cachedEnvironment.wrappedValue)
80+
}
81+
82+
@inline(__always)
83+
func detechedEnvironmentInputs() -> Self {
84+
var newInputs = self
85+
newInputs.base = self.base.detechedEnvironmentInputs()
86+
return newInputs
87+
}
88+
89+
// MARK: - base.changedDebugProperties
90+
91+
@inline(__always)
92+
func withEmptyChangedDebugPropertiesInputs<R>(_ body: (_ViewInputs) -> R) -> R {
93+
var newInputs = self
94+
return base.withEmptyChangedDebugPropertiesInputs {
95+
newInputs.base = $0
96+
return body(newInputs)
97+
}
98+
}
99+
40100
// MARK: Options
41101

42102
@inline(__always)

Sources/OpenSwiftUI/Data/Environment/EnvironmentKeyTransformModifier.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public struct _EnvironmentKeyTransformModifier<Value>: PrimitiveViewModifier, _G
2727
)
2828
let attribute = Attribute(childEnvironment)
2929
let cachedEnvironment = CachedEnvironment(attribute)
30-
inputs.cachedEnvironment = MutableBox(cachedEnvironment)
31-
inputs.changedDebugProperties.insert(.environment)
30+
inputs.updateCachedEnvironment(MutableBox(cachedEnvironment))
3231
}
3332
}
3433

Sources/OpenSwiftUI/Data/Preference/PreferenceBridge.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ final class PreferenceBridge {
152152
}
153153

154154
func wrapInputs(_ inputs: inout _ViewInputs) {
155-
inputs.base.customInputs = bridgedViewInputs
155+
inputs.withMutableCustomInputs { $0 = bridgedViewInputs }
156156
inputs.preferences.merge(requestedPreferences)
157157
inputs.preferences.hostKeys = Attribute(MergePreferenceKeys(lhs: inputs.preferences.hostKeys, rhs: _hostPreferenceKeys))
158158
}
@@ -165,7 +165,7 @@ final class PreferenceBridge {
165165
result = Attribute(PreferenceCombiner<Key>(attributes: [])).identifier
166166
}
167167
}
168-
bridgedViewInputs = inputs.base.customInputs
168+
inputs.withCustomInputs { bridgedViewInputs = $0 }
169169
for key in inputs.preferences.keys {
170170
if key == _AnyPreferenceKey<HostPreferencesKey>.self {
171171
let combiner = Attribute(HostPreferencesCombiner(

Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift

+25-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,31 @@ extension _ViewDebug {
4141
OGSubgraph.setShouldRecordTree()
4242
}
4343
}
44-
45-
// TODO:
46-
/*private*/ static func reallyWrap<Value>(_: inout _ViewOutputs, value: _GraphValue<Value>, inputs _: UnsafePointer<_ViewInputs>) {}
44+
45+
@_transparent
46+
@inline(__always)
47+
static func makeView<Value>(
48+
view: _GraphValue<Value>,
49+
inputs: _ViewInputs,
50+
body: (_ view: _GraphValue<Value>, _ inputs: _ViewInputs) -> _ViewOutputs
51+
) -> _ViewOutputs {
52+
var inputs = inputs
53+
if OGSubgraph.shouldRecordTree {
54+
OGSubgraph.beginTreeElement(value: view.value, flags: 0)
55+
}
56+
var outputs = inputs.withEmptyChangedDebugPropertiesInputs { inputs in
57+
body(view, inputs)
58+
}
59+
if OGSubgraph.shouldRecordTree {
60+
_ViewDebug.reallyWrap(&outputs, value: view, inputs: &inputs)
61+
OGSubgraph.endTreeElement(value: view.value)
62+
}
63+
return outputs
64+
}
65+
66+
private static func reallyWrap<Value>(_: inout _ViewOutputs, value: _GraphValue<Value>, inputs _: UnsafePointer<_ViewInputs>) {
67+
// TODO
68+
}
4769

4870
fileprivate static func appendDebugData(from: Int/*AGTreeElement*/ , to: [_ViewDebug.Data]) {}
4971
}

Sources/OpenSwiftUI/View/View/AnyView.swift

+45-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class AnyViewStorageBase {
7070
fileprivate var canTransition: Bool { fatalError() }
7171
fileprivate func matches(_ other: AnyViewStorageBase) -> Bool { fatalError() }
7272
fileprivate func makeChild(
73-
uniqueID: UInt32,
73+
uniqueId: UInt32,
7474
container: Attribute<AnyViewInfo>,
7575
inputs: _ViewInputs
7676
) -> _ViewOutputs {
@@ -104,6 +104,21 @@ private final class AnyViewStorage<V: View>: AnyViewStorageBase {
104104
other is AnyViewStorage<V>
105105
}
106106

107+
override func makeChild(
108+
uniqueId: UInt32,
109+
container: Attribute<AnyViewInfo>,
110+
inputs: _ViewInputs
111+
) -> _ViewOutputs {
112+
let child = AnyViewChild<V>(info: container, uniqueId: uniqueId)
113+
let graphValue = _GraphValue(Attribute(child))
114+
return _ViewDebug.makeView(
115+
view: graphValue,
116+
inputs: inputs
117+
) { view, inputs in
118+
V._makeView(view: view, inputs: inputs)
119+
}
120+
}
121+
107122
override func child<Value>() -> Value { view as! Value }
108123

109124
// TODO
@@ -155,7 +170,11 @@ private struct AnyViewContainer: StatefulRule, AsyncAttribute {
155170
parentSubgraph.addChild(childGraph)
156171
return childGraph.apply {
157172
let childInputs = inputs.detechedEnvironmentInputs()
158-
let childOutputs = storage.makeChild(uniqueID: uniqueId, container: current.unsafeCast(to: AnyViewInfo.self), inputs: childInputs)
173+
let childOutputs = storage.makeChild(
174+
uniqueId: uniqueId,
175+
container: current.unsafeCast(to: AnyViewInfo.self),
176+
inputs: childInputs
177+
)
159178
outputs.attachIndirectOutputs(to: childOutputs)
160179
return AnyViewInfo(item: storage, subgraph: childGraph, uniqueID: uniqueId)
161180
}
@@ -168,3 +187,27 @@ private struct AnyViewContainer: StatefulRule, AsyncAttribute {
168187
subgraph.invalidate()
169188
}
170189
}
190+
191+
private struct AnyViewChild<V: View>: StatefulRule, AsyncAttribute {
192+
@Attribute var info: AnyViewInfo
193+
let uniqueId: UInt32
194+
195+
typealias Value = V
196+
197+
func updateValue() {
198+
guard uniqueId == info.uniqueID else {
199+
return
200+
}
201+
value = info.item.child()
202+
}
203+
}
204+
205+
extension AnyViewChild: CustomStringConvertible {
206+
var description: String { "\(V.self)" }
207+
}
208+
209+
// TODO
210+
private struct AnyViewChildList<V: View> {
211+
@Attribute var view: AnyView
212+
var id: UniqueID?
213+
}

0 commit comments

Comments
 (0)