From 15bc85d0d945cfff321fab09db894f077a5a9c0f Mon Sep 17 00:00:00 2001 From: Amp Date: Tue, 1 Dec 2026 06:49:22 +0000 Subject: [PATCH] perf(zero-swift): draw VirtualList's debug tint and TestHost's header in a shape, and warn against bare-style backgrounds on macOS 27 On macOS 27, a hosting view whose content has a background or an overlay drawn from a bare ShapeStyle resolves its safe area against the window's corners and so follows its geometry in the window. Every scroll of the clip view then invalidates it, and the invalidation reaches the window's root hosting view through SwiftUI's platform view host: the root and that view lay out again on every frame the list scrolls. macOS 26 doesn't do this, whichever SDK the app is built with. TestHost's header used .background(.quaternary), so the frame probe measured this instead of the list. It now draws in a Rectangle, which renders the same pixels. The debug blank-space tint draws each row's backdrop the same way, and VirtualList's docs tell apps to. Amp-Thread-ID: https://ampcode.com/threads/T-884aab88-be49-4bc7-96a1-044f0e71dabf Co-authored-by: tmm --- .changeset/zero-swift-macos-shape-backgrounds.md | 5 +++++ swift/Sources/ZeroVirtual/RowsScrollView.swift | 7 +++++-- swift/Sources/ZeroVirtual/VirtualList.swift | 9 +++++++++ swift/TestHost/App/Screens.swift | 4 +++- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 .changeset/zero-swift-macos-shape-backgrounds.md diff --git a/.changeset/zero-swift-macos-shape-backgrounds.md b/.changeset/zero-swift-macos-shape-backgrounds.md new file mode 100644 index 0000000000..2589767b93 --- /dev/null +++ b/.changeset/zero-swift-macos-shape-backgrounds.md @@ -0,0 +1,5 @@ +--- +'zero-swift': patch +--- + +`VirtualList`'s docs now warn that on macOS 27 a background or an overlay drawn from a bare `ShapeStyle` in a row, the header or the footer, such as `.background(.quaternary)`, has SwiftUI lay that view and the window's root view out again on every frame the list scrolls. Drawn in a shape, as in `.background(.quaternary, in: Rectangle())`, it looks the same without that work. A debug build's blank-space tint (`-ZeroVirtualTintBlankSpace YES`) now draws each row's backdrop in a shape, so the tint no longer adds that work to every row on each frame. diff --git a/swift/Sources/ZeroVirtual/RowsScrollView.swift b/swift/Sources/ZeroVirtual/RowsScrollView.swift index 45967b319a..920e3f9b0e 100644 --- a/swift/Sources/ZeroVirtual/RowsScrollView.swift +++ b/swift/Sources/ZeroVirtual/RowsScrollView.swift @@ -488,8 +488,11 @@ .environment(\.backgroundProminence, state.emphasized ? .increased : .standard)) if RowsLayout.tintsBlankSpace { // Drawn with the row's content: a row whose content isn't drawn - // yet shows the tint. - content = AnyView(content.background(state.selected ? .clear : Self.backdrop)) + // yet shows the tint. In a shape: a bare style laid every row out + // again on each frame the list scrolled, on macOS 27 (see + // `VirtualList`). + content = AnyView( + content.background(state.selected ? .clear : Self.backdrop, in: Rectangle())) } let shown = layout.selectable diff --git a/swift/Sources/ZeroVirtual/VirtualList.swift b/swift/Sources/ZeroVirtual/VirtualList.swift index c3b0a27e8b..c062c8ebb1 100644 --- a/swift/Sources/ZeroVirtual/VirtualList.swift +++ b/swift/Sources/ZeroVirtual/VirtualList.swift @@ -38,6 +38,15 @@ /// own hosting view, which inherits /// the list's environment: a row's `@ZeroQuery` reads the list's client /// (`.zero(_:)`), and its `@Environment` values are the list's. + /// + /// Draw a background or an overlay in a row, the header or the footer in + /// a shape, as in `.background(.quaternary, in: Rectangle())`, not + /// `.background(.quaternary)`. From a bare `ShapeStyle` (a `Color`, a + /// material, `.quaternary`), SwiftUI on macOS 27 follows the view's place + /// in the window, so on every frame the list scrolls it lays that view + /// out again, and the window's root view with it, rows kept off screen + /// included. In a shape it looks the same, as does + /// `.background { Color.gray }`. public struct VirtualList< Context: Sendable, Row: ZeroRow, Key: Hashable, StartRow: Equatable & Sendable, ListContext: Equatable & Sendable, RowContent: View diff --git a/swift/TestHost/App/Screens.swift b/swift/TestHost/App/Screens.swift index 8be7e9bbec..27c1631616 100644 --- a/swift/TestHost/App/Screens.swift +++ b/swift/TestHost/App/Screens.swift @@ -230,7 +230,9 @@ struct Accessory: View { .font(.headline) .frame(maxWidth: .infinity, alignment: .leading) .padding(12) - .background(.quaternary) + // In a shape: a bare style lays the header and the window's root + // out again on every frame the list scrolls, on macOS 27. + .background(.quaternary, in: Rectangle()) .contentShape(.accessibility, Rectangle()) .accessibilityElement(children: .ignore) .accessibilityLabel(text)