// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Containers/GlassPanel.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 /// Bounds of a resizable ``GlassPanel`` and the pure rules that keep a size inside them. public struct GlassPanelSizeLimits: Equatable, Sendable { /// Smallest size the panel can take. public var minimum: CGSize /// Largest size the panel can take. public var maximum: CGSize /// Creates limits. A maximum below the minimum is raised to the minimum; negative values count as zero. public init(minimum: CGSize = CGSize(width: 240, height: 160), maximum: CGSize = CGSize(width: 720, height: 640)) { let minWidth = Self.sanitize(minimum.width), minHeight = Self.sanitize(minimum.height) self.minimum = CGSize(width: minWidth, height: minHeight) self.maximum = CGSize(width: max(minWidth, Self.sanitize(maximum.width)), height: max(minHeight, Self.sanitize(maximum.height))) } /// 240 × 160 to 720 × 640 points. public static let standard = GlassPanelSizeLimits() /// The size nearest to `size` that respects the limits. Non-finite components fall back to the minimum. public func clamp(_ size: CGSize) -> CGSize { CGSize( width: Self.clamp(size.width, minimum.width, maximum.width), height: Self.clamp(size.height, minimum.height, maximum.height) ) } /// The size after dragging the trailing-bottom handle by `translation` from `start`. /// /// In a right-to-left layout the handle sits on the left, so a drag to the left widens the panel. public func resized(from start: CGSize, translation: CGSize, isRightToLeft: Bool = false) -> CGSize { let horizontal = isRightToLeft ? -translation.width : translation.width return clamp(CGSize(width: start.width + horizontal, height: start.height + translation.height)) } private static func sanitize(_ value: CGFloat) -> CGFloat { value.isFinite ? max(0, value) : 0 } private static func clamp(_ value: CGFloat, _ lower: CGFloat, _ upper: CGFloat) -> CGFloat { guard value.isFinite else { return lower } return min(max(value, lower), upper) } } /// A floating glass panel of actions or short content, with a title and an optional close button. /// /// ```swift /// GlassPanel("Layers", size: $panelSize, onClose: { showsLayers = false }) { /// Toggle("Transit", isOn: $showsTransit) /// Toggle("Traffic", isOn: $showsTraffic) /// } /// ``` /// /// The panel belongs to the navigation layer: it floats above content and holds controls, not long text. /// On macOS, pass a `size` binding to let the user resize it with the handle in the trailing-bottom corner /// (drag, arrow keys or VoiceOver adjust); keyboard focus moves inside the panel as one section and Escape /// calls `onClose`. On iOS and iPadOS the panel sizes itself to its content and `size` is ignored. public struct GlassPanel: View { @GlassThemed private var theme @Environment(\.layoutDirection) private var layoutDirection @Namespace private var focusNamespace @State private var dragStart: CGSize? private let title: Text private let size: Binding? private let limits: GlassPanelSizeLimits private let onClose: (@MainActor () -> Void)? private let content: Content /// Creates a panel. /// /// - Parameters: /// - title: heading of the panel, also its accessibility label. /// - size: controlled size on macOS. `nil` sizes the panel to its content and hides the resize handle. /// - sizeLimits: bounds applied to `size`. /// - onClose: when set, a close button is shown and Escape calls it. public init( _ title: LocalizedStringKey, size: Binding? = nil, sizeLimits: GlassPanelSizeLimits = .standard, onClose: (@MainActor () -> Void)? = nil, @ViewBuilder content: () -> Content ) { self.init(title: Text(title), size: size, sizeLimits: sizeLimits, onClose: onClose, content: content) } /// Creates a panel whose title is already a `Text`. public init( title: Text, size: Binding? = nil, sizeLimits: GlassPanelSizeLimits = .standard, onClose: (@MainActor () -> Void)? = nil, @ViewBuilder content: () -> Content ) { self.title = title self.size = size self.limits = sizeLimits self.onClose = onClose self.content = content() } public var body: some View { platformBehaviour( sized( VStack(alignment: .leading, spacing: 0) { titleBar .padding(.leading, theme.spacing.md) .padding(.trailing, theme.spacing.xs) .padding(.vertical, theme.spacing.xxs) Rectangle() .fill(theme.color(.textPrimary).opacity(0.12)) .frame(height: 1) .accessibilityHidden(true) VStack(alignment: .leading, spacing: theme.spacing.sm) { content .font(theme.font(.body)) .foregroundStyle(theme.color(.textPrimary)) } .padding(theme.spacing.md) } ) // Neutral glass: the panel carries text and controls in the theme's colours. .glassSurface(.regular.tinted(.none), in: theme.shape(\.large)) .overlay(alignment: .bottomTrailing) { resizeHandle } ) .accessibilityElement(children: .contain) .accessibilityLabel(title) } private var titleBar: some View { HStack(alignment: .center, spacing: theme.spacing.xs) { title .font(theme.font(.headline)) .foregroundStyle(theme.color(.textPrimary)) .lineLimit(2) .accessibilityAddTraits(.isHeader) Spacer(minLength: theme.spacing.xs) if let onClose { // A plain system button: the panel is already glass, and glass never sits inside glass. Button(action: onClose) { Label(GlassL10n.string(.close), systemImage: "xmark") .labelStyle(.iconOnly) .font(.system(.footnote, weight: .bold)) .foregroundStyle(theme.color(.textSecondary)) .frame(width: 28, height: 28) .background(theme.color(.textPrimary).opacity(0.1), in: Circle()) } .buttonStyle(.plain) .glassMinimumHitTarget() #if !os(macOS) .keyboardShortcut(.cancelAction) #endif } else { Color.clear.frame(width: 1, height: 44).accessibilityHidden(true) } } } @ViewBuilder private func sized(_ view: some View) -> some View { #if os(macOS) if let size { let clamped = limits.clamp(size.wrappedValue) view.frame(width: clamped.width, height: clamped.height, alignment: .topLeading) } else { view } #else view #endif } @ViewBuilder private func platformBehaviour(_ view: some View) -> some View { #if os(macOS) if let onClose { view.focusScope(focusNamespace).focusSection().onExitCommand { onClose() } } else { view.focusScope(focusNamespace).focusSection() } #else view #endif } @ViewBuilder private var resizeHandle: some View { #if os(macOS) if let size { let step = theme.spacing.md Image(systemName: "arrow.up.left.and.arrow.down.right") .font(theme.font(.caption)) .foregroundStyle(theme.color(.textSecondary)) .padding(theme.spacing.xs) .glassMinimumHitTarget() .gesture( DragGesture(minimumDistance: 1) .onChanged { value in let start = dragStart ?? limits.clamp(size.wrappedValue) dragStart = start size.wrappedValue = limits.resized(from: start, translation: value.translation, isRightToLeft: layoutDirection == .rightToLeft) } .onEnded { _ in dragStart = nil } ) .focusable() .onMoveCommand { direction in let delta: CGSize = switch direction { case .left: CGSize(width: -step, height: 0) case .right: CGSize(width: step, height: 0) case .up: CGSize(width: 0, height: -step) case .down: CGSize(width: 0, height: step) @unknown default: .zero } size.wrappedValue = limits.resized(from: limits.clamp(size.wrappedValue), translation: delta) } .accessibilityElement() .accessibilityLabel(GlassL10n.string(.resizePanel)) .accessibilityValue(GlassL10n.string(.panelSize, Int(limits.clamp(size.wrappedValue).width), Int(limits.clamp(size.wrappedValue).height))) .accessibilityAdjustableAction { direction in let amount: CGFloat = direction == .increment ? step : -step size.wrappedValue = limits.resized(from: limits.clamp(size.wrappedValue), translation: CGSize(width: amount, height: amount)) } } #endif } }