Skip to content

Commit bf76c38

Browse files
authored
Update UIKit+Representable interface and documentation (#224)
* Add PlatformViewRepresentableLayoutOptions * Update UIViewRepresentable * Update PlatformViewRepresentable * Update UIViewControllerRepresentable * Fix Linux platform build issue
1 parent bced927 commit bf76c38

File tree

6 files changed

+687
-129
lines changed

6 files changed

+687
-129
lines changed

Sources/OpenSwiftUI/Integration/Representable/Platform/PlatformViewRepresentable.swift

Lines changed: 122 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,156 @@
22
// PlatformViewRepresentable.swift
33
// OpenSwiftUI
44
//
5-
// Audited for iOS 15.5
5+
// Audited for iOS 18.0
66
// Status: WIP
7+
// ID: A513612C07DFA438E70B9FA90719B40D (SwiftUI)
78

8-
#if os(macOS)
9+
#if canImport(AppKit)
910
import AppKit
1011
typealias PlatformView = NSView
11-
#elseif os(iOS)
12+
typealias PlatformViewController = NSViewController
13+
#elseif canImport(UIKit)
1214
import UIKit
1315
typealias PlatformView = UIView
16+
typealias PlatformViewController = UIViewController
1417
#else
1518
import Foundation
16-
typealias PlatformView = Void
19+
typealias PlatformView = NSObject
20+
typealias PlatformViewController = NSObject
1721
#endif
18-
1922
import OpenSwiftUICore
23+
import OpenGraphShims
24+
25+
// MARK: - PlatformViewRepresentable
2026

2127
protocol PlatformViewRepresentable: View {
28+
static var dynamicProperties: DynamicPropertyCache.Fields { get }
29+
2230
associatedtype PlatformViewProvider
31+
2332
associatedtype Coordinator
24-
25-
static var dynamicProperties: DynamicPropertyCache.Fields { get }
26-
func makeViewProvider(context: PlatformViewRepresentableContext<Self>) -> PlatformViewProvider
27-
func updateViewProvider(_ provider: PlatformViewProvider, context: PlatformViewRepresentableContext<Self>)
33+
34+
func makeViewProvider(context: Context) -> PlatformViewProvider
35+
36+
func updateViewProvider(_ provider: PlatformViewProvider, context: Context)
37+
2838
func resetViewProvider(_ provider: PlatformViewProvider, coordinator: Coordinator, destroy: () -> Void)
29-
static func dismantleViewProvider(_: PlatformViewProvider, coordinator: Coordinator)
30-
static func platformView(for: PlatformViewProvider) -> PlatformView
39+
40+
static func dismantleViewProvider(_ provider: PlatformViewProvider, coordinator: Coordinator)
41+
42+
static func platformView(for provider: PlatformViewProvider) -> PlatformView
43+
3144
func makeCoordinator() -> Coordinator
32-
// func _identifiedViewTree(in: PlatformViewProvider) -> _IdentifiedViewTree
33-
func overrideSizeThatFits(_ size: inout CGSize, in: _ProposedSize, platformView: PlatformViewProvider)
34-
// func overrideLayoutTraits(_ traits: inout _LayoutTraits, for provider: PlatformViewProvider)
35-
45+
46+
func _identifiedViewTree(in provider: PlatformViewProvider) -> _IdentifiedViewTree
47+
48+
func sizeThatFits(_ proposal: ProposedViewSize, provider: PlatformViewProvider, context: PlatformViewRepresentableContext<Self>) -> CGSize?
49+
50+
func overrideSizeThatFits(_ size: inout CGSize, in proposedSize: _ProposedSize, platformView: PlatformViewProvider)
51+
52+
func overrideLayoutTraits(_ traits: inout _LayoutTraits, for provider: PlatformViewProvider)
53+
3654
static func modifyBridgedViewInputs(_ inputs: inout _ViewInputs)
55+
3756
static var isViewController: Bool { get }
38-
// static var safeAreaMode: _PlatformViewRepresentable_SafeAreaMode { get }
57+
58+
static func shouldEagerlyUpdateSafeArea(_ provider: PlatformViewProvider) -> Bool
59+
60+
static func layoutOptions(_ provider: PlatformViewProvider) -> LayoutOptions
61+
62+
typealias Context = PlatformViewRepresentableContext<Self>
63+
64+
typealias LayoutOptions = _PlatformViewRepresentableLayoutOptions
65+
}
66+
67+
// MARK: - PlatformViewRepresentable + Extension [WIP]
68+
69+
extension PlatformViewRepresentable {
70+
static var dynamicProperties: DynamicPropertyCache.Fields {
71+
DynamicPropertyCache.fields(of: Self.self)
72+
}
73+
74+
nonisolated static func _makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
75+
// TODO
76+
preconditionFailure("TODO")
77+
}
78+
79+
var body: Never {
80+
bodyError()
81+
}
82+
}
83+
84+
#if canImport(UIKit) || canImport(AppKit)
85+
86+
extension PlatformViewRepresentable where PlatformViewProvider: PlatformView {
87+
static func platformView(for provider: PlatformViewProvider) -> PlatformView {
88+
provider
89+
}
90+
91+
static var isViewController: Bool { false }
92+
}
93+
94+
extension PlatformViewRepresentable where PlatformViewProvider: PlatformViewController {
95+
static func platformView(for provider: PlatformViewProvider) -> PlatformView {
96+
provider.view
97+
}
98+
99+
static var isViewController: Bool { true }
39100
}
40101

