|
| 1 | +// |
| 2 | +// UnaryLayoutComputer.swift |
| 3 | +// OpenSwiftUICore |
| 4 | +// |
| 5 | +// Status: Blocked by _PositionAwarePlacementContext |
| 6 | +// ID: 1C3B77B617AD058A6802F719E38F5D79 (SwiftUICore?) |
| 7 | + |
| 8 | +import Foundation |
| 9 | +package import OpenGraphShims |
| 10 | + |
| 11 | +// MARK: - UnaryLayout + _PositionAwarePlacementContext [6.4.41] [TODO] |
| 12 | + |
| 13 | +extension UnaryLayout where Self.PlacementContextType == _PositionAwarePlacementContext { |
| 14 | + package static func makeViewImpl( |
| 15 | + modifier: _GraphValue<Self>, |
| 16 | + inputs: _ViewInputs, |
| 17 | + body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs |
| 18 | + ) -> _ViewOutputs { |
| 19 | + preconditionFailure("TODO") |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// MARK: - UnaryLayout + PlacementContext [6.4.41] |
| 24 | + |
| 25 | +extension UnaryLayout where PlacementContextType == PlacementContext { |
| 26 | + package static func makeViewImpl( |
| 27 | + modifier: _GraphValue<Self>, |
| 28 | + inputs: _ViewInputs, |
| 29 | + body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs |
| 30 | + ) -> _ViewOutputs { |
| 31 | + guard inputs.requestsLayoutComputer || inputs.needsGeometry else { |
| 32 | + return body(_Graph(), inputs) |
| 33 | + } |
| 34 | + let animatedModifier = makeAnimatable(value: modifier, inputs: inputs.base) |
| 35 | + let computer = Attribute( UnaryLayoutComputer( |
| 36 | + layout: animatedModifier, |
| 37 | + environment: inputs.environment, |
| 38 | + childLayoutComputer: OptionalAttribute() |
| 39 | + )) |
| 40 | + var inputs = inputs |
| 41 | + let geometry: Attribute<ViewGeometry>! |
| 42 | + if inputs.needsGeometry { |
| 43 | + geometry = Attribute(UnaryChildGeometry<Self>( |
| 44 | + parentSize: inputs.size, |
| 45 | + layoutDirection: inputs.layoutDirection, |
| 46 | + parentLayoutComputer: computer |
| 47 | + )) |
| 48 | + inputs.size = geometry.size() |
| 49 | + inputs.position = Attribute(LayoutPositionQuery( |
| 50 | + parentPosition: inputs.position, |
| 51 | + localPosition: geometry.origin() |
| 52 | + )) |
| 53 | + inputs.requestsLayoutComputer = true |
| 54 | + } else { |
| 55 | + geometry = nil |
| 56 | + } |
| 57 | + let requestsLayoutComputer = inputs.requestsLayoutComputer |
| 58 | + var outputs = body(_Graph(), inputs) |
| 59 | + if inputs.needsGeometry { |
| 60 | + computer.mutateBody(as: UnaryLayoutComputer<Self>.self, invalidating: true) { computer in |
| 61 | + computer.$childLayoutComputer = outputs.layoutComputer |
| 62 | + } |
| 63 | + geometry.mutateBody(as: UnaryChildGeometry<Self>.self, invalidating: true) { geometry in |
| 64 | + geometry.$childLayoutComputer = outputs.layoutComputer |
| 65 | + } |
| 66 | + } |
| 67 | + if requestsLayoutComputer { |
| 68 | + outputs.layoutComputer = computer |
| 69 | + } |
| 70 | + return outputs |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +private struct UnaryChildGeometry<L>: Rule, AsyncAttribute, CustomStringConvertible |
| 75 | + where L: UnaryLayout, L.PlacementContextType == PlacementContext { |
| 76 | + @Attribute var parentSize: ViewSize |
| 77 | + @Attribute var layoutDirection: LayoutDirection |
| 78 | + @Attribute var parentLayoutComputer: LayoutComputer |
| 79 | + @OptionalAttribute var childLayoutComputer: LayoutComputer? |
| 80 | + |
| 81 | + var value: ViewGeometry { |
| 82 | + let parentSize = parentSize |
| 83 | + let computer = parentLayoutComputer |
| 84 | + let placement = computer.withMutableEngine(type: UnaryLayoutEngine<L>.self) { engine in |
| 85 | + engine.childPlacement(at: parentSize) |
| 86 | + } |
| 87 | + let childProxy = LayoutProxy( |
| 88 | + context: AnyRuleContext(context), |
| 89 | + layoutComputer: $childLayoutComputer |
| 90 | + ) |
| 91 | + return childProxy.finallyPlaced( |
| 92 | + at: placement, |
| 93 | + in: parentSize.value, |
| 94 | + layoutDirection: layoutDirection |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + var description: String { |
| 99 | + "\(L.self) → ChildGeometry" |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +private struct UnaryLayoutComputer<L>: StatefulRule, AsyncAttribute, CustomStringConvertible |
| 104 | + where L: UnaryLayout, L.PlacementContextType == PlacementContext { |
| 105 | + @Attribute var layout: L |
| 106 | + @Attribute var environment: EnvironmentValues |
| 107 | + @OptionalAttribute var childLayoutComputer: LayoutComputer? |
| 108 | + |
| 109 | + typealias Value = LayoutComputer |
| 110 | + |
| 111 | + mutating func updateValue() { |
| 112 | + let context = AnyRuleContext(context) |
| 113 | + let engine = UnaryLayoutEngine( |
| 114 | + layout: layout, |
| 115 | + layoutContext: SizeAndSpacingContext(context: context, environment: $environment), |
| 116 | + child: LayoutProxy(context: context, layoutComputer: $childLayoutComputer) |
| 117 | + ) |
| 118 | + update(to: engine) |
| 119 | + } |
| 120 | + |
| 121 | + var description: String { |
| 122 | + "\(L.self) → LayoutComputer" |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +private struct UnaryLayoutEngine<L>: LayoutEngine |
| 127 | + where L: UnaryLayout, L.PlacementContextType == PlacementContext { |
| 128 | + let layout: L |
| 129 | + |
| 130 | + let layoutContext: SizeAndSpacingContext |
| 131 | + |
| 132 | + let child: LayoutProxy |
| 133 | + |
| 134 | + var dimensionsCache: ViewSizeCache = .init() |
| 135 | + |
| 136 | + var placementCache: Cache3<ViewSize, _Placement> = .init() |
| 137 | + |
| 138 | + init(layout: L, layoutContext: SizeAndSpacingContext, child: LayoutProxy) { |
| 139 | + self.layout = layout |
| 140 | + self.layoutContext = layoutContext |
| 141 | + self.child = child |
| 142 | + } |
| 143 | + |
| 144 | + mutating func childPlacement(at size: ViewSize) -> _Placement { |
| 145 | + let layout = layout |
| 146 | + let child = child |
| 147 | + let context = PlacementContext(base: layoutContext, parentSize: size) |
| 148 | + return placementCache.get(size) { |
| 149 | + layout.placement(of: child, in: context) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + func layoutPriority() -> Double { |
| 154 | + layout.layoutPriority(child: child) |
| 155 | + } |
| 156 | + |
| 157 | + func ignoresAutomaticPadding() -> Bool { |
| 158 | + layout.ignoresAutomaticPadding(child: child) |
| 159 | + } |
| 160 | + |
| 161 | + func spacing() -> Spacing { |
| 162 | + layout.spacing(in: layoutContext, child: child) |
| 163 | + } |
| 164 | + |
| 165 | + mutating func sizeThatFits(_ proposedSize: _ProposedSize) -> CGSize { |
| 166 | + let layout = layout |
| 167 | + let context = layoutContext |
| 168 | + let child = child |
| 169 | + return dimensionsCache.get(proposedSize) { |
| 170 | + layout.sizeThatFits( |
| 171 | + in: proposedSize, |
| 172 | + context: context, |
| 173 | + child: child |
| 174 | + ) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + mutating func explicitAlignment(_ k: AlignmentKey, at viewSize: ViewSize) -> CGFloat? { |
| 179 | + let placement = childPlacement(at: viewSize) |
| 180 | + let dimensions = child.dimensions(in: placement.proposedSize_) |
| 181 | + let alignment = child.explicitAlignment(k, at: dimensions.size) |
| 182 | + if let alignment { |
| 183 | + return placement.frameOrigin(childSize: dimensions.size.value)[k.axis] + alignment |
| 184 | + } else { |
| 185 | + return alignment |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// MARK: - LayoutPositionQuery [6.4.41] |
| 191 | + |
| 192 | +package struct LayoutPositionQuery: Rule, AsyncAttribute { |
| 193 | + @Attribute private var parentPosition: ViewOrigin |
| 194 | + @Attribute private var localPosition: ViewOrigin |
| 195 | + |
| 196 | + package init( |
| 197 | + parentPosition: Attribute<ViewOrigin>, |
| 198 | + localPosition: Attribute<ViewOrigin>, |
| 199 | + ) { |
| 200 | + _parentPosition = parentPosition |
| 201 | + _localPosition = localPosition |
| 202 | + } |
| 203 | + |
| 204 | + package var value: ViewOrigin { |
| 205 | + let localPosition = localPosition |
| 206 | + let parentPosition = parentPosition |
| 207 | + return ViewOrigin( |
| 208 | + x: localPosition.x + parentPosition.x, |
| 209 | + y: localPosition.y + parentPosition.y |
| 210 | + ) |
| 211 | + } |
| 212 | +} |
0 commit comments