Cotton code
228 lines · 10.1 KB · SwiftRaw
// The long NOTE drawing (image 3), no face of its own since the note face// left (3 Nov 2026): a studio's card with no photograph wears it// (`StudioPlaceFace`). A journal page on the cream stock inside a// thin printed border with leafy sprigs at its corners — the one face that// carries ornament, and no picture. The title typed heavy, the day written// by hand under it, then the text typed out a page at a time, open-leaded as// a typed list is; the foot types which page this is ("2 / 4") and, while// more follow, "continued →". A swipe turns the note's pages first; past its// last page (or before its first) the swipe goes on to the deck, to move// card.//// The pages are the core's: it cuts the text into pages from its length and// the card's typing room (`engine::text_pages`, asked by CottonUI's// `NotePages` for the room `NoteRoom` measures), worked out when drawn and// never stored. Every word is the core's (`FaceRead`, `NotePage`).import CottonPaperimport CottonValuesimport SwiftUIpublic enum NoteFaceKnobs {    public static let textLead: CGFloat = 40    public static let textTrail: CGFloat = 26    public static let titleTop: CGFloat = 34    /// The full-screen editor's title (CottonUI's `DeckLongTextEditor`); the    /// face types its own heavy.    public static let titleSize: CGFloat = 22    public static let headSize: CGFloat = 26    /// The date is written by hand, as on the photo and video faces.    public static let dateHand: CGFloat = 24    /// Who keeps an agent's note, under the day and tucked up to it.    public static let keptHand: CGFloat = 17    public static let keptTuck: CGFloat = 4    /// The todo's notes and the editor type at this size and leading; the    /// note's own pages at `pageSize` and `pageLeading`.    public static let bodySize: CGFloat = 13.9    public static let bodyLeading: CGFloat = 5.3    public static let pageSize: CGFloat = 16.5    public static let pageLeading: CGFloat = 12    /// The first page's room under the title and the day, and over every page    /// the foot's, as `NoteRoom` measures them.    public static let headRoom: CGFloat = 118    public static let footRoom: CGFloat = 70    public static let pageTop: CGFloat = 24    /// The note's foot, typed: which page, and "continued →" while more follow.    public static let pageFoot: CGFloat = 13    public static let footSize: CGFloat = 12    public static let continuedHand: CGFloat = 18    /// How far a swipe travels before it turns a page.    public static let turn: CGFloat = 40}/// One page of a note as the core cut it.public struct NotePage: Equatable, Sendable {    public var text: String    /// "2 / 4".    public var label: String    public init(text: String, label: String) {        self.text = text        self.label = label    }}/// How the note's text fits the card: its typing room, measured off the/// typewriter at the face's size, for the core to cut pages by.public enum NoteRoom {    /// The characters a line holds and the lines a page holds, the first    /// page shorter by the title and date over it.    public static func measure(width: CGFloat, height: CGFloat) -> (columns: Int, first: Int, rest: Int) {        #if canImport(UIKit)            let font = UIFont(name: CottonFonts.typeFace, size: NoteFaceKnobs.pageSize) ?? .monospacedSystemFont(ofSize: NoteFaceKnobs.pageSize, weight: .regular)            let advance = ("M" as NSString).size(withAttributes: [.font: font]).width            let line = font.lineHeight + NoteFaceKnobs.pageLeading        #else            let advance = NoteFaceKnobs.pageSize * 0.6            let line = NoteFaceKnobs.pageSize * 1.2 + NoteFaceKnobs.pageLeading        #endif        let typing = width - NoteFaceKnobs.textLead - NoteFaceKnobs.textTrail        let columns = max(8, Int(typing / advance))        let page = height - NoteFaceKnobs.titleTop - NoteFaceKnobs.footRoom        let first = max(2, Int((page - NoteFaceKnobs.headRoom) / line))        let rest = max(2, Int((page + 20) / line))        return (columns, first, rest)    }}public struct NoteFace: View {    let card: DeckCard    let pages: [NotePage]    @Binding var page: Int    /// The swipe went past the note's first or last page: move card.    var passOn: (_ forward: Bool) -> Void = { _ in }    public init(card: DeckCard, pages: [NotePage], page: Binding<Int>, passOn: @escaping (_ forward: Bool) -> Void = { _ in }) {        self.card = card        self.pages = pages        _page = page        self.passOn = passOn    }    @Environment(\.theme) private var theme    private var current: NotePage? { pages.indices.contains(page) ? pages[page] : pages.first }    public var body: some View {        VStack(alignment: .leading, spacing: 0) {            if page == 0 {                head            }            if current != nil {                Text(FaceMarkdown.print(FaceMarkdown.Document(card.text("body") ?? pages.map(\.text).joined(separator: "\n"), theme: theme).pages(pages.map(\.text))[min(page, pages.count - 1)], size: NoteFaceKnobs.pageSize, font: CottonFonts.type(NoteFaceKnobs.pageSize)))                    .font(CottonFonts.type(NoteFaceKnobs.pageSize))                    .lineSpacing(NoteFaceKnobs.pageLeading)                    .faceInk(.ink)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.top, page == 0 ? NoteFaceKnobs.pageTop : NoteFaceKnobs.titleTop + 8)                    .accessibilityIdentifier("note-page")            }            Spacer(minLength: 0)            foot        }        .padding(.leading, NoteFaceKnobs.textLead)        .padding(.trailing, NoteFaceKnobs.textTrail)        .padding(.bottom, 26)        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)        .contentShape(Rectangle())        // The page is the note's words: a tap writes them on the whole        // screen, in the shell's editor.        .facePart(.body)        .facePages(pages.count > 1 ? turning : nil, at: "\(page)/\(pages.count)") { forward in            let next = page + (forward ? 1 : -1)            if pages.indices.contains(next) { page = next } else { passOn(forward) }        }        .animation(.easeOut(duration: 0.18), value: page)        .onCardPaper(FaceStock.ornamented.laid(card.pace))    }    private var head: some View {        VStack(alignment: .leading, spacing: 8) {            if let title = card.text("title") {                HeavyType(words: String(FaceMarkdown.set(title, theme: theme).characters), size: NoteFaceKnobs.headSize)                    .lineLimit(3)                    .facePart(.title)            }            if let date = card.text("date") {                Text(HandRun.tail(date))                    .font(CottonFonts.hand(NoteFaceKnobs.dateHand))                    .lineLimit(1)                    .minimumScaleFactor(0.7)                    .faceInk(.ink)            }            // Who keeps a note an agent writes ("Kept by your assistant"),            // small in pencil under its day; nothing on the owner's own.            if let kept = card.text("kept_by") {                Text(HandRun.tail(kept))                    .font(CottonFonts.hand(NoteFaceKnobs.keptHand))                    .lineLimit(1)                    .minimumScaleFactor(0.7)                    .faceInk(.pencil)                    .padding(.top, -NoteFaceKnobs.keptTuck)                    .accessibilityIdentifier("note-kept-by")            }        }        .padding(.top, NoteFaceKnobs.titleTop)    }    private var foot: some View {        HStack(alignment: .lastTextBaseline) {            if pages.count > 1, let label = current?.label {                Text(label)                    .font(CottonFonts.type(NoteFaceKnobs.pageFoot))                    .faceInk(.ink)                    .accessibilityIdentifier("note-page-label")            }            Spacer()            if page < pages.count - 1 {                Text(ShellLines.line("cards", "note.continued"))                    .font(CottonFonts.type(NoteFaceKnobs.pageFoot))                    .faceInk(.ink)            }        }    }    private var turning: some Gesture {        DragGesture(minimumDistance: 16)            .onEnded { end in                let dx = end.translation.width                guard abs(dx) > NoteFaceKnobs.turn, abs(dx) > abs(end.translation.height) else { return }                let forward = dx < 0                let next = page + (forward ? 1 : -1)                if pages.indices.contains(next) { page = next } else { passOn(forward) }            }    }}/// A note's card: its words cut into pages by the installed cutter/// (`facePageCutter`: the core's in the app) for the room this card gives,/// again whenever the words or the room change; with no cutter, one page.public struct NoteCard: View {    let card: DeckCard    @Environment(\.facePageCutter) private var cutter    @Environment(\.deckPassOn) private var passOn    @State private var pages: [NotePage] = []    @State private var page = 0    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        GeometryReader { room in            NoteFace(card: card, pages: pages.isEmpty ? [NotePage(text: card.text("body") ?? "", label: "")] : pages, page: $page,                     passOn: { forward in passOn?(forward) })                .task(id: "\(card.record ?? "")|\(card.text("body")?.hashValue ?? 0)|\(Int(room.size.width))x\(Int(room.size.height))") {                    guard let cutter, let text = card.text("body") else { return }                    let measured = NoteRoom.measure(width: room.size.width, height: room.size.height)                    let cut = await cutter.cut(text, columns: measured.columns, first: measured.first, rest: measured.rest)                    // A cut asked for a room since left is never shown: a                    // narrow room's slow cut, landing after the wide one's,                    // kept a note at "1 / 2,738" (walk 332).                    guard !Task.isCancelled else { return }                    pages = cut                    page = min(page, max(0, pages.count - 1))                }        }    }}