From a5bff1d95c0d4300b77669f4d9502c30e88d2cfc Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 13 Dec 2025 19:34:11 +0800 Subject: [PATCH] Fix index issue in DynamicContainer --- .../DynamicViewContent/ForEachUITests.swift | 48 +++++++++++++++++++ .../Layout/Dynamic/DynamicContainer.swift | 5 +- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Example/OpenSwiftUIUITests/View/DynamicViewContent/ForEachUITests.swift b/Example/OpenSwiftUIUITests/View/DynamicViewContent/ForEachUITests.swift index 6f018ea8e..8e63bd904 100644 --- a/Example/OpenSwiftUIUITests/View/DynamicViewContent/ForEachUITests.swift +++ b/Example/OpenSwiftUIUITests/View/DynamicViewContent/ForEachUITests.swift @@ -65,4 +65,52 @@ struct ForEachUITests { // openSwiftUIAssertAnimationSnapshot(of: ContentView()) } } + + @Test( + .bug( + "https://github.com/OpenSwiftUIProject/OpenSwiftUI/issues/669", + id: 669 + ) + ) + func dynamicContainerIndex() { + struct ContentView1: View { + var body: some View { + VStack { + Color.red + Color.green + ForEach(0 ..< 1) { index in + Color.red.frame(width: 20, height: 20) + Spacer() + Color.blue.frame(width: 20, height: 20) + } + } + } + } + struct ContentView2: View { + var body: some View { + VStack { + Color.red + Color.green + ForEach(0 ..< 1) { index in + Spacer() + Color.blue + } + } + } + } + struct ContentView3: View { + var body: some View { + VStack { + Color.red.frame(width: 10, height: 10) + Color.green.frame(width: 20, height: 20) + ForEach(0 ..< 1) { index in + Color.blue.frame(width: 40, height: 40) + } + } + } + } + openSwiftUIAssertSnapshot(of: ContentView1(), named: "1") + openSwiftUIAssertSnapshot(of: ContentView2(), named: "2") + openSwiftUIAssertSnapshot(of: ContentView3(), named: "3") + } } diff --git a/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicContainer.swift b/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicContainer.swift index dbc9afd54..216f0a3a5 100644 --- a/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicContainer.swift +++ b/Sources/OpenSwiftUICore/Layout/Dynamic/DynamicContainer.swift @@ -46,10 +46,11 @@ package struct DynamicContainer { fileprivate(set) var seed: UInt32 = .zero func viewIndex(id: ID) -> Int? { - guard let value = indexMap[id.uniqueId] else { + guard let index = indexMap[id.uniqueId] else { return nil } - return value + Int(id.viewIndex) + let item = items[index] + return Int(item.precedingViewCount + id.viewIndex) } func item(for subgraph: Subgraph) -> ItemInfo? {