68 lines
3.2 KiB
Swift
68 lines
3.2 KiB
Swift
import AppKit
|
|
|
|
/// Cache only display-sized pixels; never retain Workspace's original icon representations.
|
|
@MainActor final class ProcessIconCache {
|
|
static let shared = ProcessIconCache(observePressure: true)
|
|
static let pixelLimit = 1_048_576
|
|
private struct Entry {
|
|
let image: NSImage
|
|
let expires: TimeInterval
|
|
let cost: Int
|
|
var access: UInt64
|
|
}
|
|
private var entries: [String: Entry] = [:]
|
|
private var access: UInt64 = 0
|
|
private let resolve: (String) -> NSImage
|
|
private let clock: () -> TimeInterval
|
|
private var pressure: DispatchSourceMemoryPressure?
|
|
private(set) var pixelBytes = 0
|
|
var count: Int { entries.count }
|
|
|
|
init(observePressure: Bool = false, resolve: @escaping (String) -> NSImage = { NSWorkspace.shared.icon(forFile: $0) },
|
|
clock: @escaping () -> TimeInterval = { ProcessInfo.processInfo.systemUptime }) {
|
|
self.resolve = resolve; self.clock = clock
|
|
if observePressure {
|
|
let source = DispatchSource.makeMemoryPressureSource(eventMask: [.warning, .critical], queue: .main)
|
|
source.setEventHandler { [weak self] in Task { @MainActor in self?.removeAll() } }
|
|
source.resume(); pressure = source
|
|
}
|
|
}
|
|
deinit { pressure?.cancel() }
|
|
|
|
func image(for path: String) -> NSImage? {
|
|
let url = URL(fileURLWithPath: path)
|
|
let owner = sequence(first: url, next: { $0.path == "/" ? nil : $0.deletingLastPathComponent() })
|
|
.first { $0.pathExtension == "app" }?.path ?? path
|
|
let now = clock()
|
|
access &+= 1
|
|
if var entry = entries[owner], now < entry.expires {
|
|
entry.access = access; entries[owner] = entry
|
|
return entry.image
|
|
}
|
|
if let old = entries.removeValue(forKey: owner) { pixelBytes -= old.cost }
|
|
guard let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 30, pixelsHigh: 30,
|
|
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
|
|
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0),
|
|
let context = NSGraphicsContext(bitmapImageRep: bitmap) else { return nil }
|
|
let original = resolve(owner)
|
|
NSGraphicsContext.saveGraphicsState()
|
|
NSGraphicsContext.current = context
|
|
context.imageInterpolation = .high
|
|
original.draw(in: NSRect(x: 0, y: 0, width: 30, height: 30), from: .zero, operation: .copy, fraction: 1)
|
|
NSGraphicsContext.restoreGraphicsState()
|
|
bitmap.size = NSSize(width: 15, height: 15)
|
|
let image = NSImage(size: bitmap.size); image.addRepresentation(bitmap)
|
|
let cost = bitmap.bytesPerRow * bitmap.pixelsHigh
|
|
guard cost <= Self.pixelLimit else { return nil }
|
|
entries[owner] = Entry(image: image, expires: now + 30, cost: cost, access: access)
|
|
pixelBytes += cost
|
|
while entries.count > 128 || pixelBytes > Self.pixelLimit {
|
|
guard let oldest = entries.min(by: { $0.value.access < $1.value.access })?.key,
|
|
let removed = entries.removeValue(forKey: oldest) else { break }
|
|
pixelBytes -= removed.cost
|
|
}
|
|
return image
|
|
}
|
|
func removeAll() { entries.removeAll(); pixelBytes = 0 }
|
|
}
|