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 Foundation4243/// Stable identifier of a theme: lower-case letters, digits and hyphens, 1 to 64 characters.44public struct GlassThemeID: RawRepresentable, Hashable, Codable, Sendable, ExpressibleByStringLiteral, CustomStringConvertible {45 public let rawValue: String4647 public init(rawValue: String) { self.rawValue = rawValue }48 public init(_ rawValue: String) { self.rawValue = rawValue }49 public init(stringLiteral value: String) { rawValue = value }5051 public init(from decoder: any Decoder) throws {52 rawValue = try decoder.singleValueContainer().decode(String.self)53 }5455 public func encode(to encoder: any Encoder) throws {56 var container = encoder.singleValueContainer()57 try container.encode(rawValue)58 }5960 public var description: String { rawValue }6162 public var isValid: Bool {63 (1...64).contains(rawValue.utf8.count)64 && rawValue.utf8.allSatisfy { ($0 >= 97 && $0 <= 122) || ($0 >= 48 && $0 <= 57) || $0 == 45 }65 && !rawValue.hasPrefix("-") && !rawValue.hasSuffix("-")66 }67}6869/// Appearance a theme asks for. `.system` follows the user's setting.70public enum GlassAppearance: String, Codable, CaseIterable, Sendable {71 case system, light, dark72}7374/// A complete, serialisable theme: colours for both appearances and the non-colour tokens.75///76/// A theme is a value. It is injected with `.glassTheme(_:)` and read from the environment; nothing global77/// is mutated, so two themes can coexist in the same window.78public struct GlassTheme: Hashable, Codable, Sendable, Identifiable {79 public var schemaVersion: Int80 public var id: GlassThemeID81 public var name: String82 public var summary: String83 public var appearance: GlassAppearance84 public var light: GlassPalette85 public var dark: GlassPalette86 public var typography: GlassTypography87 public var spacing: GlassSpacing88 public var radii: GlassRadii89 public var motion: GlassMotion90 public var density: GlassDensity91 public var glass: GlassMaterialTokens92 public var hapticsEnabled: Bool9394 public init(95 id: GlassThemeID,96 name: String,97 summary: String = "",98 appearance: GlassAppearance = .system,99 light: GlassPalette = GlassPalette(),100 dark: GlassPalette = GlassPalette(),101 typography: GlassTypography = .standard,102 spacing: GlassSpacing = .standard,103 radii: GlassRadii = .standard,104 motion: GlassMotion = .standardMotion,105 density: GlassDensity = .regular,106 glass: GlassMaterialTokens = .standard,107 hapticsEnabled: Bool = true108 ) {109 self.schemaVersion = GlassKitInfo.schemaVersion110 self.id = id111 self.name = name112 self.summary = summary113 self.appearance = appearance114 self.light = light115 self.dark = dark116 self.typography = typography117 self.spacing = spacing118 self.radii = radii119 self.motion = motion120 self.density = density121 self.glass = glass122 self.hapticsEnabled = hapticsEnabled123 }124125 public init(from decoder: any Decoder) throws {126 let c = try decoder.container(keyedBy: CodingKeys.self)127 schemaVersion = try c.decodeIfPresent(Int.self, forKey: .schemaVersion) ?? GlassKitInfo.schemaVersion128 id = try c.decode(GlassThemeID.self, forKey: .id)129 name = try c.decode(String.self, forKey: .name)130 summary = try c.decodeIfPresent(String.self, forKey: .summary) ?? ""131 appearance = try c.decodeIfPresent(GlassAppearance.self, forKey: .appearance) ?? .system132 light = try c.decodeIfPresent(GlassPalette.self, forKey: .light) ?? GlassPalette()133 dark = try c.decodeIfPresent(GlassPalette.self, forKey: .dark) ?? GlassPalette()134 typography = try c.decodeIfPresent(GlassTypography.self, forKey: .typography) ?? .standard135 spacing = try c.decodeIfPresent(GlassSpacing.self, forKey: .spacing) ?? .standard136 radii = try c.decodeIfPresent(GlassRadii.self, forKey: .radii) ?? .standard137 motion = try c.decodeIfPresent(GlassMotion.self, forKey: .motion) ?? .standardMotion138 density = try c.decodeIfPresent(GlassDensity.self, forKey: .density) ?? .regular139 glass = try c.decodeIfPresent(GlassMaterialTokens.self, forKey: .glass) ?? .standard140 hapticsEnabled = try c.decodeIfPresent(Bool.self, forKey: .hapticsEnabled) ?? true141 }142143 /// The palette for an appearance.144 public func palette(dark isDark: Bool) -> GlassPalette {145 isDark ? dark : light146 }147148 /// A copy with another identifier and name, used when the Studio duplicates a built-in theme.149 public func duplicated(id: GlassThemeID, name: String) -> GlassTheme {150 var copy = self151 copy.id = id152 copy.name = name153 return copy154 }155}156