#if DEBUG import AppKit import SwiftUI /// Draw a view offscreen at 2x into a PNG (`-CottonSnapshot `): the /// popover's face without a screen, a click, or screen-recording permission. @MainActor enum Snapshot { static func write(_ view: some View, size: NSSize, to path: String) { // -CottonSnapshotDark YES draws the dark appearance; the panel's own colours follow it. let dark = UserDefaults.standard.bool(forKey: "CottonSnapshotDark") let host = NSHostingView(rootView: view.background(Color(nsColor: .windowBackgroundColor))) host.appearance = NSAppearance(named: dark ? .darkAqua : .aqua) host.frame = NSRect(origin: .zero, size: size) let window = NSWindow( contentRect: NSRect(origin: NSPoint(x: -20000, y: -20000), size: size), styleMask: [.borderless], backing: .buffered, defer: false ) window.appearance = NSAppearance(named: dark ? .darkAqua : .aqua) window.contentView = host window.orderFrontRegardless() host.layoutSubtreeIfNeeded() host.display() guard let rep = NSBitmapImageRep( bitmapDataPlanes: nil, pixelsWide: Int(size.width * 2), pixelsHigh: Int(size.height * 2), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0 ) else { return } rep.size = size host.cacheDisplay(in: host.bounds, to: rep) if let png = rep.representation(using: .png, properties: [:]) { try? png.write(to: URL(fileURLWithPath: path)) } window.orderOut(nil) } } #endif