feat(ui): 实现全卡堆叠趋势与单页资源进程布局

This commit is contained in:
2026-11-15 17:59:52 +08:00
parent 469766892d
commit 99e558625b
37 changed files with 579 additions and 195 deletions
+18 -18
View File
@@ -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 [] }
@@ -2,14 +2,10 @@ import AppKit
import SwiftUI
import Observation
enum PanelPage: Hashable { case overview, processes }
@MainActor @Observable final class PanelPresentation {
fileprivate(set) var isOpen = false
var isPinned = false { didSet { onPinChange?(isPinned) } }
var page: PanelPage = .overview { didSet { if oldValue != page { onPageChange?(page) } } }
@ObservationIgnored var onPinChange: ((Bool) -> Void)?
@ObservationIgnored var onPageChange: ((PanelPage) -> Void)?
}
/// Owns only status-item/window behavior. Sampling and settings are injected by App.
@@ -33,7 +29,6 @@ enum PanelPage: Hashable { case overview, processes }
private var preferredContentHeight = PanelLayout.defaultHeight
init<Content: View>(installStatusItem: Bool = true, visibility: @escaping (Bool) -> Void,
pageChanged: @escaping (PanelPage) -> Void = { _ in },
menuTitle: @escaping (String) -> String = { $0 },
openSettings: @escaping () -> Void = {},
@ViewBuilder content: @escaping (PanelPresentation) -> Content) {
@@ -57,7 +52,6 @@ enum PanelPage: Hashable { case overview, processes }
panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
panel.dismiss = { [weak self] in self?.hide() }
presentation.onPinChange = { [weak self] pinned in self?.panel.level = pinned ? .floating : .normal }
presentation.onPageChange = pageChanged
if installStatusItem {
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
item.button?.image = NSImage(named: "BrandMark")
@@ -123,7 +117,6 @@ enum PanelPage: Hashable { case overview, processes }
func show() {
guard !panel.isVisible else { panel.makeKeyAndOrderFront(nil); return }
place()
presentation.page = .overview
presentation.isOpen = true
panel.contentView = content(presentation)
panel.level = presentation.isPinned ? .floating : .normal
@@ -39,7 +39,7 @@ extension MenuPanelView {
second: samples.map(\.cpuUser), third: samples.map(\.cpuSystem),
color: Color("MetricCPU"), secondColor: Color("SeriesUser"), thirdColor: Color("SeriesSystem"),
firstLabel: "gpu.totalUtilization", secondLabel: "cpu.user", thirdLabel: "cpu.system", expectedInterval: chartInterval,
boundaryDates: chartBoundaries, intervals: chartIntervals)
boundaryDates: chartBoundaries, intervals: chartIntervals, stackOrder: [2, 1])
}
) {
HStack(spacing: 5) {
@@ -64,10 +64,12 @@ extension MenuPanelView {
chart: {
PanelTrendChart(
dates: samples.map(\.date), first: samples.map(\.gpu),
second: samples.map(\.gpuRenderer), third: samples.map(\.gpuTiler),
color: Color("MetricGPU"), secondColor: Color("MetricCPU"), thirdColor: Color("SeriesSystem"),
firstLabel: "gpu.totalUtilization", secondLabel: "gpu.renderer", thirdLabel: "gpu.tiler",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
second: samples.map { $0.gpu.map { max(0, 1 - $0) } },
third: samples.map(\.gpuRenderer), fourth: samples.map(\.gpuTiler),
color: Color("MetricGPU"), secondColor: Color("MemoryAvailable"),
thirdColor: Color("MetricCPU"), fourthColor: Color("SeriesSystem"),
firstLabel: "gpu.totalUtilization", secondLabel: "gpu.idle", thirdLabel: "gpu.renderer", fourthLabel: "gpu.tiler",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, stackOrder: [0, 1])
}
) {
HStack(spacing: 5) {
@@ -75,8 +77,6 @@ extension MenuPanelView {
Text(verbatim: String(format: AppLocalization.string("gpu.coreCountFormat", locale: locale), count))
.foregroundStyle(.secondary)
}
inlineMetric("gpu.renderer", MetricFormat.percent(snapshot.gpu?.renderer))
inlineMetric("gpu.tiler", MetricFormat.percent(snapshot.gpu?.tiler))
}.font(.system(size: 9)).lineLimit(1).minimumScaleFactor(0.75).frame(height: 12)
let fans = displayedFans
if !fans.isEmpty {
@@ -85,7 +85,8 @@ extension MenuPanelView {
FanSpeedIndicator(
title: fans.count == 2
? (fan.id == 0 ? Text("gpu.fanLeft") : Text("gpu.fanRight"))
: Text("gpu.fanNumber \(fan.id + 1)"), rpm: fan.rpm)
: Text("gpu.fanNumber \(fan.id + 1)"), rpm: fan.rpm,
isActive: metrics.isPanelVisible && !metrics.isPaused && !metrics.isSystemSleeping && !metrics.isDisplaySleeping)
.frame(height: 12)
}
}
@@ -114,7 +115,7 @@ extension MenuPanelView {
thirdColor: Color("MemoryWired"), fourthColor: Color("MemoryCompressed"), fixedScale: Double(snapshot.memory?.total ?? 1),
firstValueFormat: .memory, firstLabel: "gpu.totalUtilization",
secondLabel: "memory.app", thirdLabel: "memory.wired", fourthLabel: "memory.compressed",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, stackOrder: [1, 2, 3])
}
) {
if let memory = snapshot.memory {
+20 -29
View File
@@ -35,7 +35,7 @@ struct MenuPanelView: View {
let coreCount = max(CPUCoreTopology.cores.count, ProcessInfo.processInfo.processorCount, snapshot.cpu?.cores.count ?? 0)
let groups = CPUCoreGrid.groups(count: coreCount, kinds: CPUCoreTopology.cores)
let widths = CPUCoreGrid.groupWidths(groups: groups, width: cardContentWidth)
let coreRows = zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.reduce(0, +)
let coreRows = zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.max() ?? 0
let fanCount = reservedFanCount > 0 ? max(reservedFanCount, snapshot.fans?.count ?? 0) : snapshot.fans?.count
return PanelLayout.cardHeights(coreRows: coreRows, fanCount: fanCount)
}
@@ -51,17 +51,11 @@ struct MenuPanelView: View {
var body: some View {
VStack(spacing: PanelStyle.gap) {
header
if presentation.page == .overview {
if PanelLayout.needsResourceScroll(size: panelSize, heights: cardHeights) {
ScrollView { resourceGrid }
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
resourceGrid
.frame(maxHeight: .infinity, alignment: .top)
}
} else if presentation.isOpen {
PanelProcessSection(metrics: metrics, state: processState)
if PanelLayout.needsDashboardScroll(size: panelSize, heights: cardHeights) {
ScrollView { dashboard }
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
dashboard.frame(maxHeight: .infinity, alignment: .top)
}
footer
}
@@ -91,6 +85,17 @@ struct MenuPanelView: View {
.modifier(AppearanceStyle(preferences: preferences))
}
private var dashboard: some View {
VStack(spacing: PanelStyle.gap) {
resourceGrid
if presentation.isOpen {
PanelProcessSection(metrics: metrics, state: processState)
.frame(maxWidth: .infinity)
.frame(height: PanelLayout.processHeight(heights: cardHeights))
}
}
}
private var resourceGrid: some View {
let rows = PanelLayout.cardRows(columns: columns)
let heights = PanelLayout.rowHeights(columns: columns, heights: cardHeights)
@@ -130,25 +135,11 @@ struct MenuPanelView: View {
}
}
private var pagePicker: some View {
NeutralTabPicker(title: "panel.page", selection: Binding(get: { presentation.page }, set: { presentation.page = $0 }),
options: [(.overview, "panel.overview"), (.processes, "panel.processes")])
.frame(width: 164)
.accessibilityIdentifier("panel.tabs")
}
private var header: some View {
VStack(spacing: 4) {
HStack(spacing: 8) {
brand
Spacer(minLength: 8)
controls
}
.frame(height: 32)
.overlay {
if panelSize.width >= 560 { pagePicker }
}
if panelSize.width < 560 { pagePicker.frame(maxWidth: .infinity).frame(height: 28) }
HStack(spacing: 8) {
brand
Spacer(minLength: 8)
controls
}
.frame(height: PanelLayout.headerHeight(width: panelSize.width))
.padding(.horizontal, 4)
+16 -10
View File
@@ -9,7 +9,7 @@ nonisolated enum PanelLayout {
static let footerHeight: CGFloat = 26
static let maximumWidth: CGFloat = ResourceCardMetrics.preferredWidth * 3 + 32
static let maximumHeight: CGFloat = 800
static func headerHeight(width: CGFloat) -> CGFloat { width >= 560 ? 32 : 64 }
static func headerHeight(width: CGFloat) -> CGFloat { 32 }
static func columns(width: CGFloat) -> Int { width >= 620 ? 3 : width >= 430 ? 2 : 1 }
static func cardWidth(size: CGSize) -> CGFloat {
let count = columns(width: size.width)
@@ -25,19 +25,21 @@ nonisolated enum PanelLayout {
}
}
/// Foreground details and the edge-to-edge background trend have separate budgets.
/// Structural foreground height determines the full-height background chart.
static func cardHeights(coreRows: Int, fanCount: Int?) -> [PanelCard: CGFloat] {
let base = ResourceCardMetrics.minimumHeight
let fanRows = fanCount.map { (max(0, $0) + 1) / 2 } ?? 1
return [.cpu: base + CGFloat(max(0, coreRows - 2)) * 14,
return [.cpu: base + CGFloat(max(0, coreRows - 1)) * 34,
.gpu: base + CGFloat(max(0, fanRows - 1)) * 14,
.memory: base, .battery: base, .storage: base, .network: base]
}
static func rowHeights(columns: Int, heights: [PanelCard: CGFloat]) -> [CGFloat] {
cardRows(columns: columns).map { cards in
cards.map { heights[$0] ?? 0 }.max() ?? 0
}
Array(repeating: processHeight(heights: heights), count: cardRows(columns: columns).count)
}
static func processHeight(heights: [PanelCard: CGFloat]) -> CGFloat {
heights.values.max() ?? ResourceCardMetrics.minimumHeight
}
static func resourceHeight(columns: Int, heights: [PanelCard: CGFloat]) -> CGFloat {
@@ -45,17 +47,21 @@ nonisolated enum PanelLayout {
return rows.reduce(0, +) + CGFloat(max(0, rows.count - 1)) * 6
}
static func dashboardHeight(columns: Int, heights: [PanelCard: CGFloat]) -> CGFloat {
resourceHeight(columns: columns, heights: heights) + 6 + processHeight(heights: heights)
}
static func preferredHeight(width: CGFloat, heights: [PanelCard: CGFloat]) -> CGFloat {
min(maximumHeight, resourceHeight(columns: columns(width: width), heights: heights)
min(maximumHeight, dashboardHeight(columns: columns(width: width), heights: heights)
+ 20 + headerHeight(width: width) + footerHeight + 12)
}
static var defaultHeight: CGFloat {
preferredHeight(width: maximumWidth, heights: cardHeights(coreRows: 2, fanCount: nil))
preferredHeight(width: maximumWidth, heights: cardHeights(coreRows: 1, fanCount: nil))
}
static func needsResourceScroll(size: CGSize, heights: [PanelCard: CGFloat]) -> Bool {
resourceHeight(columns: columns(width: size.width), heights: heights)
static func needsDashboardScroll(size: CGSize, heights: [PanelCard: CGFloat]) -> Bool {
dashboardHeight(columns: columns(width: size.width), heights: heights)
> size.height - 20 - headerHeight(width: size.width) - footerHeight - 12
}
@@ -58,7 +58,6 @@ import AppKit
mark("hidden10")
for cycle in 1...3 {
controller.show()
if ProcessInfo.processInfo.environment["KYMO_AUDIT_PAGE"] == "processes" { controller.presentation.page = .processes }
mark("open\(cycle).0")
await wait(1); mark("open\(cycle).1")
await wait(9); mark("open\(cycle).10")
@@ -39,11 +39,13 @@ struct ProcessTableView: View {
if snapshot?.failed == true { Text("panel.processFailed") }
else if snapshot == nil { Text("metric.waiting").foregroundStyle(.secondary) }
NativeProcessTable(rows: rows, selection: $state.selected, sorting: $state.sortOrder, locale: locale, onExit: handleExit)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.overlay { if snapshot != nil && rows.isEmpty && snapshot?.failed != true { Text("panel.noResults").foregroundStyle(.secondary).allowsHitTesting(false) } }
if exitedSelection != nil, let departed = state.departed {
HStack { Text(verbatim: departed.name); Text("panel.exited"); Button("panel.dismiss") { state.selected = nil; state.departed = nil } }
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.font(.system(size: 12)).padding(10)
.modifier(CardSurface())
.onAppear { rebuild(retainOrder: false) }