Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/zero-swift-negative-viewport.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions swift/Sources/ZeroVirtual/LayoutHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions swift/Tests/ZeroVirtualTests/LayoutHostTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading