Cotton code
102 lines · 4.3 KB · SwiftRaw
// A face and the pages its card turns to after it (`card.pages`, 10c993c7e):// the face first, then each column that holds words under its label, cut to// the room as a note's (`FacePages`) — a meeting's summary, decisions and// transcript, a coding session's task list. A tap turns to the next page,// the last back to the face; a swipe turns either way, and past either end// moves card. At the foot, as a note's, the page's place ("1 / 4") and// "continued →" while there is a page after it. A card with no pages is// its face alone, with no foot and no turning. Every word is the core's or// the record's; the app reads the pages off the card's definition// (CottonUI's `DeckCardFill`) onto the card, and the face turns them// inside its own paper (`AudioFace`, `SessionFace`, a studio's// faces).import CottonPaperimport CottonValuesimport SwiftUIenum FaceTurnsKnobs {    static let inset = EdgeInsets(top: 26, leading: 24, bottom: 26, trailing: 22)    static let pageSize: CGFloat = 13.5    /// The foot's type, and how far it stands off the card's bottom.    static let footSize: CGFloat = 12    static let footBottom: CGFloat = 10    /// How far a swipe travels before it turns a page.    static let turn: CGFloat = 40}struct FaceTurns<Front: View>: View {    let pages: [CardPage]    let front: Front    @Environment(\.deckPassOn) private var passOn    @State private var page = 0    /// `opened` is the page shown first (a gallery's specimen of a page).    init(pages: [CardPage], opened: Int = 0, @ViewBuilder front: () -> Front) {        self.pages = pages        self.front = front()        _page = State(initialValue: opened)    }    var body: some View {        let count = 1 + pages.count        ZStack(alignment: .bottom) {            Group {                if page > 0, pages.indices.contains(page - 1) { turned(pages[page - 1], count: count) } else { front }            }            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)            if count > 1 { foot(count) }        }        .contentShape(Rectangle())        .onTapGesture { if count > 1 { page = (page + 1) % count } }        .facePages(count > 1 ? turning(count: count) : nil, at: "\(page)/\(count)") { forward in            let next = page + (forward ? 1 : -1)            if (0..<count).contains(next) { page = next } else { passOn?(forward) }        }        .animation(.easeOut(duration: 0.18), value: page)    }    private func turned(_ shown: CardPage, count: Int) -> some View {        VStack(alignment: .leading, spacing: 10) {            FaceLabel(words: shown.label)            // The column's words cut to the room as a note's; past its last            // page the card turns on, past its first back.            FacePages(text: shown.text, size: FaceTurnsKnobs.pageSize, markdown: shown.markdown)                .environment(\.deckPassOn, { forward in                    let next = page + (forward ? 1 : -1)                    if (0..<count).contains(next) { page = next } else { passOn?(forward) }                })        }        .padding(FaceTurnsKnobs.inset)        .accessibilityIdentifier("face-page-\(page)")    }    /// "2 / 4", and "continued →" while a page follows.    private func foot(_ count: Int) -> some View {        HStack(alignment: .lastTextBaseline) {            Text(ShellLines.line("cards", "note.page", ["at": String(page + 1), "of": String(count)]))                .accessibilityIdentifier("face-page-label")            Spacer(minLength: 0)            if page < count - 1 {                Text(ShellLines.line("cards", "note.continued"))            }        }        .font(CottonFonts.type(FaceTurnsKnobs.footSize))        .lineLimit(1)        .faceInk(.ink)        .padding(.horizontal, FaceTurnsKnobs.inset.leading)        .padding(.bottom, FaceTurnsKnobs.footBottom)        .accessibilityHidden(true)    }    private func turning(count: Int) -> some Gesture {        DragGesture(minimumDistance: 16)            .onEnded { end in                let dx = end.translation.width                guard abs(dx) > FaceTurnsKnobs.turn, abs(dx) > abs(end.translation.height) else { return }                let next = page + (dx < 0 ? 1 : -1)                if (0..<count).contains(next) { page = next } else { passOn?(dx < 0) }            }    }}