SPB Git

Repository Public MIT

GlassPopover.swift 5.9 KB 144 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 SwiftUI4243/// What a popover becomes when the width or the height is compact (iPhone, iPad in a narrow split).44public enum GlassPopoverAdaptation: String, CaseIterable, Sendable {45    /// The system's choice: a sheet in compact sizes.46    case automatic47    /// Stays a popover with its arrow, even on iPhone. For short content only.48    case popover49    /// Becomes a sheet.50    case sheet51    /// Becomes a full-screen cover.52    case fullScreenCover5354    /// The system adaptation.55    public var presentationAdaptation: PresentationAdaptation {56        switch self {57        case .automatic: .automatic58        case .popover: .popover59        case .sheet: .sheet60        case .fullScreenCover: .fullScreenCover61        }62    }6364    /// Whether the content is shown without an arrow and an outside area to tap, and so needs a close button.65    ///66    /// - Parameters:67    ///   - isCompact: the horizontal or the vertical size class of the presented content is compact.68    ///   - platform: macOS popovers never adapt.69    public func needsCloseButton(isCompact: Bool, platform: GlassPlatform = .current) -> Bool {70        guard platform != .macOS, isCompact else { return false }71        return self != .popover72    }73}7475extension View {76    /// Presents a system popover anchored to this view.77    ///78    /// ```swift79    /// GlassIconButton(systemImage: "info", accessibilityLabel: "Details") { showsInfo = true }80    ///     .glassPopover(isPresented: $showsInfo, title: "Details", arrowEdge: .top) {81    ///         Text("Prices include taxes.").padding()82    ///     }83    /// ```84    ///85    /// The system draws the popover and its glass. When the popover adapts to a sheet or a cover, a title row86    /// with a close button is added above the content, because the arrow and the outside tap area are gone.87    ///88    /// - Parameters:89    ///   - title: optional heading shown above the content.90    ///   - attachmentAnchor: the point or rectangle the popover is attached to.91    ///   - arrowEdge: preferred edge of the popover that carries the arrow; `nil` lets the system choose.92    ///   - compactAdaptation: what the popover becomes in a compact size class.93    public func glassPopover<PopoverContent: View>(94        isPresented: Binding<Bool>,95        title: LocalizedStringKey? = nil,96        attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds),97        arrowEdge: Edge? = nil,98        compactAdaptation: GlassPopoverAdaptation = .automatic,99        @ViewBuilder content: @escaping () -> PopoverContent100    ) -> some View {101        popover(isPresented: isPresented, attachmentAnchor: attachmentAnchor, arrowEdge: arrowEdge) {102            GlassPopoverChrome(title: title.map { Text($0) }, adaptation: compactAdaptation, content: content())103        }104    }105}106107/// The inside of a glass popover: optional title, close button when adapted, then the content.108struct GlassPopoverChrome<Content: View>: View {109    @GlassThemed private var theme110    @Environment(\.dismiss) private var dismiss111    @Environment(\.horizontalSizeClass) private var horizontalSizeClass112    @Environment(\.verticalSizeClass) private var verticalSizeClass113    let title: Text?114    let adaptation: GlassPopoverAdaptation115    let content: Content116117    var body: some View {118        VStack(alignment: .leading, spacing: 0) {119            if title != nil || needsCloseButton {120                HStack(alignment: .firstTextBaseline, spacing: theme.spacing.xs) {121                    if let title {122                        title123                            .font(theme.font(.headline))124                            .foregroundStyle(theme.color(.textPrimary))125                            .accessibilityAddTraits(.isHeader)126                    }127                    Spacer(minLength: theme.spacing.xs)128                    if needsCloseButton {129                        Button(role: .close) { dismiss() }130                            .keyboardShortcut(.cancelAction)131                    }132                }133                .padding([.horizontal, .top], theme.spacing.md)134            }135            content136        }137        .presentationCompactAdaptation(adaptation.presentationAdaptation)138    }139140    private var needsCloseButton: Bool {141        adaptation.needsCloseButton(isCompact: horizontalSizeClass == .compact || verticalSizeClass == .compact)142    }143}144
Files main
Menu

Theme