90 lines
4.0 KiB
Swift
90 lines
4.0 KiB
Swift
import SwiftUI
|
|
|
|
struct ResourceCard<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 status: String? = nil
|
|
var statusWarning = false
|
|
var warningText: Text? = nil
|
|
var detail: String? = nil
|
|
var detailColor: Color? = nil
|
|
var detailHelp: String? = nil
|
|
@ViewBuilder var content: () -> Content
|
|
|
|
var body: some View {
|
|
NativeContentHost(content: cardBody)
|
|
}
|
|
|
|
private var cardBody: some View {
|
|
GeometryReader { bounds in
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
HStack(alignment: .center, spacing: 6) {
|
|
Label {
|
|
Text(title).foregroundStyle(statusWarning ? .red : .primary)
|
|
} icon: {
|
|
Image(systemName: symbol).frame(width: 16, height: 16).foregroundStyle(statusWarning ? .red : color)
|
|
}.font(PanelStyle.title).fixedSize()
|
|
if let status {
|
|
Text(verbatim: status)
|
|
.font(.system(size: 10, weight: .medium))
|
|
.foregroundStyle(statusWarning ? .red : color)
|
|
.padding(.horizontal, 6).frame(height: 16)
|
|
.background((statusWarning ? Color.red : color).opacity(0.14), in: Capsule())
|
|
if let warningText {
|
|
warningText
|
|
.font(.system(size: 10)).foregroundStyle(.red).lineLimit(1)
|
|
}
|
|
}
|
|
if let detail {
|
|
Text(verbatim: detail)
|
|
.font(.system(size: 10, weight: .medium)).foregroundStyle(detailColor ?? .secondary)
|
|
.padding(.horizontal, 6).frame(height: 16)
|
|
.background((detailColor ?? .primary).opacity(detailColor == nil ? 0.06 : 0.14), in: Capsule())
|
|
.lineLimit(1)
|
|
.fixedSize(horizontal: true, vertical: false)
|
|
.help(Text(verbatim: detailHelp ?? detail))
|
|
.accessibilityLabel(Text(verbatim: detailHelp ?? detail))
|
|
}
|
|
Spacer(minLength: 0)
|
|
HStack(alignment: .center, spacing: 6) {
|
|
MetricText(value: value)
|
|
.font(PanelStyle.title)
|
|
.foregroundStyle(valueColor ?? color)
|
|
if let secondaryValue {
|
|
MetricText(value: secondaryValue).font(PanelStyle.title)
|
|
.foregroundStyle(secondaryValueColor ?? color)
|
|
}
|
|
}.foregroundStyle(color).lineLimit(1)
|
|
}
|
|
.frame(height: PanelStyle.titleHeight)
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
content()
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
|
}
|
|
.font(PanelStyle.label)
|
|
.padding(PanelStyle.padding)
|
|
.frame(width: bounds.size.width, height: bounds.size.height, alignment: .topLeading)
|
|
}
|
|
.modifier(CardSurface())
|
|
.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)) {
|
|
tooltip.content.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
.frame(width: proxy.size.width, height: proxy.size.height)
|
|
}
|
|
}
|
|
.allowsHitTesting(false)
|
|
.accessibilityHidden(true)
|
|
}
|
|
}
|
|
}
|