fix(power): 完善电池卡片的共同供电判定、功率图标与预估续航
按适配器和电池的实际贡献判定共同供电,以双图标和双色柱展示两路功率。同步三语预估续航文案,在仅外接供电时隐藏估时行。
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -27,17 +27,18 @@ struct PowerResourceCard: View {
|
||||
}
|
||||
|
||||
private func details(_ flow: BatteryPowerFlow) -> String {
|
||||
var rows: [(String, String)] = [
|
||||
("battery.power", formatted(flow.battery, signed: true)),
|
||||
("power.load", formatted(flow.load))
|
||||
]
|
||||
if flow.connected != false {
|
||||
rows.append(("power.input", formatted(flow.input)))
|
||||
rows.append(("power.capacity", MetricFormat.watts(flow.adapterCapacity)))
|
||||
}
|
||||
if flow.battery?.source == .average {
|
||||
rows[0].0 = "power.averageBattery"
|
||||
var rows: [(String, String)]
|
||||
if let joint = flow.jointSupply {
|
||||
let load = flow.load ?? PowerReading(watts: joint.adapter.watts + joint.discharge.watts, source: .balance)
|
||||
rows = [("power.adapterContribution", formatted(joint.adapter)),
|
||||
("power.batteryContribution", formatted(joint.discharge)),
|
||||
("power.load", formatted(load))]
|
||||
} else {
|
||||
let batteryLabel = flow.battery?.source == .average ? "power.averageBattery" : "battery.power"
|
||||
rows = [(batteryLabel, formatted(flow.battery, signed: true)), ("power.load", formatted(flow.load))]
|
||||
if flow.connected != false { rows.append(("power.input", formatted(flow.input))) }
|
||||
}
|
||||
if flow.connected != false { rows.append(("power.capacity", MetricFormat.watts(flow.adapterCapacity))) }
|
||||
return rows.map { AppLocalization.string($0.0, locale: locale) + " " + $0.1 }.joined(separator: "\n")
|
||||
}
|
||||
|
||||
@@ -66,9 +67,12 @@ struct PowerResourceCard: View {
|
||||
}
|
||||
|
||||
private var batteryPowerCard: some View {
|
||||
ResourceCard(
|
||||
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
|
||||
let joint = snapshot.battery?.flow.jointSupply
|
||||
return ResourceCard(
|
||||
title: "metric.battery", symbol: joint == nil ? "battery.100percent" : "powerplug.fill",
|
||||
color: Color(joint == nil ? "MetricBattery" : "BatteryExternal"),
|
||||
value: MetricFormat.percent(snapshot.battery?.charge),
|
||||
companionIcon: joint == nil ? nil : ("battery.50percent", Color("MetricBattery")),
|
||||
status: snapshot.battery.map { label($0.flow.state) },
|
||||
warningText: snapshot.battery.map { Text(verbatim: details($0.flow)) },
|
||||
chart: {
|
||||
@@ -81,8 +85,17 @@ struct PowerResourceCard: View {
|
||||
}
|
||||
}
|
||||
) {
|
||||
InlineMetric("battery.powerShort", formatted(snapshot.battery?.flow.battery, signed: true))
|
||||
if let joint {
|
||||
HStack(spacing: 8) {
|
||||
powerLabel(joint.adapter, symbol: "powerplug.fill", color: "BatteryExternal", title: "power.adapterContribution")
|
||||
powerLabel(joint.discharge, symbol: "battery.50percent", color: "MetricBattery", title: "power.batteryContribution")
|
||||
}
|
||||
.frame(height: 16)
|
||||
} else {
|
||||
powerLabel(snapshot.battery?.flow.battery, signed: true, symbol: "battery.50percent",
|
||||
color: "MetricBattery", title: "battery.power")
|
||||
.frame(height: 16)
|
||||
}
|
||||
if let battery = snapshot.battery {
|
||||
batteryDetails(battery)
|
||||
} else {
|
||||
@@ -91,20 +104,33 @@ struct PowerResourceCard: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
private func powerLabel(_ reading: PowerReading?, signed: Bool = false, symbol: String,
|
||||
color: String, title: LocalizedStringKey) -> some View {
|
||||
let value = formatted(reading, signed: signed)
|
||||
return Label {
|
||||
Text(verbatim: value).monospacedDigit()
|
||||
} icon: {
|
||||
Image(systemName: symbol).foregroundStyle(Color(color))
|
||||
}
|
||||
.accessibilityLabel(Text(title)).accessibilityValue(Text(verbatim: value))
|
||||
.help(Text(title))
|
||||
}
|
||||
|
||||
private func batteryDetails(_ battery: BatteryMetrics) -> some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack(spacing: 8) {
|
||||
InlineMetric("battery.healthShort", MetricFormat.percent(battery.health))
|
||||
InlineMetric("battery.cyclesShort", battery.cycles.map(String.init) ?? "--")
|
||||
}
|
||||
HStack(spacing: 4) {
|
||||
Text(battery.flow.charging == true ? "battery.timeToFull" : "battery.timeShort").foregroundStyle(.secondary)
|
||||
if let minutes = battery.minutesRemaining {
|
||||
Text("duration.minutes \(minutes)").monospacedDigit()
|
||||
} else if battery.flow.state == .external && battery.flow.charging != true {
|
||||
Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable"))
|
||||
} else {
|
||||
Text("battery.timeUnknown")
|
||||
if battery.flow.state != .external {
|
||||
HStack(spacing: 4) {
|
||||
Text(battery.flow.state == .charging ? "battery.timeToFull" : "battery.timeEstimate")
|
||||
.foregroundStyle(.secondary)
|
||||
if let minutes = battery.minutesRemaining {
|
||||
Text("duration.minutes \(minutes)").monospacedDigit()
|
||||
} else {
|
||||
Text("battery.timeUnknown")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user