115 lines
5.5 KiB
Swift
115 lines
5.5 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
extension MenuPanelView {
|
|
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")
|
|
) {
|
|
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, showsLegendValues: false
|
|
).id(selectedInterface)
|
|
CardSeparator()
|
|
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)
|
|
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)
|
|
Text(networkStatusLabel)
|
|
}
|
|
}.frame(maxWidth: .infinity, alignment: .leading)
|
|
}.font(PanelStyle.label).lineLimit(1)
|
|
CardSeparator()
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
Text("network.addresses").font(PanelStyle.section)
|
|
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)
|
|
}
|
|
}.font(PanelStyle.label)
|
|
.modifier(CardInset())
|
|
}
|
|
.modifier(PanelPublicIPRefresh(metrics: metrics, address: $publicIPAddress))
|
|
}
|
|
func networkAddress(_ title: LocalizedStringKey, address: String?) -> some View {
|
|
HStack(spacing: 4) {
|
|
Text(title).foregroundStyle(.secondary)
|
|
Text(verbatim: address ?? "--").font(PanelStyle.value).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)
|
|
}
|
|
func networkTotal(_ title: LocalizedStringKey, value: String, symbol: String, color: Color) -> some View {
|
|
CardField(title: title) {
|
|
HStack(spacing: 3) {
|
|
Image(systemName: symbol).foregroundStyle(color)
|
|
MetricText(value: value)
|
|
}
|
|
}
|
|
}
|
|
|
|
var networkStatusLabel: LocalizedStringKey {
|
|
switch snapshot.networkPath {
|
|
case .available: "network.pathAvailable"
|
|
case .unavailable: "network.pathUnavailable"
|
|
case .requiresConnection: "network.pathRequiresConnection"
|
|
case nil: "metric.waiting"
|
|
}
|
|
}
|
|
var networkStatusColor: Color {
|
|
switch snapshot.networkPath {
|
|
case .available: .green
|
|
case .unavailable: .red
|
|
case .requiresConnection: .orange
|
|
case nil: .secondary
|
|
}
|
|
}
|
|
var addressInterface: NetworkInterfaceMetrics? {
|
|
let name = selectedInterface.isEmpty ? snapshot.defaultNetworkInterface : network.first?.name
|
|
return snapshot.network?.first { $0.name == name && $0.active }
|
|
}
|
|
var networkIntervals: [TimeInterval] {
|
|
selectedInterface.isEmpty ? chartIntervals : (metrics.history.interfaces[selectedInterface] ?? []).map(\.expectedInterval)
|
|
}
|
|
var networkDates: [Date] {
|
|
selectedInterface.isEmpty ? samples.map(\.date) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.date)
|
|
}
|
|
var networkDownloads: [Double?] {
|
|
selectedInterface.isEmpty ? samples.map(\.download) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.download)
|
|
}
|
|
var networkUploads: [Double?] {
|
|
selectedInterface.isEmpty ? samples.map(\.upload) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.upload)
|
|
}
|
|
}
|