@@ -6,8 +6,12 @@ public struct NSViewRepresentableContext<Coordinator> {
66 public internal( set) var environment : EnvironmentValues
77}
88
9+ /// A wrapper that you use to integrate an AppKit view into your SwiftCrossUI
10+ /// view hierarchy.
911public protocol NSViewRepresentable : View where Content == Never {
12+ /// The underlying AppKit view.
1013 associatedtype NSViewType : NSView
14+ /// A type providing persistent storage for representable implementations.
1115 associatedtype Coordinator = Void
1216
1317 /// Create the initial NSView instance.
@@ -17,48 +21,52 @@ public protocol NSViewRepresentable: View where Content == Never {
1721 /// Update the view with new values.
1822 /// - Parameters:
1923 /// - nsView: The view to update.
20- /// - context: The context, including the coordinator and potentially new environment
21- /// values.
24+ /// - context: The context, including the coordinator and potentially new
25+ /// environment values.
2226 /// - Note: This may be called even when `context` has not changed.
2327 @MainActor
24- func updateNSView( _ nsView: NSViewType , context: NSViewRepresentableContext < Coordinator > )
28+ func updateNSView(
29+ _ nsView: NSViewType ,
30+ context: NSViewRepresentableContext < Coordinator >
31+ )
2532
2633 /// Make the coordinator for this view.
2734 ///
28- /// The coordinator is used when the view needs to communicate changes to the rest of
29- /// the view hierarchy (i.e. through bindings), and is often the view's delegate.
35+ /// The coordinator is used when the view needs to communicate changes to
36+ /// the rest of the view hierarchy (i.e. through bindings), and is often the
37+ /// view's delegate.
3038 @MainActor
3139 func makeCoordinator( ) -> Coordinator
3240
3341 /// Compute the view's size.
42+ ///
43+ /// The default implementation uses `nsView.intrinsicContentSize` and
44+ /// `nsView.sizeThatFits(_:)` to determine the return value.
3445 /// - Parameters:
3546 /// - proposal: The proposed frame for the view to render in.
3647 /// - nsVIew: The view being queried for its preferred size.
3748 /// - context: The context, including the coordinator and environment values.
3849 /// - Returns: Information about the view's size. The ``SwiftCrossUI/ViewSize/size``
39- /// property is what frame the view will actually be rendered with if the current layout
40- /// pass is not a dry run, while the other properties are used to inform the layout engine
41- /// how big or small the view can be. The ``SwiftCrossUI/ViewSize/idealSize`` property
42- /// should not vary with the `proposal`, and should only depend on the view's contents.
43- /// Pass `nil` for the maximum width/height if the view has no maximum size (and therefore
44- /// may occupy the entire screen).
45- ///
46- /// The default implementation uses `nsView.intrinsicContentSize` and `nsView.sizeThatFits(_:)`
47- /// to determine the return value.
50+ /// property is what frame the view will actually be rendered with if the
51+ /// current layout pass is not a dry run, while the other properties are
52+ /// used to inform the layout engine how big or small the view can be. The
53+ /// ``SwiftCrossUI/ViewSize/idealSize`` property should not vary with the
54+ /// `proposal`, and should only depend on the view's contents. Pass `nil`
55+ /// for the maximum width/height if the view has no maximum size (and
56+ /// therefore may occupy the entire screen).
4857 func determineViewSize(
49- for proposal: SIMD2 < Int > , nsView: NSViewType ,
58+ for proposal: SIMD2 < Int > ,
59+ nsView: NSViewType ,
5060 context: NSViewRepresentableContext < Coordinator >
5161 ) -> ViewSize
5262
5363 /// Called to clean up the view when it's removed.
64+ ///
65+ /// This method is called after all AppKit lifecycle methods, such as
66+ /// `nsView.didMoveToSuperview()`. The default implementation does nothing.
5467 /// - Parameters:
5568 /// - nsVIew: The view being dismantled.
5669 /// - coordinator: The coordinator.
57- ///
58- /// This method is called after all AppKit lifecycle methods, such as
59- /// `nsView.didMoveToSuperview()`.
60- ///
61- /// The default implementation does nothing.
6270 static func dismantleNSView( _ nsView: NSViewType , coordinator: Coordinator )
6371}
6472
@@ -146,12 +154,11 @@ extension View where Self: NSViewRepresentable {
146154 let representingWidget = widget as! RepresentingWidget < Self >
147155 representingWidget. update ( with: environment)
148156
149- let size =
150- representingWidget. representable. determineViewSize (
151- for: proposedSize,
152- nsView: representingWidget. subview,
153- context: representingWidget. context!
154- )
157+ let size = representingWidget. representable. determineViewSize (
158+ for: proposedSize,
159+ nsView: representingWidget. subview,
160+ context: representingWidget. context!
161+ )
155162
156163 if !dryRun {
157164 backend. setSize ( of: representingWidget, to: size. size)
@@ -203,7 +210,10 @@ final class RepresentingWidget<Representable: NSViewRepresentable>: NSView {
203210
204211 func update( with environment: EnvironmentValues ) {
205212 if context == nil {
206- context = . init( coordinator: representable. makeCoordinator ( ) , environment: environment)
213+ context = . init(
214+ coordinator: representable. makeCoordinator ( ) ,
215+ environment: environment
216+ )
207217 } else {
208218 context!. environment = environment
209219 representable. updateNSView ( subview, context: context!)
0 commit comments