接入有界趋势、平滑滚动、菜单式外侧详情、底部操作与三语文案,装配独立外观设置和 Dock 生命周期。 合并跟随系统外观刷新、自选颜色入口与色块绘制修复,保留用户确认的配色联动、重启保存及滚动验收。 多场景视觉与长期资源验证边界仍记录于 docs。
83 lines
3.5 KiB
Swift
83 lines
3.5 KiB
Swift
import SwiftUI
|
|
|
|
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
|
|
guard values.count > 1 else { return }
|
|
var path = Path()
|
|
var run: [CGPoint] = []
|
|
func appendRun() {
|
|
guard let first = run.first, let last = run.last else { return }
|
|
path.move(to: first)
|
|
if run.count == 1 { path.addLine(to: CGPoint(x: first.x + 0.1, y: first.y)) }
|
|
for index in 1..<run.count {
|
|
let previous = run[index - 1]
|
|
let current = run[index]
|
|
let midpoint = CGPoint(x: (previous.x + current.x) / 2, y: (previous.y + current.y) / 2)
|
|
path.addQuadCurve(to: midpoint, control: previous)
|
|
}
|
|
path.addQuadCurve(to: last, control: last)
|
|
run.removeAll(keepingCapacity: true)
|
|
}
|
|
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)
|
|
run.append(CGPoint(x: x, y: 2 + (size.height - 4) * (1 - min(1, max(0, value)))))
|
|
}
|
|
appendRun()
|
|
context.stroke(path, with: .color(color.opacity(0.85)), style: StrokeStyle(lineWidth: 1.8, lineCap: .round, lineJoin: .round))
|
|
}
|
|
.accessibilityHidden(true)
|
|
}
|
|
}
|