SPB Git

Repository Public MIT

GlassPopover.swift 5.9 KB 148 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassComponents/Containers/GlassPopover.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/// What a popover becomes when the width or the height is compact (iPhone, iPad in a narrow split).48public enum GlassPopoverAdaptation: String, CaseIterable, Sendable {49    /// The system's choice: a sheet in compact sizes.50    case automatic51    /// Stays a popover with its arrow, even on iPhone. For short content only.52    case popover53    /// Becomes a sheet.54    case sheet55    /// Becomes a full-screen cover.56    case fullScreenCover5758    /// The system adaptation.59    public var presentationAdaptation: PresentationAdaptation {60        switch self {61        case .automatic: .automatic62        case .popover: .popover63        case .sheet: .sheet64        case .fullScreenCover: .fullScreenCover65        }66    }6768    /// Whether the content is shown without an arrow and an outside area to tap, and so needs a close button.69    ///70    /// - Parameters:71    ///   - isCompact: the horizontal or the vertical size class of the presented content is compact.72    ///   - platform: macOS popovers never adapt.73    public func needsCloseButton(isCompact: Bool, platform: GlassPlatform = .current) -> Bool {74        guard platform != .macOS, isCompact else { return false }75        return self != .popover76    }77}7879extension View {80    /// Presents a system popover anchored to this view.81    ///82    /// ```swift83    /// GlassIconButton(systemImage: "info", accessibilityLabel: "Details") { showsInfo = true }84    ///     .glassPopover(isPresented: $showsInfo, title: "Details", arrowEdge: .top) {85    ///         Text("Prices include taxes.").padding()86    ///     }87    /// ```88    ///89    /// The system draws the popover and its glass. When the popover adapts to a sheet or a cover, a title row90    /// with a close button is added above the content, because the arrow and the outside tap area are gone.91    ///92    /// - Parameters:93    ///   - title: optional heading shown above the content.94    ///   - attachmentAnchor: the point or rectangle the popover is attached to.95    ///   - arrowEdge: preferred edge of the popover that carries the arrow; `nil` lets the system choose.96    ///   - compactAdaptation: what the popover becomes in a compact size class.97    public func glassPopover<PopoverContent: View>(98        isPresented: Binding<Bool>,99        title: LocalizedStringKey? = nil,100        attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds),101        arrowEdge: Edge? = nil,102        compactAdaptation: GlassPopoverAdaptation = .automatic,103        @ViewBuilder content: @escaping () -> PopoverContent104    ) -> some View {105        popover(isPresented: isPresented, attachmentAnchor: attachmentAnchor, arrowEdge: arrowEdge) {106            GlassPopoverChrome(title: title.map { Text($0) }, adaptation: compactAdaptation, content: content())107        }108    }109}110111/// The inside of a glass popover: optional title, close button when adapted, then the content.112struct GlassPopoverChrome<Content: View>: View {113    @GlassThemed private var theme114    @Environment(\.dismiss) private var dismiss115    @Environment(\.horizontalSizeClass) private var horizontalSizeClass116    @Environment(\.verticalSizeClass) private var verticalSizeClass117    let title: Text?118    let adaptation: GlassPopoverAdaptation119    let content: Content120121    var body: some View {122        VStack(alignment: .leading, spacing: 0) {123            if title != nil || needsCloseButton {124                HStack(alignment: .firstTextBaseline, spacing: theme.spacing.xs) {125                    if let title {126                        title127                            .font(theme.font(.headline))128                            .foregroundStyle(theme.color(.textPrimary))129                            .accessibilityAddTraits(.isHeader)130                    }131                    Spacer(minLength: theme.spacing.xs)132                    if needsCloseButton {133                        Button(role: .close) { dismiss() }134                            .keyboardShortcut(.cancelAction)135                    }136                }137                .padding([.horizontal, .top], theme.spacing.md)138            }139            content140        }141        .presentationCompactAdaptation(adaptation.presentationAdaptation)142    }143144    private var needsCloseButton: Bool {145        adaptation.needsCloseButton(isCompact: horizontalSizeClass == .compact || verticalSizeClass == .compact)146    }147}148
Files main
Menu

Theme