// THE SKIN. A face has structure — what goes where, which the table and tool// definitions already carry as data — and skin: colour, type, corner, spacing,// texture, shadow. The skin is this value, and it is the only place a face may// read one (design note 26203e21, section 6).//// One value, carried down the view tree as `\.theme`. Every face, page style// and paper reads its inks, its faces, its corner and its lift from it, never// from a constant; `ThemeScanGuardTests` reads `Faces/`, `Surfaces/PageStyles/`// and `Paper/` and fails on a palette, font or metric table named there.//// THE FAMILY IS CODE, THE THEME IS DATA. A family is a way of drawing — `paper`// (the photographed sheets, the typewriter, the bottle caps: Cotton's own look)// or `plain` (flat cards, system type, no texture) — and it ships in the// binary. A theme is a row in a database that picks a family and sets its// knobs, so a business's members see the business's look without a build.// Thousands of theme rows over a handful of families, never thousands of// families.//// `Theme.paper` IS TODAY'S COTTON, VALUE FOR VALUE. Every number in it is the// constant a face read before the theme existed (`Palettes.paper`, the// ribbon's channels, `CottonFonts`' faces, `PaperMetrics`, `Metrics`), so the// app draws pixel-identical with it — `ThemeDefaultTests` holds each value to// its source and `ThemeSnapshotTests` holds the pixels.importCoreTextimportSwiftUI#if canImport(UIKit)import UIKit#endif/// One database's look: a family and the knobs it is drawn with.publicstructTheme: Equatable, Sendable {/// The theme's parts live in one shared box, copied only on a write: a/// face reads `theme` from its environment hundreds of times a body, and/// an inline Theme (two palettes, the type, ~1,000 scale keys) copied/// onto the stack at each read ran the record faces and SlipFace past/// the phone's stack budget (`FaceStackTests`, 13 Oct 2026). A Theme value is/// now one reference and an appearance.privatefinalclassStorage: @unchecked Sendable {var id: Stringvar name: Stringvar family: ThemeFamilyvar palette: ThemePalettesvar type: ThemeTypevar scale: ThemeScalevar texture: ThemeTexturevar cap: ThemeCapvar acts: ThemeActsvar cards: ThemeCardsinit(id: String, name: String, family: ThemeFamily, palette: ThemePalettes, type: ThemeType, scale: ThemeScale, texture: ThemeTexture, cap: ThemeCap, acts: ThemeActs, cards: ThemeCards) {self.id = idself.name = nameself.family = familyself.palette = paletteself.type = typeself.scale = scaleself.texture = textureself.cap = capself.acts = actsself.cards = cards }func copy() -> Storage {Storage(id: id, name: name, family: family, palette: palette, type: type, scale: scale, texture: texture, cap: cap, acts: acts, cards: cards) } }privatevar storage: Storage/// The row's id; `default` for the built-in theme a database is seeded with.publicvar id: String { get { storage.id } set { unique(); storage.id = newValue } }publicvar name: String { get { storage.name } set { unique(); storage.name = newValue } }publicvar family: ThemeFamily { get { storage.family } set { unique(); storage.family = newValue } }publicvar palette: ThemePalettes { get { storage.palette } set { unique(); storage.palette = newValue } }publicvar type: ThemeType { get { storage.type } set { unique(); storage.type = newValue } }publicvar scale: ThemeScale { get { storage.scale } set { unique(); storage.scale = newValue } }publicvar texture: ThemeTexture { get { storage.texture } set { unique(); storage.texture = newValue } }publicvar cap: ThemeCap { get { storage.cap } set { unique(); storage.cap = newValue } }/// The stock each class of act is printed on.publicvar acts: ThemeActs { get { storage.acts } set { unique(); storage.acts = newValue } }/// A field card's defaults.publicvar cards: ThemeCards { get { storage.cards } set { unique(); storage.cards = newValue } }/// Which of the palette's two appearances is drawn. Set by `themed(_:)`/// at the root from the colour scheme, so a face reads `colors` and never/// asks the scheme itself.publicvar appearance: ColorScheme = .light/// The palette for the appearance being drawn.publicvar colors: ThemePalette { palette.resolved(appearance) }publicinit( id: String, name: String, family: ThemeFamily, palette: ThemePalettes, type: ThemeType, scale: ThemeScale, texture: ThemeTexture, cap: ThemeCap, acts: ThemeActs, cards: ThemeCards ) { storage = Storage(id: id, name: name, family: family, palette: palette, type: type, scale: scale, texture: texture, cap: cap, acts: acts, cards: cards) }privatemutatingfunc unique() {if !isKnownUniquelyReferenced(&storage) { storage = storage.copy() } }publicstaticfunc == (lhs: Theme, rhs: Theme) -> Bool { lhs.appearance == rhs.appearance && (lhs.storage === rhs.storage || (lhs.id == rhs.id && lhs.name == rhs.name && lhs.family == rhs.family && lhs.palette == rhs.palette && lhs.type == rhs.type && lhs.scale == rhs.scale && lhs.texture == rhs.texture && lhs.cap == rhs.cap && lhs.acts == rhs.acts && lhs.cards == rhs.cards)) }/// The family's own default theme — what a row that names only a family/// draws, and what every key a row leaves out falls back to.publicstaticfunc standard(_ family: ThemeFamily) -> Theme {switch family {case .paper: return .papercase .plain: return .plain } }}/// The ways of drawing that ship in the binary. Each draws every structure/// key a definition can name, so every definition renders in every family.publicenumThemeFamily: String, CaseIterable, Sendable {/// Cotton's own: photographed paper, the typewriter and the hand, enamel/// caps, a sheet tilted a hair and lying on the plaster.case paper/// Flat cards in the palette's own ground, the system face, no grain, no/// tilt, solid tags; corner and spacing straight from the theme.case plain}// MARK: - Palette/// One colour a theme names, as its four channels.////// Channels rather than a hex string because two of today's paints were/// written as channels (the ribbon, the photograph's fallbacks) and a hex/// rounds them; a row uses hex or precise CSS sRGB (`ThemeRow`).publicstructThemeColor: Equatable, Sendable {publicvar red: Doublepublicvar green: Doublepublicvar blue: Doublepublicvar alpha: Doublepublicinit(red: Double, green: Double, blue: Double, alpha: Double = 1) {self.red = redself.green = greenself.blue = blueself.alpha = alpha }/// RGBA supplied by a native surface or decoded document. These are/// observed values, including extended sRGB, rather than authored ink.publicinit?(components: [Double]) {guard components.count == 4, components.allSatisfy(\.isFinite) else { returnnil } red = components[0] green = components[1] blue = components[2] alpha = components[3] }/// Native paint over an opaque ground, retaining the supplied channels.publicfunc composited(over ground: ThemeColor) -> ThemeColor {var result = ground result.red = red * alpha + ground.red * (1 - alpha) result.green = green * alpha + ground.green * (1 - alpha) result.blue = blue * alpha + ground.blue * (1 - alpha) result.alpha = 1return result }/// From an authored palette colour.publicinit(_ authored: CottonColor) {let parts = authored.components red = parts.red green = parts.green blue = parts.blue alpha = parts.alpha }/// From `#RRGGBB` or `#RRGGBBAA`; nil for anything else, so a malformed/// row keeps the family's value instead of drawing black.publicinit?(hex: String) {let digits = hex.hasPrefix("#") ? String(hex.dropFirst()) : hexguard digits.count == 6 || digits.count == 8, let value = UInt64(digits, radix: 16) else { returnnil }let rgba = digits.count == 6 ? value << 8 | 0xFF : valuefunc channel(_ shift: UInt64) -> Double { Double((rgba >> shift) & 0xFF) / 255 } red = channel(24) green = channel(16) blue = channel(8) alpha = channel(0) }/// An authored palette value: hex, or CSS's precise three-channel sRGB/// form. Reject non-finite and out-of-gamut values at the data boundary.publicinit?(palette value: String) {iflet hex = ThemeColor(hex: value) { self = hex; return }guard value.hasPrefix("color(srgb "), value.hasSuffix(")") else { returnnil }let fields = value.dropFirst(11).dropLast().split(whereSeparator: \.isWhitespace)guard fields.count == 3else { returnnil }let channels = fields.compactMap { Double($0) }guard channels.count == 3, channels.allSatisfy({ $0.isFinite && (0...1).contains($0) }) else { returnnil } red = channels[0] green = channels[1] blue = channels[2] alpha = 1 }publicvar color: Color {Color(.sRGB, red: red, green: green, blue: blue, opacity: alpha) }publicfunc opacity(_ amount: Double) -> Color { color.opacity(amount) } #if canImport(UIKit)/// For the UIKit seams a face hosts (a PDF view, an image view).publicvar uiColor: UIColor { UIColor(cgColor: CGColor(srgbRed: red, green: green, blue: blue, alpha: alpha)) } #endif}/// The palette for each appearance. Cotton is light-only today, so the/// default's dark is its light; a theme row may give a dark of its own.publicstructThemePalettes: Equatable, Sendable {publicvar light: ThemePalettepublicvar dark: ThemePalettepublicinit(light: ThemePalette, dark: ThemePalette) {self.light = lightself.dark = dark }publicfunc resolved(_ scheme: ColorScheme) -> ThemePalette { scheme == .dark ? dark : light }}/// Every colour a face paints with, by role.////// The chrome roles are `CottonPalette`'s, so a face on the page ground and a/// settings row beside it agree. The print roles are what is set ON an object:/// the ribbon a sheet is typed with and the pencil that annotates it. The/// colour rule still binds every theme: monochrome plus one accent, state in/// the status colours, and colour never names a kind.publicstructThemePalette: Equatable, Sendable {publicvar ground: ThemeColorpublicvar groundMuted: ThemeColorpublicvar groundElevated: ThemeColorpublicvar ink: ThemeColorpublicvar inkBody: ThemeColorpublicvar inkSecondary: ThemeColorpublicvar inkTertiary: ThemeColorpublicvar hairline: ThemeColorpublicvar accent: ThemeColorpublicvar accentContrast: ThemeColorpublicvar chipGround: ThemeColorpublicvar highlight: ThemeColorpublicvar selection: ThemeColorpublicvar success: ThemeColorpublicvar successLight: ThemeColorpublicvar error: ThemeColorpublicvar errorLight: ThemeColorpublicvar warning: ThemeColorpublicvar warningLight: ThemeColor/// The ink typed on an object — the ribbon on paper.publicvar print: ThemeColor/// How much of `print` the pencil is: annotation, dates, counts.publicvar pencil: Double/// A card's own ground — what a sheet is filled with in a family that/// draws its cards flat. Paper's sheets are photographs and ignore it.publicvar paper: ThemeColor/// The colour a shadow is cast in.publicvar shadow: ThemeColor/// Each paper's measured colour, as the theme row says it; a dye it/// does not name is `PaperDye`'s own measurement.publicvar dyes: [PaperDye: ThemeColor] = [:]/// The paper a card is tinted to by its pace band (`late`, `soon`,/// `upcoming`, `someday`, `done`), as the theme row says it.publicvar paces: [String: ThemeColor] = [:]/// The thin rule printed on a card, by its pace band.publicvar paceRules: [String: ThemeColor] = [:]/// The deck faces' stocks and inks (`palette.faces`: cream, buff,/// shadow, slip, mount, ornament, ruling, margin, initials); an ink's/// strength is its alpha.publicvar faces: [String: ThemeColor] = [:]/// A tag: its solid fill and the ink set on it, in a family whose tags/// are solid. Paper's tags are slips of straw paper and ignore it.publicvar tags: ThemeTagspublicinit( ground: ThemeColor, groundMuted: ThemeColor, groundElevated: ThemeColor, ink: ThemeColor, inkBody: ThemeColor, inkSecondary: ThemeColor, inkTertiary: ThemeColor, hairline: ThemeColor, accent: ThemeColor, accentContrast: ThemeColor, chipGround: ThemeColor, highlight: ThemeColor, selection: ThemeColor, success: ThemeColor, successLight: ThemeColor, error: ThemeColor, errorLight: ThemeColor, warning: ThemeColor, warningLight: ThemeColor, print: ThemeColor, pencil: Double, paper: ThemeColor, shadow: ThemeColor, tags: ThemeTags ) {self.ground = groundself.groundMuted = groundMutedself.groundElevated = groundElevatedself.ink = inkself.inkBody = inkBodyself.inkSecondary = inkSecondaryself.inkTertiary = inkTertiaryself.hairline = hairlineself.accent = accentself.accentContrast = accentContrastself.chipGround = chipGroundself.highlight = highlightself.selection = selectionself.success = successself.successLight = successLightself.error = errorself.errorLight = errorLightself.warning = warningself.warningLight = warningLightself.print = printself.pencil = pencilself.paper = paperself.shadow = shadowself.tags = tags }/// A text ink by name — `TextInk`'s levels.publicfunc color(_ ink: TextInk) -> Color {switch ink {case .ground: return ground.colorcase .ink: returnself.ink.colorcase .body: return inkBody.colorcase .secondary: return inkSecondary.colorcase .tertiary: return inkTertiary.colorcase .accent: return accent.colorcase .inverse: return accentContrast.colorcase .error: return error.color } }/// A palette slot by name — the page ground a screen asks for. The Mac/// sidebar's slots are window chrome beside every theme and keep the/// generated palette's.publicfunc color(_ slot: ColorSlot) -> Color {switch slot {case .ground: return ground.colorcase .groundMuted: return groundMuted.colorcase .groundElevated: return groundElevated.colorcase .ink: return ink.colorcase .inkBody: return inkBody.colorcase .inkSecondary: return inkSecondary.colorcase .inkTertiary: return inkTertiary.colorcase .hairline: return hairline.colorcase .accent: return accent.colorcase .accentContrast: return accentContrast.colorcase .chipGround: return chipGround.colorcase .highlight: return highlight.colorcase .selection: return selection.colorcase .statusSuccess: return success.colorcase .statusSuccessLight: return successLight.colorcase .statusError: return error.colorcase .statusErrorLight: return errorLight.colorcase .statusWarning: return warning.colorcase .statusWarningLight: return warningLight.colorcase .sidebarGround, .sidebarInk, .sidebarInkMuted, .sidebarSelection, .sidebarHairline:return slot.color } }/// A print ink by name — `PaperInk`'s four.publicfunc color(_ ink: PaperInk) -> Color {switch ink {case .ink: return print.colorcase .pencil: return print.color.opacity(pencil)case .red: return error.colorcase .green: return success.color } }}/// A solid tag.publicstructThemeTags: Equatable, Sendable {publicvar fill: ThemeColorpublicvar ink: ThemeColorpublicinit(fill: ThemeColor, ink: ThemeColor) {self.fill = fillself.ink = ink }}// MARK: - Type/// The type roles, each mapped to a face that ships in the binary.////// `title` is an object's own name and `display` a page's; `body` is a/// record's content and `meta` its detail line; `hand` is annotation; `figure`/// is a number that stands alone; `cap` is the lettering on a bottle cap;/// `interface` is a control's label and a glyph's size. A theme picks among/// shipped faces — it never names a font file.publicstructThemeType: Equatable, Sendable {publicvar display: ThemeFacepublicvar title: ThemeFacepublicvar body: ThemeFacepublicvar meta: ThemeFacepublicvar hand: ThemeFacepublicvar figure: ThemeFacepublicvar cap: ThemeFacepublicvar interface: ThemeFace/// A screenplay's page. Courier in every family: the format's page count/// is a page a minute only at Courier's pitch, so it is structure, not/// skin — but it is still named here, with every other face.publicvar script: ThemeFacepublicinit( display: ThemeFace, title: ThemeFace, body: ThemeFace, meta: ThemeFace, hand: ThemeFace, figure: ThemeFace, cap: ThemeFace, interface: ThemeFace, script: ThemeFace ) {self.display = displayself.title = titleself.body = bodyself.meta = metaself.hand = handself.figure = figureself.cap = capself.interface = interfaceself.script = script }/// A page's title.publicfunc display(_ size: CGFloat) -> Font { display.font(size) }/// An object's own name.publicfunc title(_ size: CGFloat) -> Font { title.font(size) }/// A record's content.publicfunc body(_ size: CGFloat) -> Font { body.font(size) }/// A detail line.publicfunc meta(_ size: CGFloat) -> Font { meta.font(size) }/// Annotation.publicfunc hand(_ size: CGFloat) -> Font { hand.font(size) }/// A number standing alone.publicfunc figure(_ size: CGFloat) -> Font { figure.font(size) }/// The lettering on a cap.publicfunc cap(_ size: CGFloat) -> Font { cap.font(size) }/// A screenplay's page.publicfunc script(_ size: CGFloat) -> Font { script.font(size) }/// A control's label, a glyph's size — at a weight and design the control/// decides.publicfunc interface( _ size: CGFloat, weight: Font.Weight? = nil, design: Font.Design? = nil ) -> Font { interface.font(size, weight: weight, design: design) }}/// The faces that ship: the paper set's bundled faces and the system's. The/// raw value is the row's spelling.publicenumThemeFace: String, CaseIterable, Sendable {/// Fraunces at its title optical size, semibold — a page's title.case frauncesTitle = "fraunces_title"/// Fraunces at its text optical size — a name on paper.case frauncesText = "fraunces_text"/// Courier Prime — the typewriter.case courier/// Courier Prime italic.case courierItalic = "courier_italic"/// Caveat — the hand.case caveat/// Instrument Serif italic — a cap's lettering.case instrumentItalic = "instrument_italic"/// Courier New, the system's — a screenplay's page.case courierNew = "courier_new"/// The system face.case system/// The system face, semibold.case systemSemibold = "system_semibold"/// The system face, medium.case systemMedium = "system_medium"/// The system face, italic.case systemItalic = "system_italic"/// The system's monospaced figures.case systemMonospaced = "system_monospaced"/// Montserrat — a business flavour's reading face, worn only where its/// look names it (`FlavourLook`). The core's theme rows do not carry it/// yet (`THEME_FACES`), so only a look does.case montserrat/// Montserrat semibold — the same flavour's headings.case montserratSemibold = "montserrat_semibold"/// Barlow Condensed SemiBold — a letterboard tile's capitals, worn only/// where a tool's fan tile says `letterboard` (`PaintedTile`).case letterboard/// The face at `size` points, fixed: print on an object scales with the/// object, never with Dynamic Type (`Fonts.swift`).publicfunc font(_ size: CGFloat, weight: Font.Weight? = nil, design: Font.Design? = nil) -> Font {switchself {case .frauncesTitle: returnCottonFonts.display(size)case .frauncesText: returnCottonFonts.serif(size)case .courier: returnCottonFonts.type(size)case .courierItalic: returnCottonFonts.typeItalic(size)case .caveat: returnCottonFonts.hand(size)case .instrumentItalic: returnCottonFonts.cap(size)case .courierNew: return .custom("Courier New", size: size)case .system: returnSelf.system(size, weight: weight, design: design)case .systemSemibold: returnSelf.system(size, weight: weight ?? .semibold, design: design)case .systemMedium: returnSelf.system(size, weight: weight ?? .medium, design: design)case .systemItalic: returnSelf.system(size, weight: weight, design: design).italic()case .systemMonospaced: returnSelf.system(size, weight: weight, design: design).monospacedDigit()case .montserrat: returnCottonFonts.brand(CottonFonts.brandRegularFace, size, fallback: .regular)case .montserratSemibold: returnCottonFonts.brand(CottonFonts.brandHeadingFace, size, fallback: .semibold)case .letterboard: returnCottonFonts.letterboard(size) } }/// `Font.system` with the two optional knobs left to the system when/// nobody named them, never below a point (`CottonFonts.drawable`) — `.system(size:)` and `.system(size:weight:)` are/// different calls, and a call site's font must be the one it wrote.privatestaticfunc system(_ points: CGFloat, weight: Font.Weight?, design: Font.Design?) -> Font {let size = CottonFonts.drawable(points)switch (weight, design) {case (nil, nil): return .system(size: size)case (let weight?, nil): return .system(size: size, weight: weight)case (nil, let design?): return .system(size: size, design: design)case (let weight?, let design?): return .system(size: size, weight: weight, design: design) } }/// The face CoreText measures by (`InkFit`): the bundled file's/// PostScript name, or nil for the system's own.publicvar postScriptName: String? {switchself {case .frauncesTitle: returnCottonFonts.displayFacecase .frauncesText: returnCottonFonts.textFacecase .courier: returnCottonFonts.typeFacecase .courierItalic: returnCottonFonts.typeItalicFacecase .caveat: returnCottonFonts.handFacecase .instrumentItalic: returnCottonFonts.capFacecase .courierNew: return"CourierNewPSMT"case .montserrat: returnCottonFonts.brandRegularFacecase .montserratSemibold: returnCottonFonts.brandHeadingFacecase .letterboard: returnCottonFonts.letterboardFacecase .system, .systemSemibold, .systemMedium, .systemItalic, .systemMonospaced: returnnil } }/// The face as CoreText measures it (`InkFit`) at `size`: the bundled/// file, registered first, or the system's UI face.publicfunc measuring(_ size: CGFloat) -> CTFont {CottonFonts.register()let size = CottonFonts.drawable(size)guardlet name = postScriptName else {return CTFontCreateUIFontForLanguage(.system, size, nil) ?? CTFontCreateWithName("Helvetica"as CFString, size, nil) }return CTFontCreateWithName(name as CFString, size, nil) }/// Whether the face is one of the system's, which grows and shrinks with/// nothing bundled.publicvar isSystem: Bool {switchself {case .system, .systemSemibold, .systemMedium, .systemItalic, .systemMonospaced: returntruedefault: returnfalse } }}// MARK: - Scale/// Corner, spacing and stroke.////// The paper family's corner and inset are fractions of the object, clamped/// (`PaperMetrics`' design principle: lettering geometry as fractions of the/// object's own size); a flat family reads `radius` and `spacing` straight.publicstructThemeScale: Hashable, Sendable {/// The largest corner an object wears.publicvar radius: CGFloat/// The smallest corner a paper object wears.publicvar radiusMin: CGFloat/// A paper object's corner as a fraction of its shorter side.publicvar radiusFraction: CGFloat/// The gap between two objects.publicvar spacing: CGFloat/// How far content sits inside an object's edge, as a fraction of its/// width, and the clamp on it.publicvar insetFraction: CGFloatpublicvar insetMin: CGFloatpublicvar insetMax: CGFloat/// A drawn line — a ring, a border.publicvar stroke: CGFloat/// The thinnest rule.publicvar hairline: CGFloat/// A page's chrome: the nav circles and the air around them.publicvar navCircle: CGFloatpublicvar navRowTop: CGFloatpublicvar navRowAir: CGFloat/// The type sizes a print voice falls back to when a caller names none.publicvar titleSize: CGFloatpublicvar bodySize: CGFloatpublicvar metaSize: CGFloatpublicvar handSize: CGFloatpublicvar sectionTitleSize: CGFloatpublicvar screenTitleSize: CGFloat/// Every knob the row carries, by its row spelling — the named ones above/// and the rest, read through `self[key]`.publicvar values: [String: CGFloat] = [:]/// A knob the theme carries. A key the row leaves out is the shipped/// theme's (the core resolves a person's theme over it before it is read,/// so this is the shipped theme answering for an older core).publicsubscript(key: Key) -> CGFloat { values[key.rawValue] ?? Theme.paper.scale.values[key.rawValue] ?? 0 }/// A knob that counts something — columns, lines, people.publicfunc count(_ key: Key) -> Int { Int(self[key].rounded()) }publicinit( radius: CGFloat, radiusMin: CGFloat, radiusFraction: CGFloat, spacing: CGFloat, insetFraction: CGFloat, insetMin: CGFloat, insetMax: CGFloat, stroke: CGFloat, hairline: CGFloat, navCircle: CGFloat, navRowTop: CGFloat, navRowAir: CGFloat, titleSize: CGFloat, bodySize: CGFloat, metaSize: CGFloat, handSize: CGFloat, sectionTitleSize: CGFloat, screenTitleSize: CGFloat ) {self.radius = radiusself.radiusMin = radiusMinself.radiusFraction = radiusFractionself.spacing = spacingself.insetFraction = insetFractionself.insetMin = insetMinself.insetMax = insetMaxself.stroke = strokeself.hairline = hairlineself.navCircle = navCircleself.navRowTop = navRowTopself.navRowAir = navRowAirself.titleSize = titleSizeself.bodySize = bodySizeself.metaSize = metaSizeself.handSize = handSizeself.sectionTitleSize = sectionTitleSizeself.screenTitleSize = screenTitleSize }/// The corner for an object of `size`.publicfunc radius(for size: CGSize) -> CGFloat {let shorter = min(size.width, size.height)return min(radius, max(radiusMin, shorter * radiusFraction)) }/// The inset for an object `width` points wide.publicfunc inset(forWidth width: CGFloat) -> CGFloat { min(insetMax, max(insetMin, width * insetFraction)) }/// The page's top on a screen with no circles.publicfunc barePageClearance(topInset: CGFloat) -> CGFloat { topInset + self[.pageHeadGap] }}// MARK: - Texture/// The material: whether the fibre shows, whether an object is lifted off the/// ground, whether it lies a hair askew.publicstructThemeTexture: Hashable, Sendable {publicvar grain: Boolpublicvar shadow: ThemeShadow/// Whether a sheet is tilted by its seed (`PaperMetrics.tilt`).publicvar tilt: Boolpublicinit(grain: Bool, shadow: ThemeShadow, tilt: Bool) {self.grain = grainself.shadow = shadowself.tilt = tilt }}publicenumThemeShadow: String, CaseIterable, Sendable {/// The two-layer contact shadow, thrown down and right.case soft/// Nothing cast.case none}// MARK: - Cap/// The bottle cap's appearance — the database's own cap on Home and in the/// switcher.publicstructThemeCap: Equatable, Sendable {/// The enamel; nil is the one the database's name hashes to.publicvar colour: CapColour?publicvar texture: ThemeCapTexturepublicinit(colour: CapColour?, texture: ThemeCapTexture) {self.colour = colourself.texture = texture }}publicenumThemeCapTexture: String, CaseIterable, Sendable {/// The photographed enamel cap.case enamel/// A flat disc in the enamel's colour.case flat}// MARK: - The environmentprivatestructThemeKey: EnvironmentKey {staticlet defaultValue = Theme.paper}extensionEnvironmentValues {/// The look every face below is drawn in. Unset, it is `Theme.paper` —/// Cotton's own — which is what a preview and a test draw.publicvar theme: Theme { get { self[ThemeKey.self] } set { self[ThemeKey.self] = newValue } }}extensionView {/// Draw everything below in `theme`, in the appearance the colour scheme/// asks for.publicfunc themed(_ theme: Theme) -> someView { modifier(ThemeRoot(theme: theme)) }}/// Sets the theme with its appearance read off the colour scheme, once, at/// the root — the one place a scheme is asked.privatestructThemeRoot: ViewModifier {let theme: Theme @Environment(\.colorScheme) privatevar schemefunc body(content: Content) -> someView {var drawn = theme drawn.appearance = schemereturn content.environment(\.theme, drawn) }}