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 * 2 / 3, height: 176), 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 [190, 308 * 2 / 3, 400] { window.setContentSize(NSSize(width: width, height: 176)) host.rootView = AnyView(fixture(probe: probe, dualMetric: width < 300) .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(host.bounds.width - width) < 1, "AppKit may round fractional content sizes to a point") let graph = topRect(plot, in: host) let detail = topRect(probe.view!, in: host) precondition(abs(graph.width - host.bounds.width) < 0.5 && abs(graph.maxY - host.bounds.height) < 0.5) precondition(detail.maxY + 3 <= graph.minY && graph.height >= 32, "The plot must sit entirely below the text at every width and locale") precondition(plot.grid.isHidden && plot.input?.visibleSeriesCount == 1) for kind in 0...1 { host.rootView = AnyView(detailsFixture(kind: kind, probe: probe) .environment(\.locale, Locale(identifier: language)) .environment(\.colorScheme, dark ? .dark : .light)) try? await Task.sleep(for: .milliseconds(60)); host.layoutSubtreeIfNeeded() let details = topRect(probe.view!, in: host) let trend = descendants(host).compactMap { $0 as? NativePanelCurve.PlotView }.first! let plotRect = topRect(trend, in: host) if kind == 0 { precondition(plotRect == host.bounds, "Physical memory capacity must map to the top of the card") } else { precondition(plotRect.height >= 24 && plotRect.minY >= details.maxY + 3, "Storage details must leave a separate usable plot") } precondition(details.maxY <= host.bounds.height - 10 + 0.5 && details.width <= host.bounds.width - 20 + 0.5, "Larger production details must fit the unchanged card: \(kind), \(language), \(width), \(details)") } } } } // Mount the production branch with a desktop, a missing sensor and a failed battery read. for state: BatteryAvailability in [.absent, .failed, .present, .unknown] { var sample = MetricsSnapshot() sample.batteryState = state sample.systemPower = state == .absent ? 32.5 : nil if state == .present { sample.battery = BatteryMetrics(charge: 0.8, charging: false, externalPower: true, power: 0) } var history = MetricHistory() history.append(sample) sample.date = sample.date.addingTimeInterval(2); sample.systemPower = nil history.append(sample) host.rootView = AnyView(PowerResourceCard(snapshot: sample, samples: history.samples, chartInterval: 2, chartBoundaries: [], chartIntervals: [2,2])) try? await Task.sleep(for: .milliseconds(80)); host.layoutSubtreeIfNeeded() let plot = descendants(host).compactMap { $0 as? NativePanelCurve.PlotView }.first! let first = plot.input?.frame.series.first switch state { case .absent: precondition(first == [32.5, nil] && plot.input?.scales.first == 32.5) case .present: precondition(first == [0.8, 0.8] && plot.input?.scales.first == 1 && topRect(plot, in: host) == host.bounds) default: precondition(first == [nil, nil] && plot.input?.scales.first == 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 card geometry at 190/205⅓/400pt, light/dark, three languages, separate plot below text, localized memory/storage details, 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.8, count: 60), second: Array(repeating: 0.6, count: 60), color: .blue) }) { DetailMarker(probe: probe).frame(height: 40) } } @MainActor private static func detailsFixture(kind: Int, probe: DetailProbe) -> some View { let memory = MemoryMetrics(total: 48 << 30, used: 26 << 30, wired: 3 << 30, compressed: 1 << 30, swapUsed: 0, pressure: 1) let details = kind == 0 ? AnyView(MemoryDetailsView(memory: memory)) : AnyView(StorageDetailsView(disk: .init(total: 1_000_000_000_000, available: 999_000_000_000), health: .init(remainingLife: 0.98, powerOnHours: 99999), io: .init(read: 999_900_000_000, written: 999_900_000_000))) return ResourceCard(title: kind == 0 ? "metric.memory" : "metric.disk", symbol: "memorychip", color: .blue, value: "26.0 / 48 GiB", chartPlacement: kind == 0 ? .background : .belowDetails, chart: { PanelTrendChart(dates: [Date()], first: [0.5], color: .blue) }) { details.background(DetailMarker(probe: probe).allowsHitTesting(false)) } } @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) {} } }