Skip to content

Commit c888687

Browse files
committed
Update documentation for LayoutProxy
1 parent bf54e08 commit c888687

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

Sources/OpenSwiftUICore/Layout/LayoutProxy.swift

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,155 @@
77

88
package import OpenGraphShims
99

10+
/// A collection of attributes that can be applied to layout calculations.
11+
///
12+
/// `LayoutProxyAttributes` stores optional references to a layout computer and a view trait list,
13+
/// which are used to compute layouts and access view traits during layout operations.
1014
package struct LayoutProxyAttributes: Equatable {
15+
/// The layout computer used to calculate sizes and positions.
1116
@OptionalAttribute
1217
var layoutComputer: LayoutComputer?
1318

19+
/// The list of view traits that affect layout behavior.
1420
@OptionalAttribute
1521
var traitList: (any ViewList)?
1622

23+
/// Creates layout proxy attributes with the specified layout computer and traits list.
24+
///
25+
/// - Parameters:
26+
/// - layoutComputer: The optional attribute containing a layout computer.
27+
/// - traitsList: The optional attribute containing view traits.
1728
package init(layoutComputer: OptionalAttribute<LayoutComputer>, traitsList: OptionalAttribute<any ViewList>) {
1829
_layoutComputer = layoutComputer
1930
_traitList = traitsList
2031
}
2132

33+
/// Creates layout proxy attributes with only traits.
34+
///
35+
/// - Parameter traitsList: The optional attribute containing view traits.
2236
package init(traitsList: OptionalAttribute<any ViewList>) {
2337
_layoutComputer = OptionalAttribute()
2438
_traitList = traitsList
2539
}
2640

41+
/// Creates layout proxy attributes with only a layout computer.
42+
///
43+
/// - Parameter layoutComputer: The optional attribute containing a layout computer.
2744
package init(layoutComputer: OptionalAttribute<LayoutComputer>) {
2845
_layoutComputer = layoutComputer
2946
_traitList = OptionalAttribute()
3047
}
3148

49+
/// Creates empty layout proxy attributes with no layout computer or traits.
3250
package init() {
3351
_layoutComputer = OptionalAttribute()
3452
_traitList = OptionalAttribute()
3553
}
3654

55+
/// Determines if this collection of attributes is empty.
56+
///
57+
/// Returns `true` if neither the layout computer nor the trait list is set.
3758
package var isEmpty: Bool {
3859
$layoutComputer == nil && $traitList == nil
3960
}
4061
}
4162

