feat(network): 按概念图重排网络卡片与状态摘要

This commit is contained in:
2026-11-03 17:16:41 +08:00
parent 54abb08420
commit c17d974161
6 changed files with 112 additions and 22 deletions
+71 -21
View File
@@ -255,29 +255,54 @@ struct MenuPanelView: View {
}
}
private var networkCard: some View {
ResourceCard(title: "metric.network", symbol: "network", color: Color("MetricNetwork"), value: "↓ " + MetricFormat.rate(total(\.download)), secondaryValue: "↑ " + MetricFormat.rate(total(\.upload))) {
HStack {
Picker("panel.interface", selection: $selectedInterface) {
Text("panel.totalNetwork").tag("")
ForEach(snapshot.network ?? [], id: \.identity) { item in Text(verbatim: item.name).tag(item.identity) }
}.labelsHidden().controlSize(.mini).frame(maxWidth: 200)
ResourceCard(title: "metric.network", symbol: "network", color: Color("MetricNetwork"), value: "↓ " + MetricFormat.rate(total(\.download)), secondaryValue: "↑ " + MetricFormat.rate(total(\.upload)), valueColor: Color("MetricMemory"), secondaryValueColor: Color("SeriesUpload")) {
HStack(spacing: 6) {
Picker("panel.interface", selection: $selectedInterface) {
Text("panel.totalNetwork").tag("")
ForEach(snapshot.network ?? [], id: \.identity) { item in Text(verbatim: item.name).tag(item.identity) }
}.labelsHidden().controlSize(.mini).frame(maxWidth: 150)
Divider().frame(height: 14)
if let item = addressInterface {
let address = item.addresses.first(where: { !$0.contains(":") }) ?? item.addresses.first
Text(verbatim: "\(item.name) · \(address ?? "--")")
.lineLimit(1).minimumScaleFactor(0.7).textSelection(.enabled)
Spacer(minLength: 0)
if let address {
Button {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(address, forType: .string)
} label: { Image(systemName: "doc.on.doc") }
.buttonStyle(.plain).accessibilityLabel(Text("panel.copy"))
}
} else { Text("panel.addressUnknown").foregroundStyle(.secondary) }
}
.font(.system(size: 10)).padding(5)
.background(.primary.opacity(0.04), in: RoundedRectangle(cornerRadius: 5))
PanelTrendChart(dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricMemory"), secondColor: Color("SeriesUpload"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: networkIntervals, plotHeight: 58, showsLegendValues: false).id(selectedInterface)
Divider().padding(.vertical, 4)
HStack(alignment: .top, spacing: 8) {
networkTotal("network.received", value: network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.received }), symbol: "arrow.down", color: Color("MetricMemory"))
Divider().frame(height: 32)
networkTotal("network.sent", value: network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.sent }), symbol: "arrow.up", color: Color("SeriesUpload"))
Divider().frame(height: 32)
VStack(alignment: .leading, spacing: 4) {
HStack {
Text("network.status").foregroundStyle(.secondary)
Spacer(minLength: 0)
Button { showAddresses.toggle() } label: { Image(systemName: "network") }
.buttonStyle(.plain).accessibilityLabel(Text("panel.addresses"))
}
HStack(spacing: 4) {
Circle().fill(networkStatusColor).frame(width: 5, height: 5)
Text(networkStatusLabel)
}
}.frame(maxWidth: .infinity, alignment: .leading)
}.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.65)
Spacer(minLength: 0)
}
if let item = addressInterface {
Text(verbatim: "\(item.name) · \(item.addresses.first(where: { !$0.contains(":") }) ?? item.addresses.first ?? "--")")
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.8).textSelection(.enabled)
} else { Text("panel.addressUnknown").foregroundStyle(.secondary) }
PanelTrendChart(dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricMemory"), secondColor: Color("SeriesUpload"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: networkIntervals).id(selectedInterface)
if !network.isEmpty && total(\.download) == nil { Text("panel.rateBaseline").foregroundStyle(.secondary) }
HStack { line("network.received", network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.received })); line("network.sent", network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.sent })) }
switch snapshot.networkPath {
case .available: Text("network.pathAvailable").foregroundStyle(.secondary)
case .unavailable: Text("network.pathUnavailable").foregroundStyle(.secondary)
case .requiresConnection: Text("network.pathRequiresConnection").foregroundStyle(.secondary)
case nil: Text("metric.waiting")
}
HStack {
Button("panel.addresses") { showAddresses.toggle() }.popover(isPresented: $showAddresses) {
Text(selectedInterface.isEmpty ? "panel.totalScope" : "panel.interfaceScope")
.font(.system(size: 8)).foregroundStyle(.secondary).lineLimit(1)
.popover(isPresented: $showAddresses) {
VStack(alignment: .leading, spacing: 8) {
Group {
ForEach(network, id: \.identity) { item in
@@ -303,6 +328,31 @@ struct MenuPanelView: View {
}
}
private func networkTotal(_ title: LocalizedStringKey, value: String, symbol: String, color: Color) -> some View {
VStack(alignment: .leading, spacing: 4) {
Text(title).foregroundStyle(.secondary)
HStack(spacing: 3) {
Image(systemName: symbol).foregroundStyle(color)
AnimatedMetricText(value: value).fontWeight(.medium)
}
}.frame(maxWidth: .infinity, alignment: .leading)
}
private var networkStatusLabel: LocalizedStringKey {
switch snapshot.networkPath {
case .available: "network.pathAvailable"
case .unavailable: "network.pathUnavailable"
case .requiresConnection: "network.pathRequiresConnection"
case nil: "metric.waiting"
}
}
private var networkStatusColor: Color {
switch snapshot.networkPath {
case .available: .green
case .unavailable: .red
case .requiresConnection: .orange
case nil: .secondary
}
}
private var addressInterface: NetworkInterfaceMetrics? {
let name = selectedInterface.isEmpty ? snapshot.defaultNetworkInterface : network.first?.name
return snapshot.network?.first { $0.name == name && $0.active }
+22
View File
@@ -2774,6 +2774,28 @@
}
}
},
"network.status" : {
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Network status"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "网络状态"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "網路狀態"
}
}
}
},
"network.upload" : {
"localizations" : {
"en" : {
+4 -1
View File
@@ -32,6 +32,7 @@ struct PanelTrendChart: View {
var boundaryDates: Set<Date> = []
var intervals: [TimeInterval] = []
var plotHeight: CGFloat = 58
var showsLegendValues = true
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var retainedScale: Double = 1
@State private var smallSamples = 0
@@ -80,7 +81,9 @@ struct PanelTrendChart: View {
HStack(spacing: 3) {
RoundedRectangle(cornerRadius: 1).fill(tint).frame(width: 7, height: 7)
Text(label)
Text(verbatim: formatted(values.last ?? nil, format: format)).monospacedDigit()
if showsLegendValues {
Text(verbatim: formatted(values.last ?? nil, format: format)).monospacedDigit()
}
}.font(.system(size: 9)).lineLimit(1).minimumScaleFactor(0.8)
}
+4
View File
@@ -6,6 +6,8 @@ struct ResourceCard<Content: View>: View {
let color: Color
let value: String
var secondaryValue: String? = nil
var valueColor: Color? = nil
var secondaryValueColor: Color? = nil
var status: String? = nil
var statusWarning = false
var statusHelp: Text? = nil
@@ -42,8 +44,10 @@ struct ResourceCard<Content: View>: View {
HStack(alignment: .center, spacing: 6) {
AnimatedMetricText(value: value)
.font(.system(size: 13, weight: .semibold))
.foregroundStyle(valueColor ?? color)
if let secondaryValue {
AnimatedMetricText(value: secondaryValue).font(.system(size: 13, weight: .semibold))
.foregroundStyle(secondaryValueColor ?? color)
}
}.foregroundStyle(color).lineLimit(1).minimumScaleFactor(0.65)
}