feat(menubar): 完善首排监控卡片与温度风扇采样
补充 GPU 渲染分块、内存与利用率统计,增加只读 SMC 温度及风扇信息和旋转动效。完善电池功率与耗电进程列表,统一首排布局并固定异步加载区域高度。同步验证用例、架构说明和 GPU 能力调研工具。
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
import SwiftUI
|
||||
|
||||
enum TrendValueFormat {
|
||||
case percent
|
||||
case rate
|
||||
case bytes
|
||||
case watts
|
||||
}
|
||||
|
||||
/// Bounded, timestamp-based curves. Gaps remain gaps; animation only runs on append.
|
||||
struct PanelTrendChart: View {
|
||||
let dates: [Date]
|
||||
@@ -11,15 +18,20 @@ struct PanelTrendChart: View {
|
||||
var thirdColor: Color = .purple
|
||||
var fixedScale: Double? = 1
|
||||
var scaleLabel: String?
|
||||
var secondSeriesUsesAutonomousScale = false
|
||||
var secondValueFormat: TrendValueFormat?
|
||||
var firstLabel: LocalizedStringKey = "panel.total"
|
||||
var secondLabel: LocalizedStringKey = "network.upload"
|
||||
var thirdLabel: LocalizedStringKey = "cpu.system"
|
||||
var expectedInterval: TimeInterval = 2
|
||||
var boundaryDates: Set<Date> = []
|
||||
var intervals: [TimeInterval] = []
|
||||
var plotHeight: CGFloat = 58
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var retainedScale: Double = 1
|
||||
@State private var smallSamples = 0
|
||||
@State private var retainedSecondScale: Double = 1
|
||||
@State private var smallSecondSamples = 0
|
||||
@State private var hoveredDate: Date?
|
||||
@State private var hoverPoint: CGPoint?
|
||||
@State private var previousBegin: Date?
|
||||
@@ -34,39 +46,50 @@ struct PanelTrendChart: View {
|
||||
boundaryDates: boundaryDates)
|
||||
}
|
||||
private var maximum: Double { max(1, (first + second + third).compactMap { $0 }.filter(\.isFinite).max() ?? 1) }
|
||||
private var secondMaximum: Double { max(1, second.compactMap { $0 }.filter(\.isFinite).max() ?? 1) }
|
||||
private var scale: Double { max(0.0001, fixedScale ?? retainedScale) }
|
||||
private var secondaryScale: Double { max(0.0001, retainedSecondScale) }
|
||||
private var hasSecondaryScale: Bool { secondSeriesUsesAutonomousScale && !second.isEmpty }
|
||||
private var automaticFormat: TrendValueFormat {
|
||||
if fixedScale == 1 { return .percent }
|
||||
if fixedScale == nil { return .rate }
|
||||
return .bytes
|
||||
}
|
||||
private var index: Int? {
|
||||
guard !dates.isEmpty else { return nil }
|
||||
guard let hoveredDate else { return dates.count - 1 }
|
||||
return dates.indices.min { abs(dates[$0].timeIntervalSince(hoveredDate)) < abs(dates[$1].timeIntervalSince(hoveredDate)) }
|
||||
}
|
||||
private func formatted(_ value: Double?) -> String {
|
||||
private func formatted(_ value: Double?, format: TrendValueFormat? = nil) -> String {
|
||||
guard let value, value.isFinite else { return "--" }
|
||||
if fixedScale == 1 { return MetricFormat.percent(value) }
|
||||
if fixedScale == nil { return MetricFormat.rate(value) }
|
||||
return MetricFormat.bytes(UInt64(max(0, value)))
|
||||
switch format ?? automaticFormat {
|
||||
case .percent: return MetricFormat.percent(value)
|
||||
case .rate: return MetricFormat.rate(value)
|
||||
case .bytes: return MetricFormat.bytes(UInt64(max(0, value)))
|
||||
case .watts: return MetricFormat.watts(value)
|
||||
}
|
||||
}
|
||||
private func legend(_ label: LocalizedStringKey, values: [Double?], tint: Color) -> some View {
|
||||
private func legend(_ label: LocalizedStringKey, values: [Double?], tint: Color, format: TrendValueFormat? = nil) -> some View {
|
||||
HStack(spacing: 3) {
|
||||
RoundedRectangle(cornerRadius: 1).fill(tint).frame(width: 7, height: 7)
|
||||
Text(label)
|
||||
Text(verbatim: formatted(values.last ?? nil)).monospacedDigit()
|
||||
Text(verbatim: formatted(values.last ?? nil, format: format)).monospacedDigit()
|
||||
}.font(.system(size: 9)).lineLimit(1).minimumScaleFactor(0.8)
|
||||
}
|
||||
|
||||
private func tooltipRow(_ label: LocalizedStringKey, values: [Double?], tint: Color, index: Int) -> some View {
|
||||
private func tooltipRow(_ label: LocalizedStringKey, values: [Double?], tint: Color, format: TrendValueFormat? = nil, index: Int) -> some View {
|
||||
HStack(spacing: 6) {
|
||||
RoundedRectangle(cornerRadius: 2).fill(tint).frame(width: 8, height: 8)
|
||||
Text(label)
|
||||
Spacer(minLength: 4)
|
||||
Text(verbatim: formatted(index < values.count ? values[index] : nil)).monospacedDigit()
|
||||
Text(verbatim: formatted(index < values.count ? values[index] : nil, format: format)).monospacedDigit()
|
||||
}
|
||||
}
|
||||
private func tooltip(index: Int) -> some View {
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
Text(dates[index], format: .dateTime.hour().minute().second()).foregroundStyle(.secondary)
|
||||
tooltipRow(firstLabel, values: first, tint: color, index: index)
|
||||
if !second.isEmpty { tooltipRow(secondLabel, values: second, tint: secondColor, index: index) }
|
||||
if !second.isEmpty { tooltipRow(secondLabel, values: second, tint: secondColor, format: secondValueFormat, index: index) }
|
||||
if !third.isEmpty { tooltipRow(thirdLabel, values: third, tint: thirdColor, index: index) }
|
||||
}
|
||||
.font(.system(size: 10)).padding(8)
|
||||
@@ -79,7 +102,7 @@ struct PanelTrendChart: View {
|
||||
VStack(spacing: 3) {
|
||||
HStack(spacing: 10) {
|
||||
legend(firstLabel, values: first, tint: color)
|
||||
if !second.isEmpty { legend(secondLabel, values: second, tint: secondColor) }
|
||||
if !second.isEmpty { legend(secondLabel, values: second, tint: secondColor, format: secondValueFormat) }
|
||||
if !third.isEmpty { legend(thirdLabel, values: third, tint: thirdColor) }
|
||||
Spacer(minLength: 0)
|
||||
}.frame(height: 14)
|
||||
@@ -91,11 +114,12 @@ struct PanelTrendChart: View {
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: "0")
|
||||
}.font(.system(size: 8)).foregroundStyle(.secondary)
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: 58)
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: plotHeight)
|
||||
GeometryReader { proxy in
|
||||
ZStack(alignment: .topLeading) {
|
||||
PanelCurveCanvas(frame: displayedFrame ?? inputFrame, colors: [color, secondColor, thirdColor],
|
||||
scale: scale, maximumGap: max(5, expectedInterval * 2),
|
||||
scales: [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale],
|
||||
maximumGap: max(5, expectedInterval * 2),
|
||||
targetBegin: (lastFrame ?? inputFrame).dates.first, targetEnd: (lastFrame ?? inputFrame).dates.last,
|
||||
previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
|
||||
if hoveredDate != nil, let index, let begin = dates.first, let end = dates.last, end > begin {
|
||||
@@ -106,8 +130,9 @@ struct PanelTrendChart: View {
|
||||
ForEach(0..<3, id: \.self) { series in
|
||||
let values = [first, second, third][series]
|
||||
if index < values.count, let value = values[index], value.isFinite {
|
||||
let seriesScale = [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale][series]
|
||||
Circle().fill([color, secondColor, thirdColor][series]).frame(width: 5, height: 5)
|
||||
.position(x: x, y: proxy.size.height * (1 - min(1, max(0, value / scale))))
|
||||
.position(x: x, y: proxy.size.height * (1 - min(1, max(0, value / seriesScale))))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,11 +149,21 @@ struct PanelTrendChart: View {
|
||||
}
|
||||
}
|
||||
.clipped()
|
||||
}.frame(height: 58)
|
||||
}.frame(height: plotHeight)
|
||||
.anchorPreference(key: ChartTooltipPreference.self, value: .bounds) { plot in
|
||||
guard let hoverPoint, let index else { return nil }
|
||||
return ChartTooltipPresentation(plot: plot, point: hoverPoint, content: AnyView(tooltip(index: index)))
|
||||
}
|
||||
if hasSecondaryScale {
|
||||
VStack(alignment: .leading) {
|
||||
Text(verbatim: formatted(secondaryScale, format: .watts))
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: formatted(secondaryScale / 2, format: .watts))
|
||||
Spacer(minLength: 0)
|
||||
Text(verbatim: "0 W")
|
||||
}.font(.system(size: 8)).foregroundStyle(.secondary)
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: 45, height: plotHeight)
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
if let date = dates.first { Text(date, style: .time) }
|
||||
@@ -137,7 +172,12 @@ struct PanelTrendChart: View {
|
||||
}.font(.system(size: 9)).foregroundStyle(.secondary).frame(height: 12)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.onAppear { retainedScale = maximum; lastFrame = inputFrame; displayedFrame = inputFrame }
|
||||
.onAppear {
|
||||
retainedScale = maximum
|
||||
retainedSecondScale = secondMaximum
|
||||
lastFrame = inputFrame
|
||||
displayedFrame = inputFrame
|
||||
}
|
||||
.onChange(of: dates) { _, _ in
|
||||
let target = maximum
|
||||
if target > retainedScale { retainedScale = target; smallSamples = 0 }
|
||||
@@ -145,6 +185,12 @@ struct PanelTrendChart: View {
|
||||
smallSamples += 1
|
||||
if smallSamples >= 10 { retainedScale = target; smallSamples = 0 }
|
||||
} else { smallSamples = 0 }
|
||||
let secondTarget = secondMaximum
|
||||
if secondTarget > retainedSecondScale { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
|
||||
else if secondTarget < retainedSecondScale * 0.5 {
|
||||
smallSecondSamples += 1
|
||||
if smallSecondSamples >= 10 { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
|
||||
} else { smallSecondSamples = 0 }
|
||||
let next = inputFrame
|
||||
let old = lastFrame
|
||||
let gap = PanelCurveGeometry.allowedGap(at: next.dates.count - 1, intervals: next.intervals, fallback: max(5, expectedInterval * 2))
|
||||
@@ -174,7 +220,7 @@ struct PanelTrendChart: View {
|
||||
private struct PanelCurveCanvas: View, Animatable {
|
||||
let frame: PanelCurveFrame
|
||||
let colors: [Color]
|
||||
let scale: Double
|
||||
let scales: [Double]
|
||||
let maximumGap: TimeInterval
|
||||
let targetBegin: Date?
|
||||
let targetEnd: Date?
|
||||
@@ -201,7 +247,10 @@ private struct PanelCurveCanvas: View, Animatable {
|
||||
guard let first = targetBegin, let last = targetEnd else { return }
|
||||
let domain = PanelCurveFrame.domain(previousBegin: previousBegin, previousEnd: previousEnd,
|
||||
begin: first, end: last, progress: Double(progress))
|
||||
for (values, tint) in zip(frame.series, colors) {
|
||||
for seriesIndex in frame.series.indices {
|
||||
let values = frame.series[seriesIndex]
|
||||
let tint = colors[min(seriesIndex, colors.count - 1)]
|
||||
let scale = scales[min(seriesIndex, scales.count - 1)]
|
||||
for run in PanelCurveGeometry.runs(dates: frame.dates, values: values, maximumGap: maximumGap, boundaryDates: frame.boundaryDates, intervals: frame.intervals) {
|
||||
let points = run.map { item in
|
||||
CGPoint(x: size.width * domain.position(item.date),
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Approximate visual speed tiers; rotation does not attempt to reproduce physical RPM.
|
||||
nonisolated struct FanRotation {
|
||||
private(set) var phase: Double = 0
|
||||
private(set) var speed: Double = 0
|
||||
private var reference: TimeInterval = 0
|
||||
|
||||
static func speed(rpm: Double?, reducedMotion: Bool) -> Double {
|
||||
guard !reducedMotion, let rpm, rpm.isFinite, rpm > 0 else { return 0 }
|
||||
switch rpm {
|
||||
case ..<2000: return 90
|
||||
case ..<4000: return 180
|
||||
case ..<6000: return 300
|
||||
default: return 450
|
||||
}
|
||||
}
|
||||
|
||||
func angle(at time: TimeInterval) -> Double {
|
||||
(phase + max(0, time - reference) * speed).truncatingRemainder(dividingBy: 360)
|
||||
}
|
||||
|
||||
mutating func update(rpm: Double?, reducedMotion: Bool, at time: TimeInterval) {
|
||||
phase = angle(at: time)
|
||||
reference = time
|
||||
speed = Self.speed(rpm: rpm, reducedMotion: reducedMotion)
|
||||
}
|
||||
}
|
||||
|
||||
struct FanSpeedIndicator: View {
|
||||
let title: LocalizedStringKey
|
||||
let rpm: Double?
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var rotation = FanRotation()
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 5) {
|
||||
TimelineView(.animation(minimumInterval: 1.0 / 30, paused: rotation.speed == 0)) { context in
|
||||
Image(systemName: "fanblades.fill")
|
||||
.rotationEffect(.degrees(rotation.angle(at: context.date.timeIntervalSinceReferenceDate)))
|
||||
.foregroundStyle(.secondary)
|
||||
}.frame(width: 14, height: 14).accessibilityHidden(true)
|
||||
Text(title).foregroundStyle(.secondary)
|
||||
Spacer(minLength: 2)
|
||||
AnimatedMetricText(value: rpm.map { "\(Int($0.rounded())) RPM" } ?? "-- RPM")
|
||||
.monospacedDigit().fontWeight(.medium)
|
||||
}
|
||||
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.7)
|
||||
.onAppear { update() }
|
||||
.onChange(of: rpm) { _, _ in update() }
|
||||
.onChange(of: reduceMotion) { _, _ in update() }
|
||||
}
|
||||
|
||||
private func update() {
|
||||
rotation.update(rpm: rpm, reducedMotion: reduceMotion, at: Date.timeIntervalSinceReferenceDate)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import SwiftUI
|
||||
|
||||
/// A bounded percentage gauge; missing data keeps an empty track and an explicit -- label.
|
||||
struct MetricRingGauge: View {
|
||||
let title: LocalizedStringKey
|
||||
let value: Double?
|
||||
let color: Color
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
|
||||
private var fraction: Double { value.flatMap { $0.isFinite ? min(1, max(0, $0)) : nil } ?? 0 }
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 3) {
|
||||
ZStack {
|
||||
Circle().stroke(color.opacity(0.17), lineWidth: 3)
|
||||
Circle().trim(from: 0, to: fraction)
|
||||
.stroke(color, style: StrokeStyle(lineWidth: 3, lineCap: .round))
|
||||
.rotationEffect(.degrees(-90))
|
||||
.animation(reduceMotion ? nil : .easeInOut(duration: 0.35), value: fraction)
|
||||
AnimatedMetricText(value: MetricFormat.percent(value)).font(.system(size: 8, weight: .semibold))
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: 18, height: 12)
|
||||
}.padding(2).frame(width: 28, height: 28).accessibilityHidden(true)
|
||||
Text(title).font(.system(size: 8)).foregroundStyle(.secondary)
|
||||
.lineLimit(1).minimumScaleFactor(0.7)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.accessibilityElement(children: .ignore)
|
||||
.accessibilityLabel(Text(title))
|
||||
.accessibilityValue(Text(verbatim: MetricFormat.percent(value)))
|
||||
}
|
||||
}
|
||||
@@ -6,16 +6,40 @@ struct ResourceCard<Content: View>: View {
|
||||
let color: Color
|
||||
let value: String
|
||||
var secondaryValue: String? = nil
|
||||
var status: String? = nil
|
||||
var statusWarning = false
|
||||
var statusHelp: Text? = nil
|
||||
var warningText: Text? = nil
|
||||
var detail: String? = nil
|
||||
@ViewBuilder var content: () -> Content
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
||||
Label { Text(title).foregroundStyle(.primary) } icon: {
|
||||
Image(systemName: symbol).foregroundStyle(color)
|
||||
VStack(alignment: .leading, spacing: 3) {
|
||||
HStack(alignment: .center, spacing: 6) {
|
||||
Label { Text(title).foregroundStyle(statusWarning ? .red : .primary) } icon: {
|
||||
Image(systemName: symbol).foregroundStyle(statusWarning ? .red : color)
|
||||
}.font(.system(size: 13, weight: .semibold)).fixedSize()
|
||||
if let status {
|
||||
Text(verbatim: status)
|
||||
.font(.system(size: 10, weight: .medium))
|
||||
.foregroundStyle(statusWarning ? .red : color)
|
||||
.padding(.horizontal, 6).padding(.vertical, 2)
|
||||
.background((statusWarning ? Color.red : color).opacity(0.14), in: Capsule())
|
||||
.help(statusHelp ?? Text(verbatim: status))
|
||||
if let warningText {
|
||||
warningText
|
||||
.font(.system(size: 10)).foregroundStyle(.red).lineLimit(1).minimumScaleFactor(0.7)
|
||||
}
|
||||
}
|
||||
if let detail {
|
||||
Text(verbatim: detail)
|
||||
.font(.system(size: 9, weight: .medium)).foregroundStyle(.secondary)
|
||||
.padding(.horizontal, 5).padding(.vertical, 2)
|
||||
.background(.primary.opacity(0.06), in: Capsule())
|
||||
.lineLimit(1).minimumScaleFactor(0.7)
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
HStack(alignment: .firstTextBaseline, spacing: 6) {
|
||||
HStack(alignment: .center, spacing: 6) {
|
||||
AnimatedMetricText(value: value)
|
||||
.font(.system(size: 13, weight: .semibold))
|
||||
if let secondaryValue {
|
||||
|
||||
Reference in New Issue
Block a user