Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassThemes/GlassTheme.swift8// Module : SPGlassThemes9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import SPGlassCore43import SPGlassTokens4445/// Stable identifier of a theme: lower-case letters, digits and hyphens, 1 to 64 characters.46public struct GlassThemeID: RawRepresentable, Hashable, Codable, Sendable, ExpressibleByStringLiteral, CustomStringConvertible {47 public let rawValue: String4849 public init(rawValue: String) { self.rawValue = rawValue }50 public init(_ rawValue: String) { self.rawValue = rawValue }51 public init(stringLiteral value: String) { rawValue = value }5253 public init(from decoder: any Decoder) throws {54 rawValue = try decoder.singleValueContainer().decode(String.self)55 }5657 public func encode(to encoder: any Encoder) throws {58 var container = encoder.singleValueContainer()59 try container.encode(rawValue)60 }6162 public var description: String { rawValue }6364 public var isValid: Bool {65 (1...64).contains(rawValue.utf8.count)66 && rawValue.utf8.allSatisfy { ($0 >= 97 && $0 <= 122) || ($0 >= 48 && $0 <= 57) || $0 == 45 }67 && !rawValue.hasPrefix("-") && !rawValue.hasSuffix("-")68 }69}7071/// Appearance a theme asks for. `.system` follows the user's setting.72public enum GlassAppearance: String, Codable, CaseIterable, Sendable {73 case system, light, dark74}7576/// A complete, serialisable theme: colours for both appearances and the non-colour tokens.77///78/// A theme is a value. It is injected with `.glassTheme(_:)` and read from the environment; nothing global79/// is mutated, so two themes can coexist in the same window.80public struct GlassTheme: Hashable, Codable, Sendable, Identifiable {81 public var schemaVersion: Int82 public var id: GlassThemeID83 public var name: String84 public var summary: String85 public var appearance: GlassAppearance86 public var light: GlassPalette87 public var dark: GlassPalette88 public var typography: GlassTypography89 public var spacing: GlassSpacing90 public var radii: GlassRadii91 public var motion: GlassMotion92 public var density: GlassDensity93 public var glass: GlassMaterialTokens94 public var hapticsEnabled: Bool9596 public init(97 id: GlassThemeID,98 name: String,99 summary: String = "",100 appearance: GlassAppearance = .system,101 light: GlassPalette = GlassPalette(),102 dark: GlassPalette = GlassPalette(),103 typography: GlassTypography = .standard,104 spacing: GlassSpacing = .standard,105 radii: GlassRadii = .standard,106 motion: GlassMotion = .standardMotion,107 density: GlassDensity = .regular,108 glass: GlassMaterialTokens = .standard,109 hapticsEnabled: Bool = true110 ) {111 self.schemaVersion = GlassKitInfo.schemaVersion112 self.id = id113 self.name = name114 self.summary = summary115 self.appearance = appearance116 self.light = light117 self.dark = dark118 self.typography = typography119 self.spacing = spacing120 self.radii = radii121 self.motion = motion122 self.density = density123 self.glass = glass124 self.hapticsEnabled = hapticsEnabled125 }126127 public init(from decoder: any Decoder) throws {128 let c = try decoder.container(keyedBy: CodingKeys.self)129 schemaVersion = try c.decodeIfPresent(Int.self, forKey: .schemaVersion) ?? GlassKitInfo.schemaVersion130 id = try c.decode(GlassThemeID.self, forKey: .id)131 name = try c.decode(String.self, forKey: .name)132 summary = try c.decodeIfPresent(String.self, forKey: .summary) ?? ""133 appearance = try c.decodeIfPresent(GlassAppearance.self, forKey: .appearance) ?? .system134 light = try c.decodeIfPresent(GlassPalette.self, forKey: .light) ?? GlassPalette()135 dark = try c.decodeIfPresent(GlassPalette.self, forKey: .dark) ?? GlassPalette()136 typography = try c.decodeIfPresent(GlassTypography.self, forKey: .typography) ?? .standard137 spacing = try c.decodeIfPresent(GlassSpacing.self, forKey: .spacing) ?? .standard138 radii = try c.decodeIfPresent(GlassRadii.self, forKey: .radii) ?? .standard139 motion = try c.decodeIfPresent(GlassMotion.self, forKey: .motion) ?? .standardMotion140 density = try c.decodeIfPresent(GlassDensity.self, forKey: .density) ?? .regular141 glass = try c.decodeIfPresent(GlassMaterialTokens.self, forKey: .glass) ?? .standard142 hapticsEnabled = try c.decodeIfPresent(Bool.self, forKey: .hapticsEnabled) ?? true143 }144145 /// The palette for an appearance.146 public func palette(dark isDark: Bool) -> GlassPalette {147 isDark ? dark : light148 }149150 /// A copy with another identifier and name, used when the Studio duplicates a built-in theme.151 public func duplicated(id: GlassThemeID, name: String) -> GlassTheme {152 var copy = self153 copy.id = id154 copy.name = name155 return copy156 }157}158