SPB Git

Repository Public MIT

GlassFloatingButton.swift 5.4 KB 142 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 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            Group {87                if isExtended {88                    Label { title } icon: { Image(systemName: systemImage) }89                        .labelStyle(.titleAndIcon)90                } else {91                    Label { title } icon: { Image(systemName: systemImage) }92                        .labelStyle(.iconOnly)93                }94            }95            .glassButtonLabelContrast(.prominent)96        }97        .buttonBorderShape(isExtended ? .capsule : .circle)98        .glassButtonChrome(.prominent, size: .large)99        .glassMinimumHitTarget()100        .help(title)101    }102}103104extension View {105    /// Floats a prominent glass button over this view, inside its safe area.106    ///107    /// - Parameters:108    ///   - position: corner or edge, mirrored in right-to-left layouts.109    ///   - isPresented: hides the button without removing the modifier, for example while a search field is active.110    public func glassFloatingButton(111        _ titleKey: LocalizedStringKey,112        systemImage: String,113        position: GlassFloatingPosition = .bottomTrailing,114        isExtended: Bool = false,115        isPresented: Bool = true,116        action: @escaping @MainActor () -> Void117    ) -> some View {118        modifier(GlassFloatingButtonModifier(position: position, isPresented: isPresented) {119            GlassFloatingButton(titleKey, systemImage: systemImage, isExtended: isExtended, action: action)120        })121    }122}123124private struct GlassFloatingButtonModifier<Floating: View>: ViewModifier {125    @GlassThemed private var theme126    @GlassAccessibility private var accessibility127    let position: GlassFloatingPosition128    let isPresented: Bool129    @ViewBuilder let button: () -> Floating130131    func body(content: Content) -> some View {132        content.overlay(alignment: position.alignment) {133            if isPresented {134                button()135                    .padding(theme.spacing.md)136                    .transition(accessibility.reduceMotion ? .opacity : .scale.combined(with: .opacity))137            }138        }139        .animation(theme.animation(.fast, reduceMotion: accessibility.reduceMotion), value: isPresented)140    }141}142
Files main
Menu

Theme