feat(power): 展示电池实际充放电功率并及时响应电源切换
保留电量百分比和连续趋势,按历史采样展示电池、适配器和系统功率。面板可见时读取电池实时功率,插拔电源后立即刷新并复读。
This commit is contained in:
@@ -8,10 +8,36 @@ struct PowerResourceCard: View {
|
||||
let chartIntervals: [TimeInterval]
|
||||
@Environment(\.locale) private var locale
|
||||
|
||||
private func batteryPowerLabel(_ battery: BatteryMetrics) -> LocalizedStringKey {
|
||||
if battery.charging { return "battery.chargePower" }
|
||||
if !battery.externalPower { return "battery.dischargePower" }
|
||||
return "battery.power"
|
||||
private func label(_ state: BatteryPowerFlow.State) -> String {
|
||||
let key: String = switch state {
|
||||
case .charging: "power.charging"
|
||||
case .combined: "power.combined"
|
||||
case .discharging: "power.discharging"
|
||||
case .external: "battery.acPower"
|
||||
case .battery: "battery.batterySource"
|
||||
case .unknown: "power.unknown"
|
||||
}
|
||||
return AppLocalization.string(key, locale: locale)
|
||||
}
|
||||
|
||||
private func formatted(_ reading: PowerReading?, signed: Bool = false) -> String {
|
||||
let value = signed ? MetricFormat.signedWatts(reading?.watts) : MetricFormat.watts(reading?.watts)
|
||||
return reading?.source == .balance ? "≈" + value : value
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
return rows.map { AppLocalization.string($0.0, locale: locale) + " " + $0.1 }.joined(separator: "\n")
|
||||
}
|
||||
|
||||
@ViewBuilder var body: some View {
|
||||
@@ -20,6 +46,13 @@ struct PowerResourceCard: View {
|
||||
value: MetricFormat.watts(snapshot.systemPower), chart: {
|
||||
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.systemPower),
|
||||
color: Color("MetricBattery"), fixedScale: nil, firstValueFormat: .watts,
|
||||
sampleTooltip: { index in
|
||||
let sample = samples[index]
|
||||
return AnyView(VStack(alignment: .leading) {
|
||||
InlineMetric("power.system", MetricFormat.watts(sample.systemPower))
|
||||
if let input = sample.desktopInput { InlineMetric("power.input", MetricFormat.watts(input)) }
|
||||
})
|
||||
},
|
||||
firstLabel: "power.system", expectedInterval: chartInterval,
|
||||
boundaryDates: chartBoundaries, intervals: chartIntervals)
|
||||
}) {
|
||||
@@ -27,35 +60,35 @@ struct PowerResourceCard: View {
|
||||
.font(.system(size: 10)).foregroundStyle(.secondary)
|
||||
}
|
||||
} else {
|
||||
batteryChargeCard
|
||||
batteryPowerCard
|
||||
}
|
||||
}
|
||||
|
||||
private var batteryChargeCard: some View {
|
||||
private var batteryPowerCard: some View {
|
||||
ResourceCard(
|
||||
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
|
||||
value: MetricFormat.percent(snapshot.battery?.charge),
|
||||
status: snapshot.battery.map { AppLocalization.string($0.externalPower ? "battery.acPower" : "battery.batterySource", locale: locale) },
|
||||
status: snapshot.battery.map { label($0.flow.state) },
|
||||
warningText: snapshot.battery.map { Text(verbatim: details($0.flow)) },
|
||||
chartPlacement: .background,
|
||||
chart: {
|
||||
PanelTrendChart(
|
||||
dates: samples.map(\.date),
|
||||
first: samples.map(\.battery),
|
||||
second: samples.map(\.batteryPower),
|
||||
color: Color("MetricBattery"),
|
||||
secondColor: Color("SeriesUser"),
|
||||
fixedScale: 1,
|
||||
secondSeriesUsesAutonomousScale: true,
|
||||
secondValueFormat: .watts,
|
||||
firstLabel: "metric.battery",
|
||||
secondLabel: snapshot.battery.map(batteryPowerLabel) ?? "battery.power",
|
||||
expectedInterval: chartInterval,
|
||||
boundaryDates: chartBoundaries,
|
||||
intervals: chartIntervals
|
||||
color: Color("MetricBattery"), fixedScale: 1, firstValueFormat: .percent,
|
||||
sampleTooltip: { index in
|
||||
let flow = samples[index].powerFlow ?? BatteryPowerFlow()
|
||||
return AnyView(VStack(alignment: .leading, spacing: 2) {
|
||||
Text(verbatim: MetricFormat.percent(samples[index].battery) + " · " + label(flow.state)).bold()
|
||||
Text(verbatim: details(flow)).monospacedDigit()
|
||||
})
|
||||
},
|
||||
firstLabel: "metric.battery", expectedInterval: chartInterval,
|
||||
boundaryDates: chartBoundaries, intervals: chartIntervals
|
||||
)
|
||||
}
|
||||
) {
|
||||
InlineMetric("battery.powerShort", MetricFormat.watts(snapshot.battery?.power))
|
||||
InlineMetric("battery.powerShort", formatted(snapshot.battery?.flow.battery, signed: true))
|
||||
.frame(height: 16)
|
||||
if let battery = snapshot.battery {
|
||||
batteryDetails(battery)
|
||||
@@ -73,10 +106,10 @@ struct PowerResourceCard: View {
|
||||
}
|
||||
HStack(spacing: 4) {
|
||||
Text("battery.timeShort").foregroundStyle(.secondary)
|
||||
if battery.externalPower {
|
||||
Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable"))
|
||||
} else if let minutes = battery.minutesRemaining {
|
||||
if let minutes = battery.minutesRemaining {
|
||||
MetricText(value: "\(minutes) min")
|
||||
} else if battery.flow.state == .external {
|
||||
Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable"))
|
||||
} else {
|
||||
Text("battery.timeUnknown")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user