41-
// MARK: - PlatformViewRepresentableValues
102+
#endif
103+
104+
// MARK: - RepresentableContextValues
105+
106+
struct RepresentableContextValues {
107+
static var current: RepresentableContextValues?
108+
109+
var preferenceBridge: PreferenceBridge?
42110

43-
struct PlatformViewRepresentableValues {
44-
var preferenceBridge: PreferenceBridge
45111
var transaction: Transaction
46-
var environment: EnvironmentValues
47-
48-
static var current: PlatformViewRepresentableValues?
49-
112+
113+
var environmentStorage: EnvironmentStorage
114+
115+
enum EnvironmentStorage {
116+
case eager(EnvironmentValues)
117+
case lazy(Attribute<EnvironmentValues>, AnyRuleContext)
118+
}
119+
50120
func asCurrent<V>(do: () -> V) -> V {
51-
let old = PlatformViewRepresentableValues.current
52-
PlatformViewRepresentableValues.current = self
53-
defer { PlatformViewRepresentableValues.current = old }
121+
let old = Self.current
122+
Self.current = self
123+
defer { Self.current = old }
54124
return `do`()
55125
}
56126
}
57127

58-
struct PlatformViewRepresentableContext<RepresentableType: PlatformViewRepresentable> {
59-
var values: PlatformViewRepresentableValues
60-
let coordinator: RepresentableType.Coordinator
61-
}
128+
// MARK: - PlatformViewRepresentableContext
129+
130+
struct PlatformViewRepresentableContext<Representable: PlatformViewRepresentable> {
131+
var values: RepresentableContextValues
132+
let coordinator: Representable.Coordinator
62133

134+
init(
135+
coordinator: Representable.Coordinator,
136+
preferenceBridge: PreferenceBridge?,
137+
transaction: Transaction,
138+
environmentStorage: RepresentableContextValues.EnvironmentStorage
139+
) {
140+
self.values = .init(preferenceBridge: preferenceBridge, transaction: transaction, environmentStorage: environmentStorage)
141+
self.coordinator = coordinator
142+
}
63143

64-
// TODO
65-
class PlatformViewCoordinator: NSObject {
66-
// var weakDispatchUpdate: (()->Void) -> Void
67-
68-
override init() {
69-
super.init()
144+
@inlinable
145+
var environment: EnvironmentValues {
146+
switch values.environmentStorage {
147+
case let .eager(environmentValues):
148+
environmentValues
149+
case let .lazy(attribute, anyRuleContext):
150+
Update.ensure { anyRuleContext[attribute] }
151+
}
70152
}
71153
}
154+
155+
// MARK: - PlatformViewCoordinator
156+
157+
class PlatformViewCoordinator: NSObject {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// PlatformViewRepresentableLayoutOptions.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for iOS 18.0
6+
// Status: Complete
7+
8+
/// Options for defining how a platform representable interacts with the
9+
/// bridging implementation.
10+
public struct _PlatformViewRepresentableLayoutOptions: OptionSet {
11+
public let rawValue: Int
12+
13+
@_spi(Private)
14+
public static let propagatesSafeArea: _PlatformViewRepresentableLayoutOptions = .init(rawValue: 1 << 2)
15+
16+
public init(rawValue: Int) {
17+
self.rawValue = rawValue
18+
}
19+
}
20+
21+
@available(*, unavailable)
22+
extension _PlatformViewRepresentableLayoutOptions: Sendable {}

0 commit comments

Comments
 (0)