// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Containers/GlassSheet.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 /// A resting height of a ``SwiftUICore/View/glassSheet(isPresented:title:configuration:onDismiss:content:)``. public enum GlassSheetDetent: Hashable, Sendable { /// About half of the screen. case medium /// The full height. case large /// A fraction of the available height, kept between 0.1 and 1. case fraction(Double) /// A fixed height in points, at least 44. case height(Double) /// The same detent with its value brought inside the accepted range. Non-finite values become `.large`. public var normalized: GlassSheetDetent { switch self { case .medium, .large: return self case .fraction(let value): guard value.isFinite else { return .large } return .fraction(min(max(value, 0.1), 1)) case .height(let value): guard value.isFinite else { return .large } return .height(max(value, 44)) } } /// The system detent. public var presentationDetent: PresentationDetent { switch normalized { case .medium: .medium case .large: .large case .fraction(let value): .fraction(value) case .height(let value): .height(value) } } /// The set handed to `presentationDetents`. An empty list means a full-height sheet. public static func presentationDetents(_ detents: [GlassSheetDetent]) -> Set { detents.isEmpty ? [.large] : Set(detents.map(\.presentationDetent)) } } /// Options of a glass sheet. public struct GlassSheetConfiguration: Equatable, Sendable { /// Heights the sheet can rest at. public var detents: [GlassSheetDetent] /// Shows the drag indicator when the sheet has more than one detent or can be dragged. public var showsDragIndicator: Bool /// Shows a close button in the title bar. Keep it on unless the content has its own way out. public var showsCloseButton: Bool /// Prevents dismissal by swipe; the close button and the host still dismiss the sheet. public var isInteractiveDismissDisabled: Bool /// Lets a scroll gesture inside the sheet dismiss the keyboard interactively. public var scrollDismissesKeyboard: Bool public init( detents: [GlassSheetDetent] = [.large], showsDragIndicator: Bool = true, showsCloseButton: Bool = true, isInteractiveDismissDisabled: Bool = false, scrollDismissesKeyboard: Bool = true ) { self.detents = detents self.showsDragIndicator = showsDragIndicator self.showsCloseButton = showsCloseButton self.isInteractiveDismissDisabled = isInteractiveDismissDisabled self.scrollDismissesKeyboard = scrollDismissesKeyboard } /// A full-height sheet with a close button. public static let standard = GlassSheetConfiguration() /// A sheet that rests at half height and can be pulled to full height. public static let halfAndFull = GlassSheetConfiguration(detents: [.medium, .large]) /// Whether the content is wrapped in a navigation bar (a title or a close button is requested). public func needsTitleBar(hasTitle: Bool) -> Bool { hasTitle || showsCloseButton } } extension View { /// Presents a system sheet with detents, an optional title bar and an accessible close button. /// /// ```swift /// .glassSheet(isPresented: $showsFilters, title: "Filters", configuration: .halfAndFull) { /// FiltersForm() /// } /// ``` /// /// The system draws the sheet, including its Liquid Glass background at partial height; GlassKit does not /// add a surface. Keyboard avoidance is the system's. The title bar is a `NavigationStack` toolbar, so the /// content can add its own toolbar items. public func glassSheet( isPresented: Binding, title: LocalizedStringKey? = nil, configuration: GlassSheetConfiguration = .standard, onDismiss: (@MainActor () -> Void)? = nil, @ViewBuilder content: @escaping () -> SheetContent ) -> some View { sheet(isPresented: isPresented, onDismiss: onDismiss.map { handler in { handler() } }) { GlassSheetChrome(title: title.map { Text($0) }, configuration: configuration, content: content()) } } /// Presents a system sheet for an optional item; the sheet is dismissed when the item becomes `nil`. public func glassSheet( item: Binding, title: LocalizedStringKey? = nil, configuration: GlassSheetConfiguration = .standard, onDismiss: (@MainActor () -> Void)? = nil, @ViewBuilder content: @escaping (Item) -> SheetContent ) -> some View { sheet(item: item, onDismiss: onDismiss.map { handler in { handler() } }) { value in GlassSheetChrome(title: title.map { Text($0) }, configuration: configuration, content: content(value)) } } } /// The inside of a glass sheet: presentation options, title bar and close button. struct GlassSheetChrome: View { @Environment(\.dismiss) private var dismiss let title: Text? let configuration: GlassSheetConfiguration let content: Content var body: some View { framed .presentationDetents(GlassSheetDetent.presentationDetents(configuration.detents)) .presentationDragIndicator(configuration.showsDragIndicator ? .visible : .hidden) .interactiveDismissDisabled(configuration.isInteractiveDismissDisabled) } @ViewBuilder private var framed: some View { if configuration.needsTitleBar(hasTitle: title != nil) { NavigationStack { scrolling .navigationTitle(title ?? Text(verbatim: "")) #if !os(macOS) .navigationBarTitleDisplayMode(.inline) #endif .toolbar { if configuration.showsCloseButton { ToolbarItem(placement: .cancellationAction) { // The system close role supplies the symbol and the accessibility label. Button(role: .close) { dismiss() } } } } } } else { scrolling } } private var scrolling: some View { content.scrollDismissesKeyboard(configuration.scrollDismissesKeyboard ? .interactively : .automatic) } }