feat(panel): 调整资源卡片读数布局并保留趋势平移
逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。 存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
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 batteryPowerLabel(_ battery: BatteryMetrics) -> LocalizedStringKey {
|
||||
if battery.charging { return "battery.chargePower" }
|
||||
if !battery.externalPower { return "battery.dischargePower" }
|
||||
return "battery.power"
|
||||
}
|
||||
|
||||
@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,
|
||||
firstLabel: "power.system", expectedInterval: chartInterval,
|
||||
boundaryDates: chartBoundaries, intervals: chartIntervals)
|
||||
}) {
|
||||
Text(snapshot.systemPower == nil ? "power.sensorUnavailable" : "panel.noBattery")
|
||||
.font(.system(size: 10)).foregroundStyle(.secondary)
|
||||
}
|
||||
} else {
|
||||
batteryChargeCard
|
||||
}
|
||||
}
|
||||
|
||||
private var batteryChargeCard: 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) },
|
||||
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
|
||||
)
|
||||
}
|
||||
) {
|
||||
InlineMetric("battery.powerShort", MetricFormat.watts(snapshot.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 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 battery.externalPower {
|
||||
Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable"))
|
||||
} else if let minutes = battery.minutesRemaining {
|
||||
MetricText(value: "\(minutes) min")
|
||||
} else {
|
||||
Text("battery.timeUnknown")
|
||||
}
|
||||
}
|
||||
}
|
||||
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user