feat(monitors): 实现 C 阶段网络存储电池监控与三语详情

This commit is contained in:
2026-10-30 16:02:46 +08:00
parent 1cb67b86e8
commit 069e2e100f
22 changed files with 1237 additions and 13 deletions
+93
View File
@@ -22,6 +22,83 @@ struct MenuPanelView: View {
memoryDetail
}
}
VStack(spacing: 2) {
MetricRow(title: "metric.network", symbol: "network", value: networkValue,
usage: metrics.snapshot.downloadRate.map { _ in 0 }, history: networkHistory, accent: preferences.tintColor) {
ScrollView {
VStack(spacing: 6) {
switch metrics.snapshot.networkPath {
case .available: Text("network.pathAvailable")
case .unavailable: Text("network.pathUnavailable")
case .requiresConnection: Text("network.pathRequiresConnection")
case nil: Text("metric.waiting")
}
if let interfaces = metrics.snapshot.network {
if interfaces.isEmpty { Text("network.noLink") }
ForEach(interfaces, id: \.name) { item in
VStack(spacing: 4) {
Text(verbatim: item.name).fontWeight(.semibold)
detailLine("network.download", rate(item.download))
detailLine("network.upload", rate(item.upload))
detailLine("network.received", MetricFormat.bytes(item.received))
detailLine("network.sent", MetricFormat.bytes(item.sent))
}
}
} else { Text("metric.waiting") }
}
}.frame(maxHeight: 280)
}
MetricRow(title: "metric.disk", symbol: "internaldrive", value: MetricFormat.percent(metrics.snapshot.disk?.usage),
usage: metrics.snapshot.disk?.usage, history: metrics.history.samples.map(\.disk), accent: preferences.tintColor) {
ScrollView {
VStack(spacing: 6) {
Text("disk.homeVolume").foregroundStyle(.secondary)
detailLine("disk.total", MetricFormat.bytes(metrics.snapshot.disk?.total))
detailLine("memory.available", MetricFormat.bytes(metrics.snapshot.disk?.available))
Divider()
Text("disk.physicalIO").foregroundStyle(.secondary)
detailLine("disk.read", rate(metrics.snapshot.diskIO?.read))
detailLine("disk.write", rate(metrics.snapshot.diskIO?.written))
Divider()
Text("disk.volumes").foregroundStyle(.secondary)
if let volumes = metrics.snapshot.volumes {
ForEach(volumes, id: \.path) { volume in
VStack(spacing: 4) {
Text(verbatim: volume.name).fontWeight(.semibold)
detailLine("disk.total", MetricFormat.bytes(volume.capacity.total))
detailLine("memory.available", MetricFormat.bytes(volume.capacity.available))
}
}
} else { Text("metric.waiting") }
}
}.frame(maxHeight: 280)
}
if let battery = metrics.snapshot.battery {
MetricRow(title: "metric.battery", symbol: battery.charging ? "battery.100percent.bolt" : "battery.100percent",
value: MetricFormat.percent(battery.charge), usage: battery.charge.map { _ in 0 }, history: metrics.history.samples.map(\.battery), accent: preferences.tintColor) {
VStack(spacing: 6) {
Text(battery.charging ? "battery.charging" : battery.externalPower ? "battery.external" : "battery.discharging")
if battery.externalPower && !battery.charging {
HStack {
Text("battery.timeEstimate").foregroundStyle(.secondary)
Spacer()
Text("battery.timeNotApplicable")
}
} else if let minutes = battery.minutesRemaining {
detailLine(battery.charging ? "battery.timeFull" : "battery.timeEmpty", "\(minutes) min")
} else {
HStack {
Text(battery.charging ? "battery.timeFull" : "battery.timeEmpty").foregroundStyle(.secondary)
Spacer()
Text("battery.timeUnknown")
}
}
detailLine("battery.cycles", battery.cycles.map(String.init) ?? "--")
detailLine("battery.health", MetricFormat.percent(battery.health))
}
}
}
}
Divider().padding(.horizontal, 10)
HStack {
Text("system.load")
@@ -39,6 +116,22 @@ struct MenuPanelView: View {
.modifier(AppearanceStyle(preferences: preferences))
}
// Shared scale for the whole visible history; never label throughput as utilization.
private var networkHistory: [Double?] {
let values = metrics.history.samples.map(\.download)
let scale = max(1_000, values.compactMap { $0 }.max() ?? 1_000)
return values.map { $0.map { $0 / scale } }
}
private var networkValue: String {
guard let total = metrics.snapshot.downloadRate else { return "--" }
return total >= 1_000_000 ? String(format: "%.1fM↓", total / 1_000_000) : String(format: "%.0fK↓", total / 1000)
}
private func rate(_ bytes: Double?) -> String {
MetricFormat.rate(bytes)
}
@ViewBuilder private var cpuDetail: some View {
if let cpu = metrics.snapshot.cpu {
VStack(alignment: .leading, spacing: 8) {