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
+35 -4
View File
@@ -15,7 +15,7 @@ nonisolated enum PanelCurveProjection: Equatable {
}
}
/// Bounded static lines and per-run area paths. Hover never rebuilds geometry.
/// Bounded lines and per-run area paths. Hover never rebuilds geometry.
struct NativePanelCurve: NSViewRepresentable {
let frame: PanelCurveFrame
let colors: [Color]
@@ -26,6 +26,7 @@ struct NativePanelCurve: NSViewRepresentable {
var filledSeriesCount = 1
var stacked = false
var projection: PanelCurveProjection = .fullHeight
var animates = false
func sizeThatFits(_ proposal: ProposedViewSize, nsView: PlotView, context: Context) -> CGSize? {
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
@@ -34,7 +35,7 @@ struct NativePanelCurve: NSViewRepresentable {
func makeNSView(context: Context) -> PlotView { PlotView() }
func updateNSView(_ view: PlotView, context: Context) {
view.configure(Input(frame: frame, colors: colors, scales: scales, maximumGap: maximumGap,
hoveredIndex: hoveredIndex, visibleSeriesCount: visibleSeriesCount, filledSeriesCount: filledSeriesCount, stacked: stacked, projection: projection))
hoveredIndex: hoveredIndex, visibleSeriesCount: visibleSeriesCount, filledSeriesCount: filledSeriesCount, stacked: stacked, projection: projection, animates: animates))
}
struct Input: Equatable {
let frame: PanelCurveFrame
@@ -46,6 +47,7 @@ struct NativePanelCurve: NSViewRepresentable {
var filledSeriesCount = 1
var stacked = false
var projection: PanelCurveProjection = .fullHeight
var animates = false
}
final class PlotView: NSView {
private(set) var input: Input?
@@ -57,6 +59,8 @@ struct NativePanelCurve: NSViewRepresentable {
private var hoverDots: [CAShapeLayer] = []
private var seriesLayers: [SeriesLayer] = []
private var palette: [NSColor] = []
private var drawingFrame: PanelCurveFrame?
private static let slideKey = "trend.slide"
private var plottedBands: [StackedSeries.Band] = []
override var isFlipped: Bool { true }
override func hitTest(_ point: NSPoint) -> NSView? { nil }
@@ -79,7 +83,15 @@ struct NativePanelCurve: NSViewRepresentable {
guard input != value else { return }
let old = input
let frameChanged = old?.frame != value.frame
let slides = value.animates && value.hoveredIndex == nil && window?.isVisible == true
&& frameChanged && old.map { value.frame.follows($0.frame, maximumGap: value.maximumGap) } == true
input = value
if frameChanged || !value.animates || value.hoveredIndex != nil {
curves.removeAnimation(forKey: Self.slideKey)
}
if frameChanged {
drawingFrame = slides ? old.map { value.frame.includingOutgoing(from: $0.frame) } : nil
}
CATransaction.begin(); CATransaction.setDisableActions(true)
if old?.projection != value.projection { updateBaseline() }
curves.isHidden = value.colors.isEmpty || value.scales.isEmpty
@@ -93,8 +105,24 @@ struct NativePanelCurve: NSViewRepresentable {
rebuildPaths()
}
updateHover()
if slides, let old { slide(from: old.frame, to: value.frame) }
CATransaction.commit()
}
private func slide(from previous: PanelCurveFrame, to next: PanelCurveFrame) {
guard bounds.width > 0, let a = previous.dates.first, let b = previous.dates.last,
let c = next.dates.first, let d = next.dates.last else { return }
// Map the new geometry onto the old time domain, then reveal the new point.
let span = b.timeIntervalSince(a)
let transform = CGAffineTransform(a: d.timeIntervalSince(c) / span, b: 0, c: 0, d: 1,
tx: c.timeIntervalSince(a) / span * bounds.width, ty: 0)
let animation = CABasicAnimation(keyPath: "transform")
animation.fromValue = NSValue(caTransform3D: CATransform3DMakeAffineTransform(transform))
animation.toValue = NSValue(caTransform3D: CATransform3DIdentity)
animation.duration = 0.35
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
curves.add(animation, forKey: Self.slideKey)
}
private func ensureLayers(_ count: Int) {
while seriesLayers.count < count {
let series = SeriesLayer(); seriesLayers.append(series); curves.addSublayer(series.root)
@@ -153,7 +181,7 @@ struct NativePanelCurve: NSViewRepresentable {
return
}
let domain = PanelCurveFrame.domain(begin: first, end: last)
let frame = input.frame
let frame = drawingFrame ?? input.frame
plottedBands = input.stacked ? StackedSeries.bands(frame.series, count: frame.dates.count, limit: input.scales.first) : []
for index in frame.series.indices {
let series = seriesLayers[index]
@@ -205,7 +233,8 @@ struct NativePanelCurve: NSViewRepresentable {
for series in input.frame.series.indices {
guard series < (input.visibleSeriesCount ?? input.frame.series.count) else { continue }
let values = input.stacked && plottedBands.indices.contains(series) ? plottedBands[series].upper : input.frame.series[series]
guard values.indices.contains(index), let value = values[index], value.isFinite else { continue }
let plottedIndex = index + (input.stacked ? (drawingFrame?.dates.count ?? input.frame.dates.count) - input.frame.dates.count : 0)
guard values.indices.contains(plottedIndex), let value = values[plottedIndex], value.isFinite else { continue }
let scale = input.scales[input.stacked ? 0 : min(series, input.scales.count - 1)]
let y = input.projection.y(value: value, scale: scale, series: series, height: bounds.height)
hoverDots[series].path = CGPath(ellipseIn: CGRect(x: x - 2.5, y: y - 2.5, width: 5, height: 5), transform: nil)
@@ -215,12 +244,14 @@ struct NativePanelCurve: NSViewRepresentable {
let changed = frame.size != size
super.setFrameSize(size)
if changed {
curves.removeAnimation(forKey: Self.slideKey)
CATransaction.begin(); CATransaction.setDisableActions(true)
updateGrid(); rebuildPaths(); updateHover(); CATransaction.commit()
}
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
if window == nil { curves.removeAnimation(forKey: Self.slideKey) }
CATransaction.begin(); CATransaction.setDisableActions(true)
updatePalette(); CATransaction.commit()
}