@@ -6,8 +6,7 @@ public struct NSViewRepresentableContext<Coordinator> {
66 public internal( set) var environment : EnvironmentValues
77}
88
9- public protocol NSViewRepresentable : View
10- where Content == Never {
9+ public protocol NSViewRepresentable : View where Content == Never {
1110 associatedtype NSViewType : NSView
1211 associatedtype Coordinator = Void
1312
@@ -46,6 +45,7 @@ where Content == Never {
4645 ///
4746 /// The default implementation uses `nsView.intrinsicContentSize` and `nsView.sizeThatFits(_:)`
4847 /// to determine the return value.
48+ @MainActor
4949 func determineViewSize(
5050 for proposal: SIMD2 < Int > , nsView: NSViewType ,
5151 context: NSViewRepresentableContext < Coordinator >
@@ -101,8 +101,7 @@ extension NSViewRepresentable {
101101 }
102102}
103103
104- extension View
105- where Self: NSViewRepresentable {
104+ extension View where Self: NSViewRepresentable {
106105 public var body : Never {
107106 preconditionFailure ( " This should never be called " )
108107 }
@@ -121,89 +120,10 @@ where Self: NSViewRepresentable {
121120 ) -> [ LayoutSystem . LayoutableChild ] {
122121 [ ]
123122 }
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- }
161123}
162124
163- extension NSViewRepresentable
164- where Coordinator == Void {
125+ extension NSViewRepresentable where Coordinator == Void {
165126 public func makeCoordinator( ) {
166127 return ( )
167128 }
168129}
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