Cotton code
163 lines · 6.8 KB · SwiftRaw
// The reader's chrome, shown and hidden together by a tap in the middle of// the page (`reader-paper.png`): at the top the running head, the book's// title in spaced small capitals over a hairline; at the foot the page// number, centred, and under it the bar under the seek line — back, the// chapter's title (a tap opens the contents), how far through, and at the// right the hand. The seek line is the book's page edge (`ReaderSeekLine`):// chapter ticks, a ribbon where the reader is, a drag to anywhere; after a// jump the core's Back slip stands over it until it retires. Every word is live type in the face's inks; the// chrome lies on the face's own ground, so it reads as part of the page.//// While a tier stands at the hub the bar steps aside for it; the running// head and the page number stay, as in `hand-faces.png`.#if os(iOS)import CottonPaperimport CottonValuesimport SwiftUIenum ReaderChromeKnobs {    static let headSize: CGFloat = 12    static let headTracking: CGFloat = 2.4    static let headRise: CGFloat = 14    static let side: CGFloat = 24    static let pageSize: CGFloat = 15    static let barSize: CGFloat = 14    static let bar: CGFloat = 44    static let handBox: CGFloat = 44    static let hand: CGFloat = 22}struct ReaderChrome: View {    let reading: BookReading    let close: () -> Void    var body: some View {        let face = reading.face        VStack(spacing: 0) {            if reading.chromeShown {                head(face)                    .transition(.opacity)            }            Spacer(minLength: 0)            if reading.chromeShown {                foot(face)                    .transition(.opacity)            }        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("reader-chrome")    }    private func head(_ face: ReaderFace) -> some View {        VStack(spacing: ReaderChromeKnobs.headRise * 0.6) {            Text(reading.title)                .font(CottonFonts.caslon(ReaderChromeKnobs.headSize))                .textCase(.uppercase)                .tracking(ReaderChromeKnobs.headTracking)                .foregroundStyle(face.secondary)                .lineLimit(1)                .truncationMode(.tail)                .accessibilityIdentifier("reader-running-head")            Rectangle().fill(face.hairline).frame(height: 1)        }        .padding(.horizontal, ReaderChromeKnobs.side)        .padding(.top, ReaderChromeKnobs.headRise * 0.5)        .background(face.ground.ignoresSafeArea(edges: .top))    }    private func foot(_ face: ReaderFace) -> some View {        VStack(spacing: 6) {            // Back, over the page's last lines and clear of its number, until it retires.            if !reading.tiers.closes, let back = reading.backSlip, reading.seekSlip == nil {                Button { reading.seekBack() } label: { ReaderSlip(words: back.words, face: face) }                    .buttonStyle(.plain)                    .padding(.horizontal, ReaderSeekKnobs.slipGutter)                    .transition(.opacity)                    .accessibilityIdentifier("reader-seek-back")            }            if let page = reading.pageWords {                Text(page)                    .font(CottonFonts.caslon(ReaderChromeKnobs.pageSize).monospacedDigit())                    .foregroundStyle(face.ink)                    .accessibilityLabel(ShellLines.line("cards", "reader.page", ["page": page]))                    .accessibilityIdentifier("reader-page-number")            }            if !reading.tiers.closes {                VStack(spacing: 0) {                    if let seek = reading.seek {                        ReaderSeekLine(face: face, ticks: seek.ticks.map(\.at), at: reading.seekAt, slip: reading.seekSlip,                                       value: seek.words.here, label: seek.words.line,                                       drag: { reading.seeking($0) }, release: { reading.seekReleased($0, width: $1) },                                       step: { reading.seekStep($0) })                    } else {                        Rectangle().fill(face.hairline).frame(height: 1)                            .padding(.horizontal, ReaderChromeKnobs.side)                    }                    bar(face)                }                .background(face.ground.ignoresSafeArea(edges: .bottom))                .transition(.opacity)            }        }    }    private func bar(_ face: ReaderFace) -> some View {        HStack(spacing: 12) {            ReaderBackButton(ink: face.ink, close: close)            Button { reading.openContents() } label: {                Text(reading.chapter ?? "")                    .font(CottonFonts.caslon(ReaderChromeKnobs.barSize))                    .foregroundStyle(face.ink)                    .lineLimit(1)                    .truncationMode(.tail)                    .frame(minHeight: ReaderChromeKnobs.bar)                    .contentShape(Rectangle())            }            .buttonStyle(.plain)            .accessibilityIdentifier("reader-chapter")            Spacer(minLength: 8)            if let words = reading.progressWords {                Text(words)                    .font(CottonFonts.caslon(ReaderChromeKnobs.barSize).monospacedDigit())                    .foregroundStyle(face.ink)                    .accessibilityIdentifier("reader-progress")            }            Button { reading.openHand() } label: {                FaceHandMark().stroke(lineWidth: 1.3)                    .foregroundStyle(face.ink)                    .frame(width: ReaderChromeKnobs.hand, height: ReaderChromeKnobs.hand)                    .frame(width: ReaderChromeKnobs.handBox, height: ReaderChromeKnobs.handBox)                    .contentShape(Rectangle())            }            .buttonStyle(.plain)            .accessibilityLabel(ShellLines.line("cards", "reader.hand"))            .accessibilityIdentifier("reader-hand")        }        .frame(height: ReaderChromeKnobs.bar)        .padding(.leading, ReaderChromeKnobs.side - 14)        .padding(.trailing, ReaderChromeKnobs.side - 12)        .accessibilityElement(children: .contain)        .accessibilityLabel(ShellLines.line("cards", "reader.bar"))    }}/// The bar's back chevron, in the face's ink.struct ReaderBackButton: View {    let ink: SwiftUI.Color    let close: () -> Void    var body: some View {        Button(action: close) {            Image(systemName: "chevron.left")                .font(CottonFonts.symbol(17, weight: .regular))                .foregroundStyle(ink)                .frame(width: 44, height: 44)                .contentShape(Rectangle())        }        .buttonStyle(.plain)        .accessibilityLabel(ShellLines.line("cards", "reader.back"))        .accessibilityIdentifier("reader-back")    }}#endif