import AppKit import SwiftUI /// Mount the production card and chart together; verify their real native geometry. @main struct ResourceCardChecks { @MainActor static func main() { _ = NSApplication.shared NSApp.setActivationPolicy(.accessory) Task { @MainActor in let window = NSWindow(contentRect: NSRect(x: 40, y: 40, width: 308, height: 264), styleMask: [.borderless], backing: .buffered, defer: false) window.isReleasedWhenClosed = false; window.level = .floating let probe = DetailProbe() let host = NSHostingView(rootView: AnyView(EmptyView())); host.sizingOptions = [] window.contentView = host; window.orderFrontRegardless() for dark in [false, true] { window.appearance = NSAppearance(named: dark ? .darkAqua : .aqua) for language in ["zh-Hans", "zh-Hant", "en"] { for width: CGFloat in [266, 308, 460] { window.setContentSize(NSSize(width: width, height: 264)) host.rootView = AnyView(fixture(probe: probe, dualMetric: width == 308) .environment(\.locale, Locale(identifier: language)) .environment(\.colorScheme, dark ? .dark : .light)) try? await Task.sleep(for: .milliseconds(80)) host.layoutSubtreeIfNeeded() let plots = descendants(host).compactMap { $0 as? NativePanelCurve.PlotView } precondition(plots.count == 1) let plot = plots[0] precondition(abs(plot.bounds.width - width) < 0.5 && plot.bounds.height == 72, "The background chart must occupy the full card width, without axes or plot insets") let graph = topRect(plot, in: host) precondition(abs(graph.maxY - host.bounds.height) < 0.5, "The background chart must end at the card bottom") let detail = topRect(probe.view!, in: host) precondition(detail.maxY <= graph.minY + 0.5 && detail.width <= width - 32 + 0.5, "Foreground details must stay above the graph, including narrow/localized cards") precondition(plot.grid.isHidden && plot.input?.visibleSeriesCount == 1) } } } host.rootView = AnyView(EmptyView()) try? await Task.sleep(for: .milliseconds(100)) precondition(descendants(host).allSatisfy { !($0 is NativePanelCurve.PlotView) }) window.orderOut(nil); window.contentView = nil; window.close() print("PASS: production resource card/plot geometry at 266/308/460pt, light/dark, three languages, foreground separation and unmount") NSApp.terminate(nil) } NSApp.run() } @MainActor private static func fixture(probe: DetailProbe, dualMetric: Bool) -> some View { let dates = (0..<60).map { Date(timeIntervalSinceReferenceDate: Double($0 * 2)) } return ResourceCard(title: "metric.network", symbol: "network", color: .blue, value: dualMetric ? "↓ 999.9 TB/s" : "99%", secondaryValue: dualMetric ? "↑ 999.9 TB/s" : nil, chart: { PanelTrendChart(dates: dates, first: Array(repeating: 0.3, count: 60), second: Array(repeating: 0.6, count: 60), color: .blue) }) { DetailMarker(probe: probe).frame(height: 84) } } @MainActor private static func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) } @MainActor private static func topRect(_ view: NSView, in host: NSView) -> CGRect { let rect = view.convert(view.bounds, to: host) return host.isFlipped ? rect : CGRect(x: rect.minX, y: host.bounds.height - rect.maxY, width: rect.width, height: rect.height) } @MainActor private final class DetailProbe { weak var view: NSView? } private struct DetailMarker: NSViewRepresentable { let probe: DetailProbe func makeNSView(context: Context) -> NSView { let view = NSView(); probe.view = view; return view } func updateNSView(_ view: NSView, context: Context) {} } }