perf(ui): 移除监控动效并精简原生图表绘制

This commit is contained in:
2026-11-07 14:39:27 +08:00
parent 10c73c6026
commit 80706fa52e
54 changed files with 264 additions and 778 deletions
+46 -93
View File
@@ -43,107 +43,60 @@ import QuartzCore
ring.configure(NativeMetricRing.Input(value: 1, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
let full = redPixels(bitmap(ring), opaque: true)
precondition(quarter > 0 && full > quarter * 2, "Ring fraction must change the rendered arc")
// Actual native compositor transitions finish and remove their animation objects.
var moving = continuous
let extendedDates = dates + [Date(timeIntervalSinceReferenceDate: 8)]
moving = NativePanelCurve.Input(frame: PanelCurveFrame(dates: extendedDates, series: [[0.8,0.8,0.8,0.8,0.5]],
intervals: [2,2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5, animates: true)
plot.configure(continuous); plot.configure(moving)
precondition(plot.isAnimating)
let builds = plot.pathBuildCount
precondition(plot.grid.animationKeys() == nil, "The grid must stay stationary")
let movement = plot.curves.animation(forKey: MetricMotion.key) as! CAKeyframeAnimation
precondition(movement.keyPath == "transform" && movement.values?.count == 61)
precondition(plot.curves.animation(forKey: kCATransition) == nil, "A moving curve must not be replaced with a fade")
let middle = (movement.values![30] as! NSValue).caTransform3DValue
let oldDomain = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: dates.first!, end: dates.last!, progress: 1)
let newDomain = PanelCurveFrame.domain(previousBegin: nil, previousEnd: nil, begin: extendedDates.first!, end: extendedDates.last!, progress: 1)
let expected = PanelCurveFrame.horizontalTransform(from: oldDomain, to: newDomain, progress: 0.5, width: 280)
precondition(abs(middle.m11 - expected.scale) < 0.0001 && abs(middle.m41 - expected.translation) < 0.0001)
// Sampling updates are final immediately, and hover keeps cached curve geometry.
let nextDates = dates + [Date(timeIntervalSinceReferenceDate: 8)]
let next = NativePanelCurve.Input(frame: PanelCurveFrame(dates: nextDates,
series: [[0.8,0.8,0.8,0.8,0.5]], intervals: [2,2,2,2,2], boundaryDates: []),
colors: [.red], scales: [1], maximumGap: 5)
plot.configure(continuous)
let previousBuilds = plot.pathBuildCount
plot.configure(next)
precondition(plot.pathBuildCount == previousBuilds + 1 && plot.input?.frame == next.frame)
let sampled = bitmap(plot)
try? await Task.sleep(for: .milliseconds(180))
precondition(plot.pathBuildCount == builds, "Animation frames must not rebuild curve geometry")
var duringHover = moving; duringHover.hoveredIndex = 2
plot.configure(duringHover)
precondition(plot.pathBuildCount == builds && plot.isAnimating, "Hover must not rebuild or restart movement")
try? await Task.sleep(for: .milliseconds(320))
precondition(!plot.isAnimating && plot.drawingFrame == moving.frame)
var appended = moving
appended = NativePanelCurve.Input(frame: PanelCurveFrame(dates: extendedDates + [Date(timeIntervalSinceReferenceDate: 10)],
series: [[0.8,0.8,0.8,0.8,0.5,0.2]], intervals: [2,2,2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5, animates: true)
plot.configure(appended); precondition(plot.isAnimating)
appended.animates = false; plot.configure(appended)
precondition(!plot.isAnimating && plot.drawingFrame == appended.frame)
let beforeGap = appended
appended = NativePanelCurve.Input(frame: PanelCurveFrame(dates: appended.frame.dates + [Date(timeIntervalSinceReferenceDate: 30)],
series: [[0.8,0.8,0.8,0.8,0.5,0.2,0.7]], intervals: [2,2,2,2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5, animates: true)
plot.configure(appended); precondition(!plot.isAnimating, "Do not animate across real gaps")
plot.configure(moving)
var attached = beforeGap; attached.animates = true
plot.configure(attached); precondition(plot.isAnimating)
plot.removeFromSuperview(); precondition(!plot.isAnimating)
root.addSubview(plot)
precondition(plot.pathBuildCount == previousBuilds + 1)
precondition(pixels(sampled) == pixels(bitmap(plot)), "A sampled curve must not move between updates")
var hover = next; hover.hoveredIndex = 2
plot.configure(hover)
precondition(plot.pathBuildCount == previousBuilds + 1, "Hover must not rebuild curve geometry")
let fullDates = (0..<60).map { Date(timeIntervalSinceReferenceDate: Double($0 * 2)) }
let fullFrame = PanelCurveFrame(dates: fullDates, series: [Array(repeating: 0.8, count: 60)], intervals: Array(repeating: 2, count: 60), boundaryDates: [])
let nextFrame = PanelCurveFrame(dates: Array(fullDates.dropFirst()) + [Date(timeIntervalSinceReferenceDate: 120)],
series: [Array(repeating: 0.8, count: 59) + [0.5]], intervals: Array(repeating: 2, count: 60), boundaryDates: [])
plot.configure(.init(frame: fullFrame, colors: [.red], scales: [1], maximumGap: 5, animates: true))
plot.configure(.init(frame: nextFrame, colors: [.red], scales: [1], maximumGap: 5, animates: true))
precondition(plot.isAnimating && plot.drawingFrame?.dates.count == 61)
let initialTransform = ((plot.curves.animation(forKey: MetricMotion.key) as! CAKeyframeAnimation).values!.first as! NSValue).caTransform3DValue
CATransaction.begin(); CATransaction.setDisableActions(true)
plot.curves.transform = initialTransform
CATransaction.commit()
precondition(redPixels(bitmap(plot), from: 0, to: 0.01) > 0, "Outgoing fill must cover the left edge while scrolling")
CATransaction.begin(); CATransaction.setDisableActions(true); plot.curves.transform = CATransform3DIdentity; CATransaction.commit()
plot.configure(.init(frame: nextFrame, colors: [.red], scales: [1], maximumGap: 5))
precondition(!plot.isAnimating && plot.drawingFrame?.dates.count == 60)
plot.removeFromSuperview()
ring.configure(NativeMetricRing.Input(value: 0, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2, animates: true))
precondition(ring.isAnimating)
let arcAnimation = ring.arc.animation(forKey: MetricMotion.key) as! CABasicAnimation
precondition(arcAnimation.keyPath == "strokeEnd" && (arcAnimation.fromValue as! Double) == 1 && (arcAnimation.toValue as! Double) == 0)
precondition(ring.arc.animation(forKey: kCATransition) == nil)
try? await Task.sleep(for: .milliseconds(150))
let intermediate = ring.displayedValue
precondition(intermediate > 0 && intermediate < 1, "Ring must show intermediate arc lengths")
try? await Task.sleep(for: .milliseconds(350))
precondition(!ring.isAnimating && ring.displayedValue == 0)
ring.configure(NativeMetricRing.Input(value: 0.8, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2, animates: true))
precondition(ring.isAnimating)
ring.configure(NativeMetricRing.Input(value: 0.8, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
precondition(!ring.isAnimating && ring.displayedValue == 0.8)
ring.configure(NativeMetricRing.Input(value: 0.2, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2, animates: true))
ring.removeFromSuperview(); precondition(!ring.isAnimating && ring.displayedValue == 0.2)
// Shared cleanup must not stop a different motion or a newer replacement.
let firstMotion = MetricMotion(), secondMotion = MetricMotion()
let firstLayer = CALayer(), secondLayer = CALayer()
root.wantsLayer = true
for target in [firstLayer, secondLayer] {
target.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
target.backgroundColor = NSColor.red.cgColor
root.layer?.addSublayer(target)
}
func makeMovement() -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: "position.x")
animation.fromValue = 0; animation.toValue = 10
return animation
}
firstMotion.start(on: firstLayer, animation: makeMovement())
secondMotion.start(on: secondLayer, animation: makeMovement())
let fullFrame = PanelCurveFrame(dates: fullDates, series: [Array(repeating: 0.8, count: 60)],
intervals: Array(repeating: 2, count: 60), boundaryDates: [])
plot.configure(.init(frame: fullFrame, colors: [.red], scales: [1], maximumGap: 5))
let appended = PanelCurveFrame(dates: Array(fullDates.dropFirst()) + [Date(timeIntervalSinceReferenceDate: 120)],
series: [Array(repeating: 0.8, count: 59) + [0.5]], intervals: fullFrame.intervals, boundaryDates: [])
plot.configure(.init(frame: appended, colors: [.red], scales: [1], maximumGap: 5))
precondition(plot.input?.frame.dates.count == 60, "Static history must not retain an outgoing animation point")
ring.configure(.init(value: 0.25, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
precondition(ring.arc.strokeEnd == 0.25, "The arc must immediately show the new value")
let updated = bitmap(ring)
try? await Task.sleep(for: .milliseconds(180))
firstMotion.stop()
secondMotion.start(on: secondLayer, animation: makeMovement())
try? await Task.sleep(for: .milliseconds(210))
precondition(!firstMotion.isRunning && secondMotion.isRunning,
"An old batch deadline must not remove a restarted animation")
try? await Task.sleep(for: .milliseconds(250))
precondition(!secondMotion.isRunning, "The replacement must also finish and release its animation")
precondition(pixels(updated) == pixels(bitmap(ring)), "A sampled ring must not interpolate later")
// Adding a series with an unchanged palette must color the new layer too.
plot.configure(.init(frame: PanelCurveFrame(dates: dates, series: [[nil,nil,nil,nil], [0.5,0.5,0.5,0.5]],
intervals: [2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5))
precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10)
assertStatic(plot.layer!)
assertStatic(ring.layer!)
plot.removeFromSuperview(); ring.removeFromSuperview()
window.orderOut(nil); window.close()
print("PASS: native curve pixels, real gaps, missing series, hover, appearance, duplicate-input invalidation zero/quarter/full ring pixels and finite transitions/gap/policy/unmount cancellation")
print("PASS: static curve/ring pixels, 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()
}
@MainActor private static func assertStatic(_ layer: CALayer) {
precondition(layer.animationKeys()?.isEmpty != false)
precondition(!(layer is CAGradientLayer) && layer.mask == nil && layer.filters?.isEmpty != false)
for child in layer.sublayers ?? [] { assertStatic(child) }
}
private static func pixels(_ bitmap: NSBitmapImageRep) -> Data {
var data = Data()
for row in 0..<bitmap.pixelsHigh {
data.append(bitmap.bitmapData!.advanced(by: row * bitmap.bytesPerRow), count: bitmap.pixelsWide * bitmap.bitsPerPixel / 8)
}
return data
}
@MainActor private static func bitmap(_ view: NSView) -> NSBitmapImageRep {
view.layoutSubtreeIfNeeded(); view.displayIfNeeded()
let result = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(view.bounds.width * 2), pixelsHigh: Int(view.bounds.height * 2),