From cc74b53ef5fc1754acedd2e1c0fecab67929bd89 Mon Sep 17 00:00:00 2001 From: Amp Date: Sun, 22 Nov 2026 20:21:21 +0000 Subject: [PATCH] fix(zero-swift): lay out a viewport shorter than its insets as an empty one `RowsScrollView` passes `LayoutHost.layout` its height less its content insets. On iOS a scroll view shorter than its safe area's insets (a 90 pt tall window on an iPhone) makes that negative, and `LayoutHost.reach` then built `(offset - viewport)...(offset + 2 * viewport)` with its lower bound above its upper one, which traps: "Range requires lowerBound <= upperBound". macOS's clip view can do the same with its content insets. `layout(offset:viewport:)` now takes a negative viewport as 0, so the host, and the core reading `viewportHeight`, see a viewport with no room for rows. Once the view grows, its rows are measured and drawn as before. The new layout host test starts at a viewport of -40 and grows to 400; on main it traps. A scratch probe hosting `VirtualList` in an 800x90 window on the iOS Simulator (iPhone 17 Pro) trapped before and lays out every case after. Amp-Thread-ID: https://ampcode.com/threads/T-884aab88-be49-4bc7-96a1-044f0e71dabf Co-authored-by: tmm --- .changeset/zero-swift-negative-viewport.md | 5 +++++ swift/Sources/ZeroVirtual/LayoutHost.swift | 3 +++ .../Tests/ZeroVirtualTests/LayoutHostTests.swift | 15 +++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 .changeset/zero-swift-negative-viewport.md 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 }