Skip to content

Commit 95e5b87

Browse files
committed
Clean up documentation
1 parent e4ea547 commit 95e5b87

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

Sources/AppKitBackend/NSViewRepresentable.swift

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
911
public 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!)

Sources/SwiftCrossUI/Views/ScrollView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// A view that is scrollable when it would otherwise overflow available space. Use the
2-
/// ``View/frame`` moVStack to constrain height if necessary.
2+
/// ``View/frame`` modifier to constrain height if necessary.
33
public struct ScrollView<Content: View>: TypeSafeView, View {
44
public var body: VStack<Content>
55
public var axes: Axis.Set
66

7-
/// Wraps a view in a VStackrcontent: ollable container.
7+
/// Wraps a view in a scrollable container.
88
public init(_ axes: Axis.Set = .vertical, @ViewBuilder _ content: () -> Content) {
99
self.axes = axes
1010
body = VStack(content: content())

Sources/SwiftCrossUI/Views/View.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,29 @@ public protocol View {
2727

2828
/// Creates the view's widget using the supplied backend.
2929
///
30-
/// A view is represented by the same widget instance for the whole time that it's visible even
31-
/// if its content is changing; keep that in mind while deciding the structure of the widget.
32-
/// For example, a view displaying one of two children should use ``AppBackend/createContainer()``
33-
/// to create a container for the displayed child instead of just directly returning the widget
34-
/// of the currently displayed child (which would result in you not being able to ever switch
35-
/// to displaying the other child). This constraint significantly simplifies view implementations
36-
/// without requiring widgets to be re-created after every single update.
30+
/// A view is represented by the same widget instance for the whole time
31+
/// that it's visible even if its content is changing; keep that in mind
32+
/// while deciding the structure of the widget. For example, a view
33+
/// displaying one of two children should use ``AppBackend/createContainer()``
34+
/// to create a container for the displayed child instead of just directly
35+
/// returning the widget of the currently displayed child (which would result
36+
/// in you not being able to ever switch to displaying the other child). This
37+
/// constraint significantly simplifies view implementations without
38+
/// requiring widgets to be re-created after every single update.
3739
func asWidget<Backend: AppBackend>(
3840
_ children: any ViewGraphNodeChildren,
3941
backend: Backend
4042
) -> Backend.Widget
4143

42-
/// Updates the view's widget after a state change occurs (although the change isn't guaranteed
43-
/// to have affected this particular view). `proposedSize` is the size suggested by the parent
44-
/// container, but child views always get the final call on their own size.
44+
/// Updates the view's widget after a state change occurs (although the
45+
/// change isn't guaranteed to have affected this particular view).
46+
/// `proposedSize` is the size suggested by the parent container, but child
47+
/// views always get the final call on their own size.
4548
///
46-
/// Always called once immediately after creating the view's widget with. This helps reduce
47-
/// code duplication between `asWidget` and `update`.
48-
/// - Parameter dryRun: If `true`, avoids updating the UI and only computes sizing.
49+
/// Always called once immediately after creating the view's widget with.
50+
/// This helps reduce code duplication between `asWidget` and `update`.
51+
/// - Parameter dryRun: If `true`, avoids updating the UI and only computes
52+
/// sizing.
4953
/// - Returns: The view's new size.
5054
func update<Backend: AppBackend>(
5155
_ widget: Backend.Widget,

0 commit comments

Comments
 (0)