@@ -6,8 +6,7 @@ public struct NSViewRepresentableContext<Coordinator> {
6
6
public internal( set) var environment : EnvironmentValues
7
7
}
8
8
9
- public protocol NSViewRepresentable : View
10
- where Content == Never {
9
+ public protocol NSViewRepresentable : View where Content == Never {
11
10
associatedtype NSViewType : NSView
12
11
associatedtype Coordinator = Void
13
12
@@ -46,6 +45,7 @@ where Content == Never {
46
45
///
47
46
/// The default implementation uses `nsView.intrinsicContentSize` and `nsView.sizeThatFits(_:)`
48
47
/// to determine the return value.
48
+ @MainActor
49
49
func determineViewSize(
50
50
for proposal: SIMD2 < Int > , nsView: NSViewType ,
51
51
context: NSViewRepresentableContext < Coordinator >
@@ -101,8 +101,7 @@ extension NSViewRepresentable {
101
101
}
102
102
}
103
103
104
- extension View
105
- where Self: NSViewRepresentable {
104
+ extension View where Self: NSViewRepresentable {
106
105
public var body : Never {
107
106
preconditionFailure ( " This should never be called " )
108
107
}
@@ -121,89 +120,10 @@ where Self: NSViewRepresentable {
121
120
) -> [ LayoutSystem . LayoutableChild ] {
122
121
[ ]
123
122
}
124
-
125
- public func asWidget< Backend: AppBackend > (
126
- _: any ViewGraphNodeChildren ,
127
- backend _: Backend
128
- ) -> Backend . Widget {
129
- if let widget = RepresentingWidget ( representable: self ) as? Backend . Widget {
130
- return widget
131
- } else {
132
- fatalError ( " NSViewRepresentable requested by \( Backend . self) " )
133
- }
134
- }
135
-
136
- public func update< Backend: AppBackend > (
137
- _ widget: Backend . Widget ,
138
- children _: any ViewGraphNodeChildren ,
139
- proposedSize: SIMD2 < Int > ,
140
- environment: EnvironmentValues ,
141
- backend _: Backend ,
142
- dryRun: Bool
143
- ) -> ViewUpdateResult {
144
- let representingWidget = widget as! RepresentingWidget < Self >
145
- representingWidget. update ( with: environment)
146
-
147
- let size =
148
- representingWidget. representable. determineViewSize (
149
- for: proposedSize,
150
- nsView: representingWidget. subview,
151
- context: representingWidget. context!
152
- )
153
-
154
- if !dryRun {
155
- representingWidget. width = size. size. x
156
- representingWidget. height = size. size. y
157
- }
158
-
159
- return ViewUpdateResult . leafView ( size: size)
160
- }
161
123
}
162
124
163
- extension NSViewRepresentable
164
- where Coordinator == Void {
125
+ extension NSViewRepresentable where Coordinator == Void {
165
126
public func makeCoordinator( ) {
166
127
return ( )
167
128
}
168
129
}
169
-
170
- final class RepresentingWidget < Representable: NSViewRepresentable > : BaseWidget {
171
- var representable : Representable
172
- var context : NSViewRepresentableContext < Representable . Coordinator > ?
173
-
174
- lazy var subview : Representable . NSViewType = {
175
- let view = representable. makeNSView ( context: context!)
176
-
177
- self . addSubview ( view)
178
-
179
- view. translatesAutoresizingMaskIntoConstraints = false
180
- NSLayoutConstraint . activate ( [
181
- view. topAnchor. constraint ( equalTo: self . topAnchor) ,
182
- view. leadingAnchor. constraint ( equalTo: self . leadingAnchor) ,
183
- view. trailingAnchor. constraint ( equalTo: self . trailingAnchor) ,
184
- view. bottomAnchor. constraint ( equalTo: self . bottomAnchor) ,
185
- ] )
186
-
187
- return view
188
- } ( )
189
-
190
- func update( with environment: EnvironmentValues ) {
191
- if context == nil {
192
- context = . init( coordinator: representable. makeCoordinator ( ) , environment: environment)
193
- } else {
194
- context!. environment = environment
195
- representable. updateNSView ( subview, context: context!)
196
- }
197
- }
198
-
199
- init ( representable: Representable ) {
200
- self . representable = representable
201
- super. init ( )
202
- }
203
-
204
- deinit {
205
- if let context {
206
- Representable . dismantleNSView ( subview, coordinator: context. coordinator)
207
- }
208
- }
209
- }
0 commit comments