Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
import AppKit
import Foundation
import Observation
import PeelCore
@Observable
final class InstallerLibrary {
/// How long what a scan saw is shown before the page, appearing or with Peel coming forward, looks again: the
/// shortest time a reason beside a row speaks of.
static let scanAgainAfter: Duration = .seconds(60 * 60)
private(set) var scan: InstallerScan?
/// What the search in the list holds. A page's rows narrow with it, unless it names the page.
var searchText = ""
private let scanning: @Sendable ([InstalledApp], Exclusions) async -> InstallerScan
@ObservationIgnored private var scannedAt: ContinuousClock.Instant?
/// The scan this page runs, which a newer one or the Stop button ends.
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
var isRemoving = false
var selection: InstallerItem.Kind?
var selectedURLs: Set<URL> = []
init(
scanning: @escaping @Sendable ([InstalledApp], Exclusions) async -> InstallerScan = {
await Installers.scan(installedApps: $0, exclusions: $1)
}
) {
self.scanning = scanning
}
var sections: [InstallerItem.Kind] {
InstallerItem.Kind.allCases.filter { !items(in: $0).isEmpty }
}
func items(in kind: InstallerItem.Kind) -> [InstallerItem] {
scan?.items(in: kind) ?? []
}
/// The rows a kind's page shows while the list is searched.
func shownItems(in kind: InstallerItem.Kind) -> [InstallerItem] {
let items = items(in: kind)
guard !searchText.isEmpty, !SearchText.matches(String(localized: kind.title), searchText) else { return items }
return items.filter { $0.matches(searchText) }
}
var needsFullDiskAccess: Bool {
scan?.needsFullDiskAccess ?? false
}
var unreadable: [URL] {
scan?.unreadableLocations ?? []
}
func follow(canUseHelper: Bool) {
let kept = Self.selection(selectedURLs, in: scan?.items ?? [], canUseHelper: canUseHelper)
if kept != selectedURLs { selectedURLs = kept }
}
static func selection(_ selected: Set<URL>, in items: [InstallerItem], canUseHelper: Bool) -> Set<URL> {
selected.subtracting(items.filter { $0.isLocked(canUseHelper: canUseHelper) }.map(\.url))
}
/// Scans unless the last scan ended less than `age` ago, one or a removal runs, or the last scan was stopped: a
/// reason beside a row speaks of the last hour or of what runs now, so what a scan saw ages.
func refreshUnlessScanned(within age: Duration, installedApps: [InstalledApp]) async {
guard !scanRun.isWaitedFor, !isRemoving, !scanRun.wasStopped else { return }
if let scannedAt, ContinuousClock.now - scannedAt < age { return }
await refresh(installedApps: installedApps)
}
/// Takes off the page what has left the disk since the scan, moved by another page's removal or in Finder.
func leaveOutWhatLeft() {
guard let scan, scan.items.contains(where: \.url.isMissing) else { return }
let left = scan.withoutWhatLeft
self.scan = left
selectedURLs.formIntersection(left.items.map(\.url))
if let selection, left.items(in: selection).isEmpty {
self.selection = nil
}
}
func refresh(installedApps: [InstalledApp]) async {
guard
let result = await scanRun.run({ [scanning] in
await scanning(installedApps, ExclusionsStore.shared.exclusions)
})
else { return }
scannedAt = .now
scan = result
selectedURLs.formIntersection(Set(result.items.filter { $0.heldBack?.cannotBeMoved != true }.map(\.url)))
if let selection, result.items(in: selection).isEmpty {
self.selection = nil
}
}
private func selected(in kind: InstallerItem.Kind) -> [InstallerItem] {
items(in: kind).filter { !$0.isReadOnly && selectedURLs.contains($0.url) }
}
}
extension InstallerItem.Kind {
var page: CarriedSelection.Page { Tool.installers.page(rawValue) }
}
extension InstallerLibrary: CarriesSelection {
var carriedParts: [CarriedSelection.Part] {
sections.compactMap { kind in
let selected = selected(in: kind)
guard !selected.isEmpty else { return nil }
return CarriedSelection.Part(
page: kind.page,
title: String(localized: kind.title),
source: kind.title.inEnglish,
sourceKey: "installers.\(kind.rawValue)",
sizes: Dictionary(selected.map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first })
)
}
}
/// An app a selected update is for, while it runs: it may install the update when it quits, and would download it
/// again.
func appToQuit(for part: CarriedSelection.Part) -> String? {
guard let kind = InstallerItem.Kind(rawValue: part.page.scope) else { return nil }
return selected(in: kind).filter { part.sizes.keys.contains($0.url) }.compactMap(\.updatesApp).first { app in
!NSRunningApplication.runningApplications(withBundleIdentifier: app.identifier).isEmpty
}?.name
}
func move(_ part: CarriedSelection.Part, apps: AppLibrary) async -> TrashResult? {
guard let kind = InstallerItem.Kind(rawValue: part.page.scope) else { return TrashResult() }
guard appToQuit(for: part) == nil else { return nil }
let selected = selected(in: kind).filter { part.sizes.keys.contains($0.url) }
let privileged = Set(selected.filter(\.requiresPrivileges).map(\.url))
return await TrashService(exclusions: ExclusionsStore.shared.exclusions)
.trash(selected.map(\.url), usingHelperFor: privileged)
}
func refresh(after parts: [CarriedSelection.Part], apps: AppLibrary) async {
await refresh(installedApps: apps.apps)
}
func choose(_ page: CarriedSelection.Page) {
selection = InstallerItem.Kind(rawValue: page.scope)
}
}
nonisolated extension InstallerItem.Kind {
var title: LocalizedStringResource {
switch self {
case .appInstaller: "App Installers"
case .macOSInstaller: "macOS Installers"
case .firmware: "Device Firmware"
case .deviceBackup: "iPhone and iPad Backups"
case .incompleteDownload: "Unfinished Downloads"
case .updateDownload: "Downloaded Updates"
}
}
var explanation: LocalizedStringResource {
switch self {
case .appInstaller: "Disk images, packages, and archives holding an app or an installer, in Downloads, Desktop, Documents, Public, and Shared, and in the folders directly inside them, where installers are usually left, and the ones apps keep in Application Support or that Parallels Desktop and VMware Fusion keep for making virtual machines. A disk image can also be one you made to keep files in. What an installer installed stays where it is."
case .macOSInstaller: "A full copy of macOS, ready to install. Apple offers the newest release again to any Mac that can run it."
case .firmware: "Firmware for restoring Apple devices, such as an iPhone, an iPad, or a Mac. It is downloaded again when a device needs it. What Apple Configurator keeps stays in its own container, and is deleted in System Settings > General > Storage > iOS Files."
case .deviceBackup: "Almost all of a device’s data and settings, backed up to this Mac. Peel shows what is here. A backup is deleted in System Settings > General > Storage > iOS Files, or in Finder."
case .incompleteDownload: "Downloads never finished, still under the name a file has until its download ends: .crdownload for Chrome and the browsers built on Chromium, .part for Firefox and programs such as torrent clients, and .download for Safari."
case .updateDownload: "Updates an app downloaded with Sparkle, Squirrel, or electron-updater and keeps in its caches until it installs them. If one is gone, the app downloads it again the next time it updates."
}
}
var systemImage: String {
switch self {
case .appInstaller: "arrow.down.app"
case .macOSInstaller: "apple.logo"
case .firmware: "iphone.gen3"
case .deviceBackup: "externaldrive"
case .incompleteDownload: "arrow.down.circle.dotted"
case .updateDownload: "arrow.down.circle"
}
}
}