feat(power): 展示电池实际充放电功率并及时响应电源切换
保留电量百分比和连续趋势,按历史采样展示电池、适配器和系统功率。面板可见时读取电池实时功率,插拔电源后立即刷新并复读。
This commit is contained in:
@@ -29,6 +29,7 @@ final class MetricsStore {
|
||||
@ObservationIgnored private var task: Task<Void, Never>?
|
||||
@ObservationIgnored private let sampler: SystemSampler
|
||||
@ObservationIgnored private var scheduledInterval = 0
|
||||
@ObservationIgnored private var batteryWasAsleep = false
|
||||
@ObservationIgnored private var needsHistoryBoundary = true
|
||||
@ObservationIgnored private var lastPublishedAt: ContinuousClock.Instant?
|
||||
|
||||
@@ -50,7 +51,12 @@ final class MetricsStore {
|
||||
reschedule(reset: true)
|
||||
}
|
||||
|
||||
private func reschedule(reset: Bool, sampleImmediately: Bool = true) {
|
||||
func powerSourceDidChange() {
|
||||
guard panelDemand, !isPaused, !isSystemSleeping else { return }
|
||||
reschedule(reset: false, confirmPowerChange: true)
|
||||
}
|
||||
|
||||
private func reschedule(reset: Bool, sampleImmediately: Bool = true, confirmPowerChange: Bool = false) {
|
||||
generation += 1
|
||||
task?.cancel()
|
||||
task = nil
|
||||
@@ -71,7 +77,12 @@ final class MetricsStore {
|
||||
if initialDelay > .zero {
|
||||
do { try await Task.sleep(for: initialDelay, tolerance: self?.schedulingTolerance) } catch { return }
|
||||
}
|
||||
if self?.batteryWasAsleep == true {
|
||||
self?.batteryWasAsleep = false
|
||||
await sampler.invalidateBatteryReadings()
|
||||
}
|
||||
if reset { await sampler.reset() }
|
||||
var needsConfirmation = confirmPowerChange
|
||||
while !Task.isCancelled {
|
||||
let sampledAt = ContinuousClock.now
|
||||
let visibleGeneration = self?.visibilityGeneration
|
||||
@@ -105,7 +116,10 @@ final class MetricsStore {
|
||||
self?.lastPublishedAt = .now
|
||||
self?.needsHistoryBoundary = false
|
||||
SamplingDiagnostics.record(next)
|
||||
let interval = self?.effectiveMilliseconds ?? 5_000
|
||||
let cadence = self?.effectiveMilliseconds ?? 5_000
|
||||
// A source notification may arrive just before the gauge updates.
|
||||
let interval = needsConfirmation ? min(cadence, 1000) : cadence
|
||||
needsConfirmation = false
|
||||
// Start-to-start cadence; missed deadlines do not trigger catch-up bursts.
|
||||
let deadline = sampledAt.advanced(by: .milliseconds(interval))
|
||||
do { try await Task.sleep(until: max(deadline, .now), tolerance: self?.schedulingTolerance, clock: .continuous) }
|
||||
@@ -130,6 +144,7 @@ final class MetricsStore {
|
||||
|
||||
func setSystemSleeping(_ sleeping: Bool) {
|
||||
guard sleeping != isSystemSleeping else { return }
|
||||
if sleeping { batteryWasAsleep = true }
|
||||
isSystemSleeping = sleeping
|
||||
updateDemand(reset: true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user