// The theme the shown database wears, read from the core and set at the root.//// `definitions.theme` answers the database's own theme row when one stands and// the shipped `paper` otherwise, so this never leaves a screen without a look:// until the first answer, and whenever a read fails, the window keeps what it// last drew (Cotton's own theme on a cold start). A row this build cannot draw// — one that needs a newer revision, or is malformed — is passed over by the// core, which says why; the window draws the default and keeps the reason.//// WEARING A THEME IS ONE ACT. The theme is written whole as the database's// own row (`definitions.saveTheme`) and read straight back, so the window// changes the moment the write lands and never draws a theme the core// refused; the act answers the core's receipt and the undo that puts back// what was worn. The brand kit is the same act with the kit laid over the// theme the database wears now (`Theme.applying(brand:)`).//// THIS IS WHERE THE APP BUILDS ITS THEME, so it is where what the core and the// manifest say becomes the look's plain values (`ThemeAppLook.swift`): the// worn row, the flavour's look, the brand kit. The look (`CottonPaper`) reads// none of them itself.importCottonKitimportFoundationimportObservation@MainActor@ObservablepublicfinalclassThemeModel {/// What the window draws in.publicprivate(set) var theme: Theme = .paper/// Why the database's own theme row was not drawn, when it was not.publicprivate(set) var passedOver: String?privatelet themes: any ThemesCalling/// This app's flavour look, worn where the database wears no row of its/// own (`FlavourLook`); none for Cotton, whose window is exactly the/// database's theme.privatelet look: FlavourLook?publicinit(themes: any ThemesCalling, look: FlavourLook? = .current) {self.themes = themesself.look = look// A business app's cold start is already the business's look.iflet look { theme = look.theme } }/// Read the shown database's theme again — on a database switch and on/// every write that lands, since a theme row syncs like any other.publicfunc refresh() async {guardlet worn = try? await themes.theme() else { return }var read = Theme.decode(worn.definition)iflet look { read = look.dressing(read, own: worn.own) }if read != theme { theme = read } passedOver = worn.passedOver }/// Wear `theme` on the shown database, and draw it. @discardableResultpublicfunc wear(_ theme: Theme) asyncthrows -> ActAnswer {let act = tryawait themes.save(theme.row)await refresh()return act }/// A project's brand kit, laid over what the database wears, in one act. @discardableResultpublicfunc wear(brand: JSONValue) asyncthrows -> ActAnswer {tryawait wear(theme.applying(brand: ThemeBrandKit(record: brand))) }/// Take a worn theme back, and draw what was worn before. @discardableResultpublicfunc takeBack(_ undo: ActUndo) asyncthrows -> ActAnswer {let act = tryawait themes.takeBack(undo)await refresh()return act }}