Skip to content

Commit 83f62dc

Browse files
authored
Update documentation for ProposedSize (#297)
1 parent 284bbe9 commit 83f62dc

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Sources/OpenSwiftUICore/Layout/Geometry/ProposedSize.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,65 @@
77

88
package import Foundation
99

10+
/// A size proposal that can have specified or unspecified dimensions.
11+
///
12+
/// A proposed size is used in the layout system to communicate size constraints
13+
/// to views and layout containers. Either dimension can be specified with a concrete
14+
/// value or left unspecified (nil), allowing the receiver to choose its own size
15+
/// for that dimension.
1016
public struct _ProposedSize {
17+
/// The proposed width, or `nil` if unspecified.
1118
package var width: CGFloat?
19+
20+
/// The proposed height, or `nil` if unspecified.
1221
package var height: CGFloat?
1322

23+
/// Creates a proposed size with optional width and height values.
24+
///
25+
/// - Parameters:
26+
/// - width: The proposed width, or `nil` if unspecified.
27+
/// - height: The proposed height, or `nil` if unspecified.
1428
package init(width: CGFloat? = nil, height: CGFloat? = nil) {
1529
self.width = width
1630
self.height = height
1731
}
1832

33+
/// Creates a proposed size with both dimensions unspecified.
1934
package init() {
2035
self.width = nil
2136
self.height = nil
2237
}
2338

39+
/// Converts to a concrete `CGSize` by replacing unspecified dimensions with the provided defaults.
40+
///
41+
/// - Parameter defaults: The default size to use for unspecified dimensions.
42+
/// - Returns: A `CGSize` with concrete values for both dimensions.
2443
package func fixingUnspecifiedDimensions(at defaults: CGSize) -> CGSize {
2544
CGSize(width: width ?? defaults.width, height: height ?? defaults.height)
2645
}
2746

47+
/// Converts to a concrete `CGSize` by replacing unspecified dimensions with a default value of 10.0.
48+
///
49+
/// - Returns: A `CGSize` with concrete values for both dimensions.
2850
package func fixingUnspecifiedDimensions() -> CGSize {
2951
CGSize(width: width ?? 10.0, height: height ?? 10.0)
3052
}
3153

54+
/// Creates a new proposed size by scaling both dimensions by the given factor.
55+
///
56+
/// - Parameter s: The scale factor to apply.
57+
/// - Returns: A new proposed size with scaled dimensions.
3258
package func scaled(by s: CGFloat) -> _ProposedSize {
3359
_ProposedSize(width: width.map { $0 * s }, height: height.map { $0 * s })
3460
}
3561

62+
/// A proposed size with both dimensions set to zero.
3663
package static let zero = _ProposedSize(width: 0, height: 0)
64+
65+
/// A proposed size with both dimensions set to infinity.
3766
package static let infinity = _ProposedSize(width: .infinity, height: .infinity)
67+
68+
/// A proposed size with both dimensions unspecified.
3869
package static let unspecified = _ProposedSize(width: nil, height: nil)
3970
}
4071

@@ -44,32 +75,54 @@ extension _ProposedSize: Sendable {}
4475
extension _ProposedSize: Hashable {}
4576

4677
extension _ProposedSize {
78+
/// Creates a proposed size from a concrete `CGSize`.
79+
///
80+
/// - Parameter s: The concrete size to convert.
4781
package init(_ s: CGSize) {
4882
width = s.width
4983
height = s.height
5084
}
5185
}
5286

5387
extension CGSize {
88+
/// Creates a `CGSize` from a proposed size if both dimensions are specified.
89+
///
90+
/// - Parameter p: The proposed size to convert.
91+
/// - Returns: A `CGSize` if both dimensions are specified, or `nil` otherwise.
5492
package init?(_ p: _ProposedSize) {
5593
guard let width = p.width, let height = p.height else { return nil }
5694
self.init(width: width, height: height)
5795
}
5896
}
5997

6098
extension _ProposedSize {
99+
/// Creates a new proposed size by reducing both dimensions by the specified edge insets.
100+
///
101+
/// - Parameter insets: The edge insets to apply.
102+
/// - Returns: A new proposed size with insets applied.
61103
package func inset(by insets: EdgeInsets) -> _ProposedSize {
62104
_ProposedSize(
63105
width: width.map { max($0 - insets.leading - insets.trailing, .zero) },
64106
height: height.map { max($0 - insets.top - insets.bottom, .zero) }
65107
)
66108
}
67109

110+
/// Accesses the dimension specified by the given axis.
111+
///
112+
/// - Parameter axis: The axis to access (.horizontal for width, .vertical for height).
113+
/// - Returns: The dimension for the specified axis.
68114
package subscript(axis: Axis) -> CGFloat? {
69115
get { axis == .horizontal ? width : height }
70116
set { if axis == .horizontal { width = newValue } else { height = newValue } }
71117
}
72118

119+
/// Creates a proposed size by assigning dimensions based on the specified axis.
120+
///
121+
/// - Parameters:
122+
/// - l1: The dimension for the first axis.
123+
/// - first: The first axis (.horizontal or .vertical).
124+
/// - l2: The dimension for the second axis.
125+
/// - Returns: A new proposed size with dimensions assigned according to the axes.
73126
package init(_ l1: CGFloat?, in first: Axis, by l2: CGFloat?) {
74127
self = first == .horizontal ? _ProposedSize(width: l1, height: l2) : _ProposedSize(width: l2, height: l1)
75128
}

0 commit comments

Comments
 (0)