Files
Kymo/kymo/Features/MenuBar/CPUCoreGrid.swift
T

117 lines
5.3 KiB
Swift

import SwiftUI
/// Keep cores in two balanced rows when they fit, otherwise use at most three.
struct CPUCoreGrid: View {
let values: [Double]
let kinds: [Int: CPUCoreTopology.Kind]
let interval: TimeInterval
@Environment(\.accessibilityReduceMotion) private var reduceMotion
private func color(_ kind: CPUCoreTopology.Kind?) -> Color {
switch kind {
case .efficiency: Color("MetricCPU")
case .middle: Color("MetricMemory")
case .performance: Color("MetricGPU")
case nil: .secondary
}
}
private func label(_ kind: CPUCoreTopology.Kind) -> Text {
switch kind {
case .efficiency: Text("cpu.kind.efficiency", tableName: "CPUDetails")
case .middle: Text("cpu.kind.performance", tableName: "CPUDetails")
case .performance:
kinds.values.contains(.middle)
? Text("cpu.kind.super", tableName: "CPUDetails")
: Text("cpu.kind.performance", tableName: "CPUDetails")
}
}
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))")
}
}.padding(.vertical, 3)
}
}
struct CPUCoreGridLayout: Layout {
static let columnSpacing: CGFloat = 6
static let rowSpacing: CGFloat = 5
static let rowHeight: CGFloat = 12
static let preferredCellWidth: CGFloat = 38
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 quotient = count / rows, remainder = count % rows
return (0..<rows).map { quotient + ($0 < remainder ? 1 : 0) }
}
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
let width = max(0, proposal.width ?? 400)
let rows = Self.rowCounts(count: subviews.count, width: width).count
return CGSize(width: width, height: rows == 0 ? 0 : CGFloat(rows) * Self.rowHeight + CGFloat(rows - 1) * Self.rowSpacing)
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
let rows = Self.rowCounts(count: subviews.count, width: bounds.width)
guard let columns = rows.max(), columns > 0 else { return }
let spacing = min(Self.columnSpacing, max(0, (bounds.width / CGFloat(columns) - 18) / 2))
let cellWidth = max(0, (bounds.width - CGFloat(columns - 1) * spacing) / CGFloat(columns))
var index = 0
for (row, count) in rows.enumerated() {
for column in 0..<count {
subviews[index].place(at: CGPoint(x: bounds.minX + CGFloat(column) * (cellWidth + spacing),
y: bounds.minY + CGFloat(row) * (Self.rowHeight + Self.rowSpacing)),
anchor: .topLeading, proposal: ProposedViewSize(width: cellWidth, height: Self.rowHeight))
index += 1
}
}
}
}
private struct CPUCoreGridPreviews: View {
let width: CGFloat
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 12) {
ForEach([4, 8, 14, 18, 24, 32, 48], id: \.self) { count in
VStack(alignment: .leading, spacing: 4) {
Text("\(count) cores").font(.caption)
CPUCoreGrid(values: (0..<count).map { Double(($0 * 17) % 90 + 5) / 100 },
kinds: Dictionary(uniqueKeysWithValues: (0..<count).map { ($0, $0 < count / 3 ? .efficiency : .performance) }), interval: 2)
}
}
}.padding().frame(width: width)
}.frame(height: 600)
}
}
#Preview("CPU 核心 · 宽卡片") {
CPUCoreGridPreviews(width: 450)
}
#Preview("CPU 核心 · 窄卡片") {
CPUCoreGridPreviews(width: 300)
}