Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Buttons/GlassFloatingButton.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 SwiftUI4243/// Corner or edge where a floating button sits.44public enum GlassFloatingPosition: String, CaseIterable, Sendable {45 case bottomTrailing, bottomLeading, bottomCenter, topTrailing, topLeading4647 /// Alignment inside the safe area. Leading and trailing follow the layout direction.48 public var alignment: Alignment {49 switch self {50 case .bottomTrailing: .bottomTrailing51 case .bottomLeading: .bottomLeading52 case .bottomCenter: .bottom53 case .topTrailing: .topTrailing54 case .topLeading: .topLeading55 }56 }57}5859/// A prominent glass button for the main action of a screen.60///61/// Use ``SwiftUICore/View/glassFloatingButton(_:systemImage:position:isExtended:isPresented:action:)`` to place it:62///63/// ```swift64/// List(items) { … }65/// .glassFloatingButton("New note", systemImage: "plus") { compose() }66/// ```67///68/// The button is laid out inside the safe area, so it stays clear of the home indicator, of bars and of the69/// software keyboard when it appears.70public struct GlassFloatingButton: View {71 private let title: Text72 private let systemImage: String73 private let isExtended: Bool74 private let action: @MainActor () -> Void7576 /// - Parameter isExtended: shows the title next to the symbol. When false the title is the accessibility label only.77 public init(_ titleKey: LocalizedStringKey, systemImage: String, isExtended: Bool = false, action: @escaping @MainActor () -> Void) {78 self.title = Text(titleKey)79 self.systemImage = systemImage80 self.isExtended = isExtended81 self.action = action82 }8384 public var body: some View {85 Button(action: action) {86 if isExtended {87 Label { title } icon: { Image(systemName: systemImage) }88 .labelStyle(.titleAndIcon)89 } else {90 Label { title } icon: { Image(systemName: systemImage) }91 .labelStyle(.iconOnly)92 }93 }94 .buttonBorderShape(isExtended ? .capsule : .circle)95 .glassButtonChrome(.prominent, size: .large)96 .glassMinimumHitTarget()97 .help(title)98 }99}100101extension View {102 /// Floats a prominent glass button over this view, inside its safe area.103 ///104 /// - Parameters:105 /// - position: corner or edge, mirrored in right-to-left layouts.106 /// - isPresented: hides the button without removing the modifier, for example while a search field is active.107 public func glassFloatingButton(108 _ titleKey: LocalizedStringKey,109 systemImage: String,110 position: GlassFloatingPosition = .bottomTrailing,111 isExtended: Bool = false,112 isPresented: Bool = true,113 action: @escaping @MainActor () -> Void114 ) -> some View {115 modifier(GlassFloatingButtonModifier(position: position, isPresented: isPresented) {116 GlassFloatingButton(titleKey, systemImage: systemImage, isExtended: isExtended, action: action)117 })118 }119}120121private struct GlassFloatingButtonModifier<Floating: View>: ViewModifier {122 @GlassThemed private var theme123 @GlassAccessibility private var accessibility124 let position: GlassFloatingPosition125 let isPresented: Bool126 @ViewBuilder let button: () -> Floating127128 func body(content: Content) -> some View {129 content.overlay(alignment: position.alignment) {130 if isPresented {131 button()132 .padding(theme.spacing.md)133 .transition(accessibility.reduceMotion ? .opacity : .scale.combined(with: .opacity))134 }135 }136 .animation(theme.animation(.fast, reduceMotion: accessibility.reduceMotion), value: isPresented)137 }138}139