Skip to content

Commit 82cb56c

Browse files
authored
Add BodyAccessor featuere (#60)
* Update hostPreferenceValues And connect the output with the graph * Add ViewBodyAccessor * Add AppBodyAccessor
1 parent 93b09bf commit 82cb56c

File tree

11 files changed

+100
-12
lines changed

11 files changed

+100
-12
lines changed

Example/HostingExample/ViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ class ViewController: UINavigationController {
2121

2222
struct ContentView: View {
2323
var body: some View {
24-
EmptyView()
24+
AnyView(EmptyView())
2525
}
2626
}

Package.resolved

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
88
"state" : {
99
"branch" : "main",
10-
"revision" : "c952860989b69c4537146b323c0ec1cb3f8fb206"
10+
"revision" : "315468c6339cdbcbaf7d8797b866745d5bf11d8e"
1111
}
1212
},
1313
{

Sources/OpenSwiftUI/App/App.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public protocol App {
9393
/// Swift infers the app's ``OpenSwiftUI/App/Body-swift.associatedtype``
9494
/// associated type based on the scene provided by the `body` property.
9595
@SceneBuilder
96-
@MainActor
96+
@MainActor(unsafe)
9797
var body: Self.Body { get }
9898

9999
/// Creates an instance of the app using the body that you define for its
@@ -102,7 +102,7 @@ public protocol App {
102102
/// Swift synthesizes a default initializer for structures that don't
103103
/// provide one. You typically rely on the default initializer for
104104
/// your app.
105-
@MainActor
105+
@MainActor(unsafe)
106106
init()
107107
}
108108

@@ -115,7 +115,7 @@ extension App {
115115
/// the app. OpenSwiftUI provides a
116116
/// default implementation of the method that manages the launch process in
117117
/// a platform-appropriate way.
118-
@MainActor
118+
@MainActor(unsafe)
119119
public static func main() {
120120
let app = Self()
121121
runApp(app)

Sources/OpenSwiftUI/App/AppGraph.swift

+13
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,16 @@ final class AppGraph: GraphHost {
6565
}
6666
}
6767
}
68+
69+
private struct AppBodyAccessor<Container: App>: BodyAccessor {
70+
typealias Body = Container.Body
71+
72+
func updateBody(of container: Container, changed: Bool) {
73+
guard changed else {
74+
return
75+
}
76+
setBody {
77+
container.body
78+
}
79+
}
80+
}

Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessor.swift

+13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,21 @@
55
// Audited for RELEASE_2021
66
// Status: Complete
77

8+
internal import OpenGraphShims
9+
810
protocol BodyAccessor<Container, Body> {
911
associatedtype Container
1012
associatedtype Body
1113
func updateBody(of: Container, changed: Bool)
1214
}
15+
16+
extension BodyAccessor {
17+
func setBody(_ body: () -> Body) {
18+
let value = traceRuleBody(Container.self) {
19+
OGGraph.withoutUpdate(body)
20+
}
21+
withUnsafePointer(to: value) { value in
22+
OGGraph.setOutputValue(value)
23+
}
24+
}
25+
}

Sources/OpenSwiftUI/Core/BodyAccessor/ViewBodyAccessor.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
// OpenSwiftUI
44
//
55
// Audited for RELEASE_2021
6-
// Status: WIP
6+
// Status: Complete
77

88
struct ViewBodyAccessor<Container: View>: BodyAccessor {
99
typealias Body = Container.Body
1010

11-
func updateBody(of: Container, changed: Bool) {
11+
func updateBody(of container: Container, changed: Bool) {
1212
guard changed else {
1313
return
1414
}
15-
// TODO
15+
setBody {
16+
container.body
17+
}
1618
}
1719
}

Sources/OpenSwiftUI/Core/Log/Signpost.swift

+43-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Status: WIP
77
// ID: 34756F646CF7AC3DBE2A8E0B344C962F
88

9+
internal import OpenGraphShims
910
#if canImport(os)
1011
import os.signpost
1112
#endif
@@ -26,7 +27,37 @@ struct Signpost {
2627

2728
static let render = Signpost(style: .kdebug(0), stability: .published)
2829
static let renderUpdate = Signpost(style: .kdebug(0), stability: .published)
29-
static let viewHost = Signpost(style: .kdebug(0), stability: .published)
30+
static let viewHost = Signpost(style: .kdebug(9), stability: .published)
31+
static let bodyInvoke = Signpost(style: .kdebug(5), stability: .published)
32+
33+
@_transparent
34+
@inline(__always)
35+
func traceInterval<R>(
36+
object: AnyObject? = nil,
37+
_ message: StaticString?,
38+
closure: () -> R
39+
) -> R {
40+
guard isEnabled else {
41+
return closure()
42+
}
43+
// TODO
44+
return closure()
45+
}
46+
47+
@_transparent
48+
@inline(__always)
49+
func traceInterval<R>(
50+
object: AnyObject? = nil,
51+
_ message: StaticString?,
52+
_ arguments: @autoclosure () -> [CVarArg],
53+
closure: () -> R
54+
) -> R {
55+
guard isEnabled else {
56+
return closure()
57+
}
58+
// TODO
59+
return closure()
60+
}
3061
}
3162

3263
extension Signpost {
@@ -43,4 +74,14 @@ extension Signpost {
4374
}
4475
}
4576

46-
77+
@_transparent
78+
@inline(__always)
79+
// FIXME
80+
func traceRuleBody<R>(_ type: Any.Type, body: () -> R) -> R {
81+
Signpost.bodyInvoke.traceInterval(
82+
"%{public}@.body [in %{public}@]",
83+
[OGTypeID(type).description, Tracing.libraryName(defining: type)]
84+
) {
85+
body()
86+
}
87+
}

Sources/OpenSwiftUI/Core/View/View.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public protocol View {
7171
/// For more information about composing views and a view hierarchy,
7272
/// see <doc:Declaring-a-Custom-View>.
7373
@ViewBuilder
74-
@MainActor
74+
@MainActor(unsafe)
7575
var body: Self.Body { get }
7676
}
7777

Sources/OpenSwiftUI/Core/View/ViewGraph.swift

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ final class ViewGraph: GraphHost {
229229
rootGeometry.$childLayoutComputer = outputs.$layoutComputer
230230
}
231231
// TODO
232+
hostPreferenceValues.projectedValue = outputs.hostPreferences
232233
makePreferenceOutlets(outputs: outputs)
233234
#endif
234235
}

Sources/OpenSwiftUI/Data/Preference/HostPreferencesKey.swift

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// Status: Complete
77
// ID: 7429200566949B8FB892A77E01A988C8
88

9+
internal import OpenGraphShims
10+
911
struct HostPreferencesKey: PreferenceKey {
1012
static var defaultValue: PreferenceList {
1113
PreferenceList()
@@ -25,3 +27,19 @@ extension HostPreferencesKey {
2527
return nodeId
2628
}
2729
}
30+
31+
extension _ViewOutputs {
32+
@inline(__always)
33+
var hostPreferences: Attribute<PreferenceList>? {
34+
get { self[HostPreferencesKey.self] }
35+
set { self[HostPreferencesKey.self] = newValue }
36+
}
37+
}
38+
39+
extension PreferencesOutputs {
40+
@inline(__always)
41+
var hostPreferences: Attribute<PreferenceList>? {
42+
get { self[HostPreferencesKey.self] }
43+
set { self[HostPreferencesKey.self] = newValue }
44+
}
45+
}

Sources/OpenSwiftUI/Data/Preference/PreferenceBridge.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ final class PreferenceBridge {
173173
values: OptionalAttribute(base: AnyOptionalAttribute(outputs[anyKey: key])),
174174
children: []
175175
))
176-
outputs[anyKey: key] = combiner.identifier
176+
outputs.hostPreferences = combiner
177177
$hostPreferenceKeys = inputs.preferences.hostKeys
178178
$hostPreferencesCombiner = combiner
179179
} else {

0 commit comments

Comments
 (0)