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.

2.3 KB
Raw Blame
import PeelCore
import SwiftUI

/// Buttons together at the right edge, the last, the page's main action, at the edge itself, as `ActionRowPlan` says.
struct ActionRow: Layout {
    func makeCache(subviews: Subviews) -> [CGSize] {
        subviews.map { $0.sizeThatFits(.unspecified) }
    }

    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout [CGSize]) -> CGSize {
        plan(subviews, cache, width: proposal.width).size
    }

    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout [CGSize]) {
        for (subview, frame) in zip(subviews, plan(subviews, cache, width: bounds.width).frames) {
            subview.place(
                at: CGPoint(x: bounds.minX + frame.minX, y: bounds.minY + frame.minY),
                proposal: ProposedViewSize(frame.size)
            )
        }
    }

    /// What is wider than the row, such as a sentence beside a button, is asked how tall it gets at the row's width.
    private func plan(_ subviews: Subviews, _ sizes: [CGSize], width: CGFloat?) -> ActionRowPlan {
        let fitted = zip(subviews, sizes).map { subview, size in
            guard let width, size.width > width else { return size }
            return subview.sizeThatFits(ProposedViewSize(width: width, height: nil))
        }
        let leading = subviews.map { $0[LeadsActionRow.self] }
        return ActionRowPlan(sizes: fitted, width: width, leading: leading)
    }
}

nonisolated private struct LeadsActionRow: LayoutValueKey {
    static let defaultValue = false
}

extension View {
    /// Stands at the left edge of its `ActionRow`: a button that keeps what the page is about, such as Start or
    /// Disable, apart from the one that removes it.
    func leadsActionRow() -> some View {
        layoutValue(key: LeadsActionRow.self, value: true)
    }
}

/// The buttons of a page, under its last group and outside any box, as System Settings has them under a list of
/// accounts. An action that concerns one row stays at that row's right end.
struct PageActions<Buttons: View>: View {
    @ViewBuilder let buttons: Buttons

    var body: some View {
        // A grouped form keeps the box around a section's rows whatever their background, and none around a footer.
        Section {
        } footer: {
            ActionRow { buttons }
        }
    }
}