Cotton code
68 lines · 2.6 KB · SwiftRaw
// The grid: how cards are laid on a page.//// One grid, one card size (the owner and the architect, r6, 13 Oct 2026): a// tray deals its act cards into one grid, three to a row, all the same size,// and a form's acts sit in the same grid. There is no strip of thumb-size// cards and no sideways act row. The layout gives every card its frame, so a// card never measures its own width, and every card on one surface is one// width.import SwiftUIstruct CardRows: Layout {    var columns: Int    var spacing: CGFloat    /// The card's width against its height (`CardMetrics.aspect`).    var aspect: CGFloat    /// One card's width in a container this wide.    func cardWidth(in width: CGFloat) -> CGFloat {        max(0, (width - spacing * CGFloat(columns - 1)) / CGFloat(columns))    }    /// The cards row by row, `columns` to a row, in order.    private func rows(_ subviews: Subviews) -> [[Int]] {        stride(from: 0, to: subviews.count, by: max(1, columns)).map { start in            Array(start..<min(start + columns, subviews.count))        }    }    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {        let width = proposal.width ?? 0        let count = CGFloat(rows(subviews).count)        guard count > 0, width > 0 else { return CGSize(width: width, height: 0) }        let height = cardWidth(in: width) / aspect        return CGSize(width: width, height: count * height + (count - 1) * spacing)    }    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {        let card = cardWidth(in: bounds.width)        let height = card / aspect        for (row, indices) in rows(subviews).enumerated() {            let y = bounds.minY + CGFloat(row) * (height + spacing)            for (column, index) in indices.enumerated() {                let size = CGSize(width: card, height: height)                subviews[index].place(                    at: CGPoint(x: bounds.minX + CGFloat(column) * (card + spacing), y: y),                    proposal: ProposedViewSize(size)                )            }        }    }}extension CardRows {    /// The grid of every page and tray, in a theme: `card_hand_columns` to a    /// row, `card_hand_gap` apart.    init(scale: ThemeScale) {        self.init(columns: HandKnobs.cardHandColumns, spacing: HandKnobs.cardHandGap, aspect: ActCardKnobs.cardAspect)    }}/// The sizes, counts and timings Hand draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum HandKnobs {    static let cardHandColumns = 3    static let cardHandGap: CGFloat = 12}