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

161 lines
7.5 KiB
Swift

import SwiftUI
/// Core-kind columns contain one vertical segmented meter per real core.
struct CPUCoreGrid: View {
let values: [Double]
let kinds: [Int: CPUCoreTopology.Kind]
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, compact: Bool = false) -> Text {
if compact {
switch kind {
case .efficiency: return Text("cpu.kind.efficiencyShort", tableName: "CPUDetails")
case .middle: return Text("cpu.kind.performanceShort", tableName: "CPUDetails")
case .performance:
return kinds.values.contains(.middle)
? Text("cpu.kind.superShort", tableName: "CPUDetails")
: Text("cpu.kind.performanceShort", tableName: "CPUDetails")
}
}
return 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")
}
}
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] {
guard !groups.isEmpty else { return [] }
let available = width.isFinite ? max(0, width - CGFloat(groups.count - 1) * 8) : 0
let minimum = min(44, available / CGFloat(groups.count))
let remaining = max(0, available - minimum * CGFloat(groups.count))
let total = max(1, groups.reduce(0) { $0 + $1.count })
return groups.map { minimum + remaining * CGFloat($0.count) / CGFloat(total) }
}
var body: some View {
let groups = Self.groups(count: values.count, kinds: kinds)
GeometryReader { geometry in
let widths = Self.groupWidths(groups: groups, width: geometry.size.width)
HStack(alignment: .top, spacing: 8) {
ForEach(groups.indices, id: \.self) { group in
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 2) {
Circle().fill(color(kinds[groups[group][0]])).frame(width: 3, height: 3)
if let kind = kinds[groups[group][0]] { label(kind, compact: true) }
else { Text("cpu.core") }
Text(verbatim: "\(groups[group].count)").foregroundStyle(.primary)
}
.font(.system(size: 9, weight: .medium)).lineLimit(1).minimumScaleFactor(0.65)
.frame(height: 12)
CPUCoreGridLayout {
ForEach(groups[group], id: \.self) { index in core(index: index) }
}
}.frame(width: widths[group], alignment: .topLeading)
}
}
}
.frame(height: gridHeight(groups: groups))
}
var availableWidth: CGFloat = 185
private func gridHeight(groups: [[Int]]) -> CGFloat {
let widths = Self.groupWidths(groups: groups, width: availableWidth)
let rows = zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.max() ?? 0
return 16 + CGFloat(rows) * CPUCoreGridLayout.rowHeight + CGFloat(max(0, rows - 1)) * CPUCoreGridLayout.rowSpacing
}
private func core(index: Int) -> some View {
let kind = kinds[index]
let measured = values[index].isFinite ? values[index] : nil
return SegmentedCoreBar(value: measured, color: color(kind))
.frame(height: 30)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.help(Text("\(kind.map { label($0) } ?? Text("cpu.core")) · \(index + 1): \(MetricFormat.percent(measured))"))
.accessibilityElement(children: .ignore)
.accessibilityLabel(kind.map { label($0) } ?? Text("cpu.core"))
.accessibilityValue("\(index + 1): \(MetricFormat.percent(measured))")
}
}
struct CPUCoreGridLayout: Layout {
static let columnSpacing: CGFloat = 3
static let rowSpacing: CGFloat = 4
static let rowHeight: CGFloat = 30
static let minimumCellWidth: CGFloat = 6
static func rowCounts(count: Int, width: CGFloat) -> [Int] {
guard count > 0 else { return [] }
guard count > 1 else { return [1] }
let usableWidth = width.isFinite ? max(0, width) : 0
let capacity = max(1, Int(min(CGFloat(count), (usableWidth + columnSpacing) / (minimumCellWidth + columnSpacing))))
let rows = max(1, (count + capacity - 1) / capacity)
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) - Self.minimumCellWidth) / 2))
let cellWidth = min(Self.minimumCellWidth, 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) }))
}
}
}.padding().frame(width: width)
}.frame(height: 600)
}
}
#Preview("CPU 核心 · 宽卡片") {
CPUCoreGridPreviews(width: 450)
}
#Preview("CPU 核心 · 窄卡片") {
CPUCoreGridPreviews(width: 300)
}