feat(ui): 统一实现卡片背景趋势与大数值布局

This commit is contained in:
2026-11-15 15:40:29 +08:00
parent 397156249b
commit 3c1a2baace
25 changed files with 447 additions and 268 deletions
+98 -49
View File
@@ -1,6 +1,7 @@
import SwiftUI
struct ResourceCard<Content: View>: View {
/// 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
@@ -14,69 +15,51 @@ struct ResourceCard<Content: View>: View {
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)
}
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)
ZStack(alignment: .bottomLeading) {
chart()
.frame(width: bounds.size.width, height: ResourceCardMetrics.plotHeight)
VStack(alignment: .leading, spacing: ResourceCardMetrics.gap) {
header
HStack(alignment: .firstTextBaseline, spacing: 12) {
MetricHeadline(value: value, compact: secondaryValue != nil, color: valueColor ?? .primary)
if let secondaryValue {
MetricText(value: secondaryValue).font(PanelStyle.title)
.foregroundStyle(secondaryValueColor ?? color)
MetricHeadline(value: secondaryValue, compact: true, color: secondaryValueColor ?? .primary)
}
}.foregroundStyle(color).lineLimit(1)
}
.frame(height: PanelStyle.titleHeight)
VStack(alignment: .leading, spacing: 4) {
}
.frame(height: ResourceCardMetrics.headlineHeight, alignment: .leading)
.frame(maxWidth: .infinity, alignment: .leading)
content()
Spacer(minLength: 0)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.font(PanelStyle.label)
.padding(ResourceCardMetrics.padding)
.padding(.bottom, ResourceCardMetrics.plotHeight - 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(0.07)))
.background {
RoundedRectangle(cornerRadius: ResourceCardMetrics.radius)
.fill(Color("PanelCard"))
.shadow(color: .black.opacity(colorScheme == .dark ? 0.16 : 0.06), radius: 6, x: 0, y: 2)
}
.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)) {
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)
@@ -86,4 +69,70 @@ struct ResourceCard<Content: View>: View {
.accessibilityHidden(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: 14, weight: .semibold)).fixedSize()
Spacer(minLength: 4)
if let status {
Text(verbatim: status)
.font(.system(size: 11, 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, 6).frame(height: 18)
.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: 3) {
if let direction {
Image(systemName: direction == "↓" ? "arrow.down" : "arrow.up")
.font(.system(size: 16, weight: .medium))
}
Text(verbatim: parts.first.map(String.init) ?? value)
.font(.system(size: compact ? 23 : 36, weight: .semibold))
if parts.count > 1 {
Text(verbatim: String(parts[1]))
.font(.system(size: compact ? 11 : 14, weight: .medium))
.foregroundStyle(Color.secondary)
}
}
.monospacedDigit().foregroundStyle(color)
.lineLimit(1).minimumScaleFactor(0.75)
.frame(maxWidth: .infinity, alignment: .leading)
.help(Text(verbatim: value))
.accessibilityElement(children: .ignore)
.accessibilityLabel(Text(verbatim: value))
}
}