Cotton code
43 lines · 2.0 KB · SwiftRaw
// The shipped themes' rows, which is what `Theme.paper` and `Theme.plain` are// laid from (`ThemeDefaults.swift`).//// The look draws before any core has answered: the share extension, a test// and the Mac's windows never ask one, and the phone reads its first theme// before its core is open. So the rows come from the core's answer once this// process has one (`DefinitionCatalogue.receiveLastResort` hands them here), and// until then from `Resources/Themes/themes.json` — the `themes` of the// catalogue snapshot `CottonUI` carries, written beside it by the same// generation and held equal to it by the core's guard// (`the_looks_themes_are_the_snapshots`). The look reads only themes and// never decodes a definition.import Foundationimport ospublic enum ShippedThemes {    /// The bundled rows, by theme id, each its row's JSON: read once. Empty    /// when this build carries no resources at all; it never traps.    static let bundled: [String: Data] = {        guard            let url = CottonResources.bundle?.url(forResource: "themes", withExtension: "json", subdirectory: "Themes"),            let data = try? Data(contentsOf: url),            let rows = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]        else { return [:] }        return rows.compactMapValues { try? JSONSerialization.data(withJSONObject: $0) }    }()    private static let received = OSAllocatedUnfairLock<[String: Data]?>(initialState: nil)    /// The core's own answer, once this process has one.    public static func receive(_ rows: [String: Data]) {        received.withLock { $0 = rows }    }    /// The shipped themes' rows: the core's answer once it has handed one    /// over, else the bundled rows — read WITHOUT decoding a table. A theme is    /// the first thing a screen reads, before the app's core has answered.    public static var rows: [String: Data] {        if let answer = received.withLock({ $0 }) { return answer }        return bundled    }}