import SwiftUI /// Keep two balanced rows when possible; add rows to preserve readable core values. struct CPUCoreGrid: View { let values: [Double] let kinds: [Int: CPUCoreTopology.Kind] let interval: TimeInterval 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") } } static func groups(count: Int, kinds: [Int: CPUCoreTopology.Kind]) -> [[Int]] { let order: [CPUCoreTopology.Kind?] = [.efficiency, .middle, .performance, nil] return order.map { kind in (0.. [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 { 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: 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 core(index: index) } } }.frame(width: widths[group], alignment: .topLeading) } } } .frame(height: 15 + CGFloat(maxRows(groups: groups)) * 22 + CGFloat(max(0, maxRows(groups: groups) - 1)) * 4) .padding(.vertical, 1) } // 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 core(index: Int) -> some View { let kind = kinds[index] let value = values[index].isFinite ? min(1, max(0, values[index])) : 0 return VStack(spacing: 3) { Text(verbatim: "\(Int((value * 100).rounded()))") .font(.system(size: 8, weight: .medium)).monospacedDigit().lineLimit(1) MetricProgressBar(value: value, color: color(kind)).frame(height: 3) } .frame(maxWidth: .infinity, maxHeight: .infinity) .help(Text("\(kind.map { label($0) } ?? Text("cpu.core")) · \(index + 1): \(MetricFormat.percent(value))")) .accessibilityElement(children: .ignore) .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 = 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 usableWidth = width.isFinite ? max(0, width) : 0 let capacity = max(1, Int(min(CGFloat(count), (usableWidth + columnSpacing) / (minimumTwoRowCellWidth + columnSpacing)))) let rows = max(2, (count + capacity - 1) / capacity) let quotient = count / rows, remainder = count % rows return (0.. 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..