feat(menubar): 用分色进度柱展示电量、供电与睡眠历史

记录系统睡眠和唤醒区间,保留真实采样空档。电量柱按固定间距从右侧滑入,以蓝色、绿色和橙黄色分别标记外接电源、电池和睡眠,支持悬浮查看历史。
This commit is contained in:
2026-11-16 11:07:07 +08:00
parent 36518d29db
commit b9abb970d9
18 changed files with 546 additions and 45 deletions
+14 -1
View File
@@ -31,6 +31,14 @@ nonisolated struct MetricHistory: Sendable {
private(set) var samples: [TrendSample] = []
private(set) var interfaces: [String: [InterfaceTrendSample]] = [:]
private(set) var sleepIntervals: [DateInterval] = []
mutating func recordSleep(from start: Date, to end: Date) {
guard let oldest = samples.first?.date, end > start, end > oldest else { return }
sleepIntervals.append(DateInterval(start: start, end: end))
if sleepIntervals.count > Self.capacity { sleepIntervals.removeFirst() }
}
/// A slow cadence can begin and end without producing a point (for example a short hide).
/// Preserve that outgoing wait on the existing point without changing earlier segments.
mutating func recordScheduledInterval(_ interval: TimeInterval) {
@@ -44,6 +52,7 @@ nonisolated struct MetricHistory: Sendable {
}
mutating func append(_ snapshot: MetricsSnapshot, breakBefore: Bool = false, expectedInterval: TimeInterval = 2) {
if let last = samples.last, snapshot.date < last.date { sleepIntervals.removeAll() }
samples.append(TrendSample(date: snapshot.date, cpu: snapshot.cpu?.usage,
gpu: snapshot.gpuUsage, memory: snapshot.memory?.usage,
download: snapshot.downloadRate,
@@ -62,7 +71,11 @@ nonisolated struct MetricHistory: Sendable {
interfaces[item.identity, default: []].append(InterfaceTrendSample(date: snapshot.date, download: item.download, upload: item.upload, expectedInterval: expectedInterval))
if interfaces[item.identity, default: []].count > Self.capacity { interfaces[item.identity]?.removeFirst() }
}
if samples.count > Self.capacity { samples.removeFirst(samples.count - Self.capacity) }
if samples.count > Self.capacity {
samples.removeFirst(samples.count - Self.capacity)
let oldest = samples[0].date
sleepIntervals.removeAll { $0.end <= oldest }
}
}
}
+8 -1
View File
@@ -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 sleepStartedAt: Date?
@ObservationIgnored private var batteryWasAsleep = false
@ObservationIgnored private var needsHistoryBoundary = true
@ObservationIgnored private var lastPublishedAt: ContinuousClock.Instant?
@@ -144,7 +145,13 @@ final class MetricsStore {
func setSystemSleeping(_ sleeping: Bool) {
guard sleeping != isSystemSleeping else { return }
if sleeping { batteryWasAsleep = true }
if sleeping {
sleepStartedAt = Date()
batteryWasAsleep = true
} else if let start = sleepStartedAt {
history.recordSleep(from: start, to: Date())
sleepStartedAt = nil
}
isSystemSleeping = sleeping
updateDemand(reset: true)
}