29 lines
1.1 KiB
Swift
29 lines
1.1 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
/// Resolves only for visible rows; the task is keyed by process identity and executable path.
|
|
struct ProcessIcon: View {
|
|
let process: ProcessMetrics
|
|
@State private var icon: NSImage?
|
|
|
|
var body: some View {
|
|
Group {
|
|
if let icon {
|
|
Image(nsImage: icon).resizable().interpolation(.high)
|
|
} else {
|
|
Image(systemName: "app.dashed").resizable().scaledToFit().padding(2)
|
|
}
|
|
}
|
|
.frame(width: 15, height: 15)
|
|
.accessibilityHidden(true)
|
|
.task(id: "\(process.id)-\(process.path ?? "")") {
|
|
// Resolve by the sampled executable, avoiding an icon from a reused PID.
|
|
guard let path = process.path else { icon = nil; return }
|
|
let url = URL(fileURLWithPath: path)
|
|
let app = sequence(first: url, next: { $0.path == "/" ? nil : $0.deletingLastPathComponent() })
|
|
.first { $0.pathExtension == "app" }
|
|
icon = NSWorkspace.shared.icon(forFile: (app ?? url).path)
|
|
}
|
|
}
|
|
}
|