@@ -177,40 +177,72 @@ extension FrameAlignment {
177
177
static func _combineExplicit( childValue _: CGFloat , _: Int , into _: inout CGFloat ? ) { }
178
178
}
179
179
180
-
181
180
// MARK: - AlignmentKey [6.4.41]
182
181
183
- @usableFromInline
182
+ /// A single sort key type for alignment guides in both axes.
183
+ ///
184
+ /// You don't use this type directly.
185
+ @_documentation ( visibility: private)
184
186
@frozen
185
- package struct AlignmentKey : Hashable , Comparable {
186
- private let bits : UInt
187
-
188
- @usableFromInline
189
- package static func < ( lhs: AlignmentKey , rhs: AlignmentKey ) -> Bool {
190
- lhs. bits < rhs. bits
191
- }
192
-
187
+ public struct AlignmentKey : Hashable , Comparable {
193
188
@AtomicBox
194
189
private static var typeCache = TypeCache ( typeIDs: [ : ] , types: [ ] )
195
190
196
191
struct TypeCache {
197
192
var typeIDs : [ ObjectIdentifier : UInt ]
198
- var types : [ AlignmentID . Type ]
193
+ var types : [ any AlignmentID . Type ]
194
+ }
195
+
196
+ private let bits : UInt
197
+
198
+ package var id : any AlignmentID . Type {
199
+ Self . typeCache. types [ index]
199
200
}
200
201
201
- init ( id: AlignmentID . Type , axis _: Axis ) {
202
- let index : UInt
203
- if let value = AlignmentKey . typeCache. typeIDs [ ObjectIdentifier ( id) ] {
204
- index = value
205
- } else {
206
- index = UInt ( AlignmentKey . typeCache. types. count)
207
- AlignmentKey . typeCache. types. append ( id)
208
- AlignmentKey . typeCache. typeIDs [ ObjectIdentifier ( id) ] = index
202
+ package var axis : Axis { bits & 1 == 0 ? . horizontal : . vertical }
203
+
204
+ @inline ( __always)
205
+ var index : Int { Int ( bits / 2 - 1 ) }
206
+
207
+ package init ( id: AlignmentID . Type , axis: Axis ) {
208
+ let index = Self . $typeCache. access { cache in
209
+ let identifier = ObjectIdentifier ( id)
210
+ if let value = cache. typeIDs [ identifier] {
211
+ return value
212
+ } else {
213
+ let index = UInt ( cache. types. count)
214
+ cache. types. append ( id)
215
+ cache. typeIDs [ identifier] = index
216
+ return index
217
+ }
209
218
}
210
- bits = index * 2 + 3
219
+ bits = ( axis == . horizontal ? 0 : 1 ) + ( index + 1 ) * 2
220
+ }
221
+
222
+ package init ( ) { bits = . zero }
223
+
224
+ public static func < ( lhs: AlignmentKey , rhs: AlignmentKey ) -> Bool {
225
+ lhs. bits < rhs. bits
211
226
}
212
227
213
- var id : AlignmentID . Type {
214
- AlignmentKey . typeCache. types [ Int ( bits / 2 - 1 ) ]
228
+ package var fraction : CGFloat {
229
+ let computer = LayoutComputer . defaultValue
230
+ let dimensions = ViewDimensions (
231
+ guideComputer: computer,
232
+ size: . fixed( CGSize ( width: 1.0 , height: 1.0 ) )
233
+ )
234
+ return id. defaultValue ( in: dimensions)
235
+ }
236
+ }
237
+
238
+ // MARK: - AlignmentGuide [6.4.41]
239
+
240
+ package protocol AlignmentGuide : Equatable {
241
+ var key : AlignmentKey { get }
242
+ }
243
+
244
+ extension AlignmentGuide {
245
+ package var fraction : CGFloat {
246
+ key. fraction
215
247
}
216
248
}
0 commit comments