113 lines
6.4 KiB
Swift
113 lines
6.4 KiB
Swift
import SwiftUI
|
|
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
|
|
@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 || !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) {
|
|
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) }
|
|
// 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
|
|
HStack(spacing: 6) {
|
|
MetricProgressBar(value: (row.cpu ?? 0) / Double(max(1, logicalCPUCount)), color: Color("MetricCPU"))
|
|
.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")) } }
|
|
}.width(65)
|
|
}
|
|
.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 {
|
|
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)
|
|
.modifier(PanelCardBackground(radius: 14))
|
|
.overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(.primary.opacity(0.1)))
|
|
.onAppear { rebuild(retainOrder: false) }
|
|
.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 } }
|
|
.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 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) : [])
|
|
}
|
|
}
|