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.4 KB
Raw Blame
import AppKit
import SwiftUI

#if DEBUG
/// Renders Home, About and the menu bar panel to PNG files in light and dark, each also with Increase Contrast, when
/// `PEEL_SNAPSHOT` names a folder, then quits. This lets their layouts be checked without a screenshot, in any language
/// Peel runs in. Each view is drawn at the width the app gives it, on the background it has there: About on its paper,
/// the menu bar panel on the paper it draws itself, and Home on the window's plain color, since `ImageRenderer` does
/// not draw glass.
enum HomeSnapshot {
    static let directory = ProcessInfo.processInfo.environment["PEEL_SNAPSHOT"]

    static func runIfRequested() async {
        guard let directory else { return }
        let helper = HelperModel()
        let home = HomeModel(helper: helper)
        let stats = LifetimeStats()
        let found = FoundLastTimeStore()
        let homebrew = HomebrewLibrary()
        let history = RemovalHistoryStore()
        await home.refresh()
        await ExclusionsStore.shared.load()
        let window = Color(nsColor: .windowBackgroundColor)
        do {
            try FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)
            for scheme in [ColorScheme.light, .dark] {
                for contrast in [ColorSchemeContrast.standard, .increased] {
                    let name = (scheme == .light ? "light" : "dark") + (contrast == .increased ? "-contrast" : "")
                    try render(
                        HomeContent().environment(home).environment(helper).environment(stats),
                        width: 360,
                        height: 760,
                        on: window,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/home-content-\(name).png"
                    )
                    // Home again with Show Borders on, for this image only. The stickers then outline their edges.
                    if contrast == .standard {
                        try render(
                            HomeContent().environment(home).environment(helper).environment(stats)
                                .environment(\._accessibilityShowButtonShapes, true),
                            width: 360,
                            height: 760,
                            on: window,
                            scheme: scheme,
                            to: "\(directory)/home-content-borders-\(name).png"
                        )
                    }
                    // The permissions at their widest, and at their narrowest, where Home puts its two halves side
                    // by side.
                    try render(
                        HomePermissionsContent().environment(home).environment(helper)
                            .environment(ExclusionsStore.shared).environment(history),
                        width: 720,
                        on: window,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/home-detail-\(name).png"
                    )
                    try render(
                        HomePermissionsContent().environment(home).environment(helper)
                            .environment(ExclusionsStore.shared).environment(history),
                        width: 480,
                        on: window,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/detail-narrow-\(name).png"
                    )
                    try render(
                        AboutContent().environment(PeelUpdater()).fixedSize(horizontal: false, vertical: true),
                        width: AboutContent.width,
                        on: Album.sheet,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/about-\(name).png"
                    )
                    try render(
                        AboutContent().environment(PeelUpdater(found: "1.0.2"))
                            .fixedSize(horizontal: false, vertical: true),
                        width: AboutContent.width,
                        on: Album.sheet,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/about-update-\(name).png"
                    )
                    try render(
                        MenuBarPanel().environment(AppLibrary()).environment(stats).environment(found)
                            .environment(homebrew).environment(PeelUpdater()),
                        width: 320,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/menu-bar-\(name).png"
                    )
                    try render(
                        MenuBarPanel().environment(AppLibrary()).environment(stats).environment(found)
                            .environment(homebrew).environment(PeelUpdater(found: "1.0.2")),
                        width: 320,
                        scheme: scheme,
                        contrast: contrast,
                        to: "\(directory)/menu-bar-update-\(name).png"
                    )
                }
            }
        } catch {
            print("Home snapshot failed: \(error)")
        }
        NSApp.terminate(nil)
    }

    /// Saves `view` as a PNG at `path`, `width` wide and `height` tall, or as tall as it needs when `height` is nil.
    /// Increase Contrast is set through `_colorSchemeContrast`, the setter SwiftUI keeps for previews.
    private static func render(
        _ view: some View,
        width: CGFloat,
        height: CGFloat? = nil,
        on backdrop: Color = .clear,
        scheme: ColorScheme,
        contrast: ColorSchemeContrast = .standard,
        to path: String
    ) throws {
        let content = view.frame(width: width, height: height).background(backdrop)
            .environment(\.colorScheme, scheme)
            .environment(\._colorSchemeContrast, contrast)
        let renderer = ImageRenderer(content: content)
        renderer.scale = 2
        guard let image = renderer.nsImage, let data = image.tiffRepresentation,
              let png = NSBitmapImageRep(data: data)?.representation(using: .png, properties: [:])
        else { throw CocoaError(.fileWriteUnknown, userInfo: [NSFilePathErrorKey: path]) }
        try png.write(to: URL(filePath: path))
    }
}
#endif