Skip to content

Commit 4204c98

Browse files
committed
Update PlacementData
1 parent f6f46ac commit 4204c98

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

Sources/OpenSwiftUICore/Layout/Layout.swift

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ extension Layout {
671671
list: elements
672672
)
673673
}
674-
case let .dynamicList(viewList, modifier):
674+
case .dynamicList:
675675
return makeDynamicView(
676676
root: root,
677677
inputs: inputs,
@@ -1132,7 +1132,7 @@ private protocol DefaultAlignmentFunction {
11321132
) -> CGFloat?
11331133
}
11341134

1135-
// MARK: - ViewLayoutEngine [6.4.41] [WIP]
1135+
// MARK: - ViewLayoutEngine [6.4.41]
11361136

11371137
struct ViewLayoutEngine<L>: DefaultAlignmentFunction, LayoutEngine where L: Layout {
11381138
var layout: L
@@ -1225,7 +1225,51 @@ struct ViewLayoutEngine<L>: DefaultAlignmentFunction, LayoutEngine where L: Layo
12251225
}
12261226

12271227
mutating func childGeometries(at parentSize: ViewSize, origin: CGPoint) -> [ViewGeometry] {
1228-
preconditionFailure("TODO")
1228+
let count = proxies.count
1229+
if cachedAlignmentSize == parentSize, origin == .zero, cachedAlignmentGeometry.count == proxies.count {
1230+
return cachedAlignmentGeometry
1231+
} else {
1232+
let proposal = parentSize.proposal
1233+
let frame = CGRect(origin: origin, size: parentSize.value)
1234+
var data = PlacementData(
1235+
unknown: false,
1236+
geometrys: Array(repeating: ViewGeometry.invalidValue, count: count),
1237+
invalidCount: 0,
1238+
frame: frame,
1239+
layoutDirection: layoutDirection
1240+
)
1241+
withUnsafeMutablePointer(to: &data) { data in
1242+
let oldData = threadLayoutData
1243+
threadLayoutData = data
1244+
layout.placeSubviews(
1245+
in: frame,
1246+
proposal: ProposedViewSize(proposal),
1247+
subviews: LayoutSubviews(
1248+
context: proxies.context,
1249+
storage: .direct(proxies.attributes),
1250+
layoutDirection: layoutDirection
1251+
),
1252+
cache: &cache
1253+
)
1254+
threadLayoutData = oldData
1255+
guard data.pointee.invalidCount != count else {
1256+
return
1257+
}
1258+
for (index, geometry) in data.pointee.geometrys.enumerated() {
1259+
guard geometry.isInvalid else {
1260+
continue
1261+
}
1262+
let dimiensions = proxies[index].dimensions(in: parentSize.proposal)
1263+
let center = data.pointee.frame.center
1264+
data.pointee.setGeometry(
1265+
dimiensions.centered(in: CGSize(center)),
1266+
at: index,
1267+
layoutDirection: .leftToRight
1268+
)
1269+
}
1270+
}
1271+
return data.geometrys
1272+
}
12291273
}
12301274

12311275
mutating func explicitAlignment(_ k: AlignmentKey, at viewSize: ViewSize) -> CGFloat? {
@@ -1613,9 +1657,11 @@ public struct LayoutSubview: Equatable {
16131657
@available(*, unavailable)
16141658
extension LayoutSubview: Sendable {}
16151659

1616-
// MARK: - PlacementData [6.4.41] [WIP]
1660+
// MARK: - PlacementData [6.4.41]
16171661

16181662
private struct PlacementData {
1663+
var unknown: Bool // FIXME
1664+
16191665
var geometrys: [ViewGeometry]
16201666

16211667
var invalidCount: Int
@@ -1624,6 +1670,8 @@ private struct PlacementData {
16241670

16251671
var layoutDirection: LayoutDirection
16261672

1673+
static var current: UnsafeMutablePointer<PlacementData>? { threadLayoutData }
1674+
16271675
mutating func setGeometry(_ geometry: ViewGeometry, at index: Int, layoutDirection: LayoutDirection) {
16281676
if geometrys[index].isInvalid {
16291677
invalidCount += 1
@@ -1635,6 +1683,18 @@ private struct PlacementData {
16351683
}
16361684
}
16371685

1686+
// MARK: - threadLayoutData
1687+
1688+
@_transparent
1689+
private var threadLayoutData: UnsafeMutablePointer<PlacementData>? {
1690+
get {
1691+
_threadLayoutData()?.assumingMemoryBound(to: PlacementData.self)
1692+
}
1693+
set {
1694+
_setThreadLayoutData(newValue)
1695+
}
1696+
}
1697+
16381698
// MARK: - LayoutValueKey
16391699

16401700
/// A key for accessing a layout value of a layout container's subviews.
@@ -1954,17 +2014,3 @@ public typealias ViewLayoutKey = LayoutValueKey
19542014
@_spi(_)
19552015
@available(*, deprecated, renamed: "_LayoutRoot")
19562016
public typealias _ViewLayoutRoot<L> = _LayoutRoot<L> where L : Layout
1957-
1958-
// MARK: - threadLayoutData
1959-
1960-
@_transparent
1961-
private var threadLayoutData: AnyObject? {
1962-
get {
1963-
_threadLayoutData() as AnyObject?
1964-
}
1965-
set {
1966-
_setThreadLayoutData(
1967-
newValue.map { Unmanaged.passUnretained($0).toOpaque() }
1968-
)
1969-
}
1970-
}

0 commit comments

Comments
 (0)