fix(ui): 修正风扇转轴与速率图并精简进程表

This commit is contained in:
2026-11-15 19:21:18 +08:00
parent 99e558625b
commit 60c146f8b7
21 changed files with 303 additions and 107 deletions
+35 -6
View File
@@ -1,6 +1,20 @@
import AppKit
import SwiftUI
/// Rate plots share a zero axis and equal positive amplitudes on either side.
nonisolated enum PanelCurveProjection: Equatable {
case fullHeight
case split(upwardSeries: Int)
func y(value: Double, scale: Double, series: Int, height: Double) -> Double {
let fraction = min(1, max(0, value / (scale.isFinite && scale > 0 ? scale : 1)))
switch self {
case .fullHeight: return height * (1 - fraction)
case .split(let upward): return height * (0.6 + (series == upward ? -0.3 : 0.3) * fraction)
}
}
}
/// Bounded static lines and per-run area paths. Hover never rebuilds geometry.
struct NativePanelCurve: NSViewRepresentable {
let frame: PanelCurveFrame
@@ -11,6 +25,7 @@ struct NativePanelCurve: NSViewRepresentable {
var visibleSeriesCount: Int? = nil
var filledSeriesCount = 1
var stacked = false
var projection: PanelCurveProjection = .fullHeight
func sizeThatFits(_ proposal: ProposedViewSize, nsView: PlotView, context: Context) -> CGSize? {
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
@@ -19,7 +34,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))
hoveredIndex: hoveredIndex, visibleSeriesCount: visibleSeriesCount, filledSeriesCount: filledSeriesCount, stacked: stacked, projection: projection))
}
struct Input: Equatable {
let frame: PanelCurveFrame
@@ -30,11 +45,13 @@ struct NativePanelCurve: NSViewRepresentable {
var visibleSeriesCount: Int? = nil
var filledSeriesCount = 1
var stacked = false
var projection: PanelCurveProjection = .fullHeight
}
final class PlotView: NSView {
private(set) var input: Input?
private(set) var pathBuildCount = 0
let grid = CAShapeLayer()
let baseline = CAShapeLayer()
let curves = CALayer()
let hoverLine = CAShapeLayer()
private var hoverDots: [CAShapeLayer] = []
@@ -52,7 +69,8 @@ struct NativePanelCurve: NSViewRepresentable {
grid.fillColor = nil; grid.lineWidth = 0.5
grid.isHidden = true
hoverLine.fillColor = nil; hoverLine.lineWidth = 1; hoverLine.lineDashPattern = [3, 2]
for child in [grid, curves, hoverLine] { layer?.addSublayer(child) }
baseline.fillColor = nil; baseline.lineWidth = 0.5; baseline.isHidden = true
for child in [grid, baseline, curves, hoverLine] { layer?.addSublayer(child) }
setAccessibilityElement(false)
}
required init?(coder: NSCoder) { nil }
@@ -63,6 +81,7 @@ struct NativePanelCurve: NSViewRepresentable {
let frameChanged = old?.frame != value.frame
input = value
CATransaction.begin(); CATransaction.setDisableActions(true)
if old?.projection != value.projection { updateBaseline() }
curves.isHidden = value.colors.isEmpty || value.scales.isEmpty
ensureLayers(value.frame.series.count)
if old?.colors != value.colors || old?.frame.series.count != value.frame.series.count
@@ -70,7 +89,7 @@ struct NativePanelCurve: NSViewRepresentable {
for index in seriesLayers.indices {
seriesLayers[index].root.isHidden = index >= (value.visibleSeriesCount ?? value.frame.series.count)
}
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap || old?.stacked != value.stacked {
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap || old?.stacked != value.stacked || old?.projection != value.projection {
rebuildPaths()
}
updateHover()
@@ -89,6 +108,7 @@ struct NativePanelCurve: NSViewRepresentable {
private func updatePalette() {
effectiveAppearance.performAsCurrentDrawingAppearance {
palette = input?.colors.map(NSColor.init) ?? []
baseline.strokeColor = NSColor.labelColor.withAlphaComponent(0.12).cgColor
grid.strokeColor = NSColor.labelColor.withAlphaComponent(0.10).cgColor
hoverLine.strokeColor = NSColor.labelColor.withAlphaComponent(0.45).cgColor
guard !palette.isEmpty else { return }
@@ -102,7 +122,16 @@ struct NativePanelCurve: NSViewRepresentable {
}
}
}
private func updateBaseline() {
baseline.frame = bounds
baseline.isHidden = input?.projection == .fullHeight || input == nil
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: bounds.height * 0.6))
path.addLine(to: CGPoint(x: bounds.width, y: bounds.height * 0.6))
baseline.path = path
}
private func updateGrid() {
updateBaseline()
grid.frame = bounds; hoverLine.frame = bounds
curves.bounds = bounds; curves.position = .zero
let path = CGMutablePath()
@@ -138,7 +167,7 @@ struct NativePanelCurve: NSViewRepresentable {
let line = CGMutablePath(), dots = CGMutablePath(), area = CGMutablePath()
for run in runs {
let points = run.map { CGPoint(x: bounds.width * domain.position($0.date),
y: bounds.height * (1 - min(1, max(0, $0.value / scale)))) }
y: input.projection.y(value: $0.value, scale: scale, series: index, height: bounds.height)) }
guard let start = points.first else { continue }
if points.count == 1 {
dots.addEllipse(in: CGRect(x: start.x - 2, y: start.y - 2, width: 4, height: 4)); continue
@@ -151,7 +180,7 @@ struct NativePanelCurve: NSViewRepresentable {
line.addPath(segment)
// Close against the matching lower boundary, never against another run or missing component.
let lowerPoints = run.reversed().map { CGPoint(x: bounds.width * domain.position($0.date),
y: bounds.height * (1 - min(1, max(0, $0.lower / scale)))) }
y: input.projection.y(value: $0.lower, scale: scale, series: index, height: bounds.height)) }
let fill = CGMutablePath(); fill.addPath(segment)
if let lowerStart = lowerPoints.first { fill.addLine(to: lowerStart) }
for (a, b) in zip(lowerPoints, lowerPoints.dropFirst()) {
@@ -178,7 +207,7 @@ struct NativePanelCurve: NSViewRepresentable {
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 scale = input.scales[input.stacked ? 0 : min(series, input.scales.count - 1)]
let y = bounds.height * (1 - min(1, max(0, value / (scale.isFinite && scale > 0 ? scale : 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)
}
}