SPB Git

Repository Public MIT

GlassButtonChrome.swift 4.0 KB 108 lines swift

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 SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// Visual weight of a glass button.48public enum GlassButtonVariant: String, CaseIterable, Sendable {49    /// The system glass button style. For most actions.50    case regular51    /// The system prominent glass style, filled with the theme's primary colour. One per view at most.52    case prominent53}5455extension GlassControlSize {56    /// The matching system control size.57    public var controlSize: ControlSize {58        switch self {59        case .small: .small60        case .regular: .regular61        case .large: .large62        }63    }64}6566extension View {67    /// Applies the system glass button style for a variant.68    ///69    /// The system draws the control, including its own Reduce Transparency treatment. When Reduce Transparency70    /// is *simulated* through ``SwiftUICore/View/glassSimulatedAccessibility(reduceTransparency:reduceMotion:)``,71    /// the bordered system styles stand in for the glass ones.72    public func glassButtonChrome(_ variant: GlassButtonVariant, role: ButtonRole? = nil, size: GlassControlSize = .regular) -> some View {73        modifier(GlassButtonChromeModifier(variant: variant, role: role, size: size))74    }75}7677private struct GlassButtonChromeModifier: ViewModifier {78    @GlassThemed private var theme79    @Environment(\.glassAccessibilityOverrides) private var overrides80    let variant: GlassButtonVariant81    let role: ButtonRole?82    let size: GlassControlSize8384    func body(content: Content) -> some View {85        styled(content)86            .controlSize(size.controlSize)87            .tint(tint)88    }8990    @ViewBuilder91    private func styled(_ content: Content) -> some View {92        let simulatedOpaque = overrides.reduceTransparency == true93        switch (variant, simulatedOpaque) {94        case (.regular, false): content.buttonStyle(.glass)95        case (.prominent, false): content.buttonStyle(.glassProminent)96        case (.regular, true): content.buttonStyle(.bordered)97        case (.prominent, true): content.buttonStyle(.borderedProminent)98        }99    }100101    /// Prominent buttons take the theme's primary colour, destructive ones the destructive colour.102    /// A regular, non-destructive button keeps the system's untinted glass.103    private var tint: Color? {104        if role == .destructive { return theme.color(.destructive) }105        return variant == .prominent ? theme.color(.primary) : nil106    }107}108
Files main
Menu

Theme