SPB Git

Repository Public MIT

GlassButton.swift 5.2 KB 144 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-0313//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            .glassButtonLabelContrast(variant, role: role)91        }92        .glassButtonChrome(variant, role: role, size: size)93        .disabled(isLoading)94        .accessibilityValue(isLoading ? Text(GlassL10n.string(.loading)) : Text(verbatim: ""))95    }96}9798extension GlassButton where Label == GlassButtonLabel {99    /// Creates a button with a localised title and an optional SF Symbol.100    public init(101        _ titleKey: LocalizedStringKey,102        systemImage: String? = nil,103        variant: GlassButtonVariant = .regular,104        role: ButtonRole? = nil,105        size: GlassControlSize = .regular,106        isLoading: Bool = false,107        action: @escaping @MainActor () -> Void108    ) {109        self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) {110            GlassButtonLabel(title: Text(titleKey), systemImage: isLoading ? nil : systemImage)111        }112    }113114    /// Creates a button with a title that is already a string (data, user content) and an optional SF Symbol.115    public init<S: StringProtocol>(116        verbatim title: S,117        systemImage: String? = nil,118        variant: GlassButtonVariant = .regular,119        role: ButtonRole? = nil,120        size: GlassControlSize = .regular,121        isLoading: Bool = false,122        action: @escaping @MainActor () -> Void123    ) {124        self.init(variant: variant, role: role, size: size, isLoading: isLoading, action: action) {125            GlassButtonLabel(title: Text(verbatim: String(title)), systemImage: isLoading ? nil : systemImage)126        }127    }128}129130/// The standard label of a ``GlassButton``: an optional symbol followed by a title.131public struct GlassButtonLabel: View {132    let title: Text133    let systemImage: String?134135    public var body: some View {136        // One line: a button title is truncated, never broken in the middle of a word.137        if let systemImage {138            Label { title.lineLimit(1) } icon: { Image(systemName: systemImage) }139        } else {140            title.lineLimit(1)141        }142    }143}144
Files main
Menu

Theme