Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

4.3 KB
Raw Blame
import AppKit
import PeelCore
import SwiftUI

/// An app's or a file's own icon. Smart Invert leaves it as it is, as it leaves photos, because it shows the
/// item itself rather than a color Peel chose.
struct AppIcon: View {
    let url: URL

    var body: some View {
        Image(nsImage: IconCache.icon(for: url))
            .resizable()
            .aspectRatio(contentMode: .fit)
            .accessibilityIgnoresInvertColors()
            .accessibilityHidden(true)
    }
}

/// Tells what draws an icon that macOS now draws them differently. The icon style in System Settings > Appearance
/// changes every app's icon while Peel runs, and an image made earlier keeps the old one. macOS announces no change, so
/// this follows the settings it keeps the style in.
@Observable
final class IconStyle: NSObject {
    static let shared = IconStyle(defaults: .standard)

    /// The style itself, and the accent color, which a tinted style takes.
    private static let keys = ["AppleIconAppearanceTheme", "AppleAccentColor"]

    private(set) var revision = 0
    @ObservationIgnored private let defaults: UserDefaults

    init(defaults: UserDefaults) {
        self.defaults = defaults
        super.init()
        for key in Self.keys { defaults.addObserver(self, forKeyPath: key, context: nil) }
    }

    isolated deinit {
        for key in Self.keys { defaults.removeObserver(self, forKeyPath: key) }
    }

    nonisolated override func observeValue(
        forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?,
        context: UnsafeMutableRawPointer?
    ) {
        Task { @MainActor in self.changed() }
    }

    private func changed() {
        IconCache.forgetAll()
        revision += 1
    }
}

/// The icons rows draw. `NSWorkspace.icon(forFile:)` doesn't promise to cache and builds a new image each
/// time, and rows ask for icons in `body`, on the main thread, so a list scrolled quickly would build the same
/// icons again and again. `NSCache` holds them and lets them go when memory is short. Apps are held apart from
/// files, so a long list of files never pushes the apps' icons out.
enum IconCache {
    /// "You can add, remove, and query items in the cache from different threads" (`NSCache.h`).
    nonisolated(unsafe) private static let apps = cache()
    nonisolated(unsafe) private static let files = cache()

    nonisolated private static func cache() -> NSCache<NSString, NSImage> {
        let cache = NSCache<NSString, NSImage>()
        cache.countLimit = 512
        return cache
    }

    nonisolated private static func isApp(_ url: URL) -> Bool {
        url.pathExtension.lowercased() == "app"
    }

    nonisolated private static func images(for url: URL) -> NSCache<NSString, NSImage> {
        isApp(url) ? apps : files
    }

    /// An app's icon is asked of `AppInspector`, which asks IconServices only about an app Launch Services knows.
    nonisolated private static func image(for url: URL) -> NSImage {
        isApp(url) ? AppInspector.icon(ofAppAt: url) : NSWorkspace.shared.icon(forFile: url.path(percentEncoded: false))
    }

    @MainActor
    static func icon(for url: URL) -> NSImage {
        _ = IconStyle.shared.revision
        let path = url.path(percentEncoded: false)
        if let image = images(for: url).object(forKey: path as NSString) { return image }
        let image = image(for: url)
        images(for: url).setObject(image, forKey: path as NSString)
        return image
    }

    /// Builds the icons not held yet away from the main thread, so a list finds them ready when it first
    /// scrolls. `icon(forFile:)` may be called from any thread (its documentation).
    @concurrent
    static func warm(_ urls: [URL]) async {
        for url in urls {
            let path = url.path(percentEncoded: false)
            guard images(for: url).object(forKey: path as NSString) == nil else { continue }
            images(for: url).setObject(image(for: url), forKey: path as NSString)
        }
    }

    @MainActor
    static func forgetAll() {
        apps.removeAllObjects()
        files.removeAllObjects()
    }

    /// Icons are held by path, so once what was at a path is removed or replaced, its icon is let go.
    @MainActor
    static func forget(_ urls: some Sequence<URL>) {
        for url in urls {
            images(for: url).removeObject(forKey: url.path(percentEncoded: false) as NSString)
        }
    }
}