perf(efficiency): 落实首批采样减负与发行包瘦身
This commit is contained in:
@@ -8,7 +8,7 @@ enum TrendValueFormat {
|
||||
case watts
|
||||
}
|
||||
|
||||
/// Bounded, timestamp-based curves. Gaps remain gaps; animation only runs on append.
|
||||
/// Bounded, timestamp-based static curves. Gaps remain gaps; no per-sample animation task.
|
||||
struct PanelTrendChart: View {
|
||||
let dates: [Date]
|
||||
let first: [Double?]
|
||||
@@ -33,18 +33,12 @@ struct PanelTrendChart: View {
|
||||
var intervals: [TimeInterval] = []
|
||||
var plotHeight: CGFloat = PanelStyle.plotHeight
|
||||
var showsLegendValues = true
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var retainedScale: Double = 1
|
||||
@State private var smallSamples = 0
|
||||
@State private var retainedSecondScale: Double = 1
|
||||
@State private var smallSecondSamples = 0
|
||||
@State private var hoveredDate: Date?
|
||||
@State private var hoverPoint: CGPoint?
|
||||
@State private var previousBegin: Date?
|
||||
@State private var previousEnd: Date?
|
||||
@State private var progress: CGFloat = 1
|
||||
@State private var lastFrame: PanelCurveFrame?
|
||||
@State private var displayedFrame: PanelCurveFrame?
|
||||
|
||||
private var inputFrame: PanelCurveFrame {
|
||||
PanelCurveFrame(dates: dates, series: [first, second, third, fourth],
|
||||
@@ -129,11 +123,11 @@ struct PanelTrendChart: View {
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: plotHeight)
|
||||
GeometryReader { proxy in
|
||||
ZStack(alignment: .topLeading) {
|
||||
PanelCurveCanvas(frame: displayedFrame ?? inputFrame, colors: [color, secondColor, thirdColor, fourthColor],
|
||||
PanelCurveCanvas(frame: inputFrame, colors: [color, secondColor, thirdColor, fourthColor],
|
||||
scales: [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale, scale],
|
||||
maximumGap: max(5, expectedInterval * 2),
|
||||
targetBegin: (lastFrame ?? inputFrame).dates.first, targetEnd: (lastFrame ?? inputFrame).dates.last,
|
||||
previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
|
||||
targetBegin: dates.first, targetEnd: dates.last,
|
||||
previousBegin: nil, previousEnd: nil, progress: 1)
|
||||
if hoveredDate != nil, let index, let begin = dates.first, let end = dates.last, end > begin {
|
||||
let x = proxy.size.width * dates[index].timeIntervalSince(begin) / end.timeIntervalSince(begin)
|
||||
Path { path in
|
||||
@@ -187,8 +181,6 @@ struct PanelTrendChart: View {
|
||||
.onAppear {
|
||||
retainedScale = maximum
|
||||
retainedSecondScale = secondMaximum
|
||||
lastFrame = inputFrame
|
||||
displayedFrame = inputFrame
|
||||
}
|
||||
.onChange(of: dates) { _, _ in
|
||||
let target = maximum
|
||||
@@ -203,28 +195,7 @@ struct PanelTrendChart: View {
|
||||
smallSecondSamples += 1
|
||||
if smallSecondSamples >= 10 { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
|
||||
} else { smallSecondSamples = 0 }
|
||||
let next = inputFrame
|
||||
let old = lastFrame
|
||||
let gap = PanelCurveGeometry.allowedGap(at: next.dates.count - 1, intervals: next.intervals, fallback: max(5, expectedInterval * 2))
|
||||
let append = old.map { PanelCurveGeometry.isAppend(from: $0.dates, to: next.dates, maximumGap: gap) } == true
|
||||
&& !(next.dates.last.map { boundaryDates.contains($0) } ?? false)
|
||||
let animated = append && !reduceMotion
|
||||
var transaction = Transaction(); transaction.disablesAnimations = true
|
||||
withTransaction(transaction) {
|
||||
lastFrame = next
|
||||
displayedFrame = next
|
||||
if animated, let old { displayedFrame = PanelCurveFrame.transition(from: old, to: next) }
|
||||
previousBegin = animated ? old?.dates.first : nil
|
||||
previousEnd = animated ? old?.dates.last : nil
|
||||
progress = animated ? 0 : 1
|
||||
}
|
||||
}
|
||||
.task(id: lastFrame?.dates.last) {
|
||||
guard progress == 0, !reduceMotion else { return }
|
||||
do { try await Task.sleep(for: .milliseconds(16)) } catch { return }
|
||||
withAnimation(.easeInOut(duration: 0.35)) { progress = 1 }
|
||||
}
|
||||
.onChange(of: reduceMotion) { _, reduced in if reduced { progress = 1; previousBegin = nil; previousEnd = nil; lastFrame = inputFrame; displayedFrame = inputFrame } }
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import SwiftUI
|
||||
|
||||
/// The same system numeric roll/blur transition as the original metric rows.
|
||||
/// Static numbers avoid per-sample glyph rasterization and transition caches.
|
||||
struct AnimatedMetricText: View {
|
||||
let value: String
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
|
||||
var body: some View {
|
||||
Text(verbatim: value)
|
||||
.monospacedDigit()
|
||||
.contentTransition(.numericText())
|
||||
.animation(reduceMotion ? nil : .spring(duration: 0.35, bounce: 0), value: value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,57 +1,19 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Approximate visual speed tiers; rotation does not attempt to reproduce physical RPM.
|
||||
nonisolated struct FanRotation {
|
||||
private(set) var phase: Double = 0
|
||||
private(set) var speed: Double = 0
|
||||
private var reference: TimeInterval = 0
|
||||
|
||||
static func speed(rpm: Double?, reducedMotion: Bool) -> Double {
|
||||
guard !reducedMotion, let rpm, rpm.isFinite, rpm > 0 else { return 0 }
|
||||
switch rpm {
|
||||
case ..<2000: return 90
|
||||
case ..<4000: return 180
|
||||
case ..<6000: return 300
|
||||
default: return 450
|
||||
}
|
||||
}
|
||||
|
||||
func angle(at time: TimeInterval) -> Double {
|
||||
(phase + max(0, time - reference) * speed).truncatingRemainder(dividingBy: 360)
|
||||
}
|
||||
|
||||
mutating func update(rpm: Double?, reducedMotion: Bool, at time: TimeInterval) {
|
||||
phase = angle(at: time)
|
||||
reference = time
|
||||
speed = Self.speed(rpm: rpm, reducedMotion: reducedMotion)
|
||||
}
|
||||
}
|
||||
|
||||
struct FanSpeedIndicator: View {
|
||||
let title: Text
|
||||
let rpm: Double?
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var rotation = FanRotation()
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 5) {
|
||||
TimelineView(.animation(minimumInterval: 1.0 / 30, paused: rotation.speed == 0)) { context in
|
||||
Image(systemName: "fanblades.fill")
|
||||
.rotationEffect(.degrees(rotation.angle(at: context.date.timeIntervalSinceReferenceDate)))
|
||||
.foregroundStyle(.secondary)
|
||||
}.frame(width: 14, height: 14).accessibilityHidden(true)
|
||||
Image(systemName: "fanblades.fill")
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(width: 14, height: 14).accessibilityHidden(true)
|
||||
title.foregroundStyle(.secondary)
|
||||
Spacer(minLength: 2)
|
||||
AnimatedMetricText(value: rpm.map { "\(Int($0.rounded())) RPM" } ?? "-- RPM")
|
||||
.monospacedDigit().fontWeight(.medium)
|
||||
}
|
||||
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.7)
|
||||
.onAppear { update() }
|
||||
.onChange(of: rpm) { _, _ in update() }
|
||||
.onChange(of: reduceMotion) { _, _ in update() }
|
||||
}
|
||||
|
||||
private func update() {
|
||||
rotation.update(rpm: rpm, reducedMotion: reduceMotion, at: Date.timeIntervalSinceReferenceDate)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ struct MetricRingGauge: View {
|
||||
let title: LocalizedStringKey
|
||||
let value: Double?
|
||||
let color: Color
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
|
||||
private var fraction: Double { value.flatMap { $0.isFinite ? min(1, max(0, $0)) : nil } ?? 0 }
|
||||
|
||||
@@ -16,7 +15,6 @@ struct MetricRingGauge: View {
|
||||
Circle().trim(from: 0, to: fraction)
|
||||
.stroke(color, style: StrokeStyle(lineWidth: 3, lineCap: .round))
|
||||
.rotationEffect(.degrees(-90))
|
||||
.animation(reduceMotion ? nil : .easeInOut(duration: 0.35), value: fraction)
|
||||
AnimatedMetricText(value: MetricFormat.percent(value)).font(.system(size: 8, weight: .semibold))
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: 18, height: 12)
|
||||
}.padding(2).frame(width: 28, height: 28).accessibilityHidden(true)
|
||||
|
||||
@@ -54,7 +54,6 @@ struct CapacityBar: View {
|
||||
let values: [Double]
|
||||
let colors: [Color]
|
||||
let total: Double
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { bounds in
|
||||
@@ -65,7 +64,6 @@ struct CapacityBar: View {
|
||||
}
|
||||
}
|
||||
.clipShape(Capsule())
|
||||
.animation(reduceMotion ? nil : .easeInOut(duration: 0.35), value: values)
|
||||
}
|
||||
.frame(height: 8)
|
||||
.accessibilityHidden(true)
|
||||
|
||||
Reference in New Issue
Block a user