Cotton code
133 lines · 5.5 KB · SwiftRaw
// The slip-list tile: a table's records at tile size as a column of the// table's own slips (`SlipFace`) — the same slip its list draws, one face on// every list (recordface row 1, 21 Oct 2026).//// HOW MANY FIT is the layout's (`SlipTileColumn`): the slips are taken in// order while each stands whole, and what does not fit is counted as "+N// more" beside the table's name — counted from what was placed, never// guessed from a typeface.//// Which records: the list's own order and sections (Today first for Todos),// leaving out what the list reads as finished — a struck slip (a done todo)// is not work for the home screen.import CottonKitimport CottonValuesimport SwiftUI/// Which records a slip-list tile shows, and how many it asks for. Pure.public enum SlipTileLayout {    /// The most slips a size asks its read for: more than a tile holds, so    /// the column fills and the rest are counted.    public static func capacity(_ size: TileSize) -> Int {        size == .large ? SlipTileKnobs.largeCapacity : SlipTileKnobs.capacity    }}/// A slip-list table at tile size: its name with "+N more" when some do/// not fit, and its slips.struct SlipListFace: View {    @Environment(\.theme) private var theme    let definition: TableDefinition    let records: [TableRecord]    let now: Date    /// The tile says its table's name and what it left out; a section's    /// heading says both, so its slips stand alone, as tall as they are.    var named = true    /// Where a tapped slip opens; nil draws the slips inert.    var onOpenRecord: ((TableRecord) -> Void)? = nil    var body: some View {        let shown = records        if shown.isEmpty {            VStack(alignment: .leading, spacing: SlipTileKnobs.nameGap) {                if named { TileHeading(title: definition.plural) }                TileQuietSlip(line: definition.empty?.title ?? ShellLines.line("faces", "tile.empty"))                if named { Spacer(minLength: 0) }            }        } else if named {            // One heading per count the column may land on; it shows the one            // that says what it left out.            SlipTileColumn(rows: shown.count, gap: SlipTileKnobs.rowGap, headingGap: SlipTileKnobs.nameGap) {                ForEach(0...shown.count, id: \.self) { placed in                    TileHeading(title: definition.plural, note: placed < shown.count ? "+\(shown.count - placed) more" : nil)                }                ForEach(shown) { slip($0) }            }        } else {            VStack(alignment: .leading, spacing: SlipTileKnobs.rowGap) {                ForEach(shown) { slip($0) }            }        }    }    @ViewBuilder private func slip(_ record: TableRecord) -> some View {        if let onOpenRecord {            Button { onOpenRecord(record) } label: { SlipFace(definition: definition, record: record, compact: true) }                .buttonStyle(MotionButtonStyle())                .accessibilityIdentifier("tile-\(definition.id)-\(record.id)")        } else {            SlipFace(definition: definition, record: record, compact: true)                .accessibilityIdentifier("tile-\(definition.id)-\(record.id)")        }    }}/// The tile's column: `rows + 1` headings (the one for each count that may/// fit) then the rows. It places the rows that stand whole under the/// heading, then the heading for that count; the rest stand outside the/// tile, which clips them.struct SlipTileColumn: Layout {    let rows: Int    let gap: CGFloat    let headingGap: CGFloat    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {        proposal.replacingUnspecifiedDimensions()    }    /// How many rows stand whole in `height` under a heading `heading` tall.    static func fitting(_ heights: [CGFloat], height: CGFloat, heading: CGFloat, gap: CGFloat, headingGap: CGFloat) -> Int {        var used = heading + headingGap        var placed = 0        for row in heights {            let next = used + (placed == 0 ? 0 : gap) + row            if next > height { break }            used = next            placed += 1        }        return placed    }    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {        let headings = Array(subviews.prefix(rows + 1))        let slips = Array(subviews.dropFirst(rows + 1))        let width = ProposedViewSize(width: bounds.width, height: nil)        let heights = slips.map { $0.sizeThatFits(width).height }        let heading = headings.first?.sizeThatFits(width).height ?? 0        let placed = Self.fitting(heights, height: bounds.height, heading: heading, gap: gap, headingGap: headingGap)        let away = CGPoint(x: bounds.minX, y: bounds.maxY + bounds.height)        for (count, view) in headings.enumerated() {            view.place(at: count == placed ? bounds.origin : away, proposal: width)        }        var y = bounds.minY + heading + headingGap        for (index, view) in slips.enumerated() {            if index < placed {                view.place(at: CGPoint(x: bounds.minX, y: y), proposal: width)                y += heights[index] + gap            } else {                view.place(at: away, proposal: width)            }        }    }}/// The sizes and counts the slip-list tile draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum SlipTileKnobs {    static let capacity = 6    static let largeCapacity = 12    static let nameGap: CGFloat = 8    static let rowGap: CGFloat = 6}