Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassThemes/GlassThemeEnvironment.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 SPGlassTokens4445extension EnvironmentValues {46 /// The theme of this part of the view tree. Defaults to ``GlassTheme/system``.47 @Entry public var glassTheme: GlassTheme = .system48}4950extension View {51 /// Applies a theme to this view and its descendants.52 ///53 /// The theme travels through the environment: sibling subtrees can use different themes and a nested call54 /// replaces the theme for its own subtree only. A theme whose appearance is `.light` or `.dark` also sets55 /// the colour scheme of the subtree, so system controls match it.56 public func glassTheme(_ theme: GlassTheme) -> some View {57 modifier(GlassThemeModifier(theme: theme))58 }5960 /// Adjusts the inherited theme for this subtree, for example a tighter radius inside one panel.61 public func glassThemeOverride(_ transform: @escaping @Sendable (inout GlassTheme) -> Void) -> some View {62 modifier(GlassThemeOverrideModifier(transform: transform))63 }64}6566private struct GlassThemeModifier: ViewModifier {67 let theme: GlassTheme6869 func body(content: Content) -> some View {70 switch theme.appearance {71 case .system:72 content.environment(\.glassTheme, theme)73 case .light:74 content.environment(\.glassTheme, theme).environment(\.colorScheme, .light)75 case .dark:76 content.environment(\.glassTheme, theme).environment(\.colorScheme, .dark)77 }78 }79}8081private struct GlassThemeOverrideModifier: ViewModifier {82 @Environment(\.glassTheme) private var inherited83 let transform: @Sendable (inout GlassTheme) -> Void8485 func body(content: Content) -> some View {86 content.glassTheme(GlassThemeOverride.apply(transform, to: inherited))87 }88}8990/// The override rule, exposed so that it can be tested without a view hierarchy.91public enum GlassThemeOverride {92 public static func apply(_ transform: (inout GlassTheme) -> Void, to inherited: GlassTheme) -> GlassTheme {93 var theme = inherited94 transform(&theme)95 return theme96 }97}9899/// The resolved theme of the current environment.100///101/// ```swift102/// struct Row: View {103/// @GlassThemed private var theme104/// var body: some View {105/// Text("Title").font(theme.font(.headline)).foregroundStyle(theme.color(.textPrimary))106/// }107/// }108/// ```109///110/// Resolution reads the theme, the colour scheme and the contrast setting of the view's own environment, so111/// a view always matches the appearance it is displayed in.112@MainActor113@propertyWrapper114public struct GlassThemed: DynamicProperty {115 @Environment(\.glassTheme) private var theme116 @Environment(\.colorScheme) private var colorScheme117 @Environment(\.colorSchemeContrast) private var contrast118 @Environment(\.self) private var environment119120 public init() {}121122 public var wrappedValue: ResolvedGlassTheme {123 var resolved = GlassThemeResolver.resolve(theme, colorScheme: colorScheme, contrast: contrast)124 // Fill roles the theme leaves to the system (the accent colour, the system red…) are measured in125 // this environment, so that labels drawn on them stay legible whatever the host's accent is.126 let systemFills = Self.fillRoles.filter { resolved.rgba($0) == nil }127 if !systemFills.isEmpty {128 resolved.systemRolesNeedingDarkLabel = Set(systemFills.filter { token in129 let fill = resolved.color(token).resolve(in: environment)130 let luminance = 0.2126 * Double(fill.linearRed) + 0.7152 * Double(fill.linearGreen) + 0.0722 * Double(fill.linearBlue)131 // White wins unless near-black has the better contrast ratio on this fill.132 return 1.05 / (luminance + 0.05) < (luminance + 0.05) / 0.0537133 })134 }135 return resolved136 }137138 private static let fillRoles: [GlassColorToken] = [.primary, .secondary, .accent, .destructive, .success, .warning]139}140