// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Buttons/GlassIconButton.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 circular glass button that shows only an SF Symbol. /// /// ```swift /// GlassIconButton(systemImage: "square.and.arrow.up", accessibilityLabel: "Share") { share() } /// ``` /// /// The accessibility label is required: it is what VoiceOver reads, the tooltip on macOS and the name used by /// Voice Control. The hit target is at least 44 × 44 points on iOS and iPadOS whatever the size. public struct GlassIconButton: View { @GlassThemed private var theme private let systemImage: String private let label: Text private let variant: GlassButtonVariant private let role: ButtonRole? private let size: GlassControlSize private let action: @MainActor () -> Void public init( systemImage: String, accessibilityLabel: LocalizedStringKey, variant: GlassButtonVariant = .regular, role: ButtonRole? = nil, size: GlassControlSize = .regular, action: @escaping @MainActor () -> Void ) { self.init(systemImage: systemImage, label: Text(accessibilityLabel), variant: variant, role: role, size: size, action: action) } /// Creates an icon button whose label is already a `Text` (for example a string localised by GlassKit). public init( systemImage: String, label: Text, variant: GlassButtonVariant = .regular, role: ButtonRole? = nil, size: GlassControlSize = .regular, action: @escaping @MainActor () -> Void ) { self.systemImage = systemImage self.label = label self.variant = variant self.role = role self.size = size self.action = action } public var body: some View { Button(role: role, action: action) { Label { label } icon: { Image(systemName: systemImage) } .labelStyle(.iconOnly) .frame(minWidth: GlassIconButtonMetrics.symbolFrame(size, metrics: theme.metrics), minHeight: GlassIconButtonMetrics.symbolFrame(size, metrics: theme.metrics)) .glassButtonLabelContrast(variant, role: role) } .buttonBorderShape(.circle) .glassButtonChrome(variant, role: role, size: size) .glassMinimumHitTarget() .help(label) } } /// Sizing rules of ``GlassIconButton``, separated from the view so that they can be tested. public enum GlassIconButtonMetrics { /// Side of the square reserved for the symbol inside the button, before the system adds its own padding. public static func symbolFrame(_ size: GlassControlSize, metrics: GlassControlMetrics) -> Double { metrics.iconSize(size) + 4 } /// Side of the tappable area: never below the platform minimum. public static func hitTarget(_ size: GlassControlSize, metrics: GlassControlMetrics) -> Double { max(metrics.height(size), metrics.minimumHitTarget) } }