perf(ui): 恢复数字滚动并优化曲线与圆环连续动效

This commit is contained in:
2026-11-04 14:04:05 +08:00
parent f18f6652ff
commit 44f0415ab7
12 changed files with 394 additions and 144 deletions
+47 -4
View File
@@ -1,5 +1,6 @@
import AppKit
import SwiftUI
import QuartzCore
@main struct NativeMetricDrawingChecks {
@MainActor static func main() {
@@ -49,7 +50,22 @@ import SwiftUI
intervals: [2,2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5, animates: true)
plot.configure(continuous); plot.configure(moving)
precondition(plot.isAnimating)
try? await Task.sleep(for: .milliseconds(400))
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)
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)],
@@ -65,9 +81,32 @@ import SwiftUI
var attached = beforeGap; attached.animates = true
plot.configure(attached); precondition(plot.isAnimating)
plot.removeFromSuperview(); precondition(!plot.isAnimating)
root.addSubview(plot)
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)
try? await Task.sleep(for: .milliseconds(400))
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)
@@ -82,8 +121,12 @@ import SwiftUI
NSApp.run()
}
@MainActor private static func bitmap(_ view: NSView) -> NSBitmapImageRep {
let result = view.bitmapImageRepForCachingDisplay(in: view.bounds)!
view.cacheDisplay(in: view.bounds, to: result)
view.layoutSubtreeIfNeeded(); view.displayIfNeeded()
let result = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(view.bounds.width * 2), pixelsHigh: Int(view.bounds.height * 2),
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!
let context = NSGraphicsContext(bitmapImageRep: result)!.cgContext
context.scaleBy(x: 2, y: 2)
view.layer!.render(in: context)
return result
}
private static func redPixels(_ image: NSBitmapImageRep, from: Double = 0, to: Double = 1, opaque: Bool = false) -> Int {
+14
View File
@@ -32,6 +32,20 @@ import Foundation
if progress == 0 { precondition(abs(left) < 0.000001 && abs(domain.position(old.dates.last!) - 1) < 0.000001) }
if progress == 1 { precondition(abs(right) < 0.000001 && abs(domain.position(next.dates.last!) - 1) < 0.000001) }
}
// The compositor mapping must equal the original interpolated time-domain formula.
for previous in [PanelCurveFrame.Domain(begin: 0, end: 4), .init(begin: 0, end: 118), .init(begin: 2, end: 119.3)] {
for target in [PanelCurveFrame.Domain(begin: 0, end: 6), .init(begin: 2, end: 120), .init(begin: 4.4, end: 121.8)] {
for p in [0.0, 0.25, 0.5, 0.75, 1] {
let map = PanelCurveFrame.horizontalTransform(from: previous, to: target, progress: p, width: 350)
let interpolated = PanelCurveFrame.Domain(begin: previous.begin * (1-p) + target.begin * p,
end: previous.end * (1-p) + target.end * p)
for t in [0.0, 2, 60, 120] {
let date = Date(timeIntervalSinceReferenceDate: t)
precondition(abs(target.position(date) * 350 * map.scale + map.translation - interpolated.position(date) * 350) < 0.00001)
}
}
}
}
print("PASS: timestamp append/full history, pause/reset, missing/nonfinite data, short series, cadence continuity, pause boundaries and 61-point left-edge animation geometry")
}
}