145 lines
6.2 KiB
Swift
145 lines
6.2 KiB
Swift
import SwiftUI
|
|
|
|
/// One fixed-size native host owns foreground, background trend 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 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
|
|
@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
|
|
ZStack(alignment: .bottomLeading) {
|
|
chart()
|
|
.frame(width: bounds.size.width, height: bounds.size.height)
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
foreground
|
|
Spacer(minLength: 0)
|
|
}
|
|
.padding(ResourceCardMetrics.padding)
|
|
.frame(width: bounds.size.width, height: bounds.size.height, alignment: .topLeading)
|
|
}
|
|
.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: max(ResourceCardMetrics.tooltipMinimumY, plot.minY)) {
|
|
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()
|
|
Spacer(minLength: 4)
|
|
if let status {
|
|
Text(verbatim: 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)
|
|
}
|
|
Text(verbatim: parts.first.map(String.init) ?? value)
|
|
.font(.system(size: compact ? 16 : 28, weight: .semibold))
|
|
if parts.count > 1 {
|
|
Text(verbatim: 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))
|
|
}
|
|
}
|