逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。 存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
162 lines
6.9 KiB
Swift
162 lines
6.9 KiB
Swift
import SwiftUI
|
|
|
|
enum CardChartPlacement {
|
|
case background
|
|
case belowDetails
|
|
}
|
|
|
|
/// One native host owns the text, chart and hover presentation.
|
|
struct ResourceCard<Chart: View, Content: View>: View {
|
|
let title: LocalizedStringKey
|
|
let symbol: String
|
|
let color: Color
|
|
let value: String
|
|
var secondaryValue: String? = nil
|
|
var valueColor: Color? = nil
|
|
var secondaryValueColor: Color? = nil
|
|
var titleBadge: String? = nil
|
|
var status: String? = nil
|
|
var statusWarning = false
|
|
var warningText: Text? = nil
|
|
var detail: String? = nil
|
|
var detailColor: Color? = nil
|
|
var detailHelp: String? = nil
|
|
var headerAccessory: AnyView? = nil
|
|
var chartPlacement: CardChartPlacement = .belowDetails
|
|
@ViewBuilder var chart: () -> Chart
|
|
@ViewBuilder var content: () -> Content
|
|
@Environment(\.colorScheme) private var colorScheme
|
|
|
|
var body: some View { NativeContentHost(content: cardBody) }
|
|
|
|
private var cardBody: some View {
|
|
GeometryReader { bounds in
|
|
VStack(alignment: .leading, spacing: ResourceCardMetrics.gap) {
|
|
foreground
|
|
.padding(.horizontal, ResourceCardMetrics.padding)
|
|
.padding(.top, ResourceCardMetrics.padding)
|
|
if chartPlacement == .belowDetails {
|
|
chart().frame(maxWidth: .infinity, maxHeight: .infinity).clipped()
|
|
}
|
|
}
|
|
.frame(width: bounds.size.width, height: bounds.size.height, alignment: .topLeading)
|
|
.background {
|
|
if chartPlacement == .background { chart() }
|
|
}
|
|
.background(Color("PanelCard"))
|
|
.clipShape(.rect(cornerRadius: ResourceCardMetrics.radius))
|
|
.overlay(RoundedRectangle(cornerRadius: ResourceCardMetrics.radius).strokeBorder(.primary.opacity(colorScheme == .dark ? 0.06 : 0.025)))
|
|
.background {
|
|
RoundedRectangle(cornerRadius: ResourceCardMetrics.radius)
|
|
.fill(Color("PanelCard"))
|
|
.shadow(color: .black.opacity(colorScheme == .dark ? 0.12 : 0.05), radius: 4, x: 0, y: 1)
|
|
}
|
|
}
|
|
.overlayPreferenceValue(ChartTooltipPreference.self) { tooltip in
|
|
GeometryReader { proxy in
|
|
if let tooltip {
|
|
let plot = proxy[tooltip.plot]
|
|
ChartTooltipLayer(point: CGPoint(x: plot.minX + tooltip.point.x, y: plot.minY + tooltip.point.y),
|
|
minimumY: ResourceCardMetrics.tooltipMinimumY) {
|
|
tooltip.content.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
.frame(width: proxy.size.width, height: proxy.size.height)
|
|
}
|
|
}
|
|
.allowsHitTesting(false)
|
|
.accessibilityHidden(true)
|
|
}
|
|
}
|
|
|
|
private var foreground: some View {
|
|
VStack(alignment: .leading, spacing: ResourceCardMetrics.gap) {
|
|
header
|
|
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
|
MetricHeadline(value: value, compact: secondaryValue != nil, color: valueColor ?? .primary)
|
|
if let secondaryValue {
|
|
MetricHeadline(value: secondaryValue, compact: true, color: secondaryValueColor ?? .primary)
|
|
}
|
|
}
|
|
.frame(height: ResourceCardMetrics.headlineHeight, alignment: .leading)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
content()
|
|
}
|
|
.font(.system(size: ResourceCardMetrics.auxiliarySize))
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
|
|
private var header: some View {
|
|
HStack(spacing: 6) {
|
|
Label {
|
|
Text(title).foregroundStyle(statusWarning ? .red : .primary)
|
|
} icon: {
|
|
Image(systemName: symbol).foregroundStyle(statusWarning ? .red : color)
|
|
}
|
|
.font(.system(size: 11, weight: .semibold)).fixedSize()
|
|
if let titleBadge {
|
|
Text(verbatim: titleBadge)
|
|
.font(.system(size: 9, weight: .semibold)).monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
.padding(.horizontal, 5).frame(height: 14)
|
|
.background(.primary.opacity(0.08), in: Capsule())
|
|
.fixedSize()
|
|
}
|
|
Spacer(minLength: 4)
|
|
if let status {
|
|
MetricText(value: status)
|
|
.font(.system(size: 10, weight: .medium))
|
|
.foregroundStyle(statusWarning ? .red : .secondary)
|
|
.help(warningText ?? Text(verbatim: status))
|
|
.accessibilityLabel(warningText.map { Text("\(status) \($0)") } ?? Text(verbatim: status))
|
|
}
|
|
if let detail {
|
|
Text(verbatim: detail)
|
|
.font(.system(size: 10, weight: .medium)).foregroundStyle(detailColor ?? .secondary)
|
|
.padding(.horizontal, 5).frame(height: 14)
|
|
.background((detailColor ?? .primary).opacity(0.10), in: Capsule())
|
|
.lineLimit(1)
|
|
.help(Text(verbatim: detailHelp ?? detail))
|
|
.accessibilityLabel(Text(verbatim: detailHelp ?? detail))
|
|
}
|
|
headerAccessory
|
|
}
|
|
.lineLimit(1)
|
|
.frame(height: ResourceCardMetrics.titleHeight)
|
|
}
|
|
}
|
|
|
|
/// A preformatted value keeps its unit smaller without changing metric semantics.
|
|
private struct MetricHeadline: View {
|
|
let value: String
|
|
let compact: Bool
|
|
let color: Color
|
|
private var direction: Character? { value.first.flatMap { $0 == "↓" || $0 == "↑" ? $0 : nil } }
|
|
private var parts: [Substring] {
|
|
let number = direction == nil ? value : String(value.dropFirst()).trimmingCharacters(in: .whitespaces)
|
|
if number.hasSuffix("%") { return [number.dropLast(), "%"] }
|
|
return number.split(separator: " ", maxSplits: 1)
|
|
}
|
|
|
|
var body: some View {
|
|
HStack(alignment: .firstTextBaseline, spacing: 2) {
|
|
if let direction {
|
|
Image(systemName: direction == "↓" ? "arrow.down" : "arrow.up")
|
|
.font(.system(size: 12, weight: .medium)).foregroundStyle(color)
|
|
}
|
|
MetricText(value: parts.first.map(String.init) ?? value)
|
|
.font(.system(size: compact ? 16 : 28, weight: .semibold))
|
|
if parts.count > 1 {
|
|
MetricText(value: String(parts[1]))
|
|
.font(.system(size: compact ? 10 : 12, weight: .medium))
|
|
.foregroundStyle(Color.primary.opacity(0.8))
|
|
}
|
|
}
|
|
.monospacedDigit().foregroundStyle(.primary)
|
|
.lineLimit(1).minimumScaleFactor(0.75)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.help(Text(verbatim: value))
|
|
.accessibilityElement(children: .ignore)
|
|
.accessibilityLabel(Text(verbatim: value))
|
|
}
|
|
}
|