perf(ui): 移除监控动效并精简原生图表绘制
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Arc geometry is submitted once; the compositor interpolates strokeEnd continuously.
|
||||
/// Static arc geometry; sampling publishes the final value directly.
|
||||
struct NativeMetricRing: NSViewRepresentable {
|
||||
let value: Double
|
||||
let color: Color
|
||||
let lineWidth: CGFloat
|
||||
let trackOpacity: Double
|
||||
let inset: CGFloat
|
||||
@Environment(\.metricAnimationsEnabled) private var animates
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: RingView, context: Context) -> CGSize? {
|
||||
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
|
||||
@@ -17,7 +16,7 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> RingView { RingView() }
|
||||
func updateNSView(_ view: RingView, context: Context) {
|
||||
view.configure(Input(value: value.isFinite ? min(1, max(0, value)) : 0, color: color,
|
||||
lineWidth: lineWidth, trackOpacity: trackOpacity, inset: inset, animates: animates))
|
||||
lineWidth: lineWidth, trackOpacity: trackOpacity, inset: inset))
|
||||
}
|
||||
struct Input: Equatable {
|
||||
let value: Double
|
||||
@@ -25,18 +24,11 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
let lineWidth: CGFloat
|
||||
let trackOpacity: Double
|
||||
let inset: CGFloat
|
||||
var animates = false
|
||||
}
|
||||
final class RingView: NSView {
|
||||
private(set) var input: Input?
|
||||
let track = CAShapeLayer()
|
||||
let arc = CAShapeLayer()
|
||||
private let motion = MetricMotion()
|
||||
var isAnimating: Bool { motion.isRunning }
|
||||
var displayedValue: Double {
|
||||
if isAnimating, let presentation = arc.presentation() { return Double(presentation.strokeEnd) }
|
||||
return Double(arc.strokeEnd)
|
||||
}
|
||||
override var isFlipped: Bool { true }
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
init() {
|
||||
@@ -52,23 +44,12 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
func configure(_ value: Input) {
|
||||
guard value != input else { return }
|
||||
let old = input
|
||||
let start = displayedValue
|
||||
input = value
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
if old?.color != value.color || old?.trackOpacity != value.trackOpacity { updateColors() }
|
||||
if old?.inset != value.inset || old?.lineWidth != value.lineWidth { updateGeometry() }
|
||||
arc.strokeEnd = value.value
|
||||
CATransaction.commit()
|
||||
if !value.animates { motion.stop() }
|
||||
else if old?.value != value.value {
|
||||
motion.stop()
|
||||
if old != nil, window != nil, start != value.value {
|
||||
let animation = CABasicAnimation(keyPath: "strokeEnd")
|
||||
animation.fromValue = start; animation.toValue = value.value
|
||||
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
|
||||
motion.start(on: arc, animation: animation)
|
||||
}
|
||||
}
|
||||
}
|
||||
private func updateColors() {
|
||||
guard let input else { return }
|
||||
@@ -92,14 +73,12 @@ struct NativeMetricRing: NSViewRepresentable {
|
||||
let changed = frame.size != size
|
||||
super.setFrameSize(size)
|
||||
if changed {
|
||||
motion.stop()
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
updateGeometry(); CATransaction.commit()
|
||||
}
|
||||
}
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
if window == nil { motion.stop() }
|
||||
CATransaction.begin(); CATransaction.setDisableActions(true)
|
||||
updateColors(); CATransaction.commit()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user