Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Support/GlassButtonChrome.swift8// Module : SPGlassComponents9// 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 SwiftUI4243/// Visual weight of a glass button.44public enum GlassButtonVariant: String, CaseIterable, Sendable {45 /// The system glass button style. For most actions.46 case regular47 /// The system prominent glass style, filled with the theme's primary colour. One per view at most.48 case prominent49}5051extension GlassControlSize {52 /// The matching system control size.53 public var controlSize: ControlSize {54 switch self {55 case .small: .small56 case .regular: .regular57 case .large: .large58 }59 }60}6162extension View {63 /// Applies the system glass button style for a variant.64 ///65 /// The system draws the control, including its own Reduce Transparency treatment. When Reduce Transparency66 /// is *simulated* through ``SwiftUICore/View/glassSimulatedAccessibility(reduceTransparency:reduceMotion:)``,67 /// the bordered system styles stand in for the glass ones.68 public func glassButtonChrome(_ variant: GlassButtonVariant, role: ButtonRole? = nil, size: GlassControlSize = .regular) -> some View {69 modifier(GlassButtonChromeModifier(variant: variant, role: role, size: size))70 }71}7273private struct GlassButtonChromeModifier: ViewModifier {74 @GlassThemed private var theme75 @Environment(\.glassAccessibilityOverrides) private var overrides76 let variant: GlassButtonVariant77 let role: ButtonRole?78 let size: GlassControlSize7980 func body(content: Content) -> some View {81 styled(content)82 .controlSize(size.controlSize)83 .tint(tint)84 }8586 @ViewBuilder87 private func styled(_ content: Content) -> some View {88 let simulatedOpaque = overrides.reduceTransparency == true89 switch (variant, simulatedOpaque) {90 case (.regular, false): content.buttonStyle(.glass)91 case (.prominent, false): content.buttonStyle(.glassProminent)92 case (.regular, true): content.buttonStyle(.bordered)93 case (.prominent, true): content.buttonStyle(.borderedProminent)94 }95 }9697 /// Prominent buttons take the theme's primary colour, destructive ones the destructive colour.98 /// A regular, non-destructive button keeps the system's untinted glass.99 private var tint: Color? {100 if role == .destructive { return theme.color(.destructive) }101 return variant == .prominent ? theme.color(.primary) : nil102 }103}104