feat(panel): 调整资源卡片读数布局并保留趋势平移
逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。 存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
This commit is contained in:
@@ -5,6 +5,7 @@ struct IntervalControl: View {
|
||||
@Binding var milliseconds: Int
|
||||
var range: ClosedRange<Int> = 500...1_800_000
|
||||
var step = 100
|
||||
var fieldWidth: CGFloat = 64
|
||||
@State private var text = ""
|
||||
@FocusState private var focused: Bool
|
||||
|
||||
@@ -13,7 +14,7 @@ struct IntervalControl: View {
|
||||
TextField("sampling.seconds", text: $text)
|
||||
.labelsHidden()
|
||||
.textFieldStyle(.roundedBorder).multilineTextAlignment(.trailing)
|
||||
.frame(width: 64).focused($focused)
|
||||
.frame(width: fieldWidth).focused($focused)
|
||||
.onChange(of: text) { _, value in
|
||||
if let seconds = Double(value.replacingOccurrences(of: ",", with: ".")), seconds.isFinite,
|
||||
seconds >= Double(range.lowerBound) / 1000, seconds <= Double(range.upperBound) / 1000 {
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Metrics change only when a new sample is published; no numeric transition or spring.
|
||||
extension EnvironmentValues {
|
||||
@Entry var panelMotionActive: Bool = true
|
||||
}
|
||||
|
||||
struct MetricText: View {
|
||||
let value: String
|
||||
var body: some View { Text(verbatim: value).monospacedDigit() }
|
||||
|
||||
var body: some View {
|
||||
Text(verbatim: value).monospacedDigit()
|
||||
}
|
||||
}
|
||||
|
||||
struct PanelCardBackground: ViewModifier {
|
||||
@@ -13,3 +19,17 @@ struct PanelCardBackground: ViewModifier {
|
||||
content.background(RoundedRectangle(cornerRadius: radius).fill(Color("PanelCard")))
|
||||
}
|
||||
}
|
||||
|
||||
struct InlineMetric: View {
|
||||
let title: LocalizedStringKey
|
||||
let value: String
|
||||
init(_ title: LocalizedStringKey, _ value: String) { self.title = title; self.value = value }
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 4) {
|
||||
Text(title).foregroundStyle(.secondary)
|
||||
MetricText(value: value).fontWeight(.medium)
|
||||
}
|
||||
.font(.system(size: 10)).lineLimit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import SwiftUI
|
||||
|
||||
/// One fixed-size native host owns foreground, background trend and hover presentation.
|
||||
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
|
||||
@@ -9,6 +14,7 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
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
|
||||
@@ -16,6 +22,7 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
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
|
||||
@@ -24,15 +31,17 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
|
||||
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)
|
||||
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()
|
||||
}
|
||||
.padding(ResourceCardMetrics.padding)
|
||||
.frame(width: bounds.size.width, height: bounds.size.height, alignment: .topLeading)
|
||||
}
|
||||
.frame(width: bounds.size.width, height: bounds.size.height, alignment: .topLeading)
|
||||
.background {
|
||||
if chartPlacement == .background { chart() }
|
||||
}
|
||||
.background(Color("PanelCard"))
|
||||
.clipShape(.rect(cornerRadius: ResourceCardMetrics.radius))
|
||||
@@ -48,7 +57,7 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
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)) {
|
||||
minimumY: ResourceCardMetrics.tooltipMinimumY) {
|
||||
tooltip.content.fixedSize(horizontal: false, vertical: true)
|
||||
}
|
||||
.frame(width: proxy.size.width, height: proxy.size.height)
|
||||
@@ -84,9 +93,17 @@ struct ResourceCard<Chart: View, Content: View>: View {
|
||||
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 {
|
||||
Text(verbatim: status)
|
||||
MetricText(value: status)
|
||||
.font(.system(size: 10, weight: .medium))
|
||||
.foregroundStyle(statusWarning ? .red : .secondary)
|
||||
.help(warningText ?? Text(verbatim: status))
|
||||
@@ -126,10 +143,10 @@ private struct MetricHeadline: View {
|
||||
Image(systemName: direction == "↓" ? "arrow.down" : "arrow.up")
|
||||
.font(.system(size: 12, weight: .medium)).foregroundStyle(color)
|
||||
}
|
||||
Text(verbatim: parts.first.map(String.init) ?? value)
|
||||
MetricText(value: parts.first.map(String.init) ?? value)
|
||||
.font(.system(size: compact ? 16 : 28, weight: .semibold))
|
||||
if parts.count > 1 {
|
||||
Text(verbatim: String(parts[1]))
|
||||
MetricText(value: String(parts[1]))
|
||||
.font(.system(size: compact ? 10 : 12, weight: .medium))
|
||||
.foregroundStyle(Color.primary.opacity(0.8))
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Four square blocks fill from bottom to top; partial blocks retain the actual fraction.
|
||||
struct SegmentedCoreBar: View {
|
||||
let value: Double?
|
||||
let color: Color
|
||||
|
||||
static func portions(_ value: Double?, count: Int = 4) -> [Double] {
|
||||
guard count > 0 else { return [] }
|
||||
let fraction = value.flatMap { $0.isFinite ? min(1, max(0, $0)) : nil } ?? 0
|
||||
return (0..<count).map { min(1, max(0, fraction * Double(count) - Double($0))) }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
let fills = Self.portions(value)
|
||||
VStack(spacing: 2) {
|
||||
ForEach(Array(fills.indices.reversed()), id: \.self) { index in
|
||||
ZStack(alignment: .bottom) {
|
||||
Rectangle().fill(color.opacity(0.15))
|
||||
Rectangle().fill(color).frame(height: 6 * fills[index])
|
||||
}.frame(width: 6, height: 6).clipped()
|
||||
}
|
||||
}
|
||||
.accessibilityHidden(true)
|
||||
.transaction { $0.animation = nil; $0.disablesAnimations = true }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user