Cotton code
507 lines · 24.7 KB · SwiftRaw
// SETTINGS (YOU), in Comms' shape (sites/DECISIONS.md "Settings (You)", shared/DECISIONS.md "Settings"): the card,// then Sites' own rows (Look, Search with, Logins from Secrets, Hidden sites, Trackers), then the shared rows (Sync,// Privacy, Storage, Help and feedback, About), each saying how it stands, every word the core's `sites.settings`.// Your Claude does not stand (question 14). The hidden lock: Face ID shows the hidden sites, and nothing says how many.#if os(iOS)import CottonKitimport CottonWritingimport SwiftUI/// The shared settings parts in Sites' look: its group headings, Instrument Sans, outlined buttons and option rings.extension SettingsLook {    @MainActor static var sites: SettingsLook {        SettingsLook(            ink: Cranberry.ink, quiet: Cranberry.quiet, rule: Cranberry.rule, ground: Cranberry.ground,            body: SitesType.body, small: SitesType.line,            heading: { AnyView(SitesGroupHead(words: $0)) },            act: { words, id, enabled, tap in                AnyView(Button(words, action: tap).buttonStyle(SitesButtonStyle(kind: .plain)).disabled(!enabled).opacity(enabled ? 1 : 0.45)                    .accessibilityIdentifier(id))            },            choice: { words, on, id, enabled, tap in                AnyView(SitesOption(label: words, chosen: on, tag: id, choose: tap).disabled(!enabled))            },            danger: Cranberry.ink,            title: SitesType.title        )    }}/// A page's head: "‹ Sites" (or "‹ You") at the left, then the page's title.struct SitesPageHead: View {    let back: String    let title: String    let tap: () -> Void    var body: some View {        VStack(alignment: .leading, spacing: 0) {            Button(action: tap) {                HStack(spacing: 2) {                    SitesGlyph(icon: .back, size: 22)                    Text(back).font(SitesType.sans(16, .medium))                }                .foregroundStyle(Cranberry.ink)                .frame(minHeight: 44).contentShape(Rectangle())            }            .buttonStyle(.plain)            .accessibilityLabel(back)            .accessibilityIdentifier("sites-page-back")            Text(title).font(SitesType.title).foregroundStyle(Cranberry.ink)                .fixedSize(horizontal: false, vertical: true)                .padding(.top, 6)                .accessibilityAddTraits(.isHeader)        }        .frame(maxWidth: .infinity, alignment: .leading)    }}/// One of You's rows: its name, how it stands at the end, the whole row a button.struct SitesSettingRow: View {    let label: String    var value: String?    var tag: String    let tap: () -> Void    @Environment(\.dynamicTypeSize) private var size    var body: some View {        Button(action: tap) {            HStack(spacing: 12) {                // At the accessibility sizes the value stands under the name, so neither breaks inside a word.                if size.isAccessibilitySize {                    VStack(alignment: .leading, spacing: 2) {                        Text(label).font(SitesType.body).foregroundStyle(Cranberry.ink)                        if let value { Text(value).font(SitesType.line).foregroundStyle(Cranberry.quiet) }                    }                    .frame(maxWidth: .infinity, alignment: .leading)                } else {                    Text(label).font(SitesType.body).foregroundStyle(Cranberry.ink)                        .frame(maxWidth: .infinity, alignment: .leading)                    if let value {                        Text(value).font(SitesType.line).foregroundStyle(Cranberry.quiet)                            .multilineTextAlignment(.trailing)                    }                }                SitesGlyph(icon: .next, size: 16).foregroundStyle(Cranberry.faint)            }            .padding(.vertical, 13)            .contentShape(Rectangle())        }        .buttonStyle(.plain)        .accessibilityElement(children: .combine)        .accessibilityIdentifier(tag)    }}struct SitesYouPage: View {    @Bindable var model: SitesModel    @State private var home = SettingsHome()    @Environment(\.cottonSync) private var sync    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 0) {                if home.place == .profile {                    // The account page the card opens (signed out: Sign in; signed in: the number, recovery, Sign out,                    // Delete account), as Data's You draws it. Found walking SITES ROWS: the card went nowhere.                    SitesPageHead(back: model.settings?.title ?? SitesWords.line("you.title"), title: home.title(own: { _ in nil }) ?? "") {                        home.place = nil                    }                    SettingsProfileHead(home: home, look: .sites)                    SettingsProfileFoot(home: home, core: model.core, look: .sites, said: { model.tell($0) }) {                        home.place = nil                        model.place = nil                    }                } else {                SitesPageHead(back: model.home?.title ?? SitesWords.line("title"), title: model.settings?.title ?? SitesWords.line("you.title")) {                    model.place = nil                }                SettingsYouCard(home: home, core: model.core, look: .sites) { model.tell($0) }                    .padding(.top, 10)                if let settings = model.settings {                    Text(settings.card.count).font(SitesType.line).foregroundStyle(Cranberry.quiet)                        .padding(.bottom, 6)                        .accessibilityIdentifier("sites-you-count")                    VStack(spacing: 0) {                        ForEach(Array(settings.rows.enumerated()), id: \.element.id) { at, row in                            if at > 0 { SitesRule() }                            own(row)                        }                    }                    .padding(.top, 8)                    if let orphans = model.home?.orphans, !orphans.sites.isEmpty {                        // Sign-ins left on this phone by sites no longer in the list (one removed on another phone), each                        // named, removed one at a time after the ask; never all at once.                        VStack(alignment: .leading, spacing: 0) {                            SitesRule()                            Text(orphans.line).font(SitesType.line).foregroundStyle(Cranberry.quiet)                                .fixedSize(horizontal: false, vertical: true)                                .padding(.top, 13)                                .padding(.bottom, 4)                                .accessibilityAddTraits(.isHeader)                                .accessibilityIdentifier("sites-you-orphans")                            ForEach(Array(orphans.sites.enumerated()), id: \.element.id) { at, orphan in                                if at > 0 { SitesRule() }                                HStack(alignment: .center, spacing: 12) {                                    Text(orphan.label).font(SitesType.body).foregroundStyle(Cranberry.ink)                                        .lineLimit(2)                                        .frame(maxWidth: .infinity, alignment: .leading)                                        .accessibilityIdentifier("sites-orphan-\(orphan.id)")                                    Button { model.askOrphan(orphan) } label: {                                        Text(orphans.remove).font(SitesType.sans(15, .bold, .subheadline)).foregroundStyle(Cranberry.accent)                                            .frame(minWidth: 44, minHeight: 44)                                            .contentShape(Rectangle())                                    }                                    .buttonStyle(.plain)                                        .accessibilityLabel("\(orphans.remove), \(orphan.label)")                                        .accessibilityIdentifier("sites-orphan-remove-\(orphan.id)")                                }                                .padding(.vertical, 2)                            }                        }                        .padding(.top, 14)                    }                    group(settings.shared.filter { $0.id == "sync" })                    group(settings.shared.filter { ["privacy", "storage"].contains($0.id) })                    group(settings.shared.filter { ["help", "about"].contains($0.id) })                }                }            }            .padding(.horizontal, Cranberry.side)            .padding(.bottom, 32)        }        .task(id: sync?.revision) {            await home.read(model.core)            await model.readSettings()        }    }    @ViewBuilder private func own(_ row: SitesSettings.Row) -> some View {        switch row.id {        case "look":            SitesSettingRow(label: row.label, value: LookModel.standing("sites").answer?.line, tag: "sites-you-look") { model.place = .look }        case "engine":            SitesSettingRow(label: row.label, value: row.text, tag: "sites-you-engine") { model.place = .engine }        case "hide_after":            SitesSettingRow(label: row.label, value: row.choices?.first { $0.id == row.text }?.label, tag: "sites-you-hidden") { model.place = .hideAfter }        case "trackers", "login_ask":            SitesToggle(label: row.label, line: row.line, on: Binding(get: { row.on ?? false }, set: { on in Task { await model.set(row.id, .flag(on)) } }),                        tag: "sites-you-\(row.id)")        default:            SitesSettingRow(label: row.label, value: row.line, tag: "sites-you-\(row.id)") {}        }    }    @ViewBuilder private func group(_ rows: [SitesSettings.Row]) -> some View {        if !rows.isEmpty {            VStack(spacing: 0) {                SitesRule()                ForEach(Array(rows.enumerated()), id: \.element.id) { at, row in                    if at > 0 { SitesRule() }                    SitesSettingRow(label: row.label, value: row.line, tag: "sites-you-\(row.id)") {                        switch row.id {                        case "sync": model.place = .sync                        case "privacy": model.place = .privacy                        case "storage": Task { await model.readStorage() }; model.place = .storage                        case "help": model.place = .help                        default: model.place = .about                        }                    }                }            }            .padding(.top, 14)        }    }}/// Search with: one choice, each said in a few words; Google last and plainly.struct SitesEnginePage: View {    @Bindable var model: SitesModel    var body: some View {        let row = model.settings?.rows.first { $0.id == "engine" }        ScrollView {            VStack(alignment: .leading, spacing: 0) {                SitesPageHead(back: model.settings?.title ?? SitesWords.line("you.title"), title: row?.label ?? SitesWords.line("you.engine")) { model.place = .you }                VStack(spacing: 0) {                    ForEach(Array((row?.choices ?? []).enumerated()), id: \.element.id) { at, choice in                        if at > 0 { SitesRule() }                        SitesOption(label: choice.label, line: choice.line, chosen: choice.chosen == true, tag: "sites-engine-\(choice.id)") {                            Task { await model.set("engine", .text(choice.id)) }                        }                    }                }                .padding(.top, 12)            }            .padding(.horizontal, Cranberry.side)            .padding(.bottom, 32)        }    }}/// Storage: what each site keeps on this phone, measured here, largest first, and Sign out of every site.struct SitesStoragePage: View {    @Bindable var model: SitesModel    @State private var asking = false    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 0) {                SitesPageHead(back: model.settings?.title ?? SitesWords.line("you.title"), title: model.storage?.title ?? SitesWords.line("you.storage")) { model.place = .you }                if let storage = model.storage {                    VStack(spacing: 0) {                        ForEach(Array(storage.rows.enumerated()), id: \.element.id) { at, row in                            if at > 0 { SitesRule() }                            HStack(spacing: 12) {                                SitesLetter(letter: row.letter)                                Text(row.label).font(SitesType.name).foregroundStyle(Cranberry.ink).frame(maxWidth: .infinity, alignment: .leading)                                Text(row.size).font(SitesType.line).foregroundStyle(Cranberry.quiet)                            }                            .padding(.vertical, 11)                            .accessibilityElement(children: .combine)                        }                    }                    .padding(.top, 10)                    if asking {                        Text(storage.allAsk).font(SitesType.body).foregroundStyle(Cranberry.ink)                            .fixedSize(horizontal: false, vertical: true).padding(.top, 20)                        HStack(spacing: 10) {                            Button(SitesWords.line("add.cancel")) { asking = false }.buttonStyle(SitesButtonStyle(kind: .quiet))                            Button(storage.all) { asking = false; Task { await model.signOutAll() } }.buttonStyle(SitesButtonStyle(kind: .go))                                .accessibilityIdentifier("sites-storage-all-yes")                        }                        .padding(.top, 12)                    } else {                        Button(storage.all) { asking = true }.buttonStyle(SitesButtonStyle(kind: .quiet))                            .padding(.top, 20)                            .accessibilityIdentifier("sites-storage-all")                    }                }            }            .padding(.horizontal, Cranberry.side)            .padding(.bottom, 32)        }    }}/// Sync, Privacy, Hidden sites, Help and feedback, About.struct SitesYouSubPage: View {    @Bindable var model: SitesModel    let place: SitesPlace    @State private var home = SettingsHome()    var body: some View {        let shared = model.settings?.shared ?? []        let rows = model.settings?.rows ?? []        let title: String = switch place {        case .sync: shared.first { $0.id == "sync" }?.label ?? ""        case .privacy: shared.first { $0.id == "privacy" }?.label ?? ""        case .hideAfter: rows.first { $0.id == "hide_after" }?.label ?? ""        case .help: shared.first { $0.id == "help" }?.label ?? ""        default: shared.first { $0.id == "about" }?.label ?? ""        }        ScrollView {            VStack(alignment: .leading, spacing: 0) {                SitesPageHead(back: model.settings?.title ?? SitesWords.line("you.title"), title: title) { model.place = .you }                Group {                    switch place {                    case .sync:                        Text(shared.first { $0.id == "sync" }?.line ?? "").font(SitesType.body).foregroundStyle(Cranberry.quiet)                            .fixedSize(horizontal: false, vertical: true).padding(.top, 8)                    case .privacy:                        Text(shared.first { $0.id == "privacy" }?.line ?? "").font(SitesType.body).foregroundStyle(Cranberry.ink)                            .fixedSize(horizontal: false, vertical: true).padding(.top, 8)                            .accessibilityIdentifier("sites-privacy-line")                    case .hideAfter:                        let row = rows.first { $0.id == "hide_after" }                        VStack(spacing: 0) {                            ForEach(Array((row?.choices ?? []).enumerated()), id: \.element.id) { at, choice in                                if at > 0 { SitesRule() }                                SitesOption(label: choice.label, chosen: row?.text == choice.id, tag: "sites-hide-\(choice.id)") {                                    Task { await model.set("hide_after", .text(choice.id)) }                                }                            }                        }                        .padding(.top, 12)                    case .help:                        SettingsSharedPage(place: .help, home: home, core: model.core, look: .sites, said: { model.tell($0) })                            .padding(.top, 12)                    default:                        SettingsSharedPage(place: .about, home: home, core: model.core, look: .sites, said: { model.tell($0) })                            .padding(.top, 12)                    }                }            }            .padding(.horizontal, Cranberry.side)            .padding(.bottom, 32)        }        .task { await home.read(model.core) }    }}/// Look: a live preview of two of his rows and the bar with the thread half drawn; papers, inks, faces, sizes, Reset./// Look changes Sites' paper, ink and type, never the pages themselves.struct SitesLookPage: View {    @Bindable var model: SitesModel    @State private var look = LookModel.standing("sites")    @State private var asking = false    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 0) {                SitesPageHead(back: model.settings?.title ?? SitesWords.line("you.title"), title: look.answer?.word("title") ?? "") { model.place = .you }                if let answer = look.answer {                    LookScreen(choices: LookChoices(answer), words: LookWords(answer), skin: Self.skin, gutter: 0,                               choose: { pick in Task { await look.choose(pick, core: model.core); tellRefusal() } },                               reset: { asking = true }) { dark in                        SitesLookPreview(answer: answer, dark: dark)                    } more: { EmptyView() }                    .padding(.top, 10)                }            }            .padding(.horizontal, Cranberry.side)            .padding(.bottom, 32)        }        .task { await look.read(model.core) }        .overlay {            if asking, let answer = look.answer {                SitesSheet(close: { asking = false }, tag: "sites-look-reset") {                    VStack(alignment: .leading, spacing: 0) {                        Text(answer.word("reset_ask")).font(SitesType.sans(21, .bold, .title3)).foregroundStyle(Cranberry.ink)                            .fixedSize(horizontal: false, vertical: true)                        Button(answer.word("reset_yes")) {                            asking = false                            Task { await look.reset(core: model.core); tellRefusal() }                        }                        .buttonStyle(SitesButtonStyle(kind: .go))                        .padding(.top, 20)                        .accessibilityIdentifier("sites-look-reset-yes")                    }                }            }        }    }    private func tellRefusal() {        if let refused = look.refused { model.tell(refused); look.refused = nil }    }    @MainActor static var skin: LookSkin {        LookSkin(ground: Cranberry.ground, ink: Cranberry.ink, quiet: Cranberry.quiet, accent: Cranberry.accent, onAccent: Cranberry.onAccent,                 sheet: Cranberry.ground, rule: Cranberry.rule,                 font: { size, weight in SitesType.sans(size, weight == .bold || weight == .semibold ? .semibold : (weight == .medium ? .medium : .regular)) },                 face: { id, size in                     id == LookFaces.own ? SitesType.own(size) : Font(WritingFonts.font(LookFaces.drawn(id, own: "sans"), size: size))                 })    }}/// The core's two sample rows and the bar with the thread half drawn, in the paper of one appearance.struct SitesLookPreview: View {    let answer: LookAnswer    let dark: Bool    /// The letters' rounds grow with the words in them.    @ScaledMetric(relativeTo: .body) private var round: CGFloat = 32    @ScaledMetric(relativeTo: .footnote) private var small: CGFloat = 24    private func colour(_ token: String, _ fallback: Color) -> Color { answer.colour(token, dark: dark) ?? fallback }    var body: some View {        let ink = colour("ink", Cranberry.ink), quiet = colour("quiet", Cranberry.quiet), rule = colour("rule", Cranberry.rule)        let accent = colour("accent", Cranberry.accent), ground = colour("ground", Cranberry.ground)        VStack(alignment: .leading, spacing: 0) {            ForEach(Array((answer.preview?.sites ?? []).enumerated()), id: \.offset) { at, row in                if at > 0 { Rectangle().fill(rule).frame(height: 1) }                HStack(spacing: 12) {                    Text(row.letter).font(SitesType.letter(14)).foregroundStyle(ink)                        .frame(width: round, height: round)                        .overlay(Circle().strokeBorder(ink, lineWidth: 1.3))                    VStack(alignment: .leading, spacing: 2) {                        Text(row.label).font(SitesType.name).foregroundStyle(ink)                        Text(row.line).font(SitesType.line).foregroundStyle(quiet)                    }                    Spacer(minLength: 0)                }                .padding(.vertical, 9)            }            if let bar = answer.preview?.bar {                VStack(spacing: 0) {                    ZStack(alignment: .leading) {                        Rectangle().fill(rule).frame(height: 1)                        if bar.thread == true {                            GeometryReader { g in Rectangle().fill(accent).frame(width: g.size.width * 0.58, height: 1.5) }.frame(height: 1.5)                        }                    }                    HStack(spacing: 9) {                        Text(bar.label.prefix(1)).font(SitesType.letter(11)).foregroundStyle(ink)                            .frame(width: small, height: small).overlay(Circle().strokeBorder(ink, lineWidth: 1.1))                        VStack(alignment: .leading, spacing: 0) {                            Text(bar.label).font(SitesType.barName).foregroundStyle(ink)                            Text(bar.site).font(SitesType.barAddress).foregroundStyle(quiet)                        }                        Spacer(minLength: 0)                    }                    .padding(.horizontal, 8).padding(.vertical, 6)                    .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(rule, lineWidth: 1))                    .padding(.top, 9)                }                .padding(.top, 6)            }        }        .padding(.horizontal, 14).padding(.top, 4).padding(.bottom, 10)        .background(RoundedRectangle(cornerRadius: 18).fill(ground))        .overlay(RoundedRectangle(cornerRadius: 18).strokeBorder(rule, lineWidth: 1))        .accessibilityElement(children: .combine)    }}/// Hidden: locked. Face ID (or the passcode) shows them; nothing says how many, or whether there are any.struct SitesHiddenLock: View {    @Bindable var model: SitesModel    @State private var asked = false    var body: some View {        VStack(spacing: 0) {            HStack {                Spacer()                Button(SitesWords.line("add.cancel")) { model.place = nil }                    .font(SitesType.sans(16, .semibold)).foregroundStyle(Cranberry.ink).buttonStyle(.plain)                    .accessibilityIdentifier("sites-lock-cancel")            }            .frame(minHeight: 40)            Spacer()            SitesRound(size: 64) { SitesGlyph(icon: .lock, size: 28) }            Text(SitesWords.line("hidden.label")).font(SitesType.sans(26, .bold, .title1)).foregroundStyle(Cranberry.ink)                .padding(.top, 24)                .accessibilityAddTraits(.isHeader)            Text(model.home?.hidden.line ?? SitesWords.line("hidden.line")).font(SitesType.bodySmall).foregroundStyle(Cranberry.quiet)                .multilineTextAlignment(.center)                .padding(.top, 10)            Button { Task { await ask() } } label: {                SitesRound(size: 52) { SitesGlyph(icon: .face, size: 26) }            }            .buttonStyle(.plain)            .padding(.top, 40)            .accessibilityLabel(SitesWords.line("hidden.unlock"))            .accessibilityIdentifier("sites-lock-face")            Spacer()            Button(SitesWords.line("hidden.unlock")) { Task { await ask() } }                .buttonStyle(SitesButtonStyle(kind: .quiet))                .accessibilityIdentifier("sites-lock-show")        }        .padding(.horizontal, Cranberry.side)        .padding(.bottom, 20)        .task {            guard !asked else { return }            asked = true            try? await Task.sleep(for: .milliseconds(350))            await ask()        }    }    private func ask() async {        if await model.unlock() { model.place = nil }    }}#endif