Cotton code
177 lines · 8.8 KB · SwiftRaw
// WHAT YOUR ASSISTANT WROTE (owner, 21 Nov 2026; assets/design/notes/CONTRACT.md// section 13), as Android's `NotesAssistant.kt`: the assistant's memory,// read-only, as the core answers it (`notes.memory`), and the sealed reader a// web page an assistant made opens in. The reader's seal is the viewer's// (`Viewer/ViewerWeb.swift`, `ViewerSeal`): the local file only, https to the// few library and font hosts, nothing kept between openings, a link leaves// for the browser, the page never navigates away. Every word is the core's.#if os(iOS)import CottonKitimport CottonPaperimport CottonWritingimport SwiftUIimport UIKit/// MEMORY: the title, what it is, each group under its label with its count,/// each entry a sentence with when it changed quiet under it and a small pin/// where pinned. No editing and no acts: it is changed by telling the assistant.struct NotesMemoryPage: View {    @Bindable var model: NotesModel    let look: WritingLook    @State private var memory: NotesMemory?    private var theme: WritingTheme { look.theme }    private func ui(_ colour: UIColor) -> Color { Color(uiColor: colour) }    private func face(_ size: CGFloat, _ weight: UIFont.Weight = .regular) -> Font {        Font(WritingFonts.font(look.type.face, size: size, weight: weight))    }    var body: some View {        VStack(spacing: 0) {            HStack {                NotesRoundButton(icon: "back", name: NotesNav.back, id: "memory-back", look: look) { model.memoryOpen = false }                Spacer()                Text(verbatim: memory?.title ?? notesLine("memory.title"))                    .font(face(18, .bold))                    .foregroundStyle(ui(theme.heading))                    .accessibilityAddTraits(.isHeader)                Spacer()                Color.clear.frame(width: 48, height: 1)            }            .padding(.horizontal, 8).padding(.vertical, 4)            ScrollView {                if let memory {                    LazyVStack(alignment: .leading, spacing: 0) {                        Text(verbatim: memory.about)                            .font(face(15))                            .foregroundStyle(ui(theme.dim))                            .fixedSize(horizontal: false, vertical: true)                            .padding(.horizontal, 20).padding(.vertical, 8)                            .accessibilityIdentifier("memory-about")                        if let empty = memory.empty {                            Text(verbatim: empty)                                .font(face(16))                                .foregroundStyle(ui(theme.dim))                                .multilineTextAlignment(.center)                                .frame(maxWidth: .infinity)                                .padding(.horizontal, 36).padding(.vertical, 80)                                .accessibilityIdentifier("memory-empty")                        }                        ForEach(memory.groups) { group in                            HStack(alignment: .lastTextBaseline) {                                Text(verbatim: group.label)                                    .font(face(20, .bold))                                    .foregroundStyle(ui(theme.heading))                                    .frame(maxWidth: .infinity, alignment: .leading)                                    .accessibilityAddTraits(.isHeader)                                Text(verbatim: group.count.formatted())                                    .font(face(14))                                    .foregroundStyle(ui(theme.faint))                            }                            .padding(.horizontal, 20).padding(.top, 22).padding(.bottom, 6)                            .accessibilityElement(children: .combine)                            .accessibilityIdentifier("memory-group-\(group.id)")                            ForEach(group.entries) { entry in                                VStack(alignment: .leading, spacing: 3) {                                    HStack(alignment: .top, spacing: 8) {                                        Text(verbatim: entry.sentence)                                            .font(face(16))                                            .foregroundStyle(ui(theme.ink))                                            .fixedSize(horizontal: false, vertical: true)                                            .frame(maxWidth: .infinity, alignment: .leading)                                        if entry.pinned == true {                                            WritingMark("pinned", colour: ui(theme.accent), size: 14).padding(.top, 3)                                        }                                    }                                    if let changed = entry.changed {                                        Text(verbatim: changed).font(face(13)).foregroundStyle(ui(theme.faint))                                    }                                }                                .padding(.horizontal, 20).padding(.vertical, 9)                                .accessibilityElement(children: .combine)                                .accessibilityIdentifier("memory-entry-\(entry.slug ?? entry.id)")                            }                        }                        Color.clear.frame(height: 40)                    }                }            }            .accessibilityIdentifier("memory-list")        }        .frame(maxWidth: .infinity, maxHeight: .infinity)        .background(ui(theme.ground).ignoresSafeArea())        .accessibilityElement(children: .contain)        .accessibilityIdentifier("notes-memory")        .task { await model.attempt { memory = try await model.book.memory() } }    }}/// A web page an assistant made, read in the sealed reader: Close and its/// title always above the page (the title cut short at large sizes), the page/// fetched by its media key (`media.ensure`) and shown as its assistant drew/// it, the phone's text size not laid on it; a pinch enlarges it. Until the/// file is here, the core's words say it is not on this phone yet.struct NotesPageReader: View {    @Bindable var model: NotesModel    let row: NotesRow    let look: WritingLook    @State private var file: URL?    @State private var missing = false    private var theme: WritingTheme { look.theme }    private func ui(_ colour: UIColor) -> Color { Color(uiColor: colour) }    var body: some View {        VStack(spacing: 0) {            HStack {                NotesRoundButton(icon: "close", name: notesLine("assistant.reader_close"), id: "page-reader-close", look: look) { model.page = nil }                Text(verbatim: row.title ?? "")                    .font(Font(WritingFonts.font(look.type.face, size: 17, weight: .bold)))                    .foregroundStyle(ui(theme.heading))                    .lineLimit(1)                    .truncationMode(.tail)                    .frame(maxWidth: .infinity)                    .accessibilityAddTraits(.isHeader)                Color.clear.frame(width: 48, height: 1)            }            .padding(.horizontal, 8).padding(.vertical, 4)            ZStack {                if let file {                    ViewerWebPage(file: file, ground: Self.paint(theme.ground))                        .accessibilityIdentifier("page-reader-web")                } else if missing {                    Text(verbatim: notesLine("assistant.page_missing"))                        .font(Font(WritingFonts.font(look.type.face, size: 16)))                        .foregroundStyle(ui(theme.dim))                        .multilineTextAlignment(.center)                        .padding(.horizontal, 36).padding(.vertical, 80)                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)                        .accessibilityIdentifier("page-reader-missing")                }            }            .frame(maxWidth: .infinity, maxHeight: .infinity)            .clipped()        }        .frame(maxWidth: .infinity, maxHeight: .infinity)        .background(ui(theme.ground).ignoresSafeArea())        .accessibilityElement(children: .contain)        .accessibilityIdentifier("notes-page-reader")        .task {            if let key = row.page?.key, let path = await model.book.picturePath(key: key) {                file = path            } else {                missing = true            }        }    }    /// The theme's ground as the viewer's paint.    static func paint(_ colour: UIColor) -> ThemeColor {        var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0        colour.getRed(&red, green: &green, blue: &blue, alpha: &alpha)        // getRed gives four finite channels, so the components always make a colour.        return ThemeColor(components: [Double(red), Double(green), Double(blue), Double(alpha)])!    }}#endif