// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Containers/GlassPopover.swift // Module : SPGlassComponents // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-02 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// What a popover becomes when the width or the height is compact (iPhone, iPad in a narrow split). public enum GlassPopoverAdaptation: String, CaseIterable, Sendable { /// The system's choice: a sheet in compact sizes. case automatic /// Stays a popover with its arrow, even on iPhone. For short content only. case popover /// Becomes a sheet. case sheet /// Becomes a full-screen cover. case fullScreenCover /// The system adaptation. public var presentationAdaptation: PresentationAdaptation { switch self { case .automatic: .automatic case .popover: .popover case .sheet: .sheet case .fullScreenCover: .fullScreenCover } } /// Whether the content is shown without an arrow and an outside area to tap, and so needs a close button. /// /// - Parameters: /// - isCompact: the horizontal or the vertical size class of the presented content is compact. /// - platform: macOS popovers never adapt. public func needsCloseButton(isCompact: Bool, platform: GlassPlatform = .current) -> Bool { guard platform != .macOS, isCompact else { return false } return self != .popover } } extension View { /// Presents a system popover anchored to this view. /// /// ```swift /// GlassIconButton(systemImage: "info", accessibilityLabel: "Details") { showsInfo = true } /// .glassPopover(isPresented: $showsInfo, title: "Details", arrowEdge: .top) { /// Text("Prices include taxes.").padding() /// } /// ``` /// /// The system draws the popover and its glass. When the popover adapts to a sheet or a cover, a title row /// with a close button is added above the content, because the arrow and the outside tap area are gone. /// /// - Parameters: /// - title: optional heading shown above the content. /// - attachmentAnchor: the point or rectangle the popover is attached to. /// - arrowEdge: preferred edge of the popover that carries the arrow; `nil` lets the system choose. /// - compactAdaptation: what the popover becomes in a compact size class. public func glassPopover( isPresented: Binding, title: LocalizedStringKey? = nil, attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds), arrowEdge: Edge? = nil, compactAdaptation: GlassPopoverAdaptation = .automatic, @ViewBuilder content: @escaping () -> PopoverContent ) -> some View { popover(isPresented: isPresented, attachmentAnchor: attachmentAnchor, arrowEdge: arrowEdge) { GlassPopoverChrome(title: title.map { Text($0) }, adaptation: compactAdaptation, content: content()) } } } /// The inside of a glass popover: optional title, close button when adapted, then the content. struct GlassPopoverChrome: View { @GlassThemed private var theme @Environment(\.dismiss) private var dismiss @Environment(\.horizontalSizeClass) private var horizontalSizeClass @Environment(\.verticalSizeClass) private var verticalSizeClass let title: Text? let adaptation: GlassPopoverAdaptation let content: Content var body: some View { VStack(alignment: .leading, spacing: 0) { if title != nil || needsCloseButton { HStack(alignment: .firstTextBaseline, spacing: theme.spacing.xs) { if let title { title .font(theme.font(.headline)) .foregroundStyle(theme.color(.textPrimary)) .accessibilityAddTraits(.isHeader) } Spacer(minLength: theme.spacing.xs) if needsCloseButton { Button(role: .close) { dismiss() } .keyboardShortcut(.cancelAction) } } .padding([.horizontal, .top], theme.spacing.md) } content } .presentationCompactAdaptation(adaptation.presentationAdaptation) } private var needsCloseButton: Bool { adaptation.needsCloseButton(isCompact: horizontalSizeClass == .compact || verticalSizeClass == .compact) } }