// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Buttons/GlassButton.swift // Module : SPGlassComponents // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-03 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// A button drawn with the system Liquid Glass button styles. /// /// ```swift /// GlassButton("Save", systemImage: "checkmark", variant: .prominent, isLoading: isSaving) { /// save() /// } /// GlassButton("Delete", role: .destructive) { delete() } /// ``` /// /// The system draws the button: GlassKit only chooses the style, the tint and the control size. While /// `isLoading` is true the button shows a progress indicator, ignores taps and reports "Loading" to VoiceOver. public struct GlassButton: View { @GlassThemed private var theme private let variant: GlassButtonVariant private let role: ButtonRole? private let size: GlassControlSize private let isLoading: Bool private let action: @MainActor () -> Void private let label: Label /// Creates a button with a custom label. public init( variant: GlassButtonVariant = .regular, role: ButtonRole? = nil, size: GlassControlSize = .regular, isLoading: Bool = false, action: @escaping @MainActor () -> Void, @ViewBuilder label: () -> Label ) { self.variant = variant self.role = role self.size = size self.isLoading = isLoading self.action = action self.label = label() } public var body: some View { Button(role: role, action: action) { HStack(spacing: theme.spacing.xs) { if isLoading { ProgressView() .controlSize(.small) .accessibilityHidden(true) } label } .glassButtonLabelContrast(variant, role: role) } .glassButtonChrome(variant, role: role, size: size) .disabled(isLoading) .accessibilityValue(isLoading ? Text(GlassL10n.string(.loading)) : Text(verbatim: "")) } } extension GlassButton where Label == GlassButtonLabel { /// Creates a button with a localised title and an optional SF Symbol. public init( _ titleKey: LocalizedStringKey, systemImage: String? = nil, variant: GlassButtonVariant = .regular, role: ButtonRole? = nil, size: GlassControlSize = .regular, isLoading: Bool = false, action: @escaping @MainActor () -> Void ) { self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) { GlassButtonLabel(title: Text(titleKey), systemImage: isLoading ? nil : systemImage) } } /// Creates a button with a title that is already a string (data, user content) and an optional SF Symbol. public init( verbatim title: S, systemImage: String? = nil, variant: GlassButtonVariant = .regular, role: ButtonRole? = nil, size: GlassControlSize = .regular, isLoading: Bool = false, action: @escaping @MainActor () -> Void ) { self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) { GlassButtonLabel(title: Text(verbatim: String(title)), systemImage: isLoading ? nil : systemImage) } } } /// The standard label of a ``GlassButton``: an optional symbol followed by a title. public struct GlassButtonLabel: View { let title: Text let systemImage: String? public var body: some View { // One line: a button title is truncated, never broken in the middle of a word. if let systemImage { Label { title.lineLimit(1) } icon: { Image(systemName: systemImage) } } else { title.lineLimit(1) } } }