121 lines
5.7 KiB
Swift
121 lines
5.7 KiB
Swift
import SwiftUI
|
|
|
|
struct PowerResourceCard: View {
|
|
let snapshot: MetricsSnapshot
|
|
let samples: [TrendSample]
|
|
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)] = [
|
|
("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 {
|
|
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 {
|
|
ResourceCard(
|
|
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
|
|
value: MetricFormat.percent(snapshot.battery?.charge),
|
|
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),
|
|
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", formatted(snapshot.battery?.flow.battery, signed: true))
|
|
.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 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.timeShort").foregroundStyle(.secondary)
|
|
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")
|
|
}
|
|
}
|
|
}
|
|
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9)
|
|
}
|
|
}
|