Skip to content

Commit 93b09bf

Browse files
authored
Add AnyView implementation (#59)
* Update ViewDebug and OG version * Update View location * Update View Accessor * Add AnyViewInfo and AnyViewStorageBase * Update AnyViewContainer * Add intern implementation * Update AnyView._makeView * Update AnyViewStorage * Update AnyView and ViewDebug.makeView * Fix GraphHost crash bug * Fix Linux build issue
1 parent 14597bb commit 93b09bf

21 files changed

+620
-77
lines changed

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// ViewBodyAccessor.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
struct ViewBodyAccessor<Container: View>: BodyAccessor {
9+
typealias Body = Container.Body
10+
11+
func updateBody(of: Container, changed: Bool) {
12+
guard changed else {
13+
return
14+
}
15+
// TODO
16+
}
17+
}

Sources/OpenSwiftUI/Core/Graph/GraphHost.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ class GraphHost {
181181
}
182182

183183
final func intern<Value>(_ value: Value, id: _GraphInputs.ConstantID) -> Attribute<Value> {
184-
fatalError("TODO")
184+
let id = id.internID
185+
return data.globalSubgraph.apply {
186+
data.inputs.intern(value, id: id.internID)
187+
}
185188
}
186189

187190
final func setNeedsUpdate(mayDeferUpdate: Bool) {
@@ -385,7 +388,7 @@ private final class AsyncTransaction {
385388

386389
extension OGGraph {
387390
final func graphHost() -> GraphHost {
388-
context!.assumingMemoryBound(to: GraphHost.self).pointee
391+
unsafeBitCast(context, to: GraphHost.self)
389392
}
390393

391394
fileprivate final func setGraphHost(_ graphHost: GraphHost) {

Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public struct _GraphInputs {
66
var cachedEnvironment: MutableBox<CachedEnvironment>
77
var phase: Attribute<_GraphInputs.Phase>
88
var transaction: Attribute<Transaction>
9-
var changedDebugProperties: _ViewDebug.Properties
10-
var options: _GraphInputs.Options
9+
private var changedDebugProperties: _ViewDebug.Properties
10+
private var options: _GraphInputs.Options
1111
#if canImport(Darwin) // FIXME: See #39
1212
var mergedInputs: Set<OGAttribute>
1313
#endif
@@ -54,6 +54,38 @@ public struct _GraphInputs {
5454
get { customInputs[type] }
5555
set { customInputs[type] = newValue }
5656
}
57+
58+
// MARK: - cachedEnvironment
59+
60+
@inline(__always)
61+
func detechedEnvironmentInputs() -> Self {
62+
var newInputs = self
63+
newInputs.cachedEnvironment = MutableBox(cachedEnvironment.wrappedValue)
64+
return newInputs
65+
}
66+
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
83+
84+
@inline(__always)
85+
var enableLayout: Bool {
86+
get { options.contains(.enableLayout) }
87+
// TODO: setter
88+
}
5789
}
5890

5991
extension _GraphInputs {
@@ -65,9 +97,20 @@ extension _GraphInputs {
6597
extension _GraphInputs {
6698
struct Options: OptionSet {
6799
let rawValue: UInt32
100+
101+
static var enableLayout: Options { Options(rawValue: 1 << 1) }
68102
}
69103
}
70104

71105
extension _GraphInputs {
72106
typealias ConstantID = Int
107+
108+
func intern<Value>(_ value: Value, id: ConstantID) -> Attribute<Value> {
109+
cachedEnvironment.wrappedValue.intern(value, id: id.internID)
110+
}
111+
}
112+
113+
extension _GraphInputs.ConstantID {
114+
@inline(__always)
115+
var internID: Self { self & 0x1 }
73116
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// CustomView.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
// ID: 9F92ACD17B554E8AB7D29ABB1E796415
8+
9+
internal import OpenGraphShims
10+
11+
extension View {
12+
static func makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
13+
let fields = DynamicPropertyCache.fields(of: Self.self)
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)
23+
}
24+
if let buffer {
25+
buffer.traceMountedProperties(to: body, fields: fields)
26+
}
27+
return outputs
28+
}
29+
30+
private static func makeBody(
31+
view: _GraphValue<Self>,
32+
inputs: inout _GraphInputs,
33+
fields: DynamicPropertyCache.Fields
34+
) -> (_GraphValue<Body>, _DynamicPropertyBuffer?) {
35+
let kind = OGTypeID(Self.self).kind
36+
switch kind {
37+
case .struct, .enum, .optional, .tuple:
38+
let accessor = ViewBodyAccessor<Self>()
39+
return accessor.makeBody(container: view, inputs: &inputs, fields: fields)
40+
default:
41+
fatalError("views must be value types (either a struct or an enum); \(Self.self) is a class.")
42+
}
43+
}
44+
}

Sources/OpenSwiftUI/Core/View/TODO/_ViewInputs.swift

Lines changed: 0 additions & 11 deletions
This file was deleted.

Sources/OpenSwiftUI/Core/View/TODO/_ViewOutputs.swift

Lines changed: 0 additions & 12 deletions
This file was deleted.

Sources/OpenSwiftUI/Core/View/TODO/View.swift renamed to Sources/OpenSwiftUI/Core/View/View.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/// }
2020
///
2121
/// Assemble the view's body by combining one or more of the built-in views
22-
/// provided by SwiftUI, like the ``Text`` instance in the example above, plus
22+
/// provided by OpenSwiftUI, like the ``Text`` instance in the example above, plus
2323
/// other custom views that you define, into a hierarchy of views. For more
2424
/// information about creating custom views, see <doc:Declaring-a-Custom-View>.
2525
///
@@ -43,16 +43,18 @@
4343
/// custom view modifiers for easy reuse.
4444
@_typeEraser(AnyView)
4545
public protocol View {
46-
static func _makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs
47-
static func _makeViewList(view: _GraphValue<Self>, inputs: _ViewListInputs) -> _ViewListOutputs
48-
static func _viewListCount(inputs: _ViewListCountInputs) -> Int?
49-
5046
/// The type of view representing the body of this view.
5147
///
5248
/// When you create a custom view, Swift infers this type from your
5349
/// implementation of the required ``View/body-swift.property`` property.
5450
associatedtype Body: View
5551

52+
static func _makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs
53+
54+
static func _makeViewList(view: _GraphValue<Self>, inputs: _ViewListInputs) -> _ViewListOutputs
55+
56+
static func _viewListCount(inputs: _ViewListCountInputs) -> Int?
57+
5658
/// The content and behavior of the view.
5759
///
5860
/// When you implement a custom view, you must implement a computed
@@ -73,24 +75,25 @@ public protocol View {
7375
var body: Self.Body { get }
7476
}
7577

76-
// FIXME
7778
extension View {
7879
public static func _makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
79-
.init()
80+
makeView(view: view, inputs: inputs)
8081
}
82+
8183
public static func _makeViewList(view: _GraphValue<Self>, inputs: _ViewListInputs) -> _ViewListOutputs {
82-
.init()
84+
fatalError("TODO")
8385
}
86+
8487
public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? {
85-
nil
88+
Body._viewListCount(inputs: inputs)
8689
}
8790
}
8891

92+
93+
// MARK: - Never + View
94+
8995
extension Never: View {
90-
public var body: Never {
91-
// FIXME: should be "brk #1"
92-
fatalError()
93-
}
96+
public var body: Never { self }
9497
}
9598

9699
extension View {

Sources/OpenSwiftUI/Core/View/ViewGraph.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class ViewGraph: GraphHost {
2828
@Attribute var dimensions: ViewSize
2929
@Attribute var updateSeed: UInt32
3030
// TODO
31+
@Attribute var defaultLayoutComputer: LayoutComputer
32+
// TODO
3133
var cachedSizeThatFits: CGSize = .invalidValue
3234
var sizeThatFitsObserver: SizeThatFitsObserver? {
3335
didSet {
@@ -72,6 +74,8 @@ final class ViewGraph: GraphHost {
7274
_position = _rootGeometry.origin()
7375
_dimensions = _rootGeometry.size()
7476
_updateSeed = Attribute(value: .zero)
77+
// TODO
78+
_defaultLayoutComputer = Attribute(value: .defaultValue)
7579
// FIXME
7680
makeRootView = { view, inputs in
7781
let rootView = _GraphValue<Body>(view.unsafeCast(to: Body.self))
@@ -200,7 +204,7 @@ final class ViewGraph: GraphHost {
200204
)
201205
if requestedOutputs.contains(.layout) {
202206
// FIXME
203-
inputs.base.options.formUnion(.init(rawValue: 0xe2))
207+
// inputs.base.options.formUnion(.init(rawValue: 0xe2))
204208
}
205209
requestedOutputs.addRequestedPreferences(to: &inputs)
206210
_preferenceBridge?.wrapInputs(&inputs)
@@ -211,8 +215,9 @@ final class ViewGraph: GraphHost {
211215
as: RootGeometry.self,
212216
invalidating: true
213217
) { rootGeometry in
214-
// FIXME
215-
rootGeometry.$layoutDirection = inputs.base.cachedEnvironment.wrappedValue.attribute(keyPath: \.layoutDirection)
218+
inputs.withMutableCachedEnviroment {
219+
rootGeometry.$layoutDirection = $0.attribute(keyPath: \.layoutDirection)
220+
}
216221
}
217222
// TOOD
218223
return makeRootView(rootView, inputs)

0 commit comments

Comments
 (0)