Skip to content

Commit 77bfa8c

Browse files
authored
Add layoutPriority support (#337)
* Implement LayoutPriority * Add layoutPriority and overlayAndBackground test case * Fix overlay and makeSecondaryLayerView bug * Update Example VC
1 parent e0ef2b1 commit 77bfa8c

File tree

6 files changed

+104
-18
lines changed

6 files changed

+104
-18
lines changed

Example/Example.xcodeproj/xcshareddata/xcschemes/OpenSwiftUIUITests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
buildArchitectures = "Automatic">
99
</BuildAction>
1010
<TestAction
11-
buildConfiguration = "OpenSwiftUIDebug"
11+
buildConfiguration = "SwiftUIDebug"
1212
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
1313
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
1414
shouldUseLaunchSchemeArgsEnv = "YES">

Example/HostingExample/ViewController.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,18 @@ class ViewController: NSViewController {
6666

6767
struct ContentView: View {
6868
var body: some View {
69-
Color.red
70-
.frame(width: 200, height: 200)
69+
ZStack(alignment: .leading) {
70+
Color.red
71+
.opacity(0.5)
72+
.frame(width: 200, height: 200)
73+
}
74+
.overlay(alignment: .topLeading) {
75+
Color.green.opacity(0.5)
76+
.frame(width: 100, height: 100)
77+
}
78+
.background(alignment: .bottomTrailing) {
79+
Color.blue.opacity(0.5)
80+
.frame(width: 100, height: 100)
81+
}
7182
}
7283
}

Example/OpenSwiftUIUITests/Layout/Stack/ZStackUITests.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,54 @@ struct ZStackUITests {
4040
)
4141
}
4242

43-
// TODO: layoutPriority
43+
@Test
44+
func layoutPriority() {
45+
struct ContentView: View {
46+
var body: some View {
47+
ZStack(alignment: .leading) {
48+
Color.blue
49+
.opacity(0.5)
50+
.frame(width: 100, height: 100)
51+
.layoutPriority(-0.1)
52+
Color.red
53+
.opacity(0.5)
54+
.frame(width: 60, height: 60)
55+
Color.green
56+
.frame(width: 40, height: 40)
57+
.layoutPriority(1)
58+
}
59+
.overlay {
60+
Color.black
61+
.frame(width: 20, height: 20)
62+
}
63+
}
64+
}
65+
openSwiftUIAssertSnapshot(
66+
of: ContentView()
67+
)
68+
}
69+
70+
@Test
71+
func overlayAndBackground() {
72+
struct ContentView: View {
73+
var body: some View {
74+
ZStack(alignment: .leading) {
75+
Color.red
76+
.opacity(0.5)
77+
.frame(width: 200, height: 200)
78+
}
79+
.overlay(alignment: .topLeading) {
80+
Color.green.opacity(0.5)
81+
.frame(width: 100, height: 100)
82+
}
83+
.background(alignment: .bottomTrailing) {
84+
Color.blue.opacity(0.5)
85+
.frame(width: 100, height: 100)
86+
}
87+
}
88+
}
89+
openSwiftUIAssertSnapshot(
90+
of: ContentView()
91+
)
92+
}
4493
}

Sources/OpenSwiftUICore/Layout/Stack/LayoutPriorityLayout.swift renamed to Sources/OpenSwiftUICore/Layout/LayoutPriority.swift

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//
2-
// LayoutPriorityLayout.swift
2+
// LayoutPriority.swift
33
// OpenSwiftUICore
44
//
5-
// Audited for iOS 18.0
6-
// Status: WIP
5+
// Status: Complete
76

87
package import Foundation
98

9+
// MARK: - View + layoutPriority [6.4.41]
10+
11+
@available(OpenSwiftUI_v1_0, *)
1012
extension View {
1113
/// Sets the priority by which a parent layout should apportion space to
1214
/// this child.
@@ -49,6 +51,8 @@ extension View {
4951
}
5052
}
5153

54+
// MARK: - LayoutPriorityTraitKey [6.4.41]
55+
5256
@usableFromInline
5357
package struct LayoutPriorityTraitKey: _ViewTraitKey {
5458
@inlinable
@@ -58,17 +62,36 @@ package struct LayoutPriorityTraitKey: _ViewTraitKey {
5862
@available(*, unavailable)
5963
extension LayoutPriorityTraitKey: Sendable {}
6064

61-
// FIXME
65+
// MARK: - LayoutPriorityLayout [6.4.41]
66+
6267
package struct LayoutPriorityLayout: UnaryLayout {
68+
package init(priority: Double) {
69+
self.priority = priority
70+
}
71+
6372
package func placement(of child: LayoutProxy, in context: PlacementContext) -> _Placement {
64-
preconditionFailure("TODO")
73+
_Placement(
74+
proposedSize: context.proposedSize,
75+
aligning: .center,
76+
in: context.size
77+
)
6578
}
6679

6780
package func sizeThatFits(in proposedSize: _ProposedSize, context: SizeAndSpacingContext, child: LayoutProxy) -> CGSize {
68-
preconditionFailure("TODO")
81+
child.size(in: proposedSize)
6982
}
70-
71-
package static func makeViewImpl(modifier: _GraphValue<LayoutPriorityLayout>, inputs: _ViewInputs, body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs) -> _ViewOutputs {
72-
.init()
83+
84+
package func spacing(in context: SizeAndSpacingContext, child: LayoutProxy) -> Spacing {
85+
child.spacing()
86+
}
87+
88+
package func layoutPriority(child: LayoutProxy) -> Double {
89+
priority
7390
}
91+
92+
package func ignoresAutomaticPadding(child: LayoutProxy) -> Bool {
93+
child.ignoresAutomaticPadding
94+
}
95+
96+
package var priority: Double
7497
}

Sources/OpenSwiftUICore/Modifier/ViewModifier/OverlayModifier.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ package func makeSecondaryLayerView<SecondaryLayer>(
4545
key.visitKey(&visitor)
4646
}
4747
var result = visitor.result
48-
result.layoutComputer = secondaryOutputs.layoutComputer
49-
return visitor.result
48+
result.layoutComputer = primaryOutputs.layoutComputer
49+
return result
5050
}
5151

5252
// MARK: - OverlayModifier [6.4.41]
@@ -208,7 +208,7 @@ extension View {
208208
/// - Returns: A view that uses the specified content as a foreground.
209209
@inlinable
210210
nonisolated public func overlay<V>(alignment: Alignment = .center, @ViewBuilder content: () -> V) -> some View where V: View {
211-
modifier(_BackgroundModifier(background: content(), alignment: alignment))
211+
modifier(_OverlayModifier(overlay: content(), alignment: alignment))
212212
}
213213
}
214214

Sources/OpenSwiftUICore/View/Graph/ViewGraph.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ package final class ViewGraph: GraphHost {
159159
// _inheritedPhase
160160
_gestureResetSeed = Attribute(value: .zero)
161161
_gesturePreferenceKeys = Attribute(value: .init())
162-
_rootGeometry = Attribute(RootGeometry(proposedSize: _proposedSize, safeAreaInsets: OptionalAttribute(_safeAreaInsets)))
162+
_rootGeometry = Attribute(RootGeometry(
163+
proposedSize: _proposedSize,
164+
safeAreaInsets: OptionalAttribute(_safeAreaInsets)
165+
))
163166
_position = _rootGeometry.origin()
164167
_dimensions = _rootGeometry.size()
165168
makeRootView = { [_zeroPoint, _proposedSize, _safeAreaInsets] view, inputs in
@@ -236,7 +239,7 @@ package final class ViewGraph: GraphHost {
236239
as: RootGeometry.self,
237240
invalidating: true
238241
) { rootGeometry in
239-
rootGeometry.$layoutDirection = inputs.mapEnvironment(id: .layoutDirection) { $0.layoutDirection }
242+
rootGeometry.$layoutDirection = inputs.layoutDirection
240243
}
241244
for feature in features {
242245
feature.modifyViewInputs(inputs: &inputs, graph: self)

0 commit comments

Comments
 (0)