import SwiftUI /// A list row's resting lift as one picture (291 row F, architect 01:05 IST /// 17 Oct 2026: a row's paper, texture and shadow are drawn once, never per /// frame). `PaperLift`'s two layers on a sheet's composite are blurred again /// by the render server in every frame the row moves, two offscreen passes a /// row while a list is flung; here the same two layers are cast once from /// the sheet's outline, per sheet and size, and kept. The outline is filled /// with the sheet's own colour, so the sheet laid over it shows no seam. The /// resting pair only: a sheet picked up or hovered keeps `PaperLift`. public struct PaperRestingShadow: View { let edge: PaperEdge let seed: String /// The sheet's own colour, under its picture. let fill: Color @Environment(\.theme) private var theme @Environment(\.displayScale) private var displayScale @Environment(\.colorScheme) private var scheme public init(edge: PaperEdge, seed: String, fill: Color) { self.edge = edge self.seed = seed self.fill = fill } public var body: some View { // A theme that casts nothing lifts nothing (`PaperLift`). let layers = theme.texture.shadow == .none ? [] : PaperLift.layers(.resting, hovered: false, scale: theme.scale) GeometryReader { geo in if layers.dropFirst().count == 1, let image = picture(size: geo.size, layers: layers) { Image(decorative: image, scale: Self.scale(displayScale)) .resizable() .padding(-Self.reach(layers)) } } .allowsHitTesting(false) .accessibilityHidden(true) } /// How far the two layers reach past the sheet's outline, in points: a /// blur fades out within about three of its radii, plus its throw. static func reach(_ layers: [PaperShadowLayer]) -> CGFloat { ceil(layers.map { $0.radius * 3 + max(abs($0.x), abs($0.y)) }.max() ?? 0) } /// A blur needs no more than two pixels a point; a third only costs memory. static func scale(_ display: CGFloat) -> CGFloat { min(display, 2) } private func picture(size: CGSize, layers: [PaperShadowLayer]) -> CGImage? { let scale = Self.scale(displayScale) let ink = theme.colors.shadow.color let key = PaperShadowCache.Key( edge: edge, seed: seed, width: size.width, height: size.height, scale: scale, scheme: scheme, corner: theme.scale, fill: fill, ink: ink, layers: layers) return PaperShadowCache.shared.image(for: key) { let reach = Self.reach(layers) let renderer = ImageRenderer(content: PaperShape(edge: edge, seed: seed, theme: theme) .fill(fill) .frame(width: size.width, height: size.height) .shadow(color: ink.opacity(layers[0].opacity), radius: layers[0].radius, x: layers[0].x, y: layers[0].y) .shadow(color: ink.opacity(layers[1].opacity), radius: layers[1].radius, x: layers[1].x, y: layers[1].y) .padding(reach) .environment(\.colorScheme, scheme) .environment(\.theme, theme)) renderer.scale = scale return renderer.cgImage } } } /// The resting shadows drawn so far, beside the surfaces' own pictures and /// under the same budget. @MainActor final class PaperShadowCache { static let shared = PaperShadowCache() struct Key: Hashable { let edge: PaperEdge let seed: String let width: CGFloat let height: CGFloat let scale: CGFloat let scheme: ColorScheme /// The theme's corner: two themes cut two outlines. let corner: ThemeScale let fill: Color let ink: Color let layers: [PaperShadowLayer] var drawable: Bool { width.isFinite && height.isFinite && width > 0 && height > 0 && width * height * scale * scale <= PaperSurfaceImageBudget.maxPixels } } private final class Box: NSObject { let key: Key init(_ key: Key) { self.key = key } override var hash: Int { key.hashValue } override func isEqual(_ object: Any?) -> Bool { (object as? Box)?.key == key } } private let images = NSCache() init() { images.countLimit = PaperSurfaceImageBudget.countLimit images.totalCostLimit = PaperSurfaceImageBudget.costLimit } func image(for key: Key, render: () -> CGImage?) -> CGImage? { guard key.drawable else { return nil } let box = Box(key) if let image = images.object(forKey: box) { return image } guard let image = render() else { return nil } images.setObject(image, forKey: box, cost: image.bytesPerRow * image.height) return image } }