Cotton code
132 lines · 6.1 KB · SwiftRaw
// HOME AS A DECK (row C of the deck faces): Home's today is a deck of cards,// never a list — each of today's events and todos due today (the day read,// `TodayFeedModel`) and each unread conversation (the thread table's own// cards), then Home's notices, asked and done, each a card (`HomePartCard`).// A day's row is the framed cream card with its tool's picture, what it is// typed small (its table's name), its title typed heavy, where it is and the// people on it (its record's own card), and its time on the day by hand// ("4:30 pm", "due today"), struck through once done — drawn only for a row// whose table's own face is not (CottonUI's `HomeToday.drawn`): a row read as// its record draws its tool's face, as an unread thread draws its own// (`ThreadSlipsFace`). A tap opens the record. The tool's picture is the app's// (`object`, CottonUI's `ToolObject` at the side the face gives it); the// row's words are a plain `TodayItem`.import CottonPaperimport CottonValuesimport SwiftUIpublic enum TodayFaceKnobs {    /// A title longer than this many characters draws the picture smaller.    public static let longTitle = 48    public static let pictureShareLong: CGFloat = 0.26    public static let inset = EdgeInsets(top: 26, leading: 26, bottom: 22, trailing: 22)    public static let pictureShare: CGFloat = 0.5    public static let titleSize: CGFloat = 28    public static let lineHand: CGFloat = 30    public static let placeSize: CGFloat = 15    public static let mount: CGFloat = 34}public struct TodayFace<Object: View>: View {    let today: TodayItem    /// What the row is, in its table's own name ("Todo", "Event").    let kind: String?    /// The record's own card, once read: its place and people.    var card: DeckCard? = nil    /// The tool's picture, at the side the face gives it.    let object: (_ side: CGFloat) -> Object    public init(today: TodayItem, kind: String?, card: DeckCard? = nil, @ViewBuilder object: @escaping (_ side: CGFloat) -> Object) {        self.today = today        self.kind = kind        self.card = card        self.object = object    }    public var body: some View {        GeometryReader { sheet in            // A long title takes room from the picture, never the foot.            let share = today.title.count > TodayFaceKnobs.longTitle ? TodayFaceKnobs.pictureShareLong : TodayFaceKnobs.pictureShare            VStack(alignment: .leading, spacing: 0) {                if let kind { FaceKind(words: kind).facePart(.subtitle) }                object(sheet.size.width * share)                    .frame(width: sheet.size.width * share, height: sheet.size.width * share)                    .facePart(.picture)                    .frame(maxWidth: .infinity)                    .padding(.top, 6)                    .accessibilityHidden(true)                // A long title wraps on into the room below before it is cut.                HeavyType(words: today.title, size: TodayFaceKnobs.titleSize)                    .lineLimit(5)                    .minimumScaleFactor(0.7)                    .strikethrough(today.done)                    .padding(.top, 12)                    .accessibilityIdentifier("deck-title")                    .facePart(.title)                if let place = card?.text("place") {                    Text(place)                        .font(CottonFonts.type(TodayFaceKnobs.placeSize))                        .lineLimit(2)                        .faceInk(.ink)                        .padding(.top, 8)                        .facePart(.place)                }                let people = card?.entries("people") ?? []                if !people.isEmpty {                    HStack(spacing: 8) {                        ForEach(Array(people.enumerated()), id: \.offset) { _, entry in                            FaceMount(name: entry["name"]?.text ?? "", picture: entry["picture"]?.text, size: TodayFaceKnobs.mount)                        }                        if let more = card?.text("people_more") {                            Text(more)                                .font(CottonFonts.type(FacePartsKnobs.labelSize + 1))                                .faceInk(.pencil)                        }                    }                    .padding(.top, 10)                    .facePart(.people)                }                Spacer(minLength: 8)                if let line = today.line {                    Text(HandRun.tail(line))                        .font(CottonFonts.hand(TodayFaceKnobs.lineHand))                        .lineLimit(1)                        .minimumScaleFactor(0.7)                        .faceInk(.ink)                        .rotationEffect(.degrees(-3))                        .facePart(.when)                }            }            .padding(TodayFaceKnobs.inset)            .opacity(today.done ? 0.6 : 1)        }        .onCardPaper(TodayFaceCatalogue.stock.laid(card?.pace))    }}/// The day's row face, and the cards it is reviewed in. No card kind names/// it: Home draws a day's row with it (CottonUI's `HomeToday`) where no/// table's face can. From a card, its title, its line (`date`) and whether/// it is done (`check`), what it is (`subtitle`), its place and people.public enum TodayFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "cotton.today", specimens: [Specimen("dinner", dinner)]) { card, picture, _ in        TodayFace(today: TodayItem(title: card.text("title") ?? "", done: card.checked == true, line: card.text("date")),                  kind: card.text("subtitle"), card: card) { side in            if let picture { picture.frame(width: side, height: side) }        }    }    public static let dinner = DeckCard(face: .event, fields: [        "title": .text("Dinner"),        "subtitle": .text("Event"),        "date": .text("8:00 pm"),        "place": .text("12 Example Lane, Indiranagar"),        "people": .list([            .object(["name": .text("Priya Example"), "picture": .text("guest-0")]),            .object(["name": .text("Arjun Rao")]),        ]),    ])}