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.

6.0 KB
Raw Blame
import PeelCore
import SwiftUI
import UniformTypeIdentifiers

/// What a scan looks at, in a popover from the toolbar: a change applies to the next scan and costs no results.
struct DuplicateScanForm: View {
    private static let sizes: [Int64] = [100_000, 1_000_000, 10_000_000, 100_000_000]

    @Environment(DuplicateLibrary.self) private var duplicates
    @State private var isChoosingFolders = false
    @State private var rejectedFolders: [DuplicateScan.FolderSetAside] = []
    let onScan: () -> Void

    var body: some View {
        @Bindable var duplicates = duplicates

        VStack(spacing: 0) {
            Form {
                Section {
                    // Only the rows are disabled, so the note beside the Folders heading still opens during a scan.
                    Group {
                        ForEach(duplicates.folders, id: \.self) { folder in
                            FolderRow(folder: folder)
                        }
                        Button {
                            isChoosingFolders = true
                        } label: {
                            Label("Add Folder…", systemImage: "plus")
                                .minimumTarget()
                        }
                        .buttonStyle(.borderless)
                    }
                    .disabled(duplicates.isScanning)
                } header: {
                    heading("Folders", "Peel compares files by their full contents. A folder is a copy only when every file in it matches, hidden ones included. Repositories (Git, Mercurial, Subversion), projects with a build file such as package.json or Cargo.toml, node_modules, and libraries such as Photos and Music are left alone, and an app is never offered on its own. A build file at the top of a folder you chose doesn’t make it a project. A copy with Finder tags, a Finder comment, or a resource fork the others don’t have is listed with them and never moved, a hidden file counts only toward the folder around it, and a copy in a folder that syncs to your other devices is listed and never moved.")
                }

                Section {
                    Picker("Kind", selection: $duplicates.kind) {
                        ForEach(FileKind.allCases, id: \.self) { kind in
                            Text(kind.title).tag(kind)
                        }
                    }
                    Picker("Size", selection: $duplicates.minimumSize) {
                        Text("Any size").tag(Int64(1))
                        ForEach(Self.sizes, id: \.self) { size in
                            Text("At least \(size.byteCount)").tag(size)
                        }
                    }
                }
                .disabled(duplicates.isScanning)
            }
            .formStyle(.grouped)
            HStack {
                Spacer()
                Button("Scan", action: onScan)
                    .buttonStyle(.borderedProminent)
                    .keyboardShortcut(.defaultAction)
                    .disabled(!duplicates.canScan || duplicates.isScanning)
            }
            .padding([.horizontal, .bottom], 20)
        }
        .frame(width: 420)
        // The popover grows with the folders listed up to this height, and the list scrolls past it. `fixedSize`
        // makes it shrink when a folder is removed: a frame with only a maximum keeps any larger height it is offered.
        .frame(maxHeight: 460)
        .fixedSize(horizontal: false, vertical: true)
        .fileImporter(
            isPresented: $isChoosingFolders,
            allowedContentTypes: [.folder],
            allowsMultipleSelection: true
        ) { result in
            guard case .success(let urls) = result else { return }
            Task { rejectedFolders = await duplicates.add(urls) }
        }
        .alert(
            rejectedFolders.count == 1 ? "Peel can’t look for duplicates in this folder." : "Peel can’t look for duplicates in these folders.",
            isPresented: isShowingRejectedFolders
        ) {
            Button("OK", role: .cancel) {}
        } message: {
            Text(verbatim: DuplicateList.reasons(rejectedFolders))
        }
    }

    private var isShowingRejectedFolders: Binding<Bool> {
        Binding(
            get: { !rejectedFolders.isEmpty },
            set: { if !$0 { rejectedFolders = [] } }
        )
    }
}

/// A row for a folder chosen for the scan. It is a separate view so that its display name, which comes from
/// Launch Services, is looked up once per row rather than on every redraw of the form. A folder a scan would set
/// aside, such as a suggested one that syncs to other devices now, says why under its name.
private struct FolderRow: View {
    @Environment(DuplicateLibrary.self) private var duplicates
    @State private var reason: DuplicateFinder.Refusal?
    let folder: URL

    var body: some View {
        let name = FileManager.default.displayName(atPath: folder.path(percentEncoded: false))
        HStack(spacing: 8) {
            AppIcon(url: folder)
                .frame(width: 18, height: 18)
            VStack(alignment: .leading, spacing: 2) {
                Text(verbatim: name)
                    .lineLimit(1)
                    .truncationMode(.middle)
                if let reason {
                    StatusLabel(title: Text(verbatim: reason.explanation))
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }
            }
            Spacer(minLength: 8)
            Button {
                duplicates.folders.removeAll { $0 == folder }
            } label: {
                Label("Remove", systemImage: "minus.circle")
                    .labelStyle(.iconOnly)
                    .minimumTarget()
            }
            .buttonStyle(.plain)
            .foregroundStyle(.secondary)
            .accessibilityLabel(Text("Remove \(name)"))
            .help(Text("Don’t scan this folder"))
        }
        .help(folder.abbreviatedPath)
        .task(id: folder) { reason = await duplicates.reasonToSetAside(folder) }
    }
}