Cotton code
97 lines · 4.7 KB · SwiftRaw
// COMMS' LOOK (shared/DECISIONS.md "Look"): You's Look row opens the shared Look screen (Chrome/LookScreen.swift) in// Comms' warm look: a few bubbles drawn live (the core's preview) in the paper, ink, face and size chosen; the papers,// inks, faces and sizes `look {app: messages}` answers; Reset asked in Comms' own panel. Every Comms screen reads the// standing look through `ChatLook.warm` (ChatWarm.swift).#if os(iOS)import CottonKitimport CottonWritingimport SwiftUIstruct ChatYouLookPage: View {    let core: any CottonCalling    let back: () -> Void    @Environment(\.chatLook) private var look    @Environment(ToastCenter.self) private var toasts: ToastCenter?    @State private var model = LookModel.standing("messages")    @State private var asking = false    var body: some View {        ChatYouPageFrame(title: model.answer?.word("title"), id: "you-look-page", back: back) {            if let answer = model.answer {                LookScreen(choices: LookChoices(answer), words: LookWords(answer), skin: skin, gutter: Warm.side,                           choose: { pick in Task { await model.choose(pick, core: core); tellRefusal() } },                           reset: { asking = true }) { dark in                    ChatLookPreview(answer: answer, dark: dark, look: look)                } more: { EmptyView() }                    .padding(.top, 8)            }        }        .task { await model.read(core) }        .lookAsk(isPresented: $asking, style: look.panel, font: look.font(17, .semibold),                 message: model.answer?.word("reset_ask"), messageColour: look.colours.text,                 choices: [LookChoice(id: "look-reset", label: model.answer?.word("reset_yes") ?? "", colour: look.colours.accent) {                     Task { await model.reset(core: core); tellRefusal() }                 }],                 name: model.answer?.word("reset_title"))    }    private func tellRefusal() {        if let refused = model.refused { toasts?.show(refused, tone: .error); model.refused = nil }    }    /// The screen in Comms' warm look: its ground and inks, the accent for what is chosen, Hanken Grotesk (or the face chosen).    private var skin: LookSkin {        let look = look        return LookSkin(ground: look.colours.ground, ink: look.colours.text, quiet: look.colours.dim, accent: look.colours.accent,                        onAccent: look.colours.onAccent, sheet: look.colours.bar, rule: look.colours.rule,                        font: { size, weight in look.font(size, Self.weight(weight)) },                        face: { id, size in                            id == "own" ? WarmFonts.font(size) : Font(WritingFonts.font(LookFaces.drawn(id, own: "sans"), size: size))                        })    }    private static func weight(_ weight: Font.Weight) -> UIFont.Weight {        switch weight {        case .bold: .bold        case .semibold: .semibold        case .medium: .medium        default: .regular        }    }}/// The core's sample conversation in the paper of one appearance: a name and its bubbles, theirs on the ground's/// left and yours tinted on the right.struct ChatLookPreview: View {    let answer: LookAnswer    let dark: Bool    let look: ChatLook    private func colour(_ token: String, _ fallback: Color) -> Color { answer.colour(token, dark: dark) ?? fallback }    var body: some View {        let ink = colour("ink", look.colours.text)        VStack(alignment: .leading, spacing: 8) {            if let name = answer.preview?.name {                Text(verbatim: name).font(look.font(15, .semibold)).foregroundStyle(ink)            }            ForEach(Array((answer.preview?.bubbles ?? []).enumerated()), id: \.offset) { _, bubble in                Text(verbatim: bubble.text)                    .font(look.font(16))                    .foregroundStyle(bubble.mine ? colour("mine_ink", ink) : ink)                    .padding(.horizontal, 14).padding(.vertical, 9)                    .background(RoundedRectangle(cornerRadius: 18, style: .continuous)                        .fill(bubble.mine ? colour("mine", look.colours.bubbleOut) : colour("chip", look.colours.bar)))                    .fixedSize(horizontal: false, vertical: true)                    .frame(maxWidth: .infinity, alignment: bubble.mine ? .trailing : .leading)            }            Capsule().fill(colour("accent", look.colours.accent)).frame(width: 24, height: 3)        }        .padding(16)        .frame(maxWidth: .infinity, alignment: .leading)        .background(RoundedRectangle(cornerRadius: 22, style: .continuous).fill(colour("ground", look.colours.ground)))        .overlay(RoundedRectangle(cornerRadius: 22, style: .continuous).strokeBorder(colour("line", look.colours.rule), lineWidth: 1))    }}#endif