perf(ui): 移除监控动效并精简原生图表绘制
This commit is contained in:
@@ -4,54 +4,9 @@ struct MiniTrendChart: View {
|
||||
let values: [Double?]
|
||||
let color: Color
|
||||
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var displayed: [Double?] = []
|
||||
@State private var progress: CGFloat = 1
|
||||
@State private var scrolling = false
|
||||
|
||||
var body: some View {
|
||||
TrendCanvas(values: displayed, color: color, progress: progress, scrolling: scrolling)
|
||||
.clipped()
|
||||
.accessibilityHidden(true)
|
||||
.onAppear { displayed = values; scrolling = false; progress = 1 }
|
||||
.onChange(of: values) { old, new in
|
||||
var transaction = Transaction()
|
||||
transaction.disablesAnimations = true
|
||||
withTransaction(transaction) {
|
||||
scrolling = !reduceMotion && TrendScroll.isAppend(from: old, to: new)
|
||||
displayed = scrolling ? old + [new.last!] : new
|
||||
progress = scrolling ? 0 : 1
|
||||
}
|
||||
}
|
||||
.task(id: values) {
|
||||
guard scrolling, !reduceMotion else { return }
|
||||
// Commit the starting frame before requesting interpolation; no repeating timer.
|
||||
do { try await Task.sleep(for: .milliseconds(16)) } catch { return }
|
||||
guard !Task.isCancelled else { return }
|
||||
withAnimation(.easeInOut(duration: 0.35)) { progress = 1 }
|
||||
}
|
||||
.onChange(of: reduceMotion) { _, reduced in
|
||||
if reduced {
|
||||
var transaction = Transaction()
|
||||
transaction.disablesAnimations = true
|
||||
withTransaction(transaction) { displayed = values; scrolling = false; progress = 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct TrendCanvas: View, Animatable {
|
||||
let values: [Double?]
|
||||
let color: Color
|
||||
var progress: CGFloat
|
||||
let scrolling: Bool
|
||||
var animatableData: CGFloat {
|
||||
get { progress }
|
||||
set { progress = newValue }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Canvas { context, size in
|
||||
let values = Array(values.suffix(MetricHistory.capacity))
|
||||
guard values.count > 1 else { return }
|
||||
var path = Path()
|
||||
var run: [CGPoint] = []
|
||||
@@ -70,8 +25,7 @@ private struct TrendCanvas: View, Animatable {
|
||||
}
|
||||
for (index, value) in values.enumerated() {
|
||||
guard let value, value.isFinite else { appendRun(); continue }
|
||||
let x = size.width * TrendScroll.position(index: index, count: values.count,
|
||||
progress: progress, scrolling: scrolling)
|
||||
let x = size.width * CGFloat(index + MetricHistory.capacity - values.count) / CGFloat(MetricHistory.capacity - 1)
|
||||
run.append(CGPoint(x: x, y: 2 + (size.height - 4) * (1 - min(1, max(0, value)))))
|
||||
}
|
||||
appendRun()
|
||||
|
||||
Reference in New Issue
Block a user