Cotton code
182 lines · 8.1 KB · SwiftRaw
// What a card wears, as the theme says it (lane lookdata, 13 Oct 2026): the// stock each class of act is printed on, and the field card's defaults — the// stock a field is printed on when nothing names another, the stock an// overdue date or a red choice turns, the stack the tray's button draws, the// dashed outline of an empty card and how faint an empty or switched-off// object stands.//// THE SWIFT HOLDS NO VALUE HERE. These are the typed reading of the theme row// (`theme.acts`, `theme.cards`); every value comes from the theme the core// answers, and a key a person's theme leaves out is the shipped theme's,// resolved in the core's theme read — never a constant in a face. A stock// word this build does not print keeps the shipped theme's.import SwiftUI/// The stock each class of act is printed on (`theme.acts.<class>.stock`)./// An act's class is what its definition's `does` says it does to the record;/// `default` is an act that names none, and `default_held` one held for a/// reason its definition does not name.public struct ThemeActs: Hashable, Sendable {    public var completes: DefinitionCardStock    public var times: DefinitionCardStock    public var moves: DefinitionCardStock    public var destroys: DefinitionCardStock    public var putsAway: DefinitionCardStock    public var unnamed: DefinitionCardStock    public var unnamedHeld: DefinitionCardStock    /// The key each class's stock is spelled under in the row.    static let stockKey = "stock"    /// The row's spelling of each class.    static var classes: [(name: String, path: WritableKeyPath<ThemeActs, DefinitionCardStock>)] {        [            ("completes", \.completes), ("times", \.times), ("moves", \.moves), ("destroys", \.destroys),            ("puts_away", \.putsAway), ("default", \.unnamed), ("default_held", \.unnamedHeld),        ]    }}/// A field card's defaults (`theme.cards.*`).public struct ThemeCards: Hashable, Sendable {    /// A field card's stock when neither its definition nor a linked tool says one.    public var fieldDefaultStock: DefinitionCardStock    /// A date that is overdue, and a choice its column marks red.    public var overdueStock: DefinitionCardStock    /// The three cards the tray's stack button draws, back to front.    public var trayStackStocks: [DefinitionCardStock]    /// An empty card's dashed outline: the dash and the pencil's strength.    public var emptyOutlineDash: [CGFloat]    public var emptyOutlineOpacity: Double    /// How strongly an empty field's object stands, and a switch that is off.    public var faintObjectOpacity: Double    public var offObjectOpacity: Double    /// The object each kind of field stands on its card, by the kind's or    /// the column type's name (`theme.objects`: a date a `leaf`, a time a    /// `clock`, a reminder a `bell`…).    public var objects: [String: String]    /// A business look's marks, each the NAME of one of its palette roles —    /// never a colour of its own, so the brand's teal and coral live once, in    /// the palette: the role the fan's current tile is filled with    /// (`current_tile_fill`) and the role the primary act on the long-press    /// fan is printed on (`primary_act_fill`), its words in    /// `accent_contrast`. A theme that names neither — Cotton's, Rigby's,    /// every shipped row — draws its fan as it always did.    public var currentTileFill: String? = nil    public var primaryActFill: String? = nil    /// The row's spellings of the two list-valued card keys.    static let trayStackStocksKey = "tray_stack_stocks"    static let emptyOutlineDashKey = "empty_outline_dash"    /// The row's spellings of the role-valued card keys.    static var fills: [(name: String, path: WritableKeyPath<ThemeCards, String?>)] {        [("current_tile_fill", \.currentTileFill), ("primary_act_fill", \.primaryActFill)]    }    /// The object a card names by `key`: the theme's word, or the shipped    /// theme's for a key it leaves out.    public func object(named key: String) -> String? {        objects[key] ?? Theme.paper.cards.objects[key]    }    static var stocks: [(name: String, path: WritableKeyPath<ThemeCards, DefinitionCardStock>)] {        [            ("field_default_stock", \.fieldDefaultStock), ("overdue_stock", \.overdueStock),        ]    }    static var opacities: [(name: String, path: WritableKeyPath<ThemeCards, Double>)] {        [            ("empty_outline_opacity", \.emptyOutlineOpacity), ("faint_object_opacity", \.faintObjectOpacity),            ("off_object_opacity", \.offObjectOpacity),        ]    }}extension Theme {    /// The colour the fan's current tile is filled with — the palette role    /// the look names (`cards.current_tile_fill`) — or nil, the tile drawn    /// as every other.    public var currentTileFill: Color? { cards.currentTileFill.flatMap { colors.role(named: $0)?.color } }    /// The colour the primary act on the long-press fan is printed on    /// (`cards.primary_act_fill`), or nil, the act drawn as every other.    public var primaryActFill: Color? { cards.primaryActFill.flatMap { colors.role(named: $0)?.color } }    /// The words set on either fill: the ink the look sets on its accent.    public var fillInk: Color { colors.accentContrast.color }}extension ThemePalette {    /// A colour role by the name a row spells it (`ink`, `accent`…).    public func role(named name: String) -> ThemeColor? {        Self.roles.first { $0.name == name }.map { self[keyPath: $0.path] }    }}extension ThemeActs {    /// The row's `acts` laid over these: each class's `stock`, when it is a    /// stock this build prints.    func overridden(by row: [String: [String: String]]) -> ThemeActs {        var acts = self        for act in Self.classes {            if let word = row[act.name]?[Self.stockKey], let stock = DefinitionCardStock(rawValue: word) { acts[keyPath: act.path] = stock }        }        return acts    }}extension ThemeCards {    /// The row's `cards` laid over these, each value only when it reads as    /// what its key holds.    func overridden(by row: [String: ThemeValue]) -> ThemeCards {        var cards = self        for key in Self.stocks {            if case .word(let word) = row[key.name], let stock = DefinitionCardStock(rawValue: word) { cards[keyPath: key.path] = stock }        }        for key in Self.opacities {            if case .number(let value) = row[key.name], (0...1).contains(value) { cards[keyPath: key.path] = value }        }        // A fill names a palette role; any other word is not one.        for key in Self.fills {            if case .word(let role) = row[key.name], ThemePalette.roles.contains(where: { $0.name == role }) {                cards[keyPath: key.path] = role            }        }        if case .list(let words) = row[Self.trayStackStocksKey] {            let read = words.compactMap { $0.word.flatMap(DefinitionCardStock.init(rawValue:)) }            if !read.isEmpty, read.count == words.count { cards.trayStackStocks = read }        }        if case .list(let dash) = row[Self.emptyOutlineDashKey] {            let read = dash.compactMap { $0.number.map { CGFloat($0) } }            if !read.isEmpty, read.count == dash.count, read.allSatisfy({ $0 > 0 }) { cards.emptyOutlineDash = read }        }        return cards    }}/// One value of a theme group whose keys hold words, numbers or lists of them.enum ThemeValue: Decodable, Hashable {    case word(String)    case number(Double)    case list([ThemeValue])    case other    var word: String? { if case .word(let word) = self { word } else { nil } }    var number: Double? { if case .number(let number) = self { number } else { nil } }    init(from decoder: Decoder) throws {        let container = try decoder.singleValueContainer()        if let number = try? container.decode(ThemeNumber.self) {            self = .number(number.value)        } else if let word = try? container.decode(String.self) {            self = .word(word)        } else if let list = try? container.decode([ThemeValue].self) {            self = .list(list)        } else {            self = .other        }    }}