feat(ui): 统一实现卡片背景趋势与大数值布局

This commit is contained in:
2026-11-15 15:40:29 +08:00
parent 397156249b
commit 3c1a2baace
25 changed files with 447 additions and 268 deletions
+19 -1
View File
@@ -23,6 +23,10 @@ import QuartzCore
plot.configure(continuous)
let image = bitmap(plot)
precondition(redPixels(image, from: 0.4, to: 0.6) > 10, "Continuous curve must cross the middle")
let fill = image.colorAt(x: image.pixelsWide / 2, y: image.pixelsHigh / 2)!
precondition(fill.alphaComponent > 0.10 && fill.alphaComponent < 0.18,
"A continuous run must have a translucent area below its real curve")
precondition(plot.grid.isHidden, "Background trends must not draw a chart grid")
plot.needsDisplay = false; plot.configure(continuous)
precondition(!plot.needsDisplay, "Identical inputs must not invalidate the plot")
plot.configure(input([0.8,0.8,nil,0.8]))
@@ -39,6 +43,20 @@ import QuartzCore
precondition(dots.path == nil, "Connecting a missing value must not invent a measured hover dot")
plot.configure(input([nil,nil,nil,nil]))
precondition(redPixels(bitmap(plot)) == 0)
plot.configure(input([nil,0.8,nil,nil]))
let single = bitmap(plot)
precondition(single.colorAt(x: single.pixelsWide / 2, y: single.pixelsHigh / 2)!.alphaComponent == 0,
"A single sample must not create a fabricated area")
var hiddenSeries = NativePanelCurve.Input(frame: .init(dates: dates,
series: [[nil,nil,nil,nil], [0.8,0.8,0.8,0.8]], intervals: [2,2,2,2], boundaryDates: []),
colors: [.red, .red], scales: [1], maximumGap: 5)
hiddenSeries.visibleSeriesCount = 1; hiddenSeries.hoveredIndex = 1
plot.configure(hiddenSeries)
precondition(redPixels(bitmap(plot)) == 0, "A tooltip-only series must not leak lines, fill or hover dots")
var unfilled = continuous; unfilled.filledSeriesCount = 0
plot.configure(unfilled)
let noArea = bitmap(plot)
precondition(noArea.colorAt(x: noArea.pixelsWide / 2, y: noArea.pixelsHigh / 2)!.alphaComponent == 0)
let changedDates = [0.0, 2, 8, 10].map { Date(timeIntervalSinceReferenceDate: $0) }
plot.configure(.init(frame: .init(dates: changedDates, series: [[0.8,0.8,0.8,0.8]],
intervals: [2,30,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5))
@@ -84,7 +102,7 @@ import QuartzCore
assertStatic(plot.layer!)
plot.removeFromSuperview()
window.orderOut(nil); window.close()
print("PASS: static curve pixels, immediate stable updates, no animation/material/gradient layers, real gaps, hover cache, palette growth, appearance, full history and unmount")
print("PASS: static background lines/area pixels, no single-point fill, hidden-series isolation, immediate stable updates, no animation/material/gradient layers, real gaps, hover cache, palette growth, appearance, full history and unmount")
fflush(stdout); NSApp.terminate(nil)
}
NSApp.run()
+14 -14
View File
@@ -3,7 +3,7 @@ import CoreGraphics
@main struct PanelLayoutChecks {
static func main() {
precondition(PanelLayout.contentSize(visible: CGRect(x: 0, y: 0, width: 1512, height: 900)) == CGSize(width: 960, height: 514))
precondition(PanelLayout.contentSize(visible: CGRect(x: 0, y: 0, width: 1512, height: 900)) == CGSize(width: 960, height: 630))
for visible in [CGRect(x: 0, y: 67, width: 1512, height: 882), CGRect(x: -1440, y: -200, width: 1440, height: 800), CGRect(x: 0, y: 24, width: 800, height: 600)] {
let content = PanelLayout.contentSize(visible: visible, chrome: CGSize(width: 4, height: 12))
precondition(content.width + 4 <= visible.width - 32 && content.height + 12 <= visible.height - 32)
@@ -32,28 +32,28 @@ import CoreGraphics
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 500), heights: compact))
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 2400), heights: compact))
}
precondition(PanelLayout.rowHeights(columns: 3, heights: compact) == [204, 208])
precondition(PanelLayout.rowHeights(columns: 2, heights: compact) == [204, 180, 208])
precondition(PanelLayout.cardWidth(size: CGSize(width: 960, height: 514)) == 308,
precondition(PanelLayout.rowHeights(columns: 3, heights: compact) == [264, 264])
precondition(PanelLayout.rowHeights(columns: 2, heights: compact) == [264, 264, 264])
precondition(PanelLayout.cardWidth(size: CGSize(width: 960, height: 630)) == 308,
"Three columns must retain readable card widths")
precondition(PanelLayout.preferredHeight(width: 960, heights: compact) == 514)
precondition(PanelLayout.preferredHeight(width: 640, heights: compact) == 702)
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 960, height: 514), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 960, height: 513), heights: compact))
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 702), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 701), heights: compact))
precondition(PanelLayout.preferredHeight(width: 960, heights: compact) == 630)
precondition(PanelLayout.preferredHeight(width: 640, heights: compact) == 800)
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 960, height: 630), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 960, height: 629), heights: compact))
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 902), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 901), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 480, height: 800), heights: compact))
precondition(PanelLayout.columns(width: 960) == 3 && PanelLayout.columns(width: 900) == 3)
precondition(PanelLayout.columns(width: 899) == 2 && PanelLayout.columns(width: 560) == 2 && PanelLayout.columns(width: 559) == 1)
let tallerCPU = PanelLayout.cardHeights(coreRows: 6, fanCount: 2)
precondition(PanelLayout.rowHeights(columns: 3, heights: tallerCPU) == [308, 208],
precondition(PanelLayout.rowHeights(columns: 3, heights: tallerCPU) == [368, 264],
"Extra CPU rows must grow the first grid row without changing the second")
let moreFans = PanelLayout.cardHeights(coreRows: 2, fanCount: 10)
precondition(moreFans[.gpu]! > compact[.gpu]! && moreFans[.storage] == compact[.storage])
precondition(PanelLayout.rowHeights(columns: 3, heights: moreFans) == [244, 208])
precondition(PanelLayout.rowHeights(columns: 3, heights: moreFans) == [344, 264])
let noFans = PanelLayout.cardHeights(coreRows: 2, fanCount: 0)
precondition(noFans[.gpu] == 124 && PanelLayout.rowHeights(columns: 3, heights: noFans) == [204, 208])
precondition(PanelLayout.preferredHeight(width: 960, heights: noFans) == 514,
precondition(noFans[.gpu] == 264 && PanelLayout.rowHeights(columns: 3, heights: noFans) == [264, 264])
precondition(PanelLayout.preferredHeight(width: 960, heights: noFans) == 630,
"A fanless GPU must keep row alignment with CPU and memory")
precondition(PanelLayout.cardHeights(coreRows: 2, fanCount: nil) == compact,
"Discovery placeholders must reserve the ordinary fan row")
+76
View File
@@ -0,0 +1,76 @@
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) {}
}
}