perf(ui): 移除监控动效并精简原生图表绘制
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Immutable paths plus compositor time-domain transforms preserve the original scrolling curve.
|
||||
/// Bounded static Core Graphics paths. No animation, gradient fill or per-frame work.
|
||||
struct NativePanelCurve: NSViewRepresentable {
|
||||
let frame: PanelCurveFrame
|
||||
let colors: [Color]
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
var hoveredIndex: Int? = nil
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: PlotView, context: Context) -> CGSize? {
|
||||
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
|
||||
@@ -17,7 +16,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, animates: animates))
|
||||
hoveredIndex: hoveredIndex))
|
||||
}
|
||||
struct Input: Equatable {
|
||||
let frame: PanelCurveFrame
|
||||
@@ -25,11 +24,9 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
var hoveredIndex: Int? = nil
|
||||
var animates = false
|
||||
}
|
||||
final class PlotView: NSView {
|
||||
private(set) var input: Input?
|
||||
private(set) var drawingFrame: PanelCurveFrame?
|
||||
private(set) var pathBuildCount = 0
|
||||
let grid = CAShapeLayer()
|
||||
let curves = CALayer()
|
||||
@@ -37,8 +34,6 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
private var hoverDots: [CAShapeLayer] = []
|
||||
private var seriesLayers: [SeriesLayer] = []
|
||||
private var palette: [NSColor] = []
|
||||
private let motion = MetricMotion()
|
||||
var isAnimating: Bool { motion.isRunning }
|
||||
override var isFlipped: Bool { true }
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
|
||||
@@ -59,43 +54,15 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let old = input
|
||||
let frameChanged = old?.frame != value.frame
|
||||
input = value
|
||||
let animate = value.animates && !value.colors.isEmpty && !value.scales.isEmpty && window != nil && frameChanged && old.map {
|
||||
guard let end = value.frame.dates.last, !value.frame.boundaryDates.contains(end) else { return false }
|
||||
return PanelCurveGeometry.isAppend(from: $0.frame.dates, to: value.frame.dates, maximumGap: value.maximumGap)
|
||||
} == true
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
if frameChanged {
|
||||
motion.stop()
|
||||
drawingFrame = animate ? PanelCurveFrame.transition(from: old!.frame, to: value.frame) : value.frame
|
||||
} else if !value.animates {
|
||||
motion.stop(); drawingFrame = value.frame
|
||||
}
|
||||
curves.isHidden = value.colors.isEmpty || value.scales.isEmpty
|
||||
ensureLayers(value.frame.series.count)
|
||||
if old?.colors != value.colors { updatePalette() }
|
||||
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap || !value.animates && old?.animates == true {
|
||||
if old?.colors != value.colors || old?.frame.series.count != value.frame.series.count { updatePalette() }
|
||||
if frameChanged || old?.scales != value.scales || old?.maximumGap != value.maximumGap {
|
||||
rebuildPaths()
|
||||
}
|
||||
updateHover()
|
||||
CATransaction.commit()
|
||||
if animate, let old { startMovement(from: old.frame, to: value.frame) }
|
||||
}
|
||||
private func startMovement(from old: PanelCurveFrame, to next: PanelCurveFrame) {
|
||||
guard let a = old.dates.first, let b = old.dates.last,
|
||||
let c = next.dates.first, let d = next.dates.last else { return }
|
||||
let previous = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: a, end: b, progress: 1)
|
||||
let target = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: c, end: d, progress: 1)
|
||||
let animation = CAKeyframeAnimation(keyPath: "transform")
|
||||
animation.calculationMode = .linear
|
||||
animation.timingFunction = CAMediaTimingFunction(name: .linear)
|
||||
animation.keyTimes = (0...60).map { NSNumber(value: Double($0) / 60) }
|
||||
animation.values = (0...60).map { step in
|
||||
let p = MetricMotion.easeInOut(Double(step) / 60)
|
||||
let map = PanelCurveFrame.horizontalTransform(from: previous, to: target, progress: p, width: bounds.width)
|
||||
let transform = CGAffineTransform(a: map.scale, b: 0, c: 0, d: 1, tx: map.translation, ty: 0)
|
||||
return NSValue(caTransform3D: CATransform3DMakeAffineTransform(transform))
|
||||
}
|
||||
motion.start(on: curves, animation: animation)
|
||||
}
|
||||
private func ensureLayers(_ count: Int) {
|
||||
while seriesLayers.count < count {
|
||||
@@ -116,7 +83,6 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
for index in seriesLayers.indices {
|
||||
let color = palette[min(index, palette.count - 1)], series = seriesLayers[index]
|
||||
series.line.strokeColor = color.cgColor; series.points.fillColor = color.cgColor
|
||||
series.gradient.colors = [color.withAlphaComponent(0.20).cgColor, color.withAlphaComponent(0.01).cgColor]
|
||||
hoverDots[index].fillColor = color.cgColor
|
||||
}
|
||||
}
|
||||
@@ -137,25 +103,25 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
}
|
||||
private func rebuildPaths() {
|
||||
pathBuildCount += 1
|
||||
guard let input, let frame = drawingFrame, let first = input.frame.dates.first, let last = input.frame.dates.last,
|
||||
guard let input, let first = input.frame.dates.first, let last = input.frame.dates.last,
|
||||
!input.scales.isEmpty else {
|
||||
for series in seriesLayers { series.clear() }
|
||||
return
|
||||
}
|
||||
let domain = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: first, end: last, progress: 1)
|
||||
let domain = PanelCurveFrame.domain(begin: first, end: last)
|
||||
let frame = input.frame
|
||||
for index in frame.series.indices {
|
||||
let series = seriesLayers[index]
|
||||
let left = min(0, bounds.width * domain.position(frame.dates.first ?? first))
|
||||
series.resize(CGRect(x: left, y: 0, width: bounds.width - left, height: bounds.height))
|
||||
series.resize(bounds)
|
||||
let value = input.scales[min(index, input.scales.count - 1)]
|
||||
let scale = value.isFinite && value > 0 ? value : 1
|
||||
let runs = PanelCurveGeometry.runs(dates: frame.dates, values: frame.series[index], maximumGap: input.maximumGap,
|
||||
boundaryDates: frame.boundaryDates, intervals: frame.intervals)
|
||||
let line = CGMutablePath(), areas = CGMutablePath(), dots = CGMutablePath()
|
||||
let line = CGMutablePath(), dots = 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)))) }
|
||||
guard let start = points.first, let end = points.last else { continue }
|
||||
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
|
||||
}
|
||||
@@ -165,15 +131,9 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
segment.addCurve(to: b, control1: CGPoint(x: mid, y: a.y), control2: CGPoint(x: mid, y: b.y))
|
||||
}
|
||||
line.addPath(segment)
|
||||
segment.addLine(to: CGPoint(x: end.x, y: bounds.height))
|
||||
segment.addLine(to: CGPoint(x: start.x, y: bounds.height)); segment.closeSubpath()
|
||||
areas.addPath(segment)
|
||||
}
|
||||
var local = CGAffineTransform(translationX: -left, y: 0)
|
||||
series.line.path = line.copy(using: &local)
|
||||
series.mask.path = areas.copy(using: &local)
|
||||
series.points.path = dots.copy(using: &local)
|
||||
series.gradient.isHidden = areas.isEmpty
|
||||
series.line.path = line
|
||||
series.points.path = dots
|
||||
}
|
||||
}
|
||||
private func updateHover() {
|
||||
@@ -181,7 +141,7 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
hoverLine.path = nil
|
||||
guard let input, !input.colors.isEmpty, let index = input.hoveredIndex, input.frame.dates.indices.contains(index),
|
||||
let first = input.frame.dates.first, let last = input.frame.dates.last, last > first, !input.scales.isEmpty else { return }
|
||||
let domain = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: first, end: last, progress: 1)
|
||||
let domain = PanelCurveFrame.domain(begin: first, end: last)
|
||||
let x = bounds.width * domain.position(input.frame.dates[index])
|
||||
let path = CGMutablePath(); path.move(to: CGPoint(x: x, y: 0)); path.addLine(to: CGPoint(x: x, y: bounds.height)); hoverLine.path = path
|
||||
for series in input.frame.series.indices {
|
||||
@@ -196,14 +156,12 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
let changed = frame.size != size
|
||||
super.setFrameSize(size)
|
||||
if changed {
|
||||
motion.stop(); drawingFrame = input?.frame
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
updateGrid(); rebuildPaths(); updateHover(); CATransaction.commit()
|
||||
}
|
||||
}
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
if window == nil { motion.stop(); drawingFrame = input?.frame }
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
updatePalette(); CATransaction.commit()
|
||||
}
|
||||
@@ -213,21 +171,17 @@ struct NativePanelCurve: NSViewRepresentable {
|
||||
updatePalette(); CATransaction.commit()
|
||||
}
|
||||
private final class SeriesLayer {
|
||||
let root = CALayer(), line = CAShapeLayer(), mask = CAShapeLayer(), points = CAShapeLayer()
|
||||
let gradient = CAGradientLayer()
|
||||
let root = CALayer(), line = CAShapeLayer(), points = CAShapeLayer()
|
||||
init() {
|
||||
line.fillColor = nil; line.lineWidth = 1.5; line.lineCap = .round; line.lineJoin = .round
|
||||
mask.fillColor = NSColor.white.cgColor
|
||||
gradient.startPoint = CGPoint(x: 0.5, y: 0); gradient.endPoint = CGPoint(x: 0.5, y: 1)
|
||||
gradient.locations = [0, 1]; gradient.mask = mask
|
||||
root.addSublayer(gradient); root.addSublayer(line); root.addSublayer(points)
|
||||
root.addSublayer(line); root.addSublayer(points)
|
||||
}
|
||||
func resize(_ bounds: CGRect) {
|
||||
root.frame = bounds
|
||||
let local = CGRect(origin: .zero, size: bounds.size)
|
||||
gradient.frame = local; mask.frame = local; line.frame = local; points.frame = local
|
||||
line.frame = local; points.frame = local
|
||||
}
|
||||
func clear() { line.path = nil; mask.path = nil; points.path = nil }
|
||||
func clear() { line.path = nil; points.path = nil }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user