Cotton code
80 lines · 4.0 KB · SwiftRaw
// A business flavour's look, as data: the theme a flavour's app wears where// its database wears no theme row of its own.//// Cotton ships in flavours (`FlavourManifest`), and a business app looks like// the business — Sharmila's Yoga Zone in coral and teal on cream, set in// Montserrat; Nancy's Naga Kitchen on a cream sheet under dark green// headings. That look is A THEME ROW, in the row's own spelling, read by the// row's own decoder (`Theme.decode`): its family, the palette roles it names// and the type roles it names, every key it leaves out the family's shipped// value. Nothing here holds a colour or a face; the files do.//// NO APP SHIPS ONE NOW (22 Nov 2026): the two business apps that wore a look,// Sharmila's and Nancy's, left the tree with their `Resources/Looks` files,// so `shipped(for:)` answers nil for every flavour. The machinery stays for// the business flavours to come; the tests wear a fixture row// (`GalleryFaces.kitchen`).//// WHERE IT LIVES. `Resources/Looks/<flavour>.json` in this package, by the// manifest's `flavour` key (copied by `Package.swift` once a look ships). Not inside `flavour.json`: the core reads that// file with unknown keys refused (`BusinessManifest`), and a look is the// screens' business alone. In the package, not the app bundle, so the look// draws before any core answers — the first frame of a business app is// already the business's — and a test reads it the way the app does.//// WHEN IT IS WORN. Only where the database wears the shipped default: a row// the database chose is the database's (`ThemeRecordWinsTests`'s rule), and// a flavour with no look — Cotton, Cotton Project, Rigby, a test bundle —// wears exactly what it wore before (`ThemeModel`).//// HOW A FACE READS IT. Through `\.theme`, like every face: in a flavour's// app the worn theme IS the look, so `theme.colors.paper` is Nancy's cream// sheet and `theme.colors.ink` her dark green headings, with nothing// flavour-specific in the face. A face or a gallery that must name one look// whatever app it runs in asks `FlavourLook.shipped(for: <flavour>)?.theme`.//// THE CORE'S ROWS. Every face a look names is one the core's theme rows take// too (`montserrat`, `montserrat_semibold` are in `THEME_FACES`,// `cotton-core/src/definitions/theme.rs`), so a database may keep the same// look as its own row; the look ships in the app so a new database wears it// from the first frame.//// WHICH FLAVOUR THIS PROCESS IS, the look does not know: that is the app's// manifest, which CottonUI reads (`FlavourLook.current`, beside `ThemeModel`)// and hands here as a plain name. The look reads its own bundle and nothing// of the app's.import Foundationpublic struct FlavourLook: Sendable {    /// The flavour this look is (`FlavourManifest.flavour`).    public let flavour: String    /// The theme the look draws: its row over its family's shipped row.    public let theme: Theme    /// The folder the looks are copied into (`Package.swift`).    static let folder = "Looks"    /// The look a row describes; nil for data that is not a theme row.    public init?(flavour: String, row: Data) {        guard (try? JSONDecoder().decode(ThemeRowForm.self, from: row)) != nil else { return nil }        self.flavour = flavour        self.theme = Theme.decode(row)    }    /// The look this build ships for `flavour`; nil for a flavour with none,    /// or no flavour at all.    public static func shipped(for flavour: String?) -> FlavourLook? {        guard let flavour, !flavour.isEmpty,            let url = CottonResources.bundle?.url(forResource: flavour, withExtension: "json", subdirectory: folder),            let data = try? Data(contentsOf: url)        else { return nil }        return FlavourLook(flavour: flavour, row: data)    }    /// What the window draws for `worn`: the database's own row as it is,    /// the shipped default dressed as this flavour.    public func dressing(_ worn: Theme, own: Bool) -> Theme {        own ? worn : theme    }}