// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Buttons/GlassFloatingButton.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 /// Corner or edge where a floating button sits. public enum GlassFloatingPosition: String, CaseIterable, Sendable { case bottomTrailing, bottomLeading, bottomCenter, topTrailing, topLeading /// Alignment inside the safe area. Leading and trailing follow the layout direction. public var alignment: Alignment { switch self { case .bottomTrailing: .bottomTrailing case .bottomLeading: .bottomLeading case .bottomCenter: .bottom case .topTrailing: .topTrailing case .topLeading: .topLeading } } } /// A prominent glass button for the main action of a screen. /// /// Use ``SwiftUICore/View/glassFloatingButton(_:systemImage:position:isExtended:isPresented:action:)`` to place it: /// /// ```swift /// List(items) { … } /// .glassFloatingButton("New note", systemImage: "plus") { compose() } /// ``` /// /// The button is laid out inside the safe area, so it stays clear of the home indicator, of bars and of the /// software keyboard when it appears. public struct GlassFloatingButton: View { private let title: Text private let systemImage: String private let isExtended: Bool private let action: @MainActor () -> Void /// - Parameter isExtended: shows the title next to the symbol. When false the title is the accessibility label only. public init(_ titleKey: LocalizedStringKey, systemImage: String, isExtended: Bool = false, action: @escaping @MainActor () -> Void) { self.title = Text(titleKey) self.systemImage = systemImage self.isExtended = isExtended self.action = action } public var body: some View { Button(action: action) { Group { if isExtended { Label { title } icon: { Image(systemName: systemImage) } .labelStyle(.titleAndIcon) } else { Label { title } icon: { Image(systemName: systemImage) } .labelStyle(.iconOnly) } } .glassButtonLabelContrast(.prominent) } .buttonBorderShape(isExtended ? .capsule : .circle) .glassButtonChrome(.prominent, size: .large) .glassMinimumHitTarget() .help(title) } } extension View { /// Floats a prominent glass button over this view, inside its safe area. /// /// - Parameters: /// - position: corner or edge, mirrored in right-to-left layouts. /// - isPresented: hides the button without removing the modifier, for example while a search field is active. public func glassFloatingButton( _ titleKey: LocalizedStringKey, systemImage: String, position: GlassFloatingPosition = .bottomTrailing, isExtended: Bool = false, isPresented: Bool = true, action: @escaping @MainActor () -> Void ) -> some View { modifier(GlassFloatingButtonModifier(position: position, isPresented: isPresented) { GlassFloatingButton(titleKey, systemImage: systemImage, isExtended: isExtended, action: action) }) } } private struct GlassFloatingButtonModifier: ViewModifier { @GlassThemed private var theme @GlassAccessibility private var accessibility let position: GlassFloatingPosition let isPresented: Bool @ViewBuilder let button: () -> Floating func body(content: Content) -> some View { content.overlay(alignment: position.alignment) { if isPresented { button() .padding(theme.spacing.md) .transition(accessibility.reduceMotion ? .opacity : .scale.combined(with: .opacity)) } } .animation(theme.animation(.fast, reduceMotion: accessibility.reduceMotion), value: isPresented) } }