fix(ui): 缩小卡片并还原设计稿紧凑排布
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Keep two balanced rows when possible; add rows to preserve readable core values.
|
||||
/// One row per core kind; every equal-width meter represents one real core.
|
||||
struct CPUCoreGrid: View {
|
||||
let values: [Double]
|
||||
let kinds: [Int: CPUCoreTopology.Kind]
|
||||
@@ -14,8 +14,18 @@ struct CPUCoreGrid: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func label(_ kind: CPUCoreTopology.Kind) -> Text {
|
||||
switch kind {
|
||||
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:
|
||||
@@ -30,80 +40,73 @@ 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 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 }
|
||||
let available = width.isFinite ? max(0, width - labelWidth - 6) : 0
|
||||
return Array(repeating: available, count: groups.count)
|
||||
}
|
||||
|
||||
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) {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
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) }
|
||||
HStack(alignment: .top, spacing: 6) {
|
||||
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: 9, weight: .medium))
|
||||
.lineLimit(1).minimumScaleFactor(0.6)
|
||||
.font(.system(size: 8, weight: .medium))
|
||||
.lineLimit(1).minimumScaleFactor(0.65)
|
||||
.frame(width: Self.labelWidth, height: CPUCoreGridLayout.rowHeight, alignment: .leading)
|
||||
CPUCoreGridLayout {
|
||||
ForEach(groups[group], id: \.self) { index in
|
||||
core(index: index)
|
||||
}
|
||||
ForEach(groups[group], id: \.self) { index in core(index: index) }
|
||||
}
|
||||
}.frame(width: widths[group], alignment: .topLeading)
|
||||
.frame(width: widths[group])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: 15 + CGFloat(maxRows(groups: groups)) * 22 + CGFloat(max(0, maxRows(groups: groups) - 1)) * 4)
|
||||
.padding(.vertical, 1)
|
||||
.frame(height: gridHeight(groups: groups))
|
||||
}
|
||||
|
||||
// Match the content width so row reservation agrees with placement.
|
||||
var availableWidth: CGFloat = 380
|
||||
var availableWidth: CGFloat = 185
|
||||
|
||||
private func maxRows(groups: [[Int]]) -> Int {
|
||||
private func gridHeight(groups: [[Int]]) -> CGFloat {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
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))")
|
||||
let measured = values[index].isFinite ? values[index] : nil
|
||||
return MetricProgressBar(value: measured ?? 0, color: color(kind), cornerRadius: 2)
|
||||
.frame(height: 8)
|
||||
.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 = 6
|
||||
static let columnSpacing: CGFloat = 3
|
||||
static let rowSpacing: CGFloat = 4
|
||||
static let rowHeight: CGFloat = 22
|
||||
static let minimumTwoRowCellWidth: CGFloat = 16
|
||||
static let rowHeight: CGFloat = 10
|
||||
static let minimumCellWidth: CGFloat = 8
|
||||
|
||||
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 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) }
|
||||
}
|
||||
@@ -117,8 +120,8 @@ struct CPUCoreGridLayout: Layout {
|
||||
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))
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user