Files
Kymo/kymo/Features/MenuBar/MenuPanelView+Network.swift

104 lines
5.2 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"), headerAccessory: AnyView(networkInterfacePicker),
chart: {
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, visibleSeriesCount: 2, projection: .split(upwardSeries: 1)
).id(selectedInterface)
}
) {
HStack(spacing: 3) {
Text("network.totalShort").foregroundStyle(.secondary)
Image(systemName: "arrow.down").foregroundStyle(Color("MetricMemory"))
MetricText(value: network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.received })).fontWeight(.medium)
Text("·").foregroundStyle(.secondary)
Image(systemName: "arrow.up").foregroundStyle(Color("SeriesUpload"))
MetricText(value: network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.sent })).fontWeight(.medium)
}.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9).frame(height: 16)
VStack(spacing: 4) {
networkAddress("network.public", address: publicIPAddress)
networkAddress(
"network.local",
address: addressInterface?.addresses.first(where: { !$0.contains(":") }) ?? addressInterface?.addresses.first)
}
.font(.system(size: 10))
.accessibilityElement(children: .contain)
.accessibilityLabel(Text("network.addresses"))
}
.modifier(PanelPublicIPRefresh(metrics: metrics, address: $publicIPAddress))
}
private var networkInterfacePicker: some View {
HStack(spacing: 4) {
Circle().fill(networkStatusColor).frame(width: 5, height: 5)
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().frame(maxWidth: 70)
}
.help(Text(networkStatusLabel))
.accessibilityElement(children: .contain)
.accessibilityValue(Text(networkStatusLabel))
}
func networkAddress(_ title: LocalizedStringKey, address: String?) -> some View {
HStack(spacing: 3) {
Text(title).foregroundStyle(.secondary)
Text(verbatim: address ?? "--").font(.system(size: 10, weight: .medium)).monospacedDigit().textSelection(.enabled)
.help(Text(verbatim: address ?? "--"))
.frame(maxWidth: .infinity, alignment: .leading)
if let address {
Button {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(address, forType: .string)
} label: {
Image(systemName: "doc.on.doc").frame(width: 14, height: 14)
}
.buttonStyle(.plain).accessibilityLabel(Text("panel.copy"))
}
}.frame(maxWidth: .infinity, alignment: .leading).frame(height: 16).lineLimit(1)
}
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)
}
}