Cotton code
194 lines · 8.4 KB · SwiftRaw
// The reader's fan: the deck's own tier component (`FanTiers`, drawn by// `FanTierArc` on the fan's arc with the hub's close under it), hosted over// the page's foot inside the reader. Its tiers are the core's// (`shell.reader`'s `fan`): their cards, their words, which card rests// raised and what a tap writes; this file deals them onto the arc and// draws a face as a page and a pencil by its line.//// The hand in the bar opens ONE tier, flat, as the core deals it// (`fan.hand`): Paper, Sepia and Night, each a small page in its own ground// with its name in its ink over three ruled lines (`FanSwatch`), then Type// size, Line height, Margins, Typeface, Layout, Follow the phone's light and// dark, each saying what it is set to; every one a tile of the one size on// the one arc. A face's tap wears it, at once; a knob's opens its CHOICES,// and a choice is written and the hand's tier returns, resting on the knob.//// A selection in the book opens the SELECTION tier: the three pencils and// Note. A tap on a drawn highlight opens its own: the pencils (to// recolour), Note, and Remove, last and red. The hub closes whatever// stands; so does a tap on the page.#if os(iOS)import CottonPaperimport CottonValuesimport SwiftUI/// The open tier over the page's foot, its close at the hub.struct ReaderFan: View {    let reading: BookReading    @Environment(\.theme) private var theme    var body: some View {        let diameter = Double(theme.scale[.fanRecordSize])        let geometry = FanGeometry(scale: theme.scale).withHub(diameter: diameter)        ZStack(alignment: .bottom) {            if let open = reading.tiers.open, reading.tiers.closes {                FanTierArc(tier: open, geometry: geometry, tiers: reading.tiers)                    .id(open.id)                    .frame(height: geometry.height)                    .transition(.scale(scale: 0.28, anchor: .bottom).combined(with: .opacity))                GeometryReader { proxy in                    FanClose(size: theme.scale[.fanRecordSize], acts: true) { reading.back() }                        .position(geometry.hub(width: proxy.size.width, diameter: diameter))                }                .frame(height: geometry.height)            }        }        .animation(.easeOut(duration: Double(theme.scale[.fanTurnMsMin]) / 1000), value: reading.tiers.open?.id)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("reader-fan")    }}extension BookReading {    // MARK: Opening tiers    /// The hand: its one tier, resting on the face chosen.    func openHand() {        closeFan()        tiers.push(Self.handTier(fan)) { [weak self] card in self?.tappedHand(card) }    }    /// The hub: a knob's choices give way to the hand's tier; anything else closes.    func back() {        if tiers.depth > 2, tiers.open?.id.hasPrefix(Self.choicesPrefix) == true {            tiers.pop()        } else {            closeFan()        }    }    /// The pencils for the selection just made.    func openSelection() {        while tiers.pop() != nil {}        tiers.push(Self.selectionTier(fan)) { [weak self] card in self?.tappedSelection(card) }    }    /// A drawn highlight tapped: its own tier.    func openHighlight(_ id: String) {        guard let highlight = highlights.first(where: { $0.id == id }) else { return }        closeFan()        tiers.push(Self.highlightTier(fan, highlight)) { [weak self] card in self?.tappedHighlight(card, id: id) }    }    /// The hand's tier standing says the row as it now stands, resting where it rested.    func refreshTier() {        guard let open = tiers.open, open.id == Self.handID else { return }        var tier = Self.handTier(fan)        if let raised = open.raised, tier.cards.contains(where: { $0.id == raised }) { tier.raised = raised }        tiers.replaceOpen(tier) { [weak self] card in self?.tappedHand(card) }    }    // MARK: Taps    /// A face is worn at once; a knob opens its choices.    private func tappedHand(_ card: FanTier.Card) {        guard card.id.hasPrefix(Self.knobPrefix) else {            wear(String(card.id.dropFirst(Self.facePrefix.count)))            tiers.raise(card.id)            return        }        let knob = String(card.id.dropFirst(Self.knobPrefix.count))        guard let choices = Self.choicesTier(fan, knob: knob) else { return }        tiers.push(choices) { [weak self] card in self?.tappedChoice(card, of: knob) }    }    private func tappedChoice(_ card: FanTier.Card, of knob: String) {        guard let value = fan.hand.cards.first(where: { $0.id == Self.knobPrefix + knob })?            .choices?.cards.first(where: { $0.id == card.id })?.value else { return }        Task {            await set(knob, to: value)            tiers.pop()            tiers.raise(Self.knobPrefix + knob)            refreshTier()        }    }    private func tappedSelection(_ card: FanTier.Card) {        if card.id == Self.noteID {            mark(with: nil, noting: true)        } else if let pencil = Self.pencil(of: card) {            mark(with: pencil)        }    }    private func tappedHighlight(_ card: FanTier.Card, id: String) {        switch card.id {        case Self.noteID: startNoting(id)        case Self.removeID: remove(id)        default:            guard let pencil = Self.pencil(of: card) else { return }            recolour(id, to: pencil)            tiers.raise(card.id)        }    }    // MARK: The tiers, as the core dealt them    static let handID = "reader-hand"    static let facePrefix = "face-"    static let knobPrefix = "knob-"    static let choicesPrefix = "reader-choices-"    static let pencilPrefix = "pencil-"    static let noteID = "note"    static let removeID = "remove"    /// The hand's one tier: a face as a page in its own ground, a knob by    /// its name over what it is set to, in the core's order.    static func handTier(_ fan: ReaderFanAnswer) -> FanTier {        let cards = fan.hand.cards.map { card in            guard card.id.hasPrefix(knobPrefix) else {                let face = ReaderFace(rawValue: String(card.id.dropFirst(facePrefix.count))) ?? .paper                return FanTier.Card(id: card.id, label: card.label,                                    swatch: FanSwatch(kind: .page, ground: face.ground, ink: face.ink, line: face.hairline))            }            return FanTier.Card(id: card.id, label: card.label,                                item: EditingItem(id: card.id, word: card.label, detail: card.words,                                                  does: .choice(column: String(card.id.dropFirst(knobPrefix.count)), value: "")))        }        return FanTier(id: handID, cards: cards, raised: fan.hand.raised)    }    static func choicesTier(_ fan: ReaderFanAnswer, knob: String) -> FanTier? {        guard let choices = fan.hand.cards.first(where: { $0.id == knobPrefix + knob })?.choices else { return nil }        let cards = choices.cards.map { card in            FanTier.Card(id: card.id, label: card.label,                         item: EditingItem(id: card.id, word: card.label, does: .choice(column: knob, value: "")))        }        return FanTier(id: choicesPrefix + knob, cards: cards, raised: choices.raised)    }    static func selectionTier(_ fan: ReaderFanAnswer) -> FanTier {        FanTier(id: "reader-selection", cards: marks(fan.selection), raised: fan.selection.raised)    }    static func highlightTier(_ fan: ReaderFanAnswer, _ highlight: BookHighlight) -> FanTier {        FanTier(id: "reader-highlight", cards: marks(fan.highlight), raised: pencilPrefix + highlight.pencil.rawValue)    }    /// A tier of pencils and acts: a pencil drawn by its line on Paper.    private static func marks(_ tier: ReaderTierAnswer) -> [FanTier.Card] {        let paper = ReaderFace.paper        return tier.cards.map { card in            let pencil = card.id.hasPrefix(pencilPrefix) ? HighlightPencil(rawValue: String(card.id.dropFirst(pencilPrefix.count))) : nil            return FanTier.Card(id: card.id, label: card.label, red: card.red ?? false,                                swatch: pencil.map { FanSwatch(kind: .pencil, ground: paper.ground, ink: paper.ink, line: $0.colour) })        }    }    private static func pencil(of card: FanTier.Card) -> HighlightPencil? {        guard card.id.hasPrefix(pencilPrefix) else { return nil }        return HighlightPencil(rawValue: String(card.id.dropFirst(pencilPrefix.count)))    }}#endif