Repository Public License
1import SwiftUI23/// Dark or light rendition of a theme family.4public enum LunarThemeVariant: String, Sendable, Codable, CaseIterable {5 case dark, light67 public var colorScheme: ColorScheme { self == .dark ? .dark : .light }89 public init(_ colorScheme: ColorScheme) { self = colorScheme == .dark ? .dark : .light }10}1112/// A theme is data: a set of semantic colour roles. Views never hard-code a colour; they read the theme from the13/// environment (`@Environment(\.lunarTheme)`) and use its roles.14public struct LunarTheme: Hashable, Sendable, Codable, Identifiable {15 /// `"<familyID>.<variant>"`, e.g. `"lunar.dark"`.16 public var id: String17 /// Stable identifier of the family, e.g. `"lunar"`. This is the value to persist.18 public var familyID: String19 public var name: String20 public var variant: LunarThemeVariant2122 // Surfaces23 /// The window field, top of the background gradient.24 public var windowBackground: LunarRGBA25 /// Bottom of the window background gradient (same hue, slightly lifted).26 public var windowBackgroundDeep: LunarRGBA27 /// Opaque equivalent of chrome glass: sidebars, bars, docks. Used as-is under Reduce Transparency.28 public var chromeSurface: LunarRGBA29 /// Cards, popovers, sheets, selected rows.30 public var elevatedSurface: LunarRGBA31 /// Recessed wells: text fields, tracks.32 public var sunkenSurface: LunarRGBA33 /// Tint laid over the system material to give glass the theme's hue.34 public var glassTint: LunarRGBA3536 // Lines37 /// Discreet luminous border (translucent).38 public var hairline: LunarRGBA39 /// Top edge highlight of glass and the border under Increase Contrast (translucent).40 public var hairlineStrong: LunarRGBA4142 // Text43 public var textPrimary: LunarRGBA44 public var textSecondary: LunarRGBA45 public var textTertiary: LunarRGBA4647 // Accents48 public var accent: LunarRGBA49 public var accentSecondary: LunarRGBA50 /// Text and glyphs on an accent fill.51 public var onAccent: LunarRGBA52 public var success: LunarRGBA53 public var warning: LunarRGBA54 public var danger: LunarRGBA55 /// Fill behind selected rows, chips and text (translucent).56 public var selection: LunarRGBA57 /// Keyboard focus ring.58 public var focusRing: LunarRGBA5960 // Space61 /// Soft halo behind the mark, the Orbit core and active planets (translucent).62 public var glow: LunarRGBA63 /// Orbital ring colours, innermost first (translucent).64 public var orbitRings: [LunarRGBA]65 /// The core of an orbit (the "moon").66 public var orbitCore: LunarRGBA67 /// Starfield dots (translucent).68 public var star: LunarRGBA69 /// Shadow colour, alpha included.70 public var shadow: LunarRGBA7172 /// The ten workspace colours for this theme.73 public var tints: LunarTintPalette7475 public var isDark: Bool { variant == .dark }7677 /// Colour of a workspace tint in this theme.78 public func color(for tint: LunarTint) -> LunarRGBA { tints[tint] }7980 /// A soft fill of a tint for chips and planet halos.81 public func softFill(for tint: LunarTint) -> LunarRGBA { tints[tint].withAlpha(isDark ? 0.18 : 0.14) }8283 /// The window background as a vertical gradient.84 public var windowGradient: LinearGradient {85 LinearGradient(colors: [windowBackground.color, windowBackgroundDeep.color], startPoint: .top,86 endPoint: .bottom)87 }8889 /// The opaque surfaces body text may sit on (used by the contrast tests).90 public var textSurfaces: [LunarRGBA] {91 [windowBackground, windowBackgroundDeep, chromeSurface, elevatedSurface, sunkenSurface]92 }9394 /// The rendition used when the system "Increase Contrast" setting is on: stronger secondary text and borders.95 public var increasedContrast: LunarTheme {96 var theme = self97 theme.textSecondary = textSecondary.mixed(with: textPrimary, 0.6)98 theme.textTertiary = textSecondary99 theme.hairline = textPrimary.withAlpha(0.42)100 theme.hairlineStrong = textPrimary.withAlpha(0.62)101 theme.selection = accent.withAlpha(isDark ? 0.30 : 0.24)102 return theme103 }104}105106extension LunarTheme {107 /// The hand-picked colours of one variant; every other role is derived from them so the 24 renditions stay108 /// coherent.109 struct Seed {110 var background: UInt32111 var backgroundDeep: UInt32112 var chrome: UInt32113 var elevated: UInt32114 var text: UInt32115 var accent: UInt32116 var accentSecondary: UInt32117 /// Overrides for the accessibility themes.118 var secondaryText: UInt32? = nil119 var hairlineAlpha: Double? = nil120 }121122 init(familyID: String, name: String, variant: LunarThemeVariant, seed: Seed) {123 let dark = variant == .dark124 let background = LunarRGBA(hex: seed.background)125 let elevated = LunarRGBA(hex: seed.elevated)126 let text = LunarRGBA(hex: seed.text)127 let accent = LunarRGBA(hex: seed.accent)128 let accentSecondary = LunarRGBA(hex: seed.accentSecondary)129 // Secondary/tertiary text move towards the *lightest or darkest* surface they must stay legible on.130 let anchor = dark ? elevated : LunarRGBA(hex: seed.chrome)131 let hairlineAlpha = seed.hairlineAlpha ?? 0.10132133 self.id = "\(familyID).\(variant.rawValue)"134 self.familyID = familyID135 self.name = name136 self.variant = variant137 self.windowBackground = background138 self.windowBackgroundDeep = LunarRGBA(hex: seed.backgroundDeep)139 self.chromeSurface = LunarRGBA(hex: seed.chrome)140 self.elevatedSurface = elevated141 self.sunkenSurface = dark ? background.mixed(with: text, 0.05) : LunarRGBA(hex: seed.chrome).mixed(with: text, 0.03)142 self.glassTint = LunarRGBA(hex: seed.chrome).withAlpha(dark ? 0.58 : 0.62)143 self.hairline = (dark ? text : text).withAlpha(hairlineAlpha)144 self.hairlineStrong = (dark ? LunarRGBA.white : text).withAlpha(min(1, hairlineAlpha * (dark ? 2.2 : 1.7)))145 self.textPrimary = text146 self.textSecondary = seed.secondaryText.map { LunarRGBA(hex: $0) } ?? text.mixed(with: anchor, dark ? 0.34 : 0.30)147 self.textTertiary = text.mixed(with: anchor, dark ? 0.52 : 0.46)148 self.accent = accent149 self.accentSecondary = accentSecondary150 self.onAccent = accent.contrastRatio(against: .black) >= accent.contrastRatio(against: .white)151 ? LunarRGBA(hex: 0x05070D) : .white152 self.success = LunarRGBA(hex: dark ? 0x5FD9A0 : 0x0F7A55)153 self.warning = LunarRGBA(hex: dark ? 0xF2C063 : 0x8A5A00)154 self.danger = LunarRGBA(hex: dark ? 0xFF7A85 : 0xC22F45)155 self.selection = accent.withAlpha(dark ? 0.20 : 0.14)156 self.focusRing = accent.withAlpha(0.95)157 self.glow = accent.withAlpha(dark ? 0.30 : 0.20)158 self.orbitRings = [159 accent.withAlpha(dark ? 0.46 : 0.50),160 accentSecondary.withAlpha(dark ? 0.34 : 0.38),161 text.withAlpha(dark ? 0.16 : 0.18),162 ]163 self.orbitCore = dark ? text : accent164 self.star = text.withAlpha(dark ? 0.72 : 0.30)165 self.shadow = dark ? LunarRGBA.black.withAlpha(0.42) : text.withAlpha(0.16)166 self.tints = dark ? .dark : .light167 }168}169170/// A theme family: one identity, a dark and a light rendition.171public struct LunarThemeFamily: Hashable, Sendable, Identifiable {172 /// Stable identifier for persistence (`"lunar"`, `"obsidian"`…). Never rename a shipped id.173 public let id: String174 public let name: String175 /// One line describing the mood, shown in the theme picker.176 public let summary: String177 public let dark: LunarTheme178 public let light: LunarTheme179180 public func theme(_ variant: LunarThemeVariant) -> LunarTheme { variant == .dark ? dark : light }181182 public func theme(for colorScheme: ColorScheme, increasedContrast: Bool = false) -> LunarTheme {183 let base = theme(LunarThemeVariant(colorScheme))184 return increasedContrast ? base.increasedContrast : base185 }186187 init(id: String, name: String, summary: String, dark: LunarTheme.Seed, light: LunarTheme.Seed) {188 self.id = id189 self.name = name190 self.summary = summary191 self.dark = LunarTheme(familyID: id, name: name, variant: .dark, seed: dark)192 self.light = LunarTheme(familyID: id, name: name, variant: .light, seed: light)193 }194}195196/// Which rendition of a family to show.197public enum LunarAppearance: String, Sendable, Codable, CaseIterable {198 /// Follow the system (or the enclosing view's) colour scheme.199 case system200 case dark201 case light202}203204// MARK: - Environment205206extension EnvironmentValues {207 /// The current Lunar theme. Set it with `View.lunarTheme(_:)`.208 @Entry public var lunarTheme: LunarTheme = LunarThemes.lunar.dark209210 /// How Lunar surfaces render. `.snapshot` is used for offscreen renders (`ImageRenderer`), where live materials,211 /// platform text fields and running animations cannot be captured.212 @Entry public var lunarRenderMode: LunarRenderMode = .live213}214215public enum LunarRenderMode: String, Sendable {216 case live217 /// Opaque glass fallback, static text in fields, frozen animations.218 case snapshot219}220221extension View {222 /// Installs a theme family; the rendition follows the colour scheme (or `appearance`) and the system223 /// Increase Contrast setting. This is the modifier an app applies at the root of each scene.224 public func lunarTheme(_ family: LunarThemeFamily, appearance: LunarAppearance = .system) -> some View {225 modifier(LunarThemeFamilyModifier(family: family, appearance: appearance))226 }227228 /// Installs an explicit rendition (previews, reference renders, a pinned sub-tree). Also sets the colour scheme229 /// so system materials and controls match.230 public func lunarTheme(_ theme: LunarTheme) -> some View {231 environment(\.lunarTheme, theme)232 .environment(\.colorScheme, theme.variant.colorScheme)233 .tint(theme.accent.color)234 }235}236237private struct LunarThemeFamilyModifier: ViewModifier {238 @Environment(\.colorScheme) private var colorScheme239 @Environment(\.colorSchemeContrast) private var contrast240 let family: LunarThemeFamily241 let appearance: LunarAppearance242243 func body(content: Content) -> some View {244 let scheme: ColorScheme = switch appearance {245 case .system: colorScheme246 case .dark: .dark247 case .light: .light248 }249 let theme = family.theme(for: scheme, increasedContrast: contrast == .increased)250 return content251 .environment(\.lunarTheme, theme)252 .environment(\.colorScheme, scheme)253 .tint(theme.accent.color)254 }255}256