Cotton code
56 lines · 2.3 KB · SwiftRaw
// The TOOLS deck (row B of the deck faces): the Tools card on the dial opens// a deck with one card per installed tool, never a grid or a list, each// drawn by CottonFaces' `ToolFace` with its tool's object (`ToolObject`) and// what its home table holds, read here. A tap opens the tool; a hold lifts// the card with its acts.import CottonPaperimport CottonValuesimport SwiftUI/// Every installed tool as a deck of `ToolFace` cards, in the person's order.struct ToolsDeck: View {    let tiles: [GridTile]    let open: (GridTile) -> Void    @Environment(\.records) private var store    @Environment(\.tableDefinitions) private var definitions    /// Each tool's home-table figures, by tool key, as they are read.    @State private var figures: [String: String] = [:]    var body: some View {        CardDeck(            items: tiles,            open: open,            acts: { tile in                [LiftedAct(id: "open", word: ShellLines.line("faces", "link.open", ["name": tile.name]), object: nil, destroys: false) { open(tile) }]            },            words: { tile in [tile.name, purpose(of: tile)].compactMap { $0 }.joined(separator: " ") }        ) { tile in            ToolFace(name: tile.name, purpose: purpose(of: tile), figure: figures[tile.key]) { side in                ToolObject(name: tile.name, picture: tile.picture, printing: .contentOnly, side: side)            }        }        .task(id: tiles.map(\.key)) { await readFigures() }    }    private func purpose(of tile: GridTile) -> String? {        guard case .dataTool(let tool) = tile.opens else { return nil }        return tool.purpose?.trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty    }    /// Each tool's home table summed up in the core's words, one at a time.    private func readFigures() async {        guard let store else { return }        for tile in tiles {            guard figures[tile.key] == nil, case .dataTool(let tool) = tile.opens,                  let home = BoardLayout.home(for: tool, in: definitions),                  let read = try? await store.summarize(table: home.id),                  let line = SummaryFigure.line(read, in: home) else { continue }            figures[tile.key] = line        }    }}private extension String {    var nilIfEmpty: String? { isEmpty ? nil : self }}