Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassThemes/GlassThemeResolver.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 SwiftUI42import SPGlassCore43import SPGlassTokens4445/// A theme resolved for one appearance and platform: the values components actually draw with.46public struct ResolvedGlassTheme: Equatable, Sendable {47 public let theme: GlassTheme48 public let isDark: Bool49 public let increasedContrast: Bool50 public let platform: GlassPlatform51 /// The spacing scale after the density factor.52 public let spacing: GlassSpacing53 /// Roles left to the system whose system colour, in the view's environment, is light enough to need a54 /// dark label. A system colour has no stored value, so this is measured where the environment is known.55 public var systemRolesNeedingDarkLabel: Set<GlassColorToken> = []5657 public var radii: GlassRadii { theme.radii }58 public var typography: GlassTypography { theme.typography }59 public var motion: GlassMotion { theme.motion }60 public var glass: GlassMaterialTokens { theme.glass }61 public var metrics: GlassControlMetrics { GlassControlMetrics(platform: platform) }62 public var hapticsEnabled: Bool { theme.hapticsEnabled }6364 /// The stored value of a role, or `nil` when the role follows the system.65 public func rgba(_ token: GlassColorToken) -> GlassRGBA? {66 let palette = theme.palette(dark: isDark)67 if increasedContrast {68 // With Increase Contrast, secondary text and separators move to the strongest text colour.69 switch token {70 case .textSecondary, .separator: return palette[.textPrimary] ?? palette[token]71 default: break72 }73 }74 return palette[token]75 }7677 /// The colour of a role: the theme value, or the system colour for that role.78 public func color(_ token: GlassColorToken) -> Color {79 if let value = rgba(token) { return Color(glass: value) }80 return GlassSystemColors.color(for: token)81 }8283 /// A legible colour for text or symbols drawn on a fill of `token` (white or near-black).84 public func onColor(_ token: GlassColorToken) -> Color {85 let dark = GlassRGBA(rgb: 0x0B0B0F)86 guard let fill = rgba(token) else {87 return systemRolesNeedingDarkLabel.contains(token) ? Color(glass: dark) : .white88 }89 return GlassRGBA.white.contrastRatio(against: fill) >= dark.contrastRatio(against: fill) ? .white : Color(glass: dark)90 }9192 /// The tint applied to custom glass surfaces, if the theme asks for one.93 public var glassTint: Color? {94 theme.glass.tint.map(color)95 }9697 /// The system font for a role. Sizes follow Dynamic Type.98 public func font(_ role: GlassTextRole) -> Font {99 let design = typography.design.fontDesign100 let heading = typography.headingWeight.fontWeight101 let body = typography.bodyWeight.fontWeight102 switch role {103 case .largeTitle: return .system(.largeTitle, design: design, weight: heading)104 case .title: return .system(.title2, design: design, weight: heading)105 case .headline: return .system(.headline, design: design, weight: heading)106 case .body: return .system(.body, design: design, weight: body)107 case .callout: return .system(.callout, design: design, weight: body)108 case .caption: return .system(.caption, design: design, weight: body)109 case .monospacedValue: return .system(.title, design: design, weight: heading).monospacedDigit()110 }111 }112113 /// The animation for a speed. With Reduce Motion the result is a short cross-fade without spring or travel.114 public func animation(_ speed: GlassMotionSpeed = .standard, reduceMotion: Bool) -> Animation {115 let duration = motion.duration(speed)116 if reduceMotion { return .easeInOut(duration: min(duration, 0.2)) }117 switch motion.style {118 case .smooth: return .smooth(duration: duration)119 case .snappy: return .snappy(duration: duration)120 case .bouncy: return .bouncy(duration: duration)121 }122 }123124 /// A continuous rounded rectangle with one of the theme radii.125 public func shape(_ radius: KeyPath<GlassRadii, Double> = \.medium) -> RoundedRectangle {126 RoundedRectangle(cornerRadius: radii[keyPath: radius], style: .continuous)127 }128}129130/// Resolves themes. Pure: the same inputs always give the same result.131public enum GlassThemeResolver {132 public static func resolve(133 _ theme: GlassTheme,134 colorScheme: ColorScheme,135 contrast: ColorSchemeContrast = .standard,136 platform: GlassPlatform = .current137 ) -> ResolvedGlassTheme {138 let isDark: Bool139 switch theme.appearance {140 case .light: isDark = false141 case .dark: isDark = true142 case .system: isDark = colorScheme == .dark143 }144 return ResolvedGlassTheme(145 theme: theme,146 isDark: isDark,147 increasedContrast: contrast == .increased,148 platform: platform,149 spacing: theme.spacing.scaled(by: theme.density.spacingMultiplier)150 )151 }152}153154/// System colours used for roles a theme leaves undefined.155public enum GlassSystemColors {156 public static func color(for token: GlassColorToken) -> Color {157 switch token {158 case .primary, .accent, .focusRing: return .accentColor159 case .secondary, .textSecondary: return .secondary160 case .destructive: return .red161 case .success: return .green162 case .warning: return .orange163 case .textPrimary: return .primary164 case .background:165 #if os(macOS)166 return Color(nsColor: .windowBackgroundColor)167 #else168 return Color(uiColor: .systemBackground)169 #endif170 case .surface:171 #if os(macOS)172 return Color(nsColor: .controlBackgroundColor)173 #else174 return Color(uiColor: .secondarySystemBackground)175 #endif176 case .separator:177 #if os(macOS)178 return Color(nsColor: .separatorColor)179 #else180 return Color(uiColor: .separator)181 #endif182 }183 }184}185186extension Color {187 /// Converts a stored colour to SwiftUI at the UI boundary.188 public init(glass value: GlassRGBA) {189 self.init(.sRGB, red: value.red, green: value.green, blue: value.blue, opacity: value.alpha)190 }191}192193extension GlassRGBA {194 /// Reads the sRGB components of a SwiftUI colour in a given environment (used by the Studio colour picker).195 public init(_ color: Color, in environment: EnvironmentValues) {196 let resolved = color.resolve(in: environment)197 self.init(red: Double(resolved.red), green: Double(resolved.green), blue: Double(resolved.blue), alpha: Double(resolved.opacity))198 }199}200201extension GlassFontDesign {202 public var fontDesign: Font.Design {203 switch self {204 case .default: .default205 case .rounded: .rounded206 case .serif: .serif207 case .monospaced: .monospaced208 }209 }210}211212extension GlassFontWeight {213 public var fontWeight: Font.Weight {214 switch self {215 case .regular: .regular216 case .medium: .medium217 case .semibold: .semibold218 case .bold: .bold219 }220 }221}222