// A table's tile: its face at tile size — what the board shows for a tool.// The definition's tile// layout names the face; the size decides how many records it holds. The slip// list is a column of the table's own slips (`SlipListFace`), as many as// stand whole, the rest counted.importCottonValuesimportSwiftUIpublicstructBoardTile: View { @Environment(\.theme) privatevar themeprivatelet definition: TableDefinitionprivatelet records: [TableRecord]privatelet size: TileSizeprivatelet now: Dateprivatelet onOpenRecord: ((TableRecord) -> Void)?/// The rounded box is the TILE's frame. A section on the database page/// is the same face with no frame: the page is its frame, the section's/// heading names it, and it is as tall as what it holds.privatelet framed: Boolpublicinit(definition: TableDefinition, records: [TableRecord], size: TileSize, now: Date = Date(), framed: Bool = true, onOpenRecord: ((TableRecord) -> Void)? = nil) {self.framed = framedself.definition = definitionself.records = recordsself.size = sizeself.now = nowself.onOpenRecord = onOpenRecord }/// How many records a face shows at a size, by the theme's counts.publicstaticfunc capacity(_ face: TileFace, _ size: TileSize, scale: ThemeScale) -> Int {switch (face, size) {// More slips than the tile holds: its column places those that stand// whole and counts the rest as "+N more" (`SlipTileColumn`).case (.slipList, _): returnSlipTileLayout.capacity(size)// One row of covers on the small and medium canvases, two on the large.case (.coverGrid, .small), (.coverGrid, .medium): return coverColumns(size, scale: scale)case (.coverGrid, .large): return coverColumns(size, scale: scale) * BoardTileKnobs.faceBoardTileCoverRowsLargecase (.polaroidGrid, .small), (.capGrid, .small): returnBoardTileKnobs.faceBoardTileGridCapacitySmallcase (.polaroidGrid, .medium), (.capGrid, .medium): returnBoardTileKnobs.faceBoardTileGridCapacityMediumcase (.polaroidGrid, .large), (.capGrid, .large): returnBoardTileKnobs.faceBoardTileGridCapacityLargecase (.sectionList, _): returnSectionFace.showncase (.receipt, _): return1case (.cardGrid, _), (.month, _), (.sparkline, _), (.chart, _): return0 } }/// Columns of a grid face at a size.staticfunc columns(_ size: TileSize, scale: ThemeScale) -> Int { size == .small ? BoardTileKnobs.faceBoardTileGridColumnsSmall : BoardTileKnobs.faceBoardTileGridColumns }/// Paperback covers across at a size: as many as fit the canvas's height/// under the tile's name — five on the short medium canvas, four on the/// large one's two rows.staticfunc coverColumns(_ size: TileSize, scale: ThemeScale) -> Int {switch size {case .small: returnBoardTileKnobs.faceBoardTileCoverColumnsSmallcase .medium: returnBoardTileKnobs.faceBoardTileCoverColumnsMediumcase .large: returnBoardTileKnobs.faceBoardTileCoverColumnsLarge } }publicvar body: someView {let shown = Array(records.prefix(Self.capacity(definition.tile.face, size, scale: theme.scale)))// The slip list stands every record the core chose for it// (`shell.list {tile}`), so "+N more" counts what it left out.let drawn = face(definition.tile.face == .slipList ? records : shown)returnGroup {if framed { drawn .padding(TileMetrics.padding) .frame(width: size.canvas.width, height: size.canvas.height, alignment: .topLeading) .background(PaperSurface(dye: .cream, edge: .cut, seed: definition.id)) .clipShape(RoundedRectangle(cornerRadius: BoardTileKnobs.faceTileRadius, style: .continuous)) } else { drawn.frame(maxWidth: .infinity, alignment: .topLeading) } } .accessibilityElement(children: .contain) .accessibilityLabel(definition.plural) } @ViewBuilder privatefunc face(_ shown: [TableRecord]) -> someView {switch definition.tile.face {case .slipList:SlipListFace(definition: definition, records: shown, now: now, named: framed, onOpenRecord: onOpenRecord)case .coverGrid:let count = Self.coverColumns(size, scale: theme.scale)let gap = BoardTileKnobs.faceTileGridGaplet width = (size.canvas.width - TileMetrics.padding * 2 - CGFloat(count - 1) * gap) / CGFloat(count) VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) {if framed { name }LazyVGrid(columns: Array(repeating: GridItem(.fixed(width), spacing: gap, alignment: .top), count: count), alignment: .leading, spacing: gap) {ForEach(shown) { record in opening(record) { CoverFace(definition: definition, record: record, width: width) } } } }case .polaroidGrid, .capGrid:let count = Self.columns(size, scale: theme.scale)let gap = BoardTileKnobs.faceTileGridGaplet width = (size.canvas.width - TileMetrics.padding * 2 - CGFloat(count - 1) * gap) / CGFloat(count)// Offered the page's whole width (unframed), the cells start under// the heading rather than centred in the width.LazyVGrid(columns: Array(repeating: GridItem(.fixed(width), spacing: gap), count: count), alignment: .leading, spacing: gap) {ForEach(shown) { record in opening(record) {if definition.tile.face == .capGrid {CapFace(definition: definition, record: record, side: width) } else {PolaroidFace(definition: definition, record: record, width: width) } } } }case .cardGrid:CardFace(definition: definition, now: now)case .month:MonthFace(definition: definition, records: records, now: now)case .sectionList:SectionFace(definition: definition, records: records, headed: framed)case .receipt:ForEach(shown) { record in opening(record) { ReceiptFace(definition: definition, record: record) } }case .sparkline: VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) {if framed { name }SparklineFace(definition: definition) }case .chart: VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) {if framed { name }ChartFace(definition: definition, now: now) } } }/// The faces drawn one record to an object, each of which opens ITS/// record when the tile is given `onOpenRecord` (S1 extended); the others/// (a month, a card, a section list) are the tool as a whole.staticfunc opensRecords(_ face: TileFace) -> Bool {switch face {case .coverGrid, .slipList, .polaroidGrid, .capGrid, .receipt: returntruecase .cardGrid, .month, .sectionList, .sparkline, .chart: returnfalse } }/// One record's object: a button opening that record when the tile opens/// records, else the face alone (a preview). @ViewBuilder privatefunc opening(_ record: TableRecord, @ViewBuilder face: () -> someView) -> someView {iflet onOpenRecord {Button { onOpenRecord(record) } label: { face().contentShape(Rectangle()) } .buttonStyle(MotionButtonStyle()) .accessibilityIdentifier("tile-\(definition.id)-\(record.id)") .modifier(UnreadUnderTheFan()) } else { face().modifier(UnreadUnderTheFan()) } }/// The table's name over a list or a grid of covers.privatevar name: someView {Text(definition.plural) .font(theme.type.title(size == .small ? BoardTileKnobs.faceBoardTileNameSmallSize : BoardTileKnobs.faceBoardTileNameSize)) .foregroundStyle(theme.print(.ink)) .lineLimit(1) }}/// The sizes, counts and timings BoardTile draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enumBoardTileKnobs {staticlet faceBoardTileCoverColumnsLarge = 4staticlet faceBoardTileCoverColumnsMedium = 5staticlet faceBoardTileCoverColumnsSmall = 2staticlet faceBoardTileCoverRowsLarge = 2staticlet faceBoardTileGridCapacityLarge = 8staticlet faceBoardTileGridCapacityMedium = 4staticlet faceBoardTileGridCapacitySmall = 4staticlet faceBoardTileGridColumns = 4staticlet faceBoardTileGridColumnsSmall = 2staticlet faceBoardTileNameSize: CGFloat = 15staticlet faceBoardTileNameSmallSize: CGFloat = 16staticlet faceTileContentGap: CGFloat = 8staticlet faceTileGridGap: CGFloat = 8staticlet faceTileRadius: CGFloat = 22}