|
2 | 2 | // PlatformViewRepresentable.swift
|
3 | 3 | // OpenSwiftUI
|
4 | 4 | //
|
5 |
| -// Audited for iOS 15.5 |
| 5 | +// Audited for iOS 18.0 |
6 | 6 | // Status: WIP
|
| 7 | +// ID: A513612C07DFA438E70B9FA90719B40D (SwiftUI) |
7 | 8 |
|
8 |
| -#if os(macOS) |
| 9 | +#if canImport(AppKit) |
9 | 10 | import AppKit
|
10 | 11 | typealias PlatformView = NSView
|
11 |
| -#elseif os(iOS) |
| 12 | +typealias PlatformViewController = NSViewController |
| 13 | +#elseif canImport(UIKit) |
12 | 14 | import UIKit
|
13 | 15 | typealias PlatformView = UIView
|
| 16 | +typealias PlatformViewController = UIViewController |
14 | 17 | #else
|
15 | 18 | import Foundation
|
16 |
| -typealias PlatformView = Void |
| 19 | +typealias PlatformView = NSObject |
| 20 | +typealias PlatformViewController = NSObject |
17 | 21 | #endif
|
18 |
| - |
19 | 22 | import OpenSwiftUICore
|
| 23 | +import OpenGraphShims |
| 24 | + |
| 25 | +// MARK: - PlatformViewRepresentable |
20 | 26 |
|
21 | 27 | protocol PlatformViewRepresentable: View {
|
| 28 | + static var dynamicProperties: DynamicPropertyCache.Fields { get } |
| 29 | + |
22 | 30 | associatedtype PlatformViewProvider
|
| 31 | + |
23 | 32 | 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 | + |
28 | 38 | 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 | + |
31 | 44 | 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 | + |
36 | 54 | static func modifyBridgedViewInputs(_ inputs: inout _ViewInputs)
|
| 55 | + |
37 | 56 | 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 } |
39 | 100 | }
|
40 | 101 |
|
41 |
| -// MARK: - PlatformViewRepresentableValues |
| 102 | +#endif |
| 103 | + |
| 104 | +// MARK: - RepresentableContextValues |
| 105 | + |
| 106 | +struct RepresentableContextValues { |
| 107 | + static var current: RepresentableContextValues? |
| 108 | + |
| 109 | + var preferenceBridge: PreferenceBridge? |
42 | 110 |
|
43 |
| -struct PlatformViewRepresentableValues { |
44 |
| - var preferenceBridge: PreferenceBridge |
45 | 111 | 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 | + |
50 | 120 | 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 } |
54 | 124 | return `do`()
|
55 | 125 | }
|
56 | 126 | }
|
57 | 127 |
|
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 |
62 | 133 |
|
| 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 | + } |
63 | 143 |
|
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 | + } |
70 | 152 | }
|
71 | 153 | }
|
| 154 | + |
| 155 | +// MARK: - PlatformViewCoordinator |
| 156 | + |
| 157 | +class PlatformViewCoordinator: NSObject {} |
0 commit comments