63+
/// A proxy object used to perform layout calculations for views.
64+
///
65+
/// `LayoutProxy` provides an interface to compute sizes, dimensions, and positions
66+
/// of views during the layout process. It combines layout computers and view traits
67+
/// to determine the final layout characteristics of a view.
4268
package struct LayoutProxy: Equatable {
69+
/// The rule context used to resolve attribute values.
4370
var context: AnyRuleContext
4471

72+
/// The attributes that define the layout behavior.
4573
var attributes: LayoutProxyAttributes
4674

75+
/// Creates a layout proxy with the specified context and attributes.
76+
///
77+
/// - Parameters:
78+
/// - context: The rule context used to resolve attribute values.
79+
/// - attributes: The attributes that define the layout behavior.
4780
package init(context: AnyRuleContext, attributes: LayoutProxyAttributes) {
4881
self.context = context
4982
self.attributes = attributes
5083
}
5184

85+
/// Creates a layout proxy with the specified context and layout computer.
86+
///
87+
/// - Parameters:
88+
/// - context: The rule context used to resolve attribute values.
89+
/// - layoutComputer: The optional layout computer to use for calculations.
5290
package init(context: AnyRuleContext, layoutComputer: Attribute<LayoutComputer>?) {
5391
self.context = context
5492
self.attributes = LayoutProxyAttributes(layoutComputer: .init(layoutComputer))
5593
}
5694

95+
/// The layout computer to use for layout calculations.
96+
///
97+
/// If no layout computer is explicitly provided, returns the default layout computer.
5798
package var layoutComputer: LayoutComputer {
5899
guard let layoutComputer = attributes.$layoutComputer else {
59100
return .defaultValue
60101
}
61102
return context[layoutComputer]
62103
}
63104

105+
/// The collection of view traits associated with this layout proxy.
106+
///
107+
/// Returns `nil` if no trait list is available.
64108
package var traits: ViewTraitCollection? {
65109
guard let traitList = attributes.$traitList else {
66110
return nil
67111
}
68112
return context[traitList].traits
69113
}
70114

115+
/// Accesses a specific trait value by its key type.
116+
///
117+
/// - Parameter key: The trait key type to look up.
118+
/// - Returns: The value for the specified trait key, or the default value if the trait is not present.
71119
package subscript<K>(key: K.Type) -> K.Value where K: _ViewTraitKey {
72120
traits.map { $0[key] } ?? K.defaultValue
73121
}
74122

123+
/// Returns the spacing configuration for this layout.
124+
///
125+
/// - Returns: The spacing configuration derived from the layout computer.
75126
package func spacing() -> Spacing {
76127
layoutComputer.spacing()
77128
}
78129

130+
/// Calculates the ideal size for the view without any specific constraints.
131+
///
132+
/// - Returns: The ideal size as determined by the layout computer.
79133
package func idealSize() -> CGSize {
80134
size(in: .unspecified)
81135
}
82136

137+
/// Calculates the size that fits within the given proposed size.
138+
///
139+
/// - Parameter proposedSize: The size constraints to consider.
140+
/// - Returns: The calculated size that fits within the constraints.
83141
package func size(in proposedSize: _ProposedSize) -> CGSize {
84142
layoutComputer.sizeThatFits(proposedSize)
85143
}
86144

145+
/// Calculates the length that fits within the given proposal along a specific axis.
146+
///
147+
/// - Parameters:
148+
/// - proposal: The size constraints to consider.
149+
/// - direction: The axis along which to calculate the length.
150+
/// - Returns: The calculated length that fits within the constraints.
87151
package func lengthThatFits(_ proposal: _ProposedSize, in direction: Axis) -> CGFloat {
88152
layoutComputer.lengthThatFits(proposal, in: direction)
89153
}
90154

155+
/// Calculates the view dimensions within the given proposed size.
156+
///
157+
/// - Parameter proposedSize: The size constraints to consider.
158+
/// - Returns: The calculated dimensions including size and alignment guides.
91159
package func dimensions(in proposedSize: _ProposedSize) -> ViewDimensions {
92160
let computer = layoutComputer
93161
return ViewDimensions(
@@ -100,6 +168,13 @@ package struct LayoutProxy: Equatable {
100168
)
101169
}
102170

171+
/// Calculates the final geometry of a view at a specific placement within a parent.
172+
///
173+
/// - Parameters:
174+
/// - p: The placement defining position and proposed size.
175+
/// - parentSize: The size of the parent container.
176+
/// - layoutDirection: The layout direction (left-to-right or right-to-left).
177+
/// - Returns: The final view geometry including position and dimensions.
103178
package func finallyPlaced(at p: _Placement, in parentSize: CGSize, layoutDirection: LayoutDirection) -> ViewGeometry {
104179
let dimensions = dimensions(in: p.proposedSize_)
105180
var geometry = ViewGeometry(
@@ -110,28 +185,50 @@ package struct LayoutProxy: Equatable {
110185
return geometry
111186
}
112187

188+
/// Returns the explicit alignment for a specific alignment key at the given size.
189+
///
190+
/// - Parameters:
191+
/// - k: The alignment key to measure.
192+
/// - mySize: The size at which to measure the alignment.
193+
/// - Returns: The explicit alignment position, or `nil` if not explicitly defined.
113194
package func explicitAlignment(_ k: AlignmentKey, at mySize: ViewSize) -> CGFloat? {
114195
layoutComputer.explicitAlignment(k, at: mySize)
115196
}
116197

198+
/// The layout priority of the view, which influences layout decisions.
199+
///
200+
/// Higher values indicate higher priority during layout calculations.
117201
package var layoutPriority: Double {
118202
layoutComputer.layoutPriority()
119203
}
120204

205+
/// Indicates whether the view ignores automatic padding applied by container views.
121206
package var ignoresAutomaticPadding: Bool {
122207
layoutComputer.ignoresAutomaticPadding()
123208
}
124209

210+
/// Indicates whether the view requires spacing projection during layout.
125211
package var requiresSpacingProjection: Bool {
126212
layoutComputer.requiresSpacingProjection()
127213
}
128214
}
129215

216+
/// A collection of layout proxies that can be accessed by index.
217+
///
218+
/// `LayoutProxyCollection` provides random access to a collection of layout proxies,
219+
/// each representing a different view or component in a layout hierarchy.
130220
package struct LayoutProxyCollection: RandomAccessCollection {
221+
/// The rule context used to resolve attribute values.
131222
var context: AnyRuleContext
132223

224+
/// The attributes for each layout proxy in the collection.
133225
var attributes: [LayoutProxyAttributes]
134226

227+
/// Creates a layout proxy collection with the specified context and attributes.
228+
///
229+
/// - Parameters:
230+
/// - context: The rule context used to resolve attribute values.
231+
/// - attributes: The array of attributes for each layout proxy.
135232
package init(context: AnyRuleContext, attributes: [LayoutProxyAttributes]) {
136233
self.context = context
137234
self.attributes = attributes

0 commit comments

Comments
 (0)