7
7
8
8
package import OpenGraphShims
9
9
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.
10
14
package struct LayoutProxyAttributes : Equatable {
15
+ /// The layout computer used to calculate sizes and positions.
11
16
@OptionalAttribute
12
17
var layoutComputer : LayoutComputer ?
13
18
19
+ /// The list of view traits that affect layout behavior.
14
20
@OptionalAttribute
15
21
var traitList : ( any ViewList ) ?
16
22
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.
17
28
package init ( layoutComputer: OptionalAttribute < LayoutComputer > , traitsList: OptionalAttribute < any ViewList > ) {
18
29
_layoutComputer = layoutComputer
19
30
_traitList = traitsList
20
31
}
21
32
33
+ /// Creates layout proxy attributes with only traits.
34
+ ///
35
+ /// - Parameter traitsList: The optional attribute containing view traits.
22
36
package init ( traitsList: OptionalAttribute < any ViewList > ) {
23
37
_layoutComputer = OptionalAttribute ( )
24
38
_traitList = traitsList
25
39
}
26
40
41
+ /// Creates layout proxy attributes with only a layout computer.
42
+ ///
43
+ /// - Parameter layoutComputer: The optional attribute containing a layout computer.
27
44
package init ( layoutComputer: OptionalAttribute < LayoutComputer > ) {
28
45
_layoutComputer = layoutComputer
29
46
_traitList = OptionalAttribute ( )
30
47
}
31
48
49
+ /// Creates empty layout proxy attributes with no layout computer or traits.
32
50
package init ( ) {
33
51
_layoutComputer = OptionalAttribute ( )
34
52
_traitList = OptionalAttribute ( )
35
53
}
36
54
55
+ /// Determines if this collection of attributes is empty.
56
+ ///
57
+ /// Returns `true` if neither the layout computer nor the trait list is set.
37
58
package var isEmpty : Bool {
38
59
$layoutComputer == nil && $traitList == nil
39
60
}
40
61
}
41
62
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.
42
68
package struct LayoutProxy : Equatable {
69
+ /// The rule context used to resolve attribute values.
43
70
var context : AnyRuleContext
44
71
72
+ /// The attributes that define the layout behavior.
45
73
var attributes : LayoutProxyAttributes
46
74
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.
47
80
package init ( context: AnyRuleContext , attributes: LayoutProxyAttributes ) {
48
81
self . context = context
49
82
self . attributes = attributes
50
83
}
51
84
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.
52
90
package init ( context: AnyRuleContext , layoutComputer: Attribute < LayoutComputer > ? ) {
53
91
self . context = context
54
92
self . attributes = LayoutProxyAttributes ( layoutComputer: . init( layoutComputer) )
55
93
}
56
94
95
+ /// The layout computer to use for layout calculations.
96
+ ///
97
+ /// If no layout computer is explicitly provided, returns the default layout computer.
57
98
package var layoutComputer : LayoutComputer {
58
99
guard let layoutComputer = attributes. $layoutComputer else {
59
100
return . defaultValue
60
101
}
61
102
return context [ layoutComputer]
62
103
}
63
104
105
+ /// The collection of view traits associated with this layout proxy.
106
+ ///
107
+ /// Returns `nil` if no trait list is available.
64
108
package var traits : ViewTraitCollection ? {
65
109
guard let traitList = attributes. $traitList else {
66
110
return nil
67
111
}
68
112
return context [ traitList] . traits
69
113
}
70
114
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.
71
119
package subscript< K> ( key: K . Type ) -> K . Value where K: _ViewTraitKey {
72
120
traits. map { $0 [ key] } ?? K . defaultValue
73
121
}
74
122
123
+ /// Returns the spacing configuration for this layout.
124
+ ///
125
+ /// - Returns: The spacing configuration derived from the layout computer.
75
126
package func spacing( ) -> Spacing {
76
127
layoutComputer. spacing ( )
77
128
}
78
129
130
+ /// Calculates the ideal size for the view without any specific constraints.
131
+ ///
132
+ /// - Returns: The ideal size as determined by the layout computer.
79
133
package func idealSize( ) -> CGSize {
80
134
size ( in: . unspecified)
81
135
}
82
136
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.
83
141
package func size( in proposedSize: _ProposedSize ) -> CGSize {
84
142
layoutComputer. sizeThatFits ( proposedSize)
85
143
}
86
144
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.
87
151
package func lengthThatFits( _ proposal: _ProposedSize , in direction: Axis ) -> CGFloat {
88
152
layoutComputer. lengthThatFits ( proposal, in: direction)
89
153
}
90
154
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.
91
159
package func dimensions( in proposedSize: _ProposedSize ) -> ViewDimensions {
92
160
let computer = layoutComputer
93
161
return ViewDimensions (
@@ -100,6 +168,13 @@ package struct LayoutProxy: Equatable {
100
168
)
101
169
}
102
170
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.
103
178
package func finallyPlaced( at p: _Placement , in parentSize: CGSize , layoutDirection: LayoutDirection ) -> ViewGeometry {
104
179
let dimensions = dimensions ( in: p. proposedSize_)
105
180
var geometry = ViewGeometry (
@@ -110,28 +185,50 @@ package struct LayoutProxy: Equatable {
110
185
return geometry
111
186
}
112
187
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.
113
194
package func explicitAlignment( _ k: AlignmentKey , at mySize: ViewSize ) -> CGFloat ? {
114
195
layoutComputer. explicitAlignment ( k, at: mySize)
115
196
}
116
197
198
+ /// The layout priority of the view, which influences layout decisions.
199
+ ///
200
+ /// Higher values indicate higher priority during layout calculations.
117
201
package var layoutPriority : Double {
118
202
layoutComputer. layoutPriority ( )
119
203
}
120
204
205
+ /// Indicates whether the view ignores automatic padding applied by container views.
121
206
package var ignoresAutomaticPadding : Bool {
122
207
layoutComputer. ignoresAutomaticPadding ( )
123
208
}
124
209
210
+ /// Indicates whether the view requires spacing projection during layout.
125
211
package var requiresSpacingProjection : Bool {
126
212
layoutComputer. requiresSpacingProjection ( )
127
213
}
128
214
}
129
215
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.
130
220
package struct LayoutProxyCollection : RandomAccessCollection {
221
+ /// The rule context used to resolve attribute values.
131
222
var context : AnyRuleContext
132
223
224
+ /// The attributes for each layout proxy in the collection.
133
225
var attributes : [ LayoutProxyAttributes ]
134
226
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.
135
232
package init ( context: AnyRuleContext , attributes: [ LayoutProxyAttributes ] ) {
136
233
self . context = context
137
234
self . attributes = attributes
0 commit comments