140 lines
6.9 KiB
Swift
140 lines
6.9 KiB
Swift
import SwiftUI
|
|
|
|
struct PowerResourceCard: View {
|
|
let snapshot: MetricsSnapshot
|
|
let samples: [TrendSample]
|
|
let sleepIntervals: [DateInterval]
|
|
let chartInterval: TimeInterval
|
|
let chartBoundaries: Set<Date>
|
|
let chartIntervals: [TimeInterval]
|
|
@Environment(\.locale) private var locale
|
|
|
|
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)]
|
|
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")
|
|
}
|
|
|
|
@ViewBuilder var body: some View {
|
|
if snapshot.batteryState == .absent {
|
|
ResourceCard(title: "power.system", symbol: "bolt.fill", color: Color("MetricBattery"),
|
|
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)
|
|
}) {
|
|
Text(snapshot.systemPower == nil ? "power.sensorUnavailable" : "panel.noBattery")
|
|
.font(.system(size: 10)).foregroundStyle(.secondary)
|
|
}
|
|
} else {
|
|
batteryPowerCard
|
|
}
|
|
}
|
|
|
|
private var batteryPowerCard: some View {
|
|
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: {
|
|
BatteryHistoryChart(samples: samples, sleeps: sleepIntervals) { sample in
|
|
let flow = sample.powerFlow ?? BatteryPowerFlow()
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(verbatim: MetricFormat.percent(sample.battery) + " · " + label(flow.state)).bold()
|
|
Text(verbatim: details(flow)).monospacedDigit()
|
|
}
|
|
}
|
|
}
|
|
) {
|
|
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 {
|
|
Text(snapshot.batteryState == .unknown ? "metric.waiting" : "panel.batteryFailed").foregroundStyle(.secondary)
|
|
.font(.system(size: 10)).frame(maxWidth: .infinity).frame(height: 32)
|
|
}
|
|
}
|
|
}
|
|
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) ?? "--")
|
|
}
|
|
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")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9)
|
|
}
|
|
}
|