// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Containers/GlassCard.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 /// Surface of a ``GlassCard``. public enum GlassCardStyle: String, CaseIterable, Sendable { /// An opaque themed surface. The default, and the right choice for dense content (text, tables, forms). case opaque /// A Liquid Glass surface, for a card that floats above other content (a map, a photo, a scrolling feed). case floating /// The glass style used for this card, or `nil` when the card is opaque. /// /// Glass reacts to touch only when the card itself is tappable. The glass is neutral: a card carries /// text in the theme's colours, which a theme's glass tint would make hard to read. public func surfaceStyle(isTappable: Bool) -> GlassSurfaceStyle? { switch self { case .opaque: nil case .floating: GlassSurfaceStyle(tint: .none, isInteractive: isTappable) } } } /// A container for one piece of content, with an optional media area, header and footer. /// /// ```swift /// GlassCard("Monthly rent") { /// Text("1,450 $").font(.title) /// } /// /// GlassCard(media: { ListingPhoto() }) { /// Text("Two bedrooms, 74 m², balcony.") /// } header: { /// Text("Loft on the canal") /// } footer: { /// Button("Book a visit") { book() } /// } /// /// GlassCard(style: .floating, action: { openListing() }) { /// Label("3 listings nearby", systemImage: "mappin.and.ellipse") /// } /// ``` /// /// The card is opaque by default so that dense content stays readable. Use `.floating` only for a card that /// sits above other content. The media area runs edge to edge under the card's rounded top; the footer sits /// under a hairline. With an `action`, the whole card is one button: do not place other controls in it. public struct GlassCard: View { @GlassThemed private var theme private let style: GlassCardStyle private let padding: GlassSpacingStep private let action: (@MainActor () -> Void)? private let media: AnyView? private let header: Header private let content: Content private let footer: Footer /// Creates a card with a header and a footer. /// /// - Parameters: /// - style: `.opaque` for dense content, `.floating` for a card above other content. /// - padding: inner padding, a step of the theme's spacing scale. /// - action: when set, the whole card is a button that calls it. public init( style: GlassCardStyle = .opaque, padding: GlassSpacingStep = .md, action: (@MainActor () -> Void)? = nil, @ViewBuilder content: () -> Content, @ViewBuilder header: () -> Header, @ViewBuilder footer: () -> Footer ) { self.style = style self.padding = padding self.action = action self.media = nil self.header = header() self.content = content() self.footer = footer() } /// Creates a card with a media area (a photo, a map, a drawing) above its header. /// /// The media is drawn edge to edge and clipped to the card's shape; give it a height. It is decoration /// unless it carries its own accessibility label. public init( style: GlassCardStyle = .opaque, padding: GlassSpacingStep = .md, action: (@MainActor () -> Void)? = nil, @ViewBuilder media: () -> Media, @ViewBuilder content: () -> Content, @ViewBuilder header: () -> Header, @ViewBuilder footer: () -> Footer ) { self.style = style self.padding = padding self.action = action self.media = AnyView(media()) self.header = header() self.content = content() self.footer = footer() } public var body: some View { if let action { Button(action: action) { surface(isTappable: true) } .buttonStyle(GlassCardButtonStyle()) .contentShape(theme.shape(\.large)) } else { surface(isTappable: false) .accessibilityElement(children: .contain) } } private var hasHeader: Bool { Header.self != EmptyView.self } private var hasFooter: Bool { Footer.self != EmptyView.self } private var stack: some View { let inset = theme.spacing[padding] return VStack(alignment: .leading, spacing: 0) { if let media { media .frame(maxWidth: .infinity) .clipped() } VStack(alignment: .leading, spacing: theme.spacing.xs) { if hasHeader { header .font(theme.font(.headline)) .foregroundStyle(theme.color(.textPrimary)) .accessibilityAddTraits(.isHeader) } content .font(theme.font(.body)) .foregroundStyle(theme.color(.textPrimary)) } .frame(maxWidth: media == nil && !hasFooter ? nil : .infinity, alignment: .leading) .padding(inset) if hasFooter { Rectangle() .fill(theme.color(.separator).opacity(0.7)) .frame(height: 1) .accessibilityHidden(true) footer .font(theme.font(.callout)) .foregroundStyle(theme.color(.textSecondary)) .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, inset) .padding(.vertical, max(theme.spacing.xs, inset * 0.75)) } } .clipShape(theme.shape(\.large)) } @ViewBuilder private func surface(isTappable: Bool) -> some View { if let glass = style.surfaceStyle(isTappable: isTappable) { stack.glassSurface(glass, in: theme.shape(\.large)) } else { stack.glassOpaqueSurface(in: theme.shape(\.large)) } } } /// Press feedback of a tappable card: a slight dimming, no movement, so it also suits Reduce Motion. private struct GlassCardButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label.opacity(configuration.isPressed ? 0.72 : 1) } } extension GlassCard where Header == EmptyView, Footer == EmptyView { /// Creates a card with content only. public init( style: GlassCardStyle = .opaque, padding: GlassSpacingStep = .md, action: (@MainActor () -> Void)? = nil, @ViewBuilder content: () -> Content ) { self.init(style: style, padding: padding, action: action, content: content, header: { EmptyView() }, footer: { EmptyView() }) } } extension GlassCard where Header == Text, Footer == EmptyView { /// Creates a card with a localised title as its header. public init( _ titleKey: LocalizedStringKey, style: GlassCardStyle = .opaque, padding: GlassSpacingStep = .md, action: (@MainActor () -> Void)? = nil, @ViewBuilder content: () -> Content ) { self.init(style: style, padding: padding, action: action, content: content, header: { Text(titleKey) }, footer: { EmptyView() }) } /// Creates a card whose header is a title that is already a string (data, user content). public init( verbatim title: S, style: GlassCardStyle = .opaque, padding: GlassSpacingStep = .md, action: (@MainActor () -> Void)? = nil, @ViewBuilder content: () -> Content ) { self.init(style: style, padding: padding, action: action, content: content, header: { Text(verbatim: String(title)) }, footer: { EmptyView() }) } } extension GlassCard where Header == EmptyView, Footer == EmptyView { /// Creates a card with a media area and content only. public init( style: GlassCardStyle = .opaque, padding: GlassSpacingStep = .md, action: (@MainActor () -> Void)? = nil, @ViewBuilder media: () -> Media, @ViewBuilder content: () -> Content ) { self.init(style: style, padding: padding, action: action, media: media, content: content, header: { EmptyView() }, footer: { EmptyView() }) } }