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
+18 -4
View File
@@ -59,20 +59,34 @@ import SwiftUI
var sample = MetricsSnapshot()
sample.batteryState = state
sample.systemPower = state == .absent ? 32.5 : nil
if state == .present { sample.battery = BatteryMetrics(charge: 0.8, charging: false, externalPower: true, power: 0) }
if state == .present { sample.battery = BatteryMetrics(charge: 0.77, flow: BatteryPowerFlow(connected: true, externalSupply: true, battery: PowerReading(watts: 0))) }
var history = MetricHistory()
history.append(sample)
sample.date = sample.date.addingTimeInterval(2); sample.systemPower = nil
sample.battery?.flow = BatteryPowerFlow(connected: false, externalSupply: false, battery: PowerReading(watts: -18))
history.append(sample)
if state == .present {
sample.date = sample.date.addingTimeInterval(2)
sample.battery?.flow = BatteryPowerFlow(connected: true, externalSupply: true, battery: PowerReading(watts: 34.8))
history.append(sample)
}
host.rootView = AnyView(PowerResourceCard(snapshot: sample, samples: history.samples,
chartInterval: 2, chartBoundaries: [], chartIntervals: [2,2]))
chartInterval: 2, chartBoundaries: [], chartIntervals: history.samples.map(\.expectedInterval)))
try? await Task.sleep(for: .milliseconds(80)); host.layoutSubtreeIfNeeded()
let plot = descendants(host).compactMap { $0 as? NativePanelCurve.PlotView }.first!
let first = plot.input?.frame.series.first
switch state {
case .absent: precondition(first == [32.5, nil] && plot.input?.scales.first == 32.5)
case .present: precondition(first == [0.8, 0.8] && plot.input?.scales.first == 1 && topRect(plot, in: host) == host.bounds)
default: precondition(first == [nil, nil] && plot.input?.scales.first == 1)
case .present:
precondition(first == [0.77, 0.77, 0.77] && plot.input?.scales.first == 1)
precondition(plot.input?.projection == .fullHeight && plot.baseline.isHidden)
let area = (plot.curves.sublayers!.first!.sublayers!.first as! CAShapeLayer).path!
for fraction: CGFloat in [0.25, 0.75] {
precondition(area.contains(CGPoint(x: plot.bounds.width * fraction, y: plot.bounds.midY)),
"Battery charge fill must span both unplugging and reconnecting")
}
precondition(topRect(plot, in: host) == host.bounds)
default: precondition(first == [nil, nil] && plot.input?.projection == .fullHeight)
}
}
host.rootView = AnyView(EmptyView())