SPB Git

Repository Public MIT

GlassButton.swift 5.0 KB 142 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 SwiftUI4243/// A button drawn with the system Liquid Glass button styles.44///45/// ```swift46/// GlassButton("Save", systemImage: "checkmark", variant: .prominent, isLoading: isSaving) {47///     save()48/// }49/// GlassButton("Delete", role: .destructive) { delete() }50/// ```51///52/// The system draws the button: GlassKit only chooses the style, the tint and the control size. While53/// `isLoading` is true the button shows a progress indicator, ignores taps and reports "Loading" to VoiceOver.54public struct GlassButton<Label: View>: View {55    @GlassThemed private var theme56    private let variant: GlassButtonVariant57    private let role: ButtonRole?58    private let size: GlassControlSize59    private let isLoading: Bool60    private let action: @MainActor () -> Void61    private let label: Label6263    /// Creates a button with a custom label.64    public init(65        variant: GlassButtonVariant = .regular,66        role: ButtonRole? = nil,67        size: GlassControlSize = .regular,68        isLoading: Bool = false,69        action: @escaping @MainActor () -> Void,70        @ViewBuilder label: () -> Label71    ) {72        self.variant = variant73        self.role = role74        self.size = size75        self.isLoading = isLoading76        self.action = action77        self.label = label()78    }7980    public var body: some View {81        Button(role: role, action: action) {82            HStack(spacing: theme.spacing.xs) {83                if isLoading {84                    ProgressView()85                        .controlSize(.small)86                        .accessibilityHidden(true)87                }88                label89            }90        }91        .glassButtonChrome(variant, role: role, size: size)92        .disabled(isLoading)93        .accessibilityValue(isLoading ? Text(GlassL10n.string(.loading)) : Text(verbatim: ""))94    }95}9697extension GlassButton where Label == GlassButtonLabel {98    /// Creates a button with a localised title and an optional SF Symbol.99    public init(100        _ titleKey: LocalizedStringKey,101        systemImage: String? = nil,102        variant: GlassButtonVariant = .regular,103        role: ButtonRole? = nil,104        size: GlassControlSize = .regular,105        isLoading: Bool = false,106        action: @escaping @MainActor () -> Void107    ) {108        self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) {109            GlassButtonLabel(title: Text(titleKey), systemImage: isLoading ? nil : systemImage)110        }111    }112113    /// Creates a button with a title that is already a string (data, user content) and an optional SF Symbol.114    public init<S: StringProtocol>(115        verbatim title: S,116        systemImage: String? = nil,117        variant: GlassButtonVariant = .regular,118        role: ButtonRole? = nil,119        size: GlassControlSize = .regular,120        isLoading: Bool = false,121        action: @escaping @MainActor () -> Void122    ) {123        self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) {124            GlassButtonLabel(title: Text(verbatim: String(title)), systemImage: isLoading ? nil : systemImage)125        }126    }127}128129/// The standard label of a ``GlassButton``: an optional symbol followed by a title.130public struct GlassButtonLabel: View {131    let title: Text132    let systemImage: String?133134    public var body: some View {135        if let systemImage {136            Label { title } icon: { Image(systemName: systemImage) }137        } else {138            title139        }140    }141}142
Files main
Menu

Theme