SPB Git

Repository Public MIT

GlassFloatingButton.swift 5.5 KB 146 lines swift

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-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 SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// Corner or edge where a floating button sits.48public enum GlassFloatingPosition: String, CaseIterable, Sendable {49    case bottomTrailing, bottomLeading, bottomCenter, topTrailing, topLeading5051    /// Alignment inside the safe area. Leading and trailing follow the layout direction.52    public var alignment: Alignment {53        switch self {54        case .bottomTrailing: .bottomTrailing55        case .bottomLeading: .bottomLeading56        case .bottomCenter: .bottom57        case .topTrailing: .topTrailing58        case .topLeading: .topLeading59        }60    }61}6263/// A prominent glass button for the main action of a screen.64///65/// Use ``SwiftUICore/View/glassFloatingButton(_:systemImage:position:isExtended:isPresented:action:)`` to place it:66///67/// ```swift68/// List(items) { … }69///     .glassFloatingButton("New note", systemImage: "plus") { compose() }70/// ```71///72/// The button is laid out inside the safe area, so it stays clear of the home indicator, of bars and of the73/// software keyboard when it appears.74public struct GlassFloatingButton: View {75    private let title: Text76    private let systemImage: String77    private let isExtended: Bool78    private let action: @MainActor () -> Void7980    /// - Parameter isExtended: shows the title next to the symbol. When false the title is the accessibility label only.81    public init(_ titleKey: LocalizedStringKey, systemImage: String, isExtended: Bool = false, action: @escaping @MainActor () -> Void) {82        self.title = Text(titleKey)83        self.systemImage = systemImage84        self.isExtended = isExtended85        self.action = action86    }8788    public var body: some View {89        Button(action: action) {90            Group {91                if isExtended {92                    Label { title } icon: { Image(systemName: systemImage) }93                        .labelStyle(.titleAndIcon)94                } else {95                    Label { title } icon: { Image(systemName: systemImage) }96                        .labelStyle(.iconOnly)97                }98            }99            .glassButtonLabelContrast(.prominent)100        }101        .buttonBorderShape(isExtended ? .capsule : .circle)102        .glassButtonChrome(.prominent, size: .large)103        .glassMinimumHitTarget()104        .help(title)105    }106}107108extension View {109    /// Floats a prominent glass button over this view, inside its safe area.110    ///111    /// - Parameters:112    ///   - position: corner or edge, mirrored in right-to-left layouts.113    ///   - isPresented: hides the button without removing the modifier, for example while a search field is active.114    public func glassFloatingButton(115        _ titleKey: LocalizedStringKey,116        systemImage: String,117        position: GlassFloatingPosition = .bottomTrailing,118        isExtended: Bool = false,119        isPresented: Bool = true,120        action: @escaping @MainActor () -> Void121    ) -> some View {122        modifier(GlassFloatingButtonModifier(position: position, isPresented: isPresented) {123            GlassFloatingButton(titleKey, systemImage: systemImage, isExtended: isExtended, action: action)124        })125    }126}127128private struct GlassFloatingButtonModifier<Floating: View>: ViewModifier {129    @GlassThemed private var theme130    @GlassAccessibility private var accessibility131    let position: GlassFloatingPosition132    let isPresented: Bool133    @ViewBuilder let button: () -> Floating134135    func body(content: Content) -> some View {136        content.overlay(alignment: position.alignment) {137            if isPresented {138                button()139                    .padding(theme.spacing.md)140                    .transition(accessibility.reduceMotion ? .opacity : .scale.combined(with: .opacity))141            }142        }143        .animation(theme.animation(.fast, reduceMotion: accessibility.reduceMotion), value: isPresented)144    }145}146
Files main
Menu

Theme