feat(panel): 调整资源卡片读数布局并保留趋势平移

逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。

存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
This commit is contained in:
2026-11-15 22:03:46 +08:00
parent c886c4227d
commit 6300db3d15
27 changed files with 427 additions and 250 deletions
+34 -19
View File
@@ -27,32 +27,26 @@ import SwiftUI
precondition(plots.count == 1)
let plot = plots[0]
precondition(abs(host.bounds.width - width) < 1, "AppKit may round fractional content sizes to a point")
precondition(abs(plot.bounds.width - host.bounds.width) < 0.5 && abs(plot.bounds.height - host.bounds.height) < 0.5,
"The background chart must occupy the full card width and height, without axes or plot insets")
let graph = topRect(plot, in: host)
precondition(abs(graph.minY) < 0.5 && 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.minY >= 0 && detail.maxY <= host.bounds.height && detail.width <= host.bounds.width - 20 + 0.5,
"Foreground details must overlay the full-height graph within card bounds, including narrow/localized cards")
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)
let bitmap = host.bitmapImageRepForCachingDisplay(in: host.bounds)!
host.cacheDisplay(in: host.bounds, to: bitmap)
func sample(_ y: Double) -> [Double] {
let c = bitmap.colorAt(x: bitmap.pixelsWide / 2, y: Int(y * Double(bitmap.pixelsHigh) / 176))!.usingColorSpace(.deviceRGB)!
return [Double(c.redComponent), Double(c.greenComponent), Double(c.blueComponent)]
}
let base = sample(3)
let covered = zip(sample(80), base).reduce(0.0) { $0 + abs($1.0 - $1.1) }
let open = zip(sample(150), base).reduce(0.0) { $0 + abs($1.0 - $1.1) }
precondition(open > 0.02 && abs(covered - open) < 0.01,
"Charts must retain the same color under foreground details and bottom whitespace: \(covered), \(open)")
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)")
}
@@ -60,11 +54,32 @@ import SwiftUI
}
}
}
// 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, full-height chart without foreground dimming, larger localized memory/storage detail bounds, unmount")
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()
@@ -90,7 +105,7 @@ import SwiftUI
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", chart: { Color.clear }) {
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))
}
}