diff --git a/Example/OpenSwiftUIUITests/Layout/Stack/ZStackIndexUITests.swift b/Example/OpenSwiftUIUITests/Layout/Stack/ZStackIndexUITests.swift index dcad0457d..9280e55ba 100644 --- a/Example/OpenSwiftUIUITests/Layout/Stack/ZStackIndexUITests.swift +++ b/Example/OpenSwiftUIUITests/Layout/Stack/ZStackIndexUITests.swift @@ -10,7 +10,7 @@ import SnapshotTesting @Suite(.snapshots(record: .never, diffTool: diffTool)) struct ZStackIndexUITests { @Test - func zIndexExample() { + func rotateOverlap() { struct ContentView: View { var body: some View { VStack { @@ -30,11 +30,9 @@ struct ZStackIndexUITests { } } } - withKnownIssue { - openSwiftUIAssertSnapshot( - of: ContentView(), - size: CGSize(width: 200, height: 200) - ) - } + openSwiftUIAssertSnapshot( + of: ContentView(), + size: CGSize(width: 200, height: 200) + ) } } diff --git a/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicLayoutMap.swift b/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicLayoutMap.swift index acc2210f1..b541ea150 100644 --- a/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicLayoutMap.swift +++ b/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicLayoutMap.swift @@ -31,17 +31,23 @@ package struct DynamicLayoutMap { map.first(where: { $0.id == id })?.value ?? .init() } set { - let index = map.firstIndex(where: { $0.id == id }) - if let index { - if newValue.isEmpty { - map.remove(at: index) - } else { - map[index].value = newValue - } - } else { + if map.isEmpty { if !newValue.isEmpty { map.insert((id, newValue), at: 0) } + } else { + let index = map.lowerBound { $0.id < id } + if index != map.count, map[index].id == id { + if newValue.isEmpty { + map.remove(at: index) + } else { + map[index].value = newValue + } + } else { + if !newValue.isEmpty { + map.insert((id, newValue), at: index) + } + } } sortedSeed = .zero } diff --git a/Tests/OpenSwiftUICoreTests/Layout/ZIndexTests.swift b/Tests/OpenSwiftUICoreTests/Layout/ZIndexTests.swift index 8924eef12..6410e7635 100644 --- a/Tests/OpenSwiftUICoreTests/Layout/ZIndexTests.swift +++ b/Tests/OpenSwiftUICoreTests/Layout/ZIndexTests.swift @@ -3,9 +3,9 @@ // OpenSwiftUICoreTests import OpenAttributeGraphShims -@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore import Testing +import Foundation @MainActor struct ZIndexTests { @@ -16,4 +16,45 @@ struct ZIndexTests { collection.zIndex = 1.5 #expect(collection.zIndex.isApproximatelyEqual(to: 1.5)) } + + #if canImport(Darwin) + @Test + func zIndexDisplayList() { + struct ContentView: View { + var body: some View { + GeometryReader { proxy in + VStack { + Color.white + .frame(width: 100, height: 100, alignment: .center) + .zIndex(1) + Color.black + .frame(width: 100, height: 100, alignment: .center) + } + .frame(width: 200, height: 200) + } + .ignoresSafeArea() + } + } + let graph = ViewGraph( + rootViewType: ContentView.self, + requestedOutputs: [.layout, .displayList] + ) + graph.instantiateOutputs() + graph.setRootView(ContentView()) + graph.setProposedSize(CGSize(width: 1000, height: 1000)) + let (displayList, _) = graph.displayList() + let expectRegex = try! Regex(#""" + \(display-list + \(item #:identity \d+ #:version \d+ + \(frame \(50.0 104.0; 100.0 100.0\)\) + \(content-seed \d+\) + \(color #000000FF\)\) + \(item #:identity \d+ #:version \d+ + \(frame \(50.0 -4.0; 100.0 100.0\)\) + \(content-seed \d+\) + \(color #FFFFFFFF\)\)\) + """#) + #expect(displayList.description.contains(expectRegex)) + } + #endif }