Cotton code
105 lines · 4.7 KB · SwiftRaw
// Asked of you: the questions only the owner can answer, as slips. Moved out// of Home's bands unchanged; the board draws it above its tiles.import CottonKitimport SwiftUI// MARK: - Asked of you/// **Asked of you** — the questions only the owner can answer, as torn slips/// of dyed paper, three to a row: the count and what it counts, typed. Normally/// fewer than five.////// One slip per SOURCE, each opening the surface where the answer is given. It/// is deliberately not a list of items: four sources with one slip each is a/// glance; fifty items is a second inbox. The paper and the rows are/// `DecideSlips`'; the words are the core's (`shell.today`).////// **No cap, no slice, no "N more"** — `ASKED_KEYS` is four long and/// zero-count rows are dropped, so the band is 0–4 slips structurally, and/// nothing at all when nothing waits.////// **There is no loading state either.** The read answers `[]` while anything/// is in flight, so loading and empty paint identically — a skeleton here would/// be a shape promising slips that may not exist.public struct AskedBand: View {    private let items: [AskedItem]    private let onOpen: (String) -> Void    private let onAnswered: () -> Void    @Environment(\.theme) private var theme    public init(        items: [AskedItem],        onOpen: @escaping (String) -> Void,        onAnswered: @escaping () -> Void = {}    ) {        self.items = items        self.onOpen = onOpen        self.onAnswered = onAnswered    }    public var body: some View {        if !items.isEmpty {            VStack(alignment: .leading, spacing: AskedBandKnobs.boardAskedBandSpacing) {                PaperSection(title: ShellLines.line("board", "asked.title"))                // A row that asks tile by tile is answered in place: its                // tiles, never a slip that opens a list of them. A person                // asking to join is a card in their own database; an arrival                // is answered in the window's.                ForEach(items.filter { !$0.tiles.isEmpty }) { item in                    ForEach(item.tiles) { tile in                        ArrivalTile(tile: tile, onAnswered: onAnswered)                    }                }                slips(items.filter { $0.tiles.isEmpty })            }            .accessibilityIdentifier("home-asked")        }    }    /// A `Grid`, not a lazy one: the slips in a row share the row's height, so    /// a sentence that wraps to three lines does not leave its neighbours short.    private func slips(_ items: [AskedItem]) -> some View {        let rows = DecideSlips.rows(DecideSlips.slips(items), columns: AskedBandKnobs.boardAskedBandColumns)        return Grid(horizontalSpacing: RecordListSurfaceKnobs.paperGap, verticalSpacing: RecordListSurfaceKnobs.paperGap) {            ForEach(Array(rows.enumerated()), id: \.offset) { _, row in                GridRow {                    ForEach(row) { slip in                        Button(action: { onOpen(slip.path) }) {                            Paper(dye: slip.dye, edge: .torn, seed: "decide-\(slip.key.rawValue)", fills: true) {                                Text(slip.label)                                    .paperBody(theme.scale.bodySize)                                    .lineLimit(AskedBandKnobs.boardAskedBandMaxLines)                                    .fixedSize(horizontal: false, vertical: true)                                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)                            }                            .frame(minHeight: AskedBandKnobs.boardAskedBandMinHeight)                            .frame(maxWidth: .infinity, maxHeight: .infinity)                        }                        .buttonStyle(MotionButtonStyle())                        .accessibilityLabel(slip.label)                        .accessibilityIdentifier("home-asked-\(slip.key.rawValue)")                        .modifier(UnreadUnderTheFan())                    }                    // A short last row keeps the slips their width: the empty                    // cells are air, not stretched paper.                    ForEach(0..<(DecideSlips.width(of: rows) - row.count), id: \.self) { _ in                        Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])                    }                }            }        }    }}/// The sizes, counts and timings AskedBand draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum AskedBandKnobs {    static let boardAskedBandColumns = 3    static let boardAskedBandMaxLines = 4    static let boardAskedBandMinHeight: CGFloat = 70    static let boardAskedBandSpacing: CGFloat = 14}