diff --git a/.changeset/zero-swift-negative-viewport.md b/.changeset/zero-swift-negative-viewport.md new file mode 100644 index 0000000000..f902609bd0 --- /dev/null +++ b/.changeset/zero-swift-negative-viewport.md @@ -0,0 +1,5 @@ +--- +'zero-swift': patch +--- + +`VirtualList` no longer crashes when it's shorter than its insets, as on iOS in a window shorter than its safe area: it lays out as a list with no room for rows, and shows them once it has some. diff --git a/swift/Sources/ZeroVirtual/LayoutHost.swift b/swift/Sources/ZeroVirtual/LayoutHost.swift index a53d12fc70..c5cbb7fbf4 100644 --- a/swift/Sources/ZeroVirtual/LayoutHost.swift +++ b/swift/Sources/ZeroVirtual/LayoutHost.swift @@ -126,6 +126,9 @@ package final class LayoutHost: VirtualHost { ) -> ( frames: [VirtualRowFrame], contentHeight: Double, offset: Double ) { + // A view shorter than its insets reports a negative height, which has + // no rows on screen: an empty viewport. + let viewport = max(0, viewport) inPass = true defer { inPass = false } if frameStarted { diff --git a/swift/Tests/ZeroVirtualTests/LayoutHostTests.swift b/swift/Tests/ZeroVirtualTests/LayoutHostTests.swift index ab11f9399d..7d3fe16243 100644 --- a/swift/Tests/ZeroVirtualTests/LayoutHostTests.swift +++ b/swift/Tests/ZeroVirtualTests/LayoutHostTests.swift @@ -161,6 +161,21 @@ final class LayoutScrollView { #expect(view.scrollOffset == 0) } + @Test("a viewport shorter than its insets lays out empty, and shows its rows once it grows") + func negativeViewport() { + // On iOS, a scroll view shorter than its safe area's insets reports a + // negative viewport (its height less the insets). + let h = Harness(rowCount: 100) { $0.anchoring = .manual } + let view = LayoutScrollView(h, viewportHeight: -40) + view.settle() + #expect(view.host.viewportHeight == 0) + #expect(view.scrollOffset == 0) + view.viewportHeight = 400 + view.settle() + #expect(view.rowTop("r0") == 0) + #expect(view.blank == 0) + } + @Test("rows taller than estimated above the viewport don't move it") func dynamicHeights() { let h = Harness(rowCount: 300) { $0.anchoring = .manual }