Cotton code
126 lines · 6.1 KB · SwiftRaw
// NOTES' LOOK (assets/design/notes/DECISIONS.md "Look"): the theme standing// on this phone for light and dark, and the faces Notes is set in (Figtree for// the words, JetBrains Mono for code). The themes and the choice are the// core's (`notes.themes`, `notes.settings`, CONTRACT.md §10): what// `notes.settings` last answered stands, and is kept on this phone so the next// launch's first frame (every shell card, the gate, the sign-in sheet, the// splash: `AppLook`) already stands in it, before the core has answered.// Before the first answer ever, the launch screen's own ground stands.#if os(iOS)import CottonKitimport CottonWritingimport SwiftUIimport UIKitenum NotesLook {    /// The theme standing now for the dark appearance or the light: read through `NotesStanding`, so a    /// screen drawn in it draws again when it changes.    @MainActor static func standing(dark: Bool) -> WritingTheme {        let standing = NotesStanding.shared        return WritingTheme(id: dark ? standing.darkId : standing.lightId, name: "", dark: dark,                            colours: dark ? standing.dark : standing.light)    }    /// The core's face id as Notes draws it: its `sans` is Figtree, carried by Notes alone; Look's faces    /// (shared/DECISIONS.md "Look") are the bundled `LookFonts`: `serif` Literata, `typewriter` Courier Prime,    /// `hyperlegible` Atkinson Hyperlegible Next, `dyslexic` OpenDyslexic.    static func face(_ id: String) -> String {        switch id {        case "sans": WritingFonts.hasFigtree ? "figtree" : id        case "serif": WritingFonts.carries("literata") ? "literata" : id        case "typewriter": "courier"        case "hyperlegible": "atkinson"        case "dyslexic": "opendyslexic"        default: id        }    }}/// The look `notes.settings` last answered, watched: a screen that reads it draws again when it changes.@MainActor@Observablefinal class NotesStanding {    static let shared = NotesStanding()    private(set) var lightId: String    private(set) var darkId: String    private(set) var light: WritingTheme.Colours    private(set) var dark: WritingTheme.Colours    private(set) var hideMarks: Bool    /// What is kept for the next launch: the core's last answer, as it said it.    private struct Kept: Codable {        var lightId: String        var darkId: String        var light: WritingTheme.Colours        var dark: WritingTheme.Colours        var hideMarks: Bool    }    private static let key = "notes-look.standing"    private init() {        let defaults = UserDefaults.standard        // What earlier builds kept on the phone before the core carried the look: gone.        for old in ["light", "dark", "hideMarks", "paragraph", "pinnedNotes", "pinnedTags"] { defaults.removeObject(forKey: "notes-look.\(old)") }        for name in defaults.dictionaryRepresentation().keys where name.hasPrefix("notes-look.width:") { defaults.removeObject(forKey: name) }        if let data = defaults.data(forKey: Self.key), let kept = try? JSONDecoder().decode(Kept.self, from: data) {            lightId = kept.lightId            darkId = kept.darkId            light = kept.light            dark = kept.dark            hideMarks = kept.hideMarks        } else {            lightId = ""            darkId = ""            light = Self.launch(dark: false)            dark = Self.launch(dark: true)            hideMarks = true        }    }    /// The core's answer taken: it stands now, and is kept for the next launch's first frame.    func take(_ chosen: NotesSettingsChosen) {        lightId = chosen.lightTheme        darkId = chosen.darkTheme        light = chosen.light        dark = chosen.dark        hideMarks = chosen.hideMarks        let kept = Kept(lightId: lightId, darkId: darkId, light: light, dark: dark, hideMarks: hideMarks)        if let data = try? JSONEncoder().encode(kept) { UserDefaults.standard.set(data, forKey: Self.key) }    }    /// Before the core's first answer ever: the launch screen's ground (`LaunchGround`), in the system's ink.    private static func launch(dark: Bool) -> WritingTheme.Colours {        let traits = UITraitCollection(userInterfaceStyle: dark ? .dark : .light)        func hex(_ colour: UIColor?) -> String {            var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0            (colour ?? .systemBackground).resolvedColor(with: traits).getRed(&r, green: &g, blue: &b, alpha: &a)            return String(format: "#%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))        }        let ground = hex(UIColor(named: "LaunchGround") ?? .systemBackground)        return WritingTheme.Colours(ground: ground, text: hex(.label), heading: hex(.label), secondary: hex(.secondaryLabel),                                    accent: hex(.tintColor), rule: hex(.separator), highlight: hex(.systemYellow), codeGround: hex(.secondarySystemBackground),                                    selection: hex(.systemGray4), link: hex(.label), danger: hex(.systemRed))    }}#endif#if os(iOS)extension NotesLook {    /// THE APP LOOK Notes sets (research.md A5): the shell's own pieces inside Notes (the sealed-key and    /// recovery cards, the sign-in ask, the unlock request, the read-only card, the sign-in sheet, the gate,    /// the toasts) draw the app look the root hands them (`\.vaultLook`), as they do in Vault and Health. Notes    /// answers it from its standing theme, light and dark, in Figtree and JetBrains Mono, so every one of them    /// follows Notes' theme from the first frame, before the core has said anything.    @MainActor static var appLook: VaultLook {        let standing = NotesStanding.shared        func inks(_ c: WritingTheme.Colours, dark: Bool) -> AppLookInks {            AppLookInks(ground: c.ground, field: c.codeGround, rule: c.rule, text: c.text, secondary: c.secondary, accent: c.accent,                        onAccent: c.ground, danger: c.danger ?? c.accent, safe: dark ? "#7FBF8E" : "#3E7D4F", scrim: dark ? "#0000008A" : "#00000047")        }        return VaultLook.app(light: inks(standing.light, dark: false), dark: inks(standing.dark, dark: true), text: .figtree, display: .figtree, mono: .jetbrains)    }}#endif