Repository Public License
1import Foundation23/// The design tokens as a serialisable document (`DesignSystem/tokens.json`), for tooling outside Swift.4public struct LunarTokenDocument: Codable, Sendable, Equatable {5 public struct ThemeEntry: Codable, Sendable, Equatable {6 public var id: String7 public var name: String8 public var summary: String9 public var dark: [String: String]10 public var light: [String: String]11 }1213 public struct TypeEntry: Codable, Sendable, Equatable {14 public var role: String15 public var systemTextStyle: String16 }1718 public struct MotionEntry: Codable, Sendable, Equatable {19 public var name: String20 public var response: Double21 public var dampingFraction: Double22 }2324 public var identity: String25 public var version: String26 public var palette: [String: String]27 public var spacing: [String: Double]28 public var radius: [String: Double]29 public var stroke: [String: Double]30 public var metrics: [String: Double]31 public var typography: [TypeEntry]32 public var motion: [MotionEntry]33 public var tints: [String]34 public var glyphs: [String]35 public var defaultTheme: String36 public var themes: [ThemeEntry]3738 /// The current token set.39 public static var current: LunarTokenDocument {40 LunarTokenDocument(41 identity: LunarDesign.identityName,42 version: LunarDesign.tokenVersion,43 palette: Dictionary(uniqueKeysWithValues: LunarPalette.all.map { (camel($0.name), $0.color.hexString) }),44 spacing: ["unit": 4, "xs": 8, "sm": 12, "md": 16, "lg": 24, "xl": 32],45 radius: ["xs": 10, "sm": 16, "md": 22, "lg": 28, "xl": 36],46 stroke: ["hairline": 1, "hairlineIncreasedContrast": 1.5, "focus": 2, "ring": 1],47 metrics: ["touchTarget": 44, "pointerTarget": 28, "controlHeight": 36, "controlHeightCompact": 28,48 "barHeight": 44, "tabChipHeight": 36, "icon": 20, "iconSmall": 16, "iconLarge": 28],49 typography: LunarTextStyle.allCases.map { TypeEntry(role: $0.rawValue, systemTextStyle: $0.systemStyleName) },50 motion: LunarMotion.allCases.map {51 MotionEntry(name: $0.rawValue, response: $0.response, dampingFraction: $0.dampingFraction)52 },53 tints: LunarTint.allCases.map(\.rawValue),54 glyphs: LunarGlyph.allCases.map(\.rawValue),55 defaultTheme: LunarThemes.default.id,56 themes: LunarThemes.all.map {57 ThemeEntry(id: $0.id, name: $0.name, summary: $0.summary, dark: roles($0.dark), light: roles($0.light))58 })59 }6061 /// Deterministic JSON (sorted keys, pretty printed) so the exported file diffs cleanly.62 public func jsonData() throws -> Data {63 let encoder = JSONEncoder()64 encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]65 var data = try encoder.encode(self)66 data.append(0x0A)67 return data68 }6970 static func roles(_ theme: LunarTheme) -> [String: String] {71 var roles: [String: String] = [72 "windowBackground": theme.windowBackground.hexString,73 "windowBackgroundDeep": theme.windowBackgroundDeep.hexString,74 "chromeSurface": theme.chromeSurface.hexString,75 "elevatedSurface": theme.elevatedSurface.hexString,76 "sunkenSurface": theme.sunkenSurface.hexString,77 "glassTint": theme.glassTint.hexString,78 "hairline": theme.hairline.hexString,79 "hairlineStrong": theme.hairlineStrong.hexString,80 "textPrimary": theme.textPrimary.hexString,81 "textSecondary": theme.textSecondary.hexString,82 "textTertiary": theme.textTertiary.hexString,83 "accent": theme.accent.hexString,84 "accentSecondary": theme.accentSecondary.hexString,85 "onAccent": theme.onAccent.hexString,86 "success": theme.success.hexString,87 "warning": theme.warning.hexString,88 "danger": theme.danger.hexString,89 "selection": theme.selection.hexString,90 "focusRing": theme.focusRing.hexString,91 "glow": theme.glow.hexString,92 "orbitCore": theme.orbitCore.hexString,93 "star": theme.star.hexString,94 "shadow": theme.shadow.hexString,95 ]96 for (index, ring) in theme.orbitRings.enumerated() { roles["orbitRing\(index + 1)"] = ring.hexString }97 for tint in LunarTint.allCases { roles["tint.\(tint.rawValue)"] = theme.color(for: tint).hexString }98 return roles99 }100101 private static func camel(_ name: String) -> String {102 let words = name.split(separator: " ")103 guard let first = words.first else { return name }104 return first.lowercased() + words.dropFirst().map { $0.prefix(1).uppercased() + $0.dropFirst() }.joined()105 }106}107