SPB Git

Repository Public MIT

GlassIconButton.swift 4.4 KB 114 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassComponents/Buttons/GlassIconButton.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 circular glass button that shows only an SF Symbol.44///45/// ```swift46/// GlassIconButton(systemImage: "square.and.arrow.up", accessibilityLabel: "Share") { share() }47/// ```48///49/// The accessibility label is required: it is what VoiceOver reads, the tooltip on macOS and the name used by50/// Voice Control. The hit target is at least 44 × 44 points on iOS and iPadOS whatever the size.51public struct GlassIconButton: View {52    @GlassThemed private var theme53    private let systemImage: String54    private let label: Text55    private let variant: GlassButtonVariant56    private let role: ButtonRole?57    private let size: GlassControlSize58    private let action: @MainActor () -> Void5960    public init(61        systemImage: String,62        accessibilityLabel: LocalizedStringKey,63        variant: GlassButtonVariant = .regular,64        role: ButtonRole? = nil,65        size: GlassControlSize = .regular,66        action: @escaping @MainActor () -> Void67    ) {68        self.init(systemImage: systemImage, label: Text(accessibilityLabel), variant: variant, role: role, size: size, action: action)69    }7071    /// Creates an icon button whose label is already a `Text` (for example a string localised by GlassKit).72    public init(73        systemImage: String,74        label: Text,75        variant: GlassButtonVariant = .regular,76        role: ButtonRole? = nil,77        size: GlassControlSize = .regular,78        action: @escaping @MainActor () -> Void79    ) {80        self.systemImage = systemImage81        self.label = label82        self.variant = variant83        self.role = role84        self.size = size85        self.action = action86    }8788    public var body: some View {89        Button(role: role, action: action) {90            Label { label } icon: { Image(systemName: systemImage) }91                .labelStyle(.iconOnly)92                .frame(minWidth: GlassIconButtonMetrics.symbolFrame(size, metrics: theme.metrics), minHeight: GlassIconButtonMetrics.symbolFrame(size, metrics: theme.metrics))93                .glassButtonLabelContrast(variant, role: role)94        }95        .buttonBorderShape(.circle)96        .glassButtonChrome(variant, role: role, size: size)97        .glassMinimumHitTarget()98        .help(label)99    }100}101102/// Sizing rules of ``GlassIconButton``, separated from the view so that they can be tested.103public enum GlassIconButtonMetrics {104    /// Side of the square reserved for the symbol inside the button, before the system adds its own padding.105    public static func symbolFrame(_ size: GlassControlSize, metrics: GlassControlMetrics) -> Double {106        metrics.iconSize(size) + 4107    }108109    /// Side of the tappable area: never below the platform minimum.110    public static func hitTarget(_ size: GlassControlSize, metrics: GlassControlMetrics) -> Double {111        max(metrics.height(size), metrics.minimumHitTarget)112    }113}114
Files main
Menu

Theme