perf(menubar): 降低绘制驻留并复用原生进程表与缩略图

This commit is contained in:
2026-11-04 10:39:15 +08:00
parent 1c26fc18f3
commit 065c53622a
28 changed files with 964 additions and 160 deletions
+8 -38
View File
@@ -28,34 +28,7 @@ struct ProcessTableView: View {
}
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, sortOrder: $sortOrder) {
TableColumn("PID", sortUsing: ProcessSort(key: .pid)) { row in Text(verbatim: "\(row.id.pid)") }.width(48)
TableColumn("panel.name", sortUsing: ProcessSort(key: .name)) { row in
HStack(spacing: 5) { ProcessIcon(process: row); Text(verbatim: row.name).lineLimit(1) }
}.width(min: 90, ideal: 140, max: 180)
TableColumn("panel.path", sortUsing: ProcessSort(key: .path)) { row in Text(verbatim: row.path ?? "--").lineLimit(1).truncationMode(.middle).foregroundStyle(.secondary) }.width(min: 150, ideal: 400)
TableColumn("CPU %", sortUsing: ProcessSort(key: .cpu)) { row in
Text(verbatim: row.cpu.map { String(format: "%.1f", $0 * 100) } ?? "--")
.frame(maxWidth: .infinity, alignment: .trailing)
}.width(65)
TableColumn("panel.rss", sortUsing: ProcessSort(key: .memory)) { row in Text(verbatim: row.residentBytes.map { String(format: "%.1f MB", Double($0) / 1_000_000) } ?? "--") }.width(85)
TableColumn("panel.threads", sortUsing: ProcessSort(key: .threads)) { row in
HStack { Text(verbatim: row.threads.map(String.init) ?? "--"); if row.restricted { Image(systemName: "lock").accessibilityLabel(Text("panel.restricted")) } }
}.width(65)
}
.contextMenu(forSelectionType: ProcessIdentity.self) { identities in
if let id = identities.first, let row = rows.first(where: { $0.id == id }) {
Button { copy(String(row.id.pid)) } label: { Text("copyPID", tableName: "Process") }
Button { copy(row.name) } label: { Text("copyName", tableName: "Process") }
Button { if let path = row.path { copy(path) } } label: { Text("copyPath", tableName: "Process") }
.disabled(row.path == nil)
}
}
.font(.system(size: 11)).monospacedDigit()
.tableStyle(.inset(alternatesRowBackgrounds: true))
.scrollContentBackground(.hidden)
.transaction { $0.animation = nil }
NativeProcessTable(rows: rows, selection: $selected, sorting: $sortOrder, locale: locale, onExit: handleExit)
.overlay { if snapshot != nil && rows.isEmpty && snapshot?.failed != true { Text("panel.noResults").foregroundStyle(.secondary).allowsHitTesting(false) } }
if exitedSelection != nil, let departed {
HStack { Text(verbatim: departed.name); Text("panel.exited"); Button("panel.dismiss") { selected = nil; self.departed = nil } }
@@ -75,12 +48,13 @@ struct ProcessTableView: View {
do { try await Task.sleep(for: .seconds(3)) } catch { return }
if exitedSelection == 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() }
}
.onExitCommand(perform: handleExit)
}
private func handleExit() {
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 {
Label("panel.processes", systemImage: "list.bullet.rectangle")
@@ -91,10 +65,6 @@ struct ProcessTableView: View {
placeholder: AppLocalization.string("panel.search", locale: locale))
}
private func copy(_ value: String) {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(value, forType: .string)
}
private func rebuild(retainOrder: Bool) {
rows = ProcessTableProjection.rows(snapshot?.rows ?? [], query: search, sorting: sortOrder, order: retainOrder ? rows.map(\.id) : [])
}