SPB Git

Repository Public MIT

GlassButton.swift 5.1 KB 146 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassComponents/Buttons/GlassButton.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/// A button drawn with the system Liquid Glass button styles.48///49/// ```swift50/// GlassButton("Save", systemImage: "checkmark", variant: .prominent, isLoading: isSaving) {51///     save()52/// }53/// GlassButton("Delete", role: .destructive) { delete() }54/// ```55///56/// The system draws the button: GlassKit only chooses the style, the tint and the control size. While57/// `isLoading` is true the button shows a progress indicator, ignores taps and reports "Loading" to VoiceOver.58public struct GlassButton<Label: View>: View {59    @GlassThemed private var theme60    private let variant: GlassButtonVariant61    private let role: ButtonRole?62    private let size: GlassControlSize63    private let isLoading: Bool64    private let action: @MainActor () -> Void65    private let label: Label6667    /// Creates a button with a custom label.68    public init(69        variant: GlassButtonVariant = .regular,70        role: ButtonRole? = nil,71        size: GlassControlSize = .regular,72        isLoading: Bool = false,73        action: @escaping @MainActor () -> Void,74        @ViewBuilder label: () -> Label75    ) {76        self.variant = variant77        self.role = role78        self.size = size79        self.isLoading = isLoading80        self.action = action81        self.label = label()82    }8384    public var body: some View {85        Button(role: role, action: action) {86            HStack(spacing: theme.spacing.xs) {87                if isLoading {88                    ProgressView()89                        .controlSize(.small)90                        .accessibilityHidden(true)91                }92                label93            }94        }95        .glassButtonChrome(variant, role: role, size: size)96        .disabled(isLoading)97        .accessibilityValue(isLoading ? Text(GlassL10n.string(.loading)) : Text(verbatim: ""))98    }99}100101extension GlassButton where Label == GlassButtonLabel {102    /// Creates a button with a localised title and an optional SF Symbol.103    public init(104        _ titleKey: LocalizedStringKey,105        systemImage: String? = nil,106        variant: GlassButtonVariant = .regular,107        role: ButtonRole? = nil,108        size: GlassControlSize = .regular,109        isLoading: Bool = false,110        action: @escaping @MainActor () -> Void111    ) {112        self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) {113            GlassButtonLabel(title: Text(titleKey), systemImage: isLoading ? nil : systemImage)114        }115    }116117    /// Creates a button with a title that is already a string (data, user content) and an optional SF Symbol.118    public init<S: StringProtocol>(119        verbatim title: S,120        systemImage: String? = nil,121        variant: GlassButtonVariant = .regular,122        role: ButtonRole? = nil,123        size: GlassControlSize = .regular,124        isLoading: Bool = false,125        action: @escaping @MainActor () -> Void126    ) {127        self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) {128            GlassButtonLabel(title: Text(verbatim: String(title)), systemImage: isLoading ? nil : systemImage)129        }130    }131}132133/// The standard label of a ``GlassButton``: an optional symbol followed by a title.134public struct GlassButtonLabel: View {135    let title: Text136    let systemImage: String?137138    public var body: some View {139        if let systemImage {140            Label { title } icon: { Image(systemName: systemImage) }141        } else {142            title143        }144    }145}146
Files main
Menu

Theme