feat(ui): 实现全卡堆叠趋势与单页资源进程布局
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import SwiftUI
|
||||
|
||||
/// One row per core kind; every equal-width meter represents one real core.
|
||||
/// Core-kind columns contain one vertical segmented meter per real core.
|
||||
struct CPUCoreGrid: View {
|
||||
let values: [Double]
|
||||
let kinds: [Int: CPUCoreTopology.Kind]
|
||||
@@ -40,34 +40,34 @@ struct CPUCoreGrid: View {
|
||||
return order.map { kind in (0..<max(0, count)).filter { kinds[$0] == kind } }.filter { !$0.isEmpty }
|
||||
}
|
||||
|
||||
static let labelWidth: CGFloat = 44
|
||||
|
||||
static func groupWidths(groups: [[Int]], width: CGFloat) -> [CGFloat] {
|
||||
let available = width.isFinite ? max(0, width - labelWidth - 6) : 0
|
||||
return Array(repeating: available, count: groups.count)
|
||||
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)
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack(alignment: .top, spacing: 8) {
|
||||
ForEach(groups.indices, id: \.self) { group in
|
||||
HStack(alignment: .top, spacing: 6) {
|
||||
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(.secondary)
|
||||
}
|
||||
.font(.system(size: 8, weight: .medium))
|
||||
.lineLimit(1).minimumScaleFactor(0.65)
|
||||
.frame(width: Self.labelWidth, height: CPUCoreGridLayout.rowHeight, alignment: .leading)
|
||||
.font(.system(size: 8, weight: .medium)).lineLimit(1).minimumScaleFactor(0.65)
|
||||
.frame(height: 10)
|
||||
CPUCoreGridLayout {
|
||||
ForEach(groups[group], id: \.self) { index in core(index: index) }
|
||||
}
|
||||
.frame(width: widths[group])
|
||||
}
|
||||
}.frame(width: widths[group], alignment: .topLeading)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,15 +78,15 @@ struct CPUCoreGrid: View {
|
||||
|
||||
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 }.reduce(0, +)
|
||||
return CGFloat(rows) * CPUCoreGridLayout.rowHeight + CGFloat(max(0, rows - 1)) * CPUCoreGridLayout.rowSpacing
|
||||
let rows = zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.max() ?? 0
|
||||
return 14 + 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 MetricProgressBar(value: measured ?? 0, color: color(kind), cornerRadius: 2)
|
||||
.frame(height: 8)
|
||||
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)
|
||||
@@ -98,8 +98,8 @@ struct CPUCoreGrid: View {
|
||||
struct CPUCoreGridLayout: Layout {
|
||||
static let columnSpacing: CGFloat = 3
|
||||
static let rowSpacing: CGFloat = 4
|
||||
static let rowHeight: CGFloat = 10
|
||||
static let minimumCellWidth: CGFloat = 8
|
||||
static let rowHeight: CGFloat = 30
|
||||
static let minimumCellWidth: CGFloat = 6
|
||||
|
||||
static func rowCounts(count: Int, width: CGFloat) -> [Int] {
|
||||
guard count > 0 else { return [] }
|
||||
|
||||
Reference in New Issue
Block a user