Cotton code
78 lines · 3.2 KB · SwiftRaw
// 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.import CottonKitimport Foundationimport Observation@MainActor@Observablepublic final class ThemeModel {    /// What the window draws in.    public private(set) var theme: Theme = .paper    /// Why the database's own theme row was not drawn, when it was not.    public private(set) var passedOver: String?    private let 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.    private let look: FlavourLook?    public init(themes: any ThemesCalling, look: FlavourLook? = .current) {        self.themes = themes        self.look = look        // A business app's cold start is already the business's look.        if let 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.    public func refresh() async {        guard let worn = try? await themes.theme() else { return }        var read = Theme.decode(worn.definition)        if let 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.    @discardableResult    public func wear(_ theme: Theme) async throws -> ActAnswer {        let act = try await themes.save(theme.row)        await refresh()        return act    }    /// A project's brand kit, laid over what the database wears, in one act.    @discardableResult    public func wear(brand: JSONValue) async throws -> ActAnswer {        try await wear(theme.applying(brand: ThemeBrandKit(record: brand)))    }    /// Take a worn theme back, and draw what was worn before.    @discardableResult    public func takeBack(_ undo: ActUndo) async throws -> ActAnswer {        let act = try await themes.takeBack(undo)        await refresh()        return act    }}