Cotton code
130 lines · 5.8 KB · SwiftRaw
// The MOOD face (KINDS, 7f4a5f0fb; house references 97 and 98): a moment// of a day on the framed cream card — the day written large by hand at the// top, the line the owner said typed heavy under it, as large as the card// lets it stand, the emotions named in it on little paper tags under that// (`subtitle`: the core's list of words, or words it joined), and under them// a house watercolour chosen by those emotions (`MoodScene`): the night sky// of reference 97 for a quiet moment, the rain cloud of 98 for a heavy one;// a moment that names neither keeps the sprig in the corner. Every word is// the core's (`DeckCard`); the face lays them out.import CottonPaperimport CottonValuesimport SwiftUIenum MoodFaceKnobs {    static let inset = EdgeInsets(top: 30, leading: 30, bottom: 26, trailing: 28)    static let dayHand: CGFloat = 34    static let lineSize: CGFloat = 24    static let lineLines = 7    static let artShare: CGFloat = 0.3    static let gap: CGFloat = 16    static let chipGap: CGFloat = 10    /// The scenes a moment may be drawn under, each with the emotions that    /// call it. A picture, not a score: the vocabulary itself stays flat and    /// alphabetical (`Emotions`), and a word in neither set draws neither.    static let scenes: [(art: String, emotions: Set<String>)] = [        ("face-art-night-sky", ["bored", "calm", "content", "numb", "okay", "relieved", "tired"]),        ("face-art-rain-cloud", [            "afraid", "angry", "anxious", "ashamed", "awful", "frustrated", "guilty", "hurt", "lonely", "low",            "overwhelmed", "restless", "sad", "stressed",        ]),    ]    static let sceneGap: CGFloat = 18}/// The house watercolour a moment is drawn under: the scene most of its/// emotions call, the one its first such emotion calls on a tie; none when/// no emotion calls one.enum MoodScene {    static func art(for emotions: [String]) -> String? {        let called = emotions.compactMap { word in MoodFaceKnobs.scenes.firstIndex { $0.emotions.contains(word.lowercased()) } }        guard let first = called.first else { return nil }        let most = Dictionary(called.map { ($0, 1) }, uniquingKeysWith: +).max { a, b in            a.value < b.value || (a.value == b.value && a.key != first)        }?.key ?? first        return MoodFaceKnobs.scenes[most].art    }}public struct MoodFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        let emotions = card.words("subtitle")        let scene = MoodScene.art(for: emotions)        GeometryReader { sheet in            ZStack(alignment: .bottomTrailing) {                if scene == nil {                    FaceArt(name: "face-art-sprig")                        .frame(width: sheet.size.width * MoodFaceKnobs.artShare)                        .padding(.trailing, 18)                        .padding(.bottom, 16)                }                VStack(alignment: .leading, spacing: 0) {                    if let day = card.text("date") {                        Text(HandRun.tail(day))                            .font(CottonFonts.hand(MoodFaceKnobs.dayHand))                            .lineLimit(1)                            .minimumScaleFactor(0.6)                            .faceInk(.ink)                            .accessibilityIdentifier("face-date")                            .facePart(.when)                    }                    if let line = card.text("title") {                        HeavyType(words: line, size: MoodFaceKnobs.lineSize)                            .lineLimit(MoodFaceKnobs.lineLines)                            .minimumScaleFactor(0.6)                            .padding(.top, MoodFaceKnobs.gap)                            .accessibilityIdentifier("deck-title")                            .facePart(.title)                    }                    if !emotions.isEmpty {                        ChipWrap(spacing: MoodFaceKnobs.chipGap) {                            ForEach(Array(emotions.enumerated()), id: \.offset) { _, emotion in PaperChip(words: emotion) }                        }                        .padding(.top, MoodFaceKnobs.gap + 4)                        .accessibilityElement(children: .combine)                        .accessibilityIdentifier("face-chips")                    }                    if let scene {                        FaceArt(name: scene)                            .frame(maxWidth: .infinity, maxHeight: .infinity)                            .padding(.top, MoodFaceKnobs.sceneGap)                            .layoutPriority(-1)                    }                    Spacer(minLength: 0)                }                .padding(MoodFaceKnobs.inset)                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)            }        }        .onCardPaper(MoodFaceCatalogue.stock.laid(card.pace))    }}/// The mood face, by the kind's name, and the cards it is reviewed in.public enum MoodFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "mood", specimens: [Specimen("evening", eveningMood), Specimen("quiet", quietMood)]) { card, _, _ in        MoodFace(card: card)    }    /// A moment of an invented evening, two emotions named in it.    public static let eveningMood = DeckCard(face: .mood, fields: [        "date": .text("Fri 23 Oct"),        "title": .text("Tired, but the example build finally went green and the evening is quiet."),        "subtitle": .text("Tired \u{00B7} Relieved \u{00B7} Hopeful"),    ])    /// A moment nobody has named an emotion in yet.    public static let quietMood = DeckCard(face: .mood, fields: [        "date": .text("Thu 22 Oct"),        "title": .text("Slow morning."),    ])}