Cotton code
130 lines · 5.8 KB · SwiftRaw
// FULL SCREEN IS THE THING (owner, 26 Oct 2026), not the deck's card a// little larger: the card is the thing's tile in the deck. A pinch out on a// card, or a tap on the picture of a photo or a film, opens what the card// holds. One function decides which, from the record's definition alone// (`FullScreenRoute`): a picture, a film or a document opens in the one// viewer (`MediaViewer`), over the deck's own records in deck order, so a// swipe sideways walks the deck and a swipe down puts it back on the card it// grew out of; a note, a memory or a book's notes on the reader (`ReadPage`); every// other card ignores pinch out; tap opens its card at rest with acts.import CottonValuesimport SwiftUI/// What a pinch out on a card opens.enum DeckOpening {    /// The one viewer, by the act that opens it there.    case viewer(() -> Void)    /// The record's reading page (`ReadPage`).    case reading(ReadDocument)    /// No reader or viewer: pinch out is inert.    case whole}/// Which of the three a pinch out on a record's card opens, decided from its/// definition alone; nothing is wired to a table.enum FullScreenRoute: Equatable {    /// The one viewer: the table names the viewer, or a film's player.    case viewer    /// The reading page, with the columns it reads.    case reading(ReadSlots)    /// No reader or viewer.    case whole    static func of(_ definition: TableDefinition?) -> FullScreenRoute {        guard let definition else { return .whole }        if DeckViewer.shows(definition) { return .viewer }        if let slots = ReadSlots.of(definition) { return .reading(slots) }        return .whole    }    /// Does it open the reading page?    var reads: Bool {        if case .reading = self { return true }        return false    }}extension DeckOpening {    /// What a pinch out on `record`'s card opens, by its route: the viewer    /// over `records` (the deck, in deck order), its reading page, or the    /// inert pinch. A deck drawn where no viewer is hosted (a preview)    /// also ignores pinch out.    @MainActor    static func of(_ record: TableRecord, card: DeckCard?, in records: [TableRecord], definition: TableDefinition?,                   presenter: ViewerPresenter?, readAll: (@MainActor () async -> [TableRecord])? = nil) -> DeckOpening {        guard let definition else { return .whole }        switch FullScreenRoute.of(definition) {        case .viewer:            guard let presenter else { return .whole }            // The viewer opens at once on the cards dealt; the deck's whole            // read follows it, and stops when the viewer closes.            return .viewer {                DeckViewer.open(record.id, in: records, definition: definition, presenter: presenter, rest: readAll)            }        case .reading(let slots):            // A record with nothing to read (a book with neither notes nor            // a quote) has no reader to open.            let document = ReadSource(record: record, card: card, slots: slots, definition: definition).reading(record)            return document.bodies.isEmpty ? .whole : .reading(document)        case .whole:            return .whole        }    }    var reads: Bool {        if case .reading = self { return true }        return false    }    /// The act that opens the card's thing in the viewer — what a tap on a    /// photo's print or a film's still does — when that is where it opens.    var viewer: (() -> Void)? {        if case .viewer(let open) = self { return open }        return nil    }}/// The deck's records opened in the one viewer.enum DeckViewer {    /// A record of `definition` is a thing the viewer shows: its table names    /// the viewer (`viewer`: a picture, a document), or its slip names the    /// column a film plays from (`player`).    static func shows(_ definition: TableDefinition) -> Bool {        definition.viewer != nil || definition.slip.player != nil    }    /// The item id a record of `definition` stands under in the viewer — the    /// id the card is the viewer's source by (`ViewerItem(record:definition:)`).    static func itemID(_ definition: TableDefinition, _ record: String) -> String { "\(definition.id)/\(record)" }    /// The deck's records the viewer can show, in deck order, opened on    /// `record`; nil when that one is not among them.    static func set(opening record: String, in records: [TableRecord], definition: TableDefinition) -> ViewerSet? {        let items = records.compactMap { ViewerItem(record: $0, definition: definition) }        guard let start = items.firstIndex(where: { $0.id == itemID(definition, record) }) else { return nil }        return ViewerSet(items: items, start: start)    }    /// Opens `record` in `presenter` over the deck's records; false when its    /// table is not one the viewer shows, or nothing of it can be shown.    /// `rest`, where `records` are a window of more, reads the whole for    /// the viewer to grow into (`ViewerPresenter.open`).    @MainActor @discardableResult    static func open(_ record: String, in records: [TableRecord], definition: TableDefinition, presenter: ViewerPresenter,                     rest: (@MainActor () async -> [TableRecord])? = nil) -> Bool {        guard shows(definition), let set = set(opening: record, in: records, definition: definition) else { return false }        presenter.open(set, rest: rest.map { items($0, definition: definition) })        return true    }    /// The whole read `read` gives, as the viewer's items.    @MainActor    private static func items(_ read: @escaping @MainActor () async -> [TableRecord],                              definition: TableDefinition) -> @MainActor () async -> [ViewerItem] {        return { @MainActor in            let all = await read()            return all.compactMap { ViewerItem(record: $0, definition: definition) }        }    }}