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.

3.1 KB
Raw Blame
import AppKit
import PeelCore
import SwiftUI

/// The Control-click menu of a row for one file, folder, or app Peel can move, the same on every page.
///
/// Excluding adds the item to the list Settings keeps, and every tool stops offering it; an excluded item's menu
/// takes it off again. An app is added by its identifier, as Settings does, so it stays excluded wherever it moves.
/// Excluding is off while the list can't be read, since adding to it then would start it over and drop what
/// it held, and for anything Settings refuses as too broad.
struct ItemMenu: View {
    @Environment(\.openSettings) private var openSettings
    let url: URL
    /// The identifier of the app itself, when the row is one.
    var appIdentifier: String?
    var isExcluded = false
    /// Opens the row's note, which a keyboard or VoiceOver reaches here, since its button in the row takes no
    /// keyboard focus.
    var showNote: (() -> Void)?

    var body: some View {
        if let showNote {
            Button("Show Note", systemImage: "info.circle", action: showNote)
            Divider()
        }
        Button("Show in Finder", systemImage: "folder") {
            NSWorkspace.shared.activateFileViewerSelecting([url])
        }
        Button("Copy Path", systemImage: "doc.on.doc") {
            NSPasteboard.general.clearContents()
            NSPasteboard.general.setString(url.fullPath, forType: .string)
        }
        Divider()
        if isExcluded {
            wayBack
        } else {
            exclude
        }
    }

    /// Takes off the list exactly what excludes the item. An excluded folder or app around it holds other things too,
    /// so that one is named and left to Settings.
    @ViewBuilder private var wayBack: some View {
        let reasons = ExclusionsStore.shared.exclusions.reasons(excluding: url, app: appIdentifier)
        if let around = reasons.folders.first?.lastPathComponent ?? reasons.apps.first {
            Text("Excluded with “\(around)”")
            Button("Open Exclusions in Settings…", systemImage: "gearshape") {
                SettingsPane.exclusions.open(with: openSettings)
            }
        } else {
            Button("Include in Peel", systemImage: "hand.raised.slash") {
                Task {
                    if let identifier = reasons.identifier {
                        await ExclusionsStore.shared.remove(bundleIdentifiers: [identifier])
                    }
                    await ExclusionsStore.shared.remove(paths: reasons.paths)
                }
            }
            .disabled(ExclusionsStore.shared.exclusions.isUnreadable || reasons.isEmpty)
        }
    }

    private var exclude: some View {
        Button("Exclude from Peel", systemImage: "hand.raised") {
            Task {
                if let appIdentifier {
                    await ExclusionsStore.shared.add(bundleIdentifier: appIdentifier)
                } else {
                    await ExclusionsStore.shared.add(paths: [url])
                }
            }
        }
        .disabled(
            ExclusionsStore.shared.exclusions.isUnreadable || (appIdentifier == nil && ExclusionsStore.isTooBroad(url))
        )
    }
}