feat(menubar): 完善首排监控卡片与温度风扇采样

补充 GPU 渲染分块、内存与利用率统计,增加只读 SMC 温度及风扇信息和旋转动效。完善电池功率与耗电进程列表,统一首排布局并固定异步加载区域高度。同步验证用例、架构说明和 GPU 能力调研工具。
This commit is contained in:
2026-11-03 16:42:09 +08:00
parent 45a6e16bb8
commit cc38066de2
43 changed files with 2658 additions and 536 deletions
+80 -24
View File
@@ -27,42 +27,98 @@ struct CPUCoreGrid: View {
}
}
static func groups(count: Int, kinds: [Int: CPUCoreTopology.Kind]) -> [[Int]] {
let order: [CPUCoreTopology.Kind?] = [.efficiency, .middle, .performance, nil]
return order.map { kind in (0..<max(0, count)).filter { kinds[$0] == kind } }.filter { !$0.isEmpty }
}
static func groupWidths(groups: [[Int]], width: CGFloat) -> [CGFloat] {
let weights = groups.map { CGFloat(($0.count + 1) / 2) }
let total = max(1, weights.reduce(0, +))
let available = max(0, width - CGFloat(max(0, groups.count - 1)) * 13)
return weights.map { available * $0 / total }
}
var body: some View {
CPUCoreGridLayout {
ForEach(Array(values.enumerated()), id: \.offset) { index, value in
let kind = kinds[index]
GeometryReader { proxy in
let numberWidth = min(max(12, CGFloat(String(values.count).count) * 6), max(0, proxy.size.width * 0.45))
let spacing = min(3, proxy.size.width * 0.08)
ZStack(alignment: .leading) {
Text(verbatim: "\(index + 1)").font(.system(size: 9))
.lineLimit(1).minimumScaleFactor(0.3).frame(width: numberWidth)
MetricProgressBar(value: value, color: color(kind))
.frame(width: max(0, proxy.size.width - numberWidth - spacing), height: 7)
.offset(x: numberWidth + spacing)
.animation(reduceMotion ? nil : .easeInOut(duration: min(0.35, interval * 0.8)), value: value)
}.frame(width: proxy.size.width, height: proxy.size.height, alignment: .leading)
}.clipped()
.help((kind.map { label($0) + Text(verbatim: " · ") } ?? Text("cpu.core")) + Text(verbatim: "\(index + 1): \(MetricFormat.percent(value))"))
.accessibilityLabel(kind.map { label($0) } ?? Text("cpu.core"))
.accessibilityValue("\(index + 1): \(MetricFormat.percent(value))")
let groups = Self.groups(count: values.count, kinds: kinds)
GeometryReader { geometry in
let widths = Self.groupWidths(groups: groups, width: geometry.size.width)
let diameter = zip(groups, widths).map { indices, width in
let columns = CPUCoreGridLayout.rowCounts(count: indices.count, width: width).max() ?? 1
let spacing = min(CPUCoreGridLayout.columnSpacing, max(0, (width / CGFloat(columns) - 18) / 2))
return max(0, (width - CGFloat(columns - 1) * spacing) / CGFloat(columns) - 4)
}.min() ?? 20
HStack(alignment: .top, spacing: 6) {
ForEach(groups.indices, id: \.self) { group in
if group > 0 {
Rectangle().fill(.primary.opacity(0.08)).frame(width: 1)
}
VStack(alignment: .leading, spacing: 5) {
HStack(spacing: 3) {
Circle().fill(color(kinds[groups[group][0]])).frame(width: 5, height: 5)
if let kind = kinds[groups[group][0]] { label(kind) }
else { Text("cpu.core") }
Text(verbatim: "\(groups[group].count)").foregroundStyle(.secondary)
}
.font(.system(size: 9, weight: .medium))
.lineLimit(1).minimumScaleFactor(0.6)
CPUCoreGridLayout {
ForEach(groups[group], id: \.self) { index in
ring(index: index, maximumDiameter: min(20, diameter))
}
}
}.frame(width: widths[group], alignment: .topLeading)
}
}
}.padding(.vertical, 3)
}
.frame(height: 15 + CGFloat(maxRows(groups: groups)) * 22 + CGFloat(max(0, maxRows(groups: groups) - 1)) * 4)
.padding(.vertical, 4)
}
// Match the content width so row reservation agrees with placement.
var availableWidth: CGFloat = 380
private func maxRows(groups: [[Int]]) -> Int {
let widths = Self.groupWidths(groups: groups, width: availableWidth)
return zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.max() ?? 0
}
private func ring(index: Int, maximumDiameter: CGFloat) -> some View {
let kind = kinds[index]
let value = values[index].isFinite ? min(1, max(0, values[index])) : 0
return GeometryReader { proxy in
let diameter = min(maximumDiameter, max(0, proxy.size.width - 4))
ZStack {
Circle().inset(by: 1).stroke(color(kind).opacity(0.16), lineWidth: 2)
Circle().inset(by: 1).trim(from: 0, to: value)
.stroke(color(kind), style: StrokeStyle(lineWidth: 2, lineCap: .round))
.rotationEffect(.degrees(-90))
.animation(reduceMotion ? nil : .easeInOut(duration: min(0.35, interval * 0.8)), value: value)
Text(verbatim: "\(Int((value * 100).rounded()))")
.font(.system(size: 8, weight: .medium)).lineLimit(1).minimumScaleFactor(0.4)
}
.frame(width: diameter, height: diameter)
.frame(width: proxy.size.width, height: proxy.size.height)
}
.help((kind.map { label($0) + Text(verbatim: " · ") } ?? Text("cpu.core")) + Text(verbatim: "\(index + 1): \(MetricFormat.percent(value))"))
.accessibilityLabel(kind.map { label($0) } ?? Text("cpu.core"))
.accessibilityValue("\(index + 1): \(MetricFormat.percent(value))")
}
}
struct CPUCoreGridLayout: Layout {
static let columnSpacing: CGFloat = 6
static let rowSpacing: CGFloat = 5
static let rowHeight: CGFloat = 12
static let preferredCellWidth: CGFloat = 38
static let rowSpacing: CGFloat = 4
static let rowHeight: CGFloat = 22
static let minimumTwoRowCellWidth: CGFloat = 16
static func rowCounts(count: Int, width: CGFloat) -> [Int] {
guard count > 0 else { return [] }
guard count > 1 else { return [1] }
let twoColumns = (count + 1) / 2
let widthForTwoRows = CGFloat(twoColumns) * preferredCellWidth + CGFloat(twoColumns - 1) * columnSpacing
let rows = count > 2 && width < widthForTwoRows ? 3 : 2
let availableForCells = width - CGFloat(twoColumns - 1) * columnSpacing
let twoRowCellWidth = availableForCells / CGFloat(twoColumns)
let rows = count > 2 && twoRowCellWidth < minimumTwoRowCellWidth ? 3 : 2
let quotient = count / rows, remainder = count % rows
return (0..<rows).map { quotient + ($0 < remainder ? 1 : 0) }
}