SPB Git

Repository Public MIT

GlassIconButton.swift 4.4 KB 117 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-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 circular glass button that shows only an SF Symbol.48///49/// ```swift50/// GlassIconButton(systemImage: "square.and.arrow.up", accessibilityLabel: "Share") { share() }51/// ```52///53/// The accessibility label is required: it is what VoiceOver reads, the tooltip on macOS and the name used by54/// Voice Control. The hit target is at least 44 × 44 points on iOS and iPadOS whatever the size.55public struct GlassIconButton: View {56    @GlassThemed private var theme57    private let systemImage: String58    private let label: Text59    private let variant: GlassButtonVariant60    private let role: ButtonRole?61    private let size: GlassControlSize62    private let action: @MainActor () -> Void6364    public init(65        systemImage: String,66        accessibilityLabel: LocalizedStringKey,67        variant: GlassButtonVariant = .regular,68        role: ButtonRole? = nil,69        size: GlassControlSize = .regular,70        action: @escaping @MainActor () -> Void71    ) {72        self.init(systemImage: systemImage, label: Text(accessibilityLabel), variant: variant, role: role, size: size, action: action)73    }7475    /// Creates an icon button whose label is already a `Text` (for example a string localised by GlassKit).76    public init(77        systemImage: String,78        label: Text,79        variant: GlassButtonVariant = .regular,80        role: ButtonRole? = nil,81        size: GlassControlSize = .regular,82        action: @escaping @MainActor () -> Void83    ) {84        self.systemImage = systemImage85        self.label = label86        self.variant = variant87        self.role = role88        self.size = size89        self.action = action90    }9192    public var body: some View {93        Button(role: role, action: action) {94            Label { label } icon: { Image(systemName: systemImage) }95                .labelStyle(.iconOnly)96                .frame(minWidth: GlassIconButtonMetrics.symbolFrame(size, metrics: theme.metrics), minHeight: GlassIconButtonMetrics.symbolFrame(size, metrics: theme.metrics))97        }98        .buttonBorderShape(.circle)99        .glassButtonChrome(variant, role: role, size: size)100        .glassMinimumHitTarget()101        .help(label)102    }103}104105/// Sizing rules of ``GlassIconButton``, separated from the view so that they can be tested.106public enum GlassIconButtonMetrics {107    /// Side of the square reserved for the symbol inside the button, before the system adds its own padding.108    public static func symbolFrame(_ size: GlassControlSize, metrics: GlassControlMetrics) -> Double {109        metrics.iconSize(size) + 4110    }111112    /// Side of the tappable area: never below the platform minimum.113    public static func hitTarget(_ size: GlassControlSize, metrics: GlassControlMetrics) -> Double {114        max(metrics.height(size), metrics.minimumHitTarget)115    }116}117
Files main
Menu

Theme