fix(design): 按概念图重排面板层级并验证真实组件

This commit is contained in:
2026-11-02 23:47:12 +08:00
parent ef358794dc
commit 0e8f598fe6
12 changed files with 221 additions and 86 deletions
+82 -43
View File
@@ -24,26 +24,40 @@ struct MenuPanelView: View {
}
var body: some View {
VStack(spacing: 10) {
VStack(spacing: 8) {
header
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 12, alignment: .top), count: columns), alignment: .leading, spacing: 12) {
cpuCard
gpuCard
memoryCard
networkCard
diskCard
batteryCard
}.padding(.trailing, 2)
if columns == 3 && panelSize.height >= 810 {
VStack(spacing: 8) {
HStack(alignment: .top, spacing: 8) {
cpuCard.frame(width: (panelSize.width - 36) * 0.35)
gpuCard.frame(width: (panelSize.width - 36) * 0.33)
memoryCard.frame(width: (panelSize.width - 36) * 0.32)
}.frame(height: panelSize.height - 590)
HStack(alignment: .top, spacing: 8) {
networkCard.frame(width: (panelSize.width - 36) * 0.42)
diskCard.frame(width: (panelSize.width - 36) * 0.27)
batteryCard.frame(width: (panelSize.width - 36) * 0.31)
}.frame(height: 240)
}
} else {
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 8, alignment: .top), count: columns), spacing: 8) {
cpuCard.frame(minHeight: 240)
gpuCard.frame(minHeight: 240)
memoryCard.frame(minHeight: 240)
networkCard.frame(minHeight: 250)
diskCard.frame(minHeight: 250)
batteryCard.frame(minHeight: 250)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
if metrics.isPanelVisible {
ProcessTableView(snapshot: snapshot.processes)
.frame(height: min(300, max(220, panelSize.height * 0.36)))
ProcessTableView(snapshot: snapshot.processes, wide: panelSize.width >= 900, logicalCPUCount: snapshot.cpu?.cores.count ?? 1).frame(maxWidth: .infinity, maxHeight: .infinity)
.frame(minHeight: 170)
}
footer
}
.padding(12)
.padding(10)
.frame(width: panelSize.width, height: panelSize.height)
.background(PanelWindowObserver { size, visible in
if panelSize != size { panelSize = size }
@@ -86,17 +100,23 @@ struct MenuPanelView: View {
chart(\.cpu, color: Color("MetricCPU"))
if let cpu = snapshot.cpu {
HStack { line("cpu.user", MetricFormat.percent(cpu.user)); line("cpu.system", MetricFormat.percent(cpu.system)) }
LazyVGrid(columns: [GridItem(.adaptive(minimum: 28), spacing: 4)], spacing: 4) {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 7), spacing: 5) {
ForEach(Array(cpu.cores.enumerated()), id: \.offset) { index, value in
Text(verbatim: "\(index + 1)").font(.system(size: 9)).frame(maxWidth: .infinity).frame(height: 18)
.background(Color("MetricCPU").opacity(0.12 + value * 0.65), in: .rect(cornerRadius: 3))
.help("\(index + 1): \(MetricFormat.percent(value))")
HStack(spacing: 3) {
Text(verbatim: "\(index + 1)").font(.system(size: 9)).frame(width: 12)
GeometryReader { proxy in
Capsule().fill(Color("MetricCPU").opacity(0.15))
Capsule().fill(Color("MetricCPU")).frame(width: proxy.size.width * value)
}.frame(height: 7)
}.help("\(index + 1): \(MetricFormat.percent(value))")
.accessibilityLabel(Text("cpu.core")).accessibilityValue("\(index + 1): \(MetricFormat.percent(value))")
}
}
}.padding(.vertical, 3)
} else { Text("metric.waiting").foregroundStyle(.secondary) }
line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / "))
line("panel.uptime", String(format: "%.1f h", snapshot.uptime / 3600))
Spacer(minLength: 0)
HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")); Spacer(minLength: 8); Text(verbatim: String(format: "%.1f h", snapshot.uptime / 3600)) }
}
}
private var gpuCard: some View {
@@ -105,17 +125,18 @@ struct MenuPanelView: View {
if snapshot.gpuUsage == nil { Text("gpu.unavailable").foregroundStyle(.secondary) }
line("panel.gpuCores", snapshot.gpu?.coreCount.map(String.init) ?? "--")
Label("panel.gpuPerCore", systemImage: "info.circle").foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
Text("panel.gpuSource").font(.system(size: 10)).foregroundStyle(.secondary)
Spacer(minLength: 0)
Text("panel.gpuSource").font(.system(size: 9)).foregroundStyle(.secondary)
}
}
private var memoryCard: some View {
ResourceCard(title: "metric.memory", symbol: "memorychip", color: Color("MetricMemory"), value: MetricFormat.percent(snapshot.memory?.usage)) {
ResourceCard(title: "metric.memory", symbol: "memorychip", color: Color("MetricMemory"), value: snapshot.memory.map { String(format: "%.1f / %.1f GB", Double($0.used) / 1_000_000_000, Double($0.total) / 1_000_000_000) } ?? "--") {
PanelTrendChart(dates: samples.map(\.date),
first: samples.map { $0.memory.map { $0 * Double(snapshot.memory?.total ?? 1) } },
color: Color("MetricMemory"), fixedScale: Double(snapshot.memory?.total ?? 1),
scaleLabel: MetricFormat.bytes(snapshot.memory?.total))
if let m = snapshot.memory {
line("memory.used", "\(MetricFormat.bytes(m.used)) / \(MetricFormat.bytes(m.total))")
GeometryReader { geometry in
HStack(spacing: 0) {
ForEach(Array([m.used - min(m.used, m.wired + m.compressed), m.wired, m.compressed, m.total - min(m.total, m.used)].enumerated()), id: \.offset) { i, bytes in
@@ -127,7 +148,7 @@ struct MenuPanelView: View {
line("memory.app", MetricFormat.bytes(m.used - min(m.used, m.wired + m.compressed)))
HStack { line("memory.wired", MetricFormat.bytes(m.wired)); line("memory.compressed", MetricFormat.bytes(m.compressed)) }
HStack { line("memory.available", MetricFormat.bytes(m.total - min(m.total, m.used))); line("memory.swap", MetricFormat.bytes(m.swapUsed)) }
HStack { Text("memory.pressure").foregroundStyle(.secondary); Spacer(); pressure(m.pressure) }
HStack { Text("memory.pressure").foregroundStyle(.secondary); Spacer(); pressure(m.pressure); Text(verbatim: MetricFormat.percent(m.usage)).foregroundStyle(Color("MetricMemory")) }
} else { Text("metric.waiting") }
}
}
@@ -140,14 +161,19 @@ struct MenuPanelView: View {
}
}
private var networkCard: some View {
ResourceCard(title: "metric.network", symbol: "network", color: Color("MetricNetwork"), value: MetricFormat.rate(total(\.download))) {
ResourceCard(title: "metric.network", symbol: "network", color: Color("MetricNetwork"), value: "↓ " + MetricFormat.rate(total(\.download)), secondaryValue: "↑ " + MetricFormat.rate(total(\.upload))) {
HStack {
Picker("panel.interface", selection: $selectedInterface) {
Text("panel.totalNetwork").tag("")
ForEach(snapshot.network ?? [], id: \.identity) { item in Text(verbatim: item.name).tag(item.identity) }
}.labelsHidden().controlSize(.small)
}.labelsHidden().controlSize(.mini).frame(maxWidth: 200)
Spacer(minLength: 0)
}
if let item = network.first, let address = item.addresses.first {
Text(verbatim: "\(item.name) · \(address)").font(.system(size: 10)).lineLimit(1).truncationMode(.middle).textSelection(.enabled)
}
PanelTrendChart(dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricNetwork"), fixedScale: nil).id(selectedInterface)
if !network.isEmpty && total(\.download) == nil { Text("panel.rateBaseline").foregroundStyle(.secondary) }
HStack { line("network.download", MetricFormat.rate(total(\.download))); line("network.upload", MetricFormat.rate(total(\.upload))) }
HStack { line("network.received", network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.received })); line("network.sent", network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.sent })) }
switch snapshot.networkPath {
case .available: Text("network.pathAvailable").foregroundStyle(.secondary)
@@ -155,7 +181,10 @@ struct MenuPanelView: View {
case .requiresConnection: Text("network.pathRequiresConnection").foregroundStyle(.secondary)
case nil: Text("metric.waiting")
}
DisclosureGroup("panel.addresses", isExpanded: $showAddresses) {
HStack {
Button("panel.addresses") { showAddresses.toggle() }.popover(isPresented: $showAddresses) {
VStack(alignment: .leading, spacing: 8) {
Group {
ForEach(network, id: \.identity) { item in
VStack(alignment: .leading, spacing: 3) {
Text(verbatim: item.name).fontWeight(.medium)
@@ -171,22 +200,33 @@ struct MenuPanelView: View {
}
}
Text(selectedInterface.isEmpty ? "panel.totalScope" : "panel.interfaceScope").font(.system(size: 9)).foregroundStyle(.secondary)
}.padding(12).frame(width: 350)
}
Spacer()
Image(systemName: "info.circle").help(Text(selectedInterface.isEmpty ? "panel.totalScope" : "panel.interfaceScope"))
}
}
}
private var networkDates: [Date] { selectedInterface.isEmpty ? samples.map(\.date) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.date) }
private var networkDownloads: [Double?] { selectedInterface.isEmpty ? samples.map(\.download) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.download) }
private var networkUploads: [Double?] { selectedInterface.isEmpty ? samples.map(\.upload) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.upload) }
private var diskCard: some View {
ResourceCard(title: "metric.disk", symbol: "internaldrive", color: Color("MetricDisk"), value: MetricFormat.percent(snapshot.disk?.usage)) {
Text("disk.homeVolume").foregroundStyle(.secondary)
line("memory.used", snapshot.disk.map { "\(MetricFormat.bytes($0.total - min($0.total, $0.available))) / \(MetricFormat.bytes($0.total))" } ?? "--")
ResourceCard(title: "metric.disk", symbol: "internaldrive", color: Color("MetricDisk"), value: snapshot.disk.map { String(format: "%.0f / %.0f GB", Double($0.total - min($0.total, $0.available)) / 1_000_000_000, Double($0.total) / 1_000_000_000) } ?? "--") {
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.read), second: samples.map(\.write), color: Color("MetricDisk"), secondColor: Color("MetricMemory"), fixedScale: nil)
Text("disk.physicalIO").foregroundStyle(.secondary)
HStack { line("disk.read", MetricFormat.rate(snapshot.diskIO?.read)); line("disk.write", MetricFormat.rate(snapshot.diskIO?.written)) }
HStack { line("disk.remainingLife", MetricFormat.percent(snapshot.diskHealth?.remainingLife)); line("disk.powerOnHours", snapshot.diskHealth?.powerOnHours.map { "\($0) h" } ?? "--") }
Text("panel.smartScope").font(.system(size: 9)).foregroundStyle(.secondary)
if let date = snapshot.diskHealthDate { Text(date, style: .time).font(.system(size: 9)).foregroundStyle(.secondary) }
DisclosureGroup("disk.volumes", isExpanded: $showVolumes) {
Spacer(minLength: 0)
Button("disk.volumes") { showVolumes.toggle() }.popover(isPresented: $showVolumes) {
VStack(alignment: .leading, spacing: 8) {
Text("disk.homeVolume")
Text("panel.smartScope").foregroundStyle(.secondary)
Group {
ForEach(snapshot.volumes ?? [], id: \.path) { volume in
VStack(alignment: .leading) {
Text(verbatim: volume.name).lineLimit(1).truncationMode(.middle)
@@ -195,6 +235,9 @@ struct MenuPanelView: View {
}
Text("panel.apfsScope").font(.system(size: 9)).foregroundStyle(.secondary)
}
}.padding(12).frame(width: 330)
}
}
}
private var batteryCard: some View {
@@ -211,7 +254,7 @@ struct MenuPanelView: View {
else if let minutes = battery.minutesRemaining { Text(verbatim: "\(minutes) min") }
else { Text("battery.timeUnknown") }
}
if let date = snapshot.batteryHealthDate { Text(date, style: .time).font(.system(size: 9)).foregroundStyle(.secondary) }
} else {
Text(snapshot.batteryState == .absent ? "panel.noBattery" : "panel.batteryFailed").foregroundStyle(.secondary)
}
@@ -219,11 +262,7 @@ struct MenuPanelView: View {
}
private var footer: some View {
HStack(spacing: 8) {
VStack(alignment: .leading, spacing: 2) {
Text(verbatim: "Kymo").font(.system(size: 12, weight: .semibold))
Text(verbatim: "v\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--")")
.font(.system(size: 9)).foregroundStyle(.tertiary)
}
Text(verbatim: "v\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--")").font(.system(size: 10)).foregroundStyle(.secondary)
Spacer()
HStack(spacing: 2) {
Button { keepAwake.toggle() } label: {
@@ -258,7 +297,7 @@ struct MenuPanelView: View {
.glassEffect(.regular, in: .capsule)
}
.controlSize(.small)
.padding(.horizontal, 8).padding(.vertical, 6)
.padding(.horizontal, 2).padding(.vertical, 0)
}
}
@@ -278,7 +317,7 @@ private struct FooterButtonBody<Content: View>: View {
var body: some View {
content()
.frame(width: 34, height: 34)
.frame(width: 26, height: 24)
.background {
Circle().fill(.primary.opacity(isPressed ? 0.16 : (isHovered ? 0.08 : 0)))
}
@@ -35,7 +35,7 @@ struct PanelWindowObserver: NSViewRepresentable {
let chrome = CGSize(width: max(0, window.frame.width - window.contentLayoutRect.width),
height: max(0, window.frame.height - window.contentLayoutRect.height))
let size = PanelLayout.contentSize(visible: frame, chrome: chrome)
let visible = window.isVisible && window.occlusionState.contains(.visible)
let visible = window.isVisible
DispatchQueue.main.async { [weak self, weak window] in
guard let self, let window else { return }
self.update(size, visible)
+42 -19
View File
@@ -3,6 +3,8 @@ import AppKit
struct ProcessTableView: View {
let snapshot: ProcessSnapshot?
var wide: Bool = true
var logicalCPUCount: Int = 1
@State private var search = ""
@State private var byMemory = false
@State private var hovering = false
@@ -11,7 +13,7 @@ struct ProcessTableView: View {
@State private var departed: ProcessMetrics?
@FocusState private var searchFocused: Bool
private var held: Bool { hovering || selected != nil || searchFocused || !search.isEmpty }
private var held: Bool { hovering || selected != nil || !search.isEmpty }
private var selectedRow: ProcessMetrics? { rows.first { $0.id == selected } }
private var exitedSelection: ProcessIdentity? {
guard let selected, snapshot != nil, !rows.contains(where: { $0.id == selected }) else { return nil }
@@ -20,22 +22,18 @@ struct ProcessTableView: View {
var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack {
Label("panel.processes", systemImage: "list.bullet.rectangle").fontWeight(.semibold)
Text(verbatim: "\(snapshot?.readable ?? 0) / \(snapshot?.enumerated ?? 0) · \(rows.count)")
.foregroundStyle(.secondary).help(Text("panel.coverage"))
Spacer(minLength: 0)
if held { Button("panel.liveSort") { selected = nil; rebuild(retainOrder: false) } }
Picker("panel.sort", selection: $byMemory) {
Text("CPU %").tag(false)
Text("panel.rss").tag(true)
}.labelsHidden().frame(width: 100)
}
HStack {
Text("panel.processScope").font(.system(size: 10)).foregroundStyle(.secondary).lineLimit(2)
Spacer(minLength: 4)
TextField("panel.search", text: $search).textFieldStyle(.roundedBorder)
.frame(width: 210).focused($searchFocused)
if wide {
HStack(spacing: 10) {
processTitle
Image(systemName: "info.circle").foregroundStyle(.secondary).help(Text("panel.processScope"))
Spacer(minLength: 0)
if held { Button { selected = nil; rebuild(retainOrder: false) } label: { Image(systemName: "arrow.up.arrow.down") }.help(Text("panel.liveSort")) }
searchField.frame(width: 220)
sorting.frame(width: 160)
}
} else {
HStack { processTitle; Spacer(); sorting.frame(width: 140) }
searchField
}
if snapshot?.failed == true { Text("panel.processFailed") }
else if snapshot == nil { Text("metric.waiting").foregroundStyle(.secondary) }
@@ -44,7 +42,15 @@ struct ProcessTableView: View {
TableColumn("PID") { row in Text(verbatim: "\(row.id.pid)") }.width(48)
TableColumn("panel.name") { row in Text(verbatim: row.name).lineLimit(1) }.width(min: 90, ideal: 140, max: 180)
TableColumn("panel.path") { row in Text(verbatim: row.path ?? "--").lineLimit(1).truncationMode(.middle).foregroundStyle(.secondary) }.width(min: 150, ideal: 400)
TableColumn("CPU %") { row in Text(verbatim: row.cpu.map { String(format: "%.1f", $0 * 100) } ?? "--") }.width(62)
TableColumn("CPU %") { row in
HStack(spacing: 6) {
GeometryReader { proxy in
Capsule().fill(Color("MetricCPU").opacity(0.15))
if let cpu = row.cpu { Capsule().fill(Color("MetricCPU")).frame(width: proxy.size.width * min(1, max(0, cpu / Double(max(1, logicalCPUCount))))) }
}.frame(width: 45, height: 6).accessibilityHidden(true)
Text(verbatim: row.cpu.map { String(format: "%.1f", $0 * 100) } ?? "--").frame(maxWidth: .infinity, alignment: .trailing)
}
}.width(105)
TableColumn("panel.rss") { row in Text(verbatim: row.residentBytes.map { String(format: "%.1f MB", Double($0) / 1_000_000) } ?? "--") }.width(85)
TableColumn("panel.threads") { row in
HStack { Text(verbatim: row.threads.map(String.init) ?? "--"); if row.restricted { Image(systemName: "lock").accessibilityLabel(Text("panel.restricted")) } }
@@ -52,6 +58,7 @@ struct ProcessTableView: View {
}
.font(.system(size: 11)).monospacedDigit()
.tableStyle(.inset(alternatesRowBackgrounds: true))
.scrollContentBackground(.hidden)
.transaction { $0.animation = nil }
.overlay { if snapshot != nil && rows.isEmpty && snapshot?.failed != true { Text("panel.noResults").foregroundStyle(.secondary).allowsHitTesting(false) } }
if let row = selectedRow {
@@ -67,7 +74,7 @@ struct ProcessTableView: View {
.background(Color("PanelCard"), in: .rect(cornerRadius: 14))
.overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(.primary.opacity(0.1)))
.onAppear { rebuild(retainOrder: false) }
.onChange(of: snapshot?.date) { _, _ in rebuild(retainOrder: held) }
.onChange(of: snapshot?.date) { _, _ in rebuild(retainOrder: held && (byMemory ? rows.contains { $0.residentBytes != nil } : rows.contains { $0.cpu != nil })) }
.onChange(of: search) { _, _ in rebuild(retainOrder: held) }
.onChange(of: byMemory) { _, _ in rebuild(retainOrder: false) }
.onChange(of: selected) { _, _ in if let row = selectedRow { departed = row } }
@@ -85,6 +92,22 @@ struct ProcessTableView: View {
else { NSApp.keyWindow?.close() }
}
}
private var processTitle: some View {
HStack(spacing: 8) {
Label("panel.processes", systemImage: "list.bullet.rectangle").fontWeight(.semibold)
Text(verbatim: "\(snapshot?.readable ?? 0) / \(snapshot?.enumerated ?? 0) · \(rows.count)")
.foregroundStyle(.secondary).help(Text("panel.coverage"))
}
}
private var searchField: some View {
TextField("panel.search", text: $search).textFieldStyle(.roundedBorder).focused($searchFocused)
}
private var sorting: some View {
Picker("panel.sort", selection: $byMemory) {
Text("CPU %").tag(false)
Text("panel.rss").tag(true)
}.labelsHidden().pickerStyle(.segmented).controlSize(.small)
}
private func rebuild(retainOrder: Bool) {
rows = ProcessTableProjection.rows(snapshot?.rows ?? [], query: search, byMemory: byMemory, order: retainOrder ? rows.map(\.id) : [])
}