feat(menubar): 实现统一资源面板与独立滚动进程表
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
|
||||
struct ProcessTableView: View {
|
||||
let snapshot: ProcessSnapshot?
|
||||
@State private var search = ""
|
||||
@State private var byMemory = false
|
||||
@State private var hovering = false
|
||||
@State private var selected: ProcessIdentity?
|
||||
@State private var rows: [ProcessMetrics] = []
|
||||
@State private var departed: ProcessMetrics?
|
||||
@FocusState private var searchFocused: Bool
|
||||
|
||||
private var held: Bool { hovering || selected != nil || searchFocused || !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 }
|
||||
return selected
|
||||
}
|
||||
|
||||
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 snapshot?.failed == true { Text("panel.processFailed") }
|
||||
else if snapshot == nil { Text("metric.waiting").foregroundStyle(.secondary) }
|
||||
// Native Table owns its vertical/horizontal scrolling and recycles visible cells.
|
||||
Table(rows, selection: $selected) {
|
||||
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("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")) } }
|
||||
}.width(65)
|
||||
}
|
||||
.font(.system(size: 11)).monospacedDigit()
|
||||
.tableStyle(.inset(alternatesRowBackgrounds: true))
|
||||
.transaction { $0.animation = nil }
|
||||
.overlay { if snapshot != nil && rows.isEmpty && snapshot?.failed != true { Text("panel.noResults").foregroundStyle(.secondary).allowsHitTesting(false) } }
|
||||
if let row = selectedRow {
|
||||
HStack(alignment: .top) {
|
||||
Text(verbatim: row.path ?? "--").textSelection(.enabled).lineLimit(2).frame(maxWidth: .infinity, alignment: .leading)
|
||||
if let path = row.path { Button("panel.copy") { NSPasteboard.general.clearContents(); NSPasteboard.general.setString(path, forType: .string) } }
|
||||
}.font(.system(size: 11))
|
||||
} else if exitedSelection != nil, let departed {
|
||||
HStack { Text(verbatim: departed.name); Text("panel.exited"); Button("panel.dismiss") { selected = nil; self.departed = nil } }
|
||||
}
|
||||
}
|
||||
.font(.system(size: 12)).padding(10)
|
||||
.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: search) { _, _ in rebuild(retainOrder: held) }
|
||||
.onChange(of: byMemory) { _, _ in rebuild(retainOrder: false) }
|
||||
.onChange(of: selected) { _, _ in if let row = selectedRow { departed = row } }
|
||||
.onHover { hovering = $0; if !$0 && !held { rebuild(retainOrder: false) } }
|
||||
.onChange(of: searchFocused) { _, value in if !value && !held { rebuild(retainOrder: false) } }
|
||||
.task(id: exitedSelection) {
|
||||
guard let id = exitedSelection else { return }
|
||||
do { try await Task.sleep(for: .seconds(3)) } catch { return }
|
||||
if selected == id { selected = nil; departed = nil }
|
||||
}
|
||||
.onExitCommand {
|
||||
if !search.isEmpty { search = "" }
|
||||
else if selected != nil { selected = nil; departed = nil }
|
||||
else if searchFocused { searchFocused = false }
|
||||
else { NSApp.keyWindow?.close() }
|
||||
}
|
||||
}
|
||||
private func rebuild(retainOrder: Bool) {
|
||||
rows = ProcessTableProjection.rows(snapshot?.rows ?? [], query: search, byMemory: byMemory, order: retainOrder ? rows.map(\.id) : [])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user