feat(power): 展示电池实际充放电功率并及时响应电源切换

保留电量百分比和连续趋势,按历史采样展示电池、适配器和系统功率。面板可见时读取电池实时功率,插拔电源后立即刷新并复读。
This commit is contained in:
2026-11-15 23:54:19 +08:00
parent 1595af6d13
commit 4b8deb2e48
31 changed files with 739 additions and 119 deletions
+5 -2
View File
@@ -4,11 +4,13 @@ import SwiftUI
/// Rate plots share a zero axis and equal positive amplitudes on either side.
nonisolated enum PanelCurveProjection: Equatable {
case fullHeight
case signed
case split(upwardSeries: Int)
func y(value: Double, scale: Double, series: Int, height: Double) -> Double {
let fraction = min(1, max(0, value / (scale.isFinite && scale > 0 ? scale : 1)))
switch self {
case .signed: return height * (0.5 - 0.5 * min(1, max(-1, value / max(0.0001, scale))))
case .fullHeight: return height * (1 - fraction)
case .split(let upward): return height * (0.6 + (series == upward ? -0.3 : 0.3) * fraction)
}
@@ -154,8 +156,9 @@ struct NativePanelCurve: NSViewRepresentable {
baseline.frame = bounds
baseline.isHidden = input?.projection == .fullHeight || input == nil
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: bounds.height * 0.6))
path.addLine(to: CGPoint(x: bounds.width, y: bounds.height * 0.6))
let zero = input?.projection.y(value: 0, scale: 1, series: 0, height: bounds.height) ?? 0
path.move(to: CGPoint(x: 0, y: zero))
path.addLine(to: CGPoint(x: bounds.width, y: zero))
baseline.path = path
}
private func updateGrid() {
+17 -23
View File
@@ -5,6 +5,7 @@ enum TrendValueFormat {
case rate
case bytes
case memory
case signedWatts
case watts
}
@@ -20,9 +21,8 @@ struct PanelTrendChart: View {
var thirdColor: Color = .purple
var fourthColor: Color = .yellow
var fixedScale: Double? = 1
var secondSeriesUsesAutonomousScale = false
var firstValueFormat: TrendValueFormat?
var secondValueFormat: TrendValueFormat?
var sampleTooltip: ((Int) -> AnyView)?
var firstLabel: LocalizedStringKey = "panel.total"
var secondLabel: LocalizedStringKey = "network.upload"
var thirdLabel: LocalizedStringKey = "cpu.system"
@@ -38,8 +38,6 @@ struct PanelTrendChart: View {
@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?
@@ -64,12 +62,10 @@ struct PanelTrendChart: View {
private var maximum: Double {
let values = stackOrder.isEmpty ? first + second + third + fourth
: StackedSeries.bands(renderFrame.series, count: dates.count).last?.upper ?? []
if projection == .signed { return max(1, values.compactMap { $0 }.filter(\.isFinite).map(abs).max() ?? 1) }
return max(1, values.compactMap { $0 }.filter(\.isFinite).max() ?? 1)
}
private var secondMaximum: Double { max(1, second.compactMap { $0 }.filter(\.isFinite).max() ?? 1) }
private var scale: Double { max(0.0001, fixedScale ?? retainedScale) }
private var secondaryScale: Double { max(0.0001, retainedSecondScale) }
private var hasSecondaryScale: Bool { secondSeriesUsesAutonomousScale && !second.isEmpty }
private var automaticFormat: TrendValueFormat {
if let firstValueFormat { return firstValueFormat }
if fixedScale == 1 { return .percent }
@@ -81,31 +77,36 @@ struct PanelTrendChart: View {
guard let hoveredDate else { return dates.count - 1 }
return dates.indices.min { abs(dates[$0].timeIntervalSince(hoveredDate)) < abs(dates[$1].timeIntervalSince(hoveredDate)) }
}
private func formatted(_ value: Double?, format: TrendValueFormat? = nil) -> String {
private func formatted(_ value: Double?) -> String {
guard let value, value.isFinite else { return "--" }
switch format ?? automaticFormat {
switch automaticFormat {
case .percent: return MetricFormat.percent(value)
case .rate: return MetricFormat.rate(value)
case .bytes: return MetricFormat.bytes(UInt64(max(0, value)))
case .memory: return MetricFormat.memory(UInt64(max(0, value)))
case .signedWatts: return MetricFormat.signedWatts(value)
case .watts: return MetricFormat.watts(value)
}
}
private func tooltipRow(_ label: LocalizedStringKey, values: [Double?], tint: Color, format: TrendValueFormat? = nil, index: Int) -> some View {
private func tooltipRow(_ label: LocalizedStringKey, values: [Double?], tint: Color, index: Int) -> some View {
HStack(spacing: 6) {
RoundedRectangle(cornerRadius: 2).fill(tint).frame(width: 8, height: 8)
Text(label)
Spacer(minLength: 4)
Text(verbatim: formatted(index < values.count ? values[index] : nil, format: format)).monospacedDigit()
Text(verbatim: formatted(index < values.count ? values[index] : nil)).monospacedDigit()
}
}
private func tooltip(index: Int) -> some View {
VStack(alignment: .leading, spacing: 3) {
Text(dates[index], format: .dateTime.hour().minute().second()).foregroundStyle(.secondary)
tooltipRow(firstLabel, values: first, tint: color, index: index)
if !second.isEmpty { tooltipRow(secondLabel, values: second, tint: secondColor, format: secondValueFormat, index: index) }
if !third.isEmpty { tooltipRow(thirdLabel, values: third, tint: thirdColor, index: index) }
if !fourth.isEmpty { tooltipRow(fourthLabel, values: fourth, tint: fourthColor, index: index) }
if let sampleTooltip {
sampleTooltip(index)
} else {
tooltipRow(firstLabel, values: first, tint: color, index: index)
if !second.isEmpty { tooltipRow(secondLabel, values: second, tint: secondColor, index: index) }
if !third.isEmpty { tooltipRow(thirdLabel, values: third, tint: thirdColor, index: index) }
if !fourth.isEmpty { tooltipRow(fourthLabel, values: fourth, tint: fourthColor, index: index) }
}
}
.font(.system(size: 9)).padding(6)
.background { TooltipBackground() }
@@ -115,7 +116,7 @@ struct PanelTrendChart: View {
var body: some View {
GeometryReader { proxy in
NativePanelCurve(frame: renderFrame, colors: renderColors,
scales: [scale, hasSecondaryScale ? secondaryScale : scale, scale, scale],
scales: [scale, scale, scale, scale],
maximumGap: max(5, expectedInterval * 2), hoveredIndex: hoveredDate == nil ? nil : index,
visibleSeriesCount: stackOrder.isEmpty ? visibleSeriesCount : stackOrder.count,
filledSeriesCount: stackOrder.isEmpty ? (filledSeriesCount ?? visibleSeriesCount) : stackOrder.count, stacked: !stackOrder.isEmpty, projection: projection,
@@ -139,7 +140,6 @@ struct PanelTrendChart: View {
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onAppear {
retainedScale = maximum
retainedSecondScale = secondMaximum
}
.onChange(of: dates) { _, _ in
let target = maximum
@@ -148,12 +148,6 @@ struct PanelTrendChart: View {
smallSamples += 1
if smallSamples >= 10 { retainedScale = target; smallSamples = 0 }
} else { smallSamples = 0 }
let secondTarget = secondMaximum
if secondTarget > retainedSecondScale { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
else if secondTarget < retainedSecondScale * 0.5 {
smallSecondSamples += 1
if smallSecondSamples >= 10 { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
} else { smallSecondSamples = 0 }
}
.accessibilityElement(children: .ignore)
.accessibilityLabel(Text(firstLabel))