fix(power): 完善电池卡片的共同供电判定、功率图标与预估续航

按适配器和电池的实际贡献判定共同供电,以双图标和双色柱展示两路功率。同步三语预估续航文案,在仅外接供电时隐藏估时行。
This commit is contained in:
2026-11-16 15:06:02 +08:00
parent ae448bce5c
commit 451d31682d
16 changed files with 262 additions and 147 deletions
+30 -10
View File
@@ -10,6 +10,7 @@ struct BatteryHistoryLayout {
let range: ClosedRange<CGFloat>
let charge: Double?
let joinsPrevious: Bool
let batteryFraction: Double?
let state: BatteryPowerFlow.State
}
struct SleepBand {
@@ -50,7 +51,8 @@ struct BatteryHistoryLayout {
let charge = sample.battery.flatMap { (0...1).contains($0) ? $0 : nil }
if charge != nil || sample.powerFlow != nil {
bars.append(Bar(index: i, x: (left + right) / 2, range: left...right,
charge: charge, joinsPrevious: continuous, state: sample.powerFlow?.state ?? .unknown))
charge: charge, joinsPrevious: continuous, batteryFraction: sample.powerFlow?.jointSupply?.batteryFraction,
state: sample.powerFlow?.state ?? .unknown))
}
guard let previous, elapsed > 0, left > -pitch else { break }
right = left
@@ -134,8 +136,7 @@ private extension BatteryPowerFlow.State {
switch self {
case .battery, .discharging: (NSColor(named: "MetricBattery") ?? .systemGreen, "battery.50percent")
case .charging: (NSColor(named: "BatteryExternal") ?? .systemBlue, "bolt.fill")
case .external: (NSColor(named: "BatteryExternal") ?? .systemBlue, "powerplug.fill")
case .combined: (NSColor(named: "BatteryExternal") ?? .systemBlue, "plus.circle.fill")
case .external, .combined: (NSColor(named: "BatteryExternal") ?? .systemBlue, "powerplug.fill")
case .unknown: (.secondaryLabelColor, "questionmark")
}
}
@@ -212,10 +213,20 @@ final class BatteryHistoryPlot: NSView {
}
let range = bar.range.lowerBound...bars[end].range.upperBound
let style = bar.state.style
drawSymbol(style.symbol, color: style.color, in: range)
if bar.state == .combined {
let left = max(range.lowerBound, 0), right = min(range.upperBound, layoutData.width)
if right - left >= 12 {
let mid = (left + right) / 2
drawSymbol(style.symbol, color: style.color, in: left...mid, minimumWidth: 6)
drawSymbol("battery.50percent", color: NSColor(named: "MetricBattery") ?? .systemGreen,
in: mid...right, minimumWidth: 6)
}
} else {
drawSymbol(style.symbol, color: style.color, in: range)
}
for item in bars[start...end] {
column(at: item.x, width: columnWidth, height: item.charge.map { $0 * baseline },
baseline: baseline, color: style.color)
baseline: baseline, color: style.color, batteryFraction: item.batteryFraction)
}
start = end + 1
}
@@ -238,7 +249,7 @@ final class BatteryHistoryPlot: NSView {
}
}
private func column(at x: CGFloat, width: CGFloat, height: CGFloat?, baseline: CGFloat, color: NSColor) {
private func column(at x: CGFloat, width: CGFloat, height: CGFloat?, baseline: CGFloat, color: NSColor, batteryFraction: Double? = nil) {
let track = CGRect(x: x - width / 2, y: 0, width: width, height: baseline)
let radius = min(width, baseline) / 2
if (height ?? 0) < baseline {
@@ -249,15 +260,24 @@ final class BatteryHistoryPlot: NSView {
let fill = CGRect(x: track.minX, y: baseline - height, width: width, height: height)
let fillRadius = min(radius, height / 2)
color.setFill()
NSBezierPath(roundedRect: fill, xRadius: fillRadius, yRadius: fillRadius).fill()
let path = NSBezierPath(roundedRect: fill, xRadius: fillRadius, yRadius: fillRadius)
path.fill()
if let batteryFraction {
NSGraphicsContext.saveGraphicsState()
path.addClip()
(NSColor(named: "MetricBattery") ?? .systemGreen).setFill()
let greenHeight = height * batteryFraction
NSBezierPath(rect: CGRect(x: fill.minX, y: baseline - greenHeight, width: width, height: greenHeight)).fill()
NSGraphicsContext.restoreGraphicsState()
}
}
private func drawSymbol(_ symbol: String, color: NSColor, in range: ClosedRange<CGFloat>) {
private func drawSymbol(_ symbol: String, color: NSColor, in range: ClosedRange<CGFloat>, minimumWidth: CGFloat = 13) {
let visibleLeft = max(0, range.lowerBound)
let visibleWidth = min(layoutData.width, range.upperBound) - visibleLeft
guard visibleWidth >= 13, let image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)?
guard visibleWidth >= minimumWidth, let image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)?
.withSymbolConfiguration(.init(paletteColors: [color])) else { return }
let scale = min(11 / image.size.width, 9 / image.size.height)
let scale = min(min(11, visibleWidth) / image.size.width, 9 / image.size.height)
let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
let icon = CGRect(x: visibleLeft + (visibleWidth - size.width) / 2,
y: bounds.maxY - 5.5 - size.height / 2, width: size.width, height: size.height)