feat(menubar): 用分色进度柱展示电量、供电与睡眠历史
记录系统睡眠和唤醒区间,保留真实采样空档。电量柱按固定间距从右侧滑入,以蓝色、绿色和橙黄色分别标记外接电源、电池和睡眠,支持悬浮查看历史。
This commit is contained in:
@@ -70,25 +70,26 @@ import SwiftUI
|
||||
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,
|
||||
host.rootView = AnyView(PowerResourceCard(snapshot: sample, samples: history.samples, sleepIntervals: history.sleepIntervals,
|
||||
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.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)
|
||||
if state == .absent {
|
||||
let plot = descendants(host).compactMap { $0 as? NativePanelCurve.PlotView }.first!
|
||||
precondition(plot.input?.frame.series.first == [32.5, nil] && plot.input?.scales.first == 32.5)
|
||||
} else {
|
||||
let plot = descendants(host).compactMap { $0 as? BatteryHistoryPlot }.first!
|
||||
let bars = plot.layoutData.bars
|
||||
precondition(plot.bounds.height >= 24 && plot.bounds.maxY < host.bounds.height)
|
||||
if state == .present {
|
||||
precondition(bars.map(\.charge) == [0.77, 0.77, 0.77])
|
||||
precondition(bars.map(\.state) == [.external, .battery, .charging])
|
||||
for bar in bars { precondition(plot.layoutData.hit(at: bar.x) == .sample(bar.index)) }
|
||||
} else { precondition(bars.isEmpty) }
|
||||
}
|
||||
}
|
||||
await checkBatteryScrolling(in: host)
|
||||
checkBatteryGaps()
|
||||
checkBatteryTracks()
|
||||
host.rootView = AnyView(EmptyView())
|
||||
try? await Task.sleep(for: .milliseconds(100))
|
||||
precondition(descendants(host).allSatisfy { !($0 is NativePanelCurve.PlotView) })
|
||||
@@ -99,6 +100,134 @@ import SwiftUI
|
||||
NSApp.run()
|
||||
}
|
||||
|
||||
@MainActor private static func checkBatteryScrolling(in host: NSView) async {
|
||||
let viewport = BatteryHistoryViewport()
|
||||
let window = host.window!
|
||||
let originalSize = host.bounds.size
|
||||
window.contentView = viewport
|
||||
window.setContentSize(CGSize(width: 190, height: 50))
|
||||
defer {
|
||||
window.contentView = host
|
||||
window.setContentSize(originalSize)
|
||||
}
|
||||
try? await Task.sleep(for: .milliseconds(60))
|
||||
var history = MetricHistory()
|
||||
var snapshot = MetricsSnapshot()
|
||||
snapshot.battery = BatteryMetrics(charge: 0.8)
|
||||
var last = BatteryHistoryLayout(samples: [], sleeps: [], width: 190)
|
||||
for i in 0...MetricHistory.capacity {
|
||||
snapshot.date.addTimeInterval(i.isMultiple(of: 2) ? 1.7 : 2.3)
|
||||
history.append(snapshot)
|
||||
let layout = BatteryHistoryLayout(samples: history.samples, sleeps: [], width: 190)
|
||||
let newest = layout.bars.last!
|
||||
precondition(newest.range.upperBound == 190 && newest.range.lowerBound == 190 - layout.pitch)
|
||||
viewport.update(layout, hit: nil, animates: true)
|
||||
viewport.layoutSubtreeIfNeeded()
|
||||
if i == 0 {
|
||||
precondition(layout.hit(at: 95) == nil && viewport.plot.layer?.animation(forKey: BatteryHistoryViewport.slideKey) == nil)
|
||||
} else {
|
||||
let oldX = last.bars.last!.x
|
||||
precondition(layout.bars.dropLast().last!.x == oldX - layout.pitch)
|
||||
let slide = viewport.plot.layer?.animation(forKey: BatteryHistoryViewport.slideKey) as? CABasicAnimation
|
||||
precondition(slide?.fromValue as? CGFloat == layout.pitch && slide?.toValue as? CGFloat == 0)
|
||||
}
|
||||
if i == 2 {
|
||||
let deadline = ContinuousClock.now.advanced(by: .milliseconds(500))
|
||||
var movedLeft = false
|
||||
while !movedLeft && ContinuousClock.now < deadline {
|
||||
try? await Task.sleep(for: .milliseconds(10))
|
||||
let offset = viewport.plot.layer?.presentation()?.transform.m41 ?? 0
|
||||
movedLeft = offset > 0 && offset < layout.pitch
|
||||
}
|
||||
precondition(movedLeft, "The rendered layer must travel between adjacent column positions")
|
||||
}
|
||||
last = layout
|
||||
}
|
||||
precondition(last.bars.contains { $0.x < 0 }, "Retain a departing column for the slide")
|
||||
viewport.update(last, hit: .sample(history.samples.count - 1), animates: false)
|
||||
precondition(viewport.plot.layer?.animation(forKey: BatteryHistoryViewport.slideKey) == nil)
|
||||
snapshot.date.addTimeInterval(300)
|
||||
history.append(snapshot, breakBefore: true)
|
||||
viewport.update(BatteryHistoryLayout(samples: history.samples, sleeps: [], width: 190), hit: nil, animates: true)
|
||||
precondition(viewport.plot.layer?.animation(forKey: BatteryHistoryViewport.slideKey) == nil)
|
||||
}
|
||||
|
||||
@MainActor private static func checkBatteryTracks() {
|
||||
var history = MetricHistory()
|
||||
var snapshot = MetricsSnapshot()
|
||||
let flow = BatteryPowerFlow(connected: true, externalSupply: true)
|
||||
for charge: Double? in [0, 0.5, 1, nil] {
|
||||
snapshot.date.addTimeInterval(2)
|
||||
snapshot.battery = BatteryMetrics(charge: charge, flow: flow)
|
||||
history.append(snapshot)
|
||||
}
|
||||
let plot = BatteryHistoryPlot(frame: CGRect(x: 0, y: 0, width: 590, height: 51))
|
||||
let layout = BatteryHistoryLayout(samples: history.samples, sleeps: [], width: plot.bounds.width)
|
||||
plot.layoutData = layout
|
||||
let bitmap = plot.bitmapImageRepForCachingDisplay(in: plot.bounds)!
|
||||
plot.cacheDisplay(in: plot.bounds, to: bitmap)
|
||||
func alpha(_ x: CGFloat, _ y: CGFloat) -> CGFloat {
|
||||
bitmap.colorAt(x: Int(x * CGFloat(bitmap.pixelsWide) / plot.bounds.width),
|
||||
y: Int(y * CGFloat(bitmap.pixelsHigh) / plot.bounds.height))!.alphaComponent
|
||||
}
|
||||
let bars = layout.bars
|
||||
for i in [0, 3] {
|
||||
precondition((0.1...0.4).contains(alpha(bars[i].x, 16)))
|
||||
precondition((0.1...0.4).contains(alpha(bars[i].x, 34)))
|
||||
}
|
||||
let half = [alpha(bars[1].x, 16), alpha(bars[1].x, 34)].sorted()
|
||||
precondition((0.1...0.4).contains(half[0]) && half[1] > 0.95)
|
||||
precondition(alpha(bars[2].x, 16) > 0.95 && alpha(bars[2].x, 34) > 0.95)
|
||||
precondition(alpha((bars[1].x + bars[2].x) / 2, 24) == 0, "Adjacent tracks need a transparent gap")
|
||||
}
|
||||
|
||||
@MainActor private static func checkBatteryGaps() {
|
||||
let origin = Date(timeIntervalSinceReferenceDate: 100)
|
||||
var history = MetricHistory()
|
||||
var snapshot = MetricsSnapshot()
|
||||
snapshot.battery = BatteryMetrics(charge: 0.8)
|
||||
for seconds in [0, 2, 10, 30, 40, 42] {
|
||||
snapshot.date = origin.addingTimeInterval(Double(seconds))
|
||||
history.append(snapshot, breakBefore: seconds == 30 || seconds == 40)
|
||||
}
|
||||
let sleep = DateInterval(start: origin.addingTimeInterval(12), end: origin.addingTimeInterval(25))
|
||||
history.recordSleep(from: sleep.start, to: sleep.end)
|
||||
let layout = BatteryHistoryLayout(samples: history.samples, sleeps: history.sleepIntervals, width: 240)
|
||||
let band = layout.sleeps[0].range
|
||||
precondition(layout.hit(at: (band.lowerBound + band.upperBound) / 2) == .sleep(sleep))
|
||||
let plot = BatteryHistoryPlot(frame: CGRect(x: 0, y: 0, width: layout.width, height: 50))
|
||||
plot.layoutData = layout
|
||||
let bitmap = plot.bitmapImageRepForCachingDisplay(in: plot.bounds)!
|
||||
plot.cacheDisplay(in: plot.bounds, to: bitmap)
|
||||
let scale = CGFloat(bitmap.pixelsWide) / layout.width
|
||||
var columns = 0
|
||||
var insideColumn = false
|
||||
for x in Int(ceil(band.lowerBound * scale))..<Int(floor(band.upperBound * scale)) {
|
||||
let color = bitmap.colorAt(x: x, y: bitmap.pixelsHigh / 2)!.usingColorSpace(.sRGB)!
|
||||
let orange = color.alphaComponent > 0.5 && color.redComponent > color.blueComponent + 0.3
|
||||
if orange && !insideColumn { columns += 1 }
|
||||
insideColumn = orange
|
||||
}
|
||||
precondition(columns == 3, "Observed sleep must render separate orange columns at the sample pitch")
|
||||
for i in [2, 4] {
|
||||
let middle = (layout.bars[i - 1].x + layout.bars[i].x) / 2
|
||||
precondition(layout.hit(at: middle) == nil, "Unobserved gaps stay empty")
|
||||
}
|
||||
// The observed sleep covers only part of the gap between two samples.
|
||||
precondition(layout.hit(at: (band.upperBound + layout.bars[3].x) / 2) == nil)
|
||||
for bar in layout.bars { precondition(layout.hit(at: bar.x) == .sample(bar.index)) }
|
||||
snapshot.date.addTimeInterval(2)
|
||||
snapshot.battery = BatteryMetrics(charge: nil, flow: BatteryPowerFlow(connected: true, externalSupply: true))
|
||||
history.append(snapshot)
|
||||
let missingCharge = BatteryHistoryLayout(samples: history.samples, sleeps: history.sleepIntervals, width: 240)
|
||||
precondition(missingCharge.bars.last?.state == .external && missingCharge.bars.last?.charge == nil)
|
||||
for _ in 0..<MetricHistory.capacity {
|
||||
snapshot.date.addTimeInterval(2)
|
||||
history.append(snapshot)
|
||||
}
|
||||
precondition(history.sleepIntervals.isEmpty)
|
||||
}
|
||||
|
||||
@MainActor private static func fixture(probe: DetailProbe, dualMetric: Bool) -> some View {
|
||||
let dates = (0..<60).map { Date(timeIntervalSinceReferenceDate: Double($0 * 2)) }
|
||||
return ResourceCard(title: "metric.network", symbol: "network", color: .blue,
|
||||
|
||||
Reference in New Issue
Block a user