feat(network): 调整地址表格并增加公网 IP 查询

This commit is contained in:
2026-11-03 17:23:30 +08:00
parent c17d974161
commit e60fb0825b
8 changed files with 227 additions and 54 deletions
+39 -53
View File
@@ -11,7 +11,7 @@ struct MenuPanelView: View {
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var panelSize = PanelLayout.contentSize(visible: NSScreen.main?.visibleFrame ?? CGRect(x: 0, y: 0, width: 1000, height: 760))
@State private var selectedInterface = ""
@State private var showAddresses = false
@State private var publicIPAddress: String?
@State private var showVolumes = false
private var snapshot: MetricsSnapshot { metrics.snapshot }
@@ -256,28 +256,6 @@ 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)), 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) {
@@ -288,9 +266,12 @@ struct MenuPanelView: View {
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"))
Picker("panel.interface", selection: $selectedInterface) {
Text("network.all").tag("")
ForEach(snapshot.network ?? [], id: \.identity) { item in
Text(verbatim: item.name).tag(item.identity)
}
}.labelsHidden().controlSize(.mini).fixedSize()
}
HStack(spacing: 4) {
Circle().fill(networkStatusColor).frame(width: 5, height: 5)
@@ -298,35 +279,40 @@ struct MenuPanelView: View {
}
}.frame(maxWidth: .infinity, alignment: .leading)
}.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.65)
Spacer(minLength: 0)
HStack {
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
VStack(alignment: .leading, spacing: 3) {
Text(verbatim: item.name).fontWeight(.medium)
if item.addresses.isEmpty { Text("panel.addressUnknown") }
ForEach(item.addresses, id: \.self) { address in
HStack(alignment: .top) {
Text(verbatim: address).textSelection(.enabled).fixedSize(horizontal: false, vertical: true)
Spacer(minLength: 0)
Button { NSPasteboard.general.clearContents(); NSPasteboard.general.setString(address, forType: .string) } label: { Image(systemName: "doc.on.doc") }.accessibilityLabel(Text("panel.copy"))
}
}
}
Divider().padding(.vertical, 4)
VStack(alignment: .leading, spacing: 5) {
Text("network.addresses").fontWeight(.medium).foregroundStyle(.secondary)
HStack(spacing: 8) {
networkAddress("network.public", address: publicIPAddress)
Divider().frame(height: 15)
networkAddress("network.local", address: addressInterface?.addresses.first(where: { !$0.contains(":") }) ?? addressInterface?.addresses.first)
}
}
Text(selectedInterface.isEmpty ? "panel.totalScope" : "panel.interfaceScope").font(.system(size: 9)).foregroundStyle(.secondary)
}.padding(12).frame(width: 350)
}
Spacer()
Image(systemName: "info.circle").help(Text(selectedInterface.isEmpty ? "panel.totalScope" : "panel.interfaceScope"))
}
}.font(.system(size: 10))
}
.task(id: publicIPRoute) {
publicIPAddress = nil
guard snapshot.networkPath == .available else { return }
let address = await PublicIPAddressService.shared.address(route: publicIPRoute)
guard !Task.isCancelled else { return }
publicIPAddress = address
}
}
private var publicIPRoute: String {
"\(snapshot.defaultNetworkInterface ?? ""):\(String(describing: snapshot.networkPath)):\(snapshot.network?.filter { $0.active }.map { $0.addresses.joined(separator: ",") }.joined(separator: ";") ?? "")"
}
private func networkAddress(_ title: LocalizedStringKey, address: String?) -> some View {
HStack(spacing: 4) {
Text(title).foregroundStyle(.secondary)
Text(verbatim: address ?? "--").monospacedDigit().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"))
}
}.frame(maxWidth: .infinity, alignment: .leading).lineLimit(1).minimumScaleFactor(0.6)
}
private func networkTotal(_ title: LocalizedStringKey, value: String, symbol: String, color: Color) -> some View {
VStack(alignment: .leading, spacing: 4) {