SPB Git

Repository Public MIT

GlassCard.swift 6.6 KB 178 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassComponents/Containers/GlassCard.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/// Surface of a ``GlassCard``.44public enum GlassCardStyle: String, CaseIterable, Sendable {45    /// An opaque themed surface. The default, and the right choice for dense content (text, tables, forms).46    case opaque47    /// A Liquid Glass surface, for a card that floats above other content (a map, a photo, a scrolling feed).48    case floating4950    /// The glass style used for this card, or `nil` when the card is opaque.51    ///52    /// Glass reacts to touch only when the card itself is tappable.53    public func surfaceStyle(isTappable: Bool) -> GlassSurfaceStyle? {54        switch self {55        case .opaque: nil56        case .floating: isTappable ? .interactive : .regular57        }58    }59}6061/// A container for one piece of content, with an optional header and footer.62///63/// ```swift64/// GlassCard("Monthly rent") {65///     Text("1,450 $").font(.title)66/// }67///68/// GlassCard(style: .floating, action: { openListing() }) {69///     Label("3 listings nearby", systemImage: "mappin.and.ellipse")70/// }71/// ```72///73/// The card is opaque by default so that dense content stays readable. Use `.floating` only for a card that74/// sits above other content. With an `action`, the whole card is one button: do not place other controls in it.75public struct GlassCard<Header: View, Content: View, Footer: View>: View {76    @GlassThemed private var theme77    private let style: GlassCardStyle78    private let padding: GlassSpacingStep79    private let action: (@MainActor () -> Void)?80    private let header: Header81    private let content: Content82    private let footer: Footer8384    /// Creates a card with a header and a footer.85    ///86    /// - Parameters:87    ///   - style: `.opaque` for dense content, `.floating` for a card above other content.88    ///   - padding: inner padding, a step of the theme's spacing scale.89    ///   - action: when set, the whole card is a button that calls it.90    public init(91        style: GlassCardStyle = .opaque,92        padding: GlassSpacingStep = .md,93        action: (@MainActor () -> Void)? = nil,94        @ViewBuilder content: () -> Content,95        @ViewBuilder header: () -> Header,96        @ViewBuilder footer: () -> Footer97    ) {98        self.style = style99        self.padding = padding100        self.action = action101        self.header = header()102        self.content = content()103        self.footer = footer()104    }105106    public var body: some View {107        if let action {108            Button(action: action) { surface(isTappable: true) }109                .buttonStyle(.plain)110                .contentShape(theme.shape(\.large))111        } else {112            surface(isTappable: false)113                .accessibilityElement(children: .contain)114        }115    }116117    private var stack: some View {118        VStack(alignment: .leading, spacing: theme.spacing.sm) {119            header120                .font(theme.font(.headline))121                .foregroundStyle(theme.color(.textPrimary))122                .accessibilityAddTraits(.isHeader)123            content124                .font(theme.font(.body))125                .foregroundStyle(theme.color(.textPrimary))126            footer127                .font(theme.font(.caption))128                .foregroundStyle(theme.color(.textSecondary))129        }130        .padding(theme.spacing[padding])131    }132133    @ViewBuilder134    private func surface(isTappable: Bool) -> some View {135        if let glass = style.surfaceStyle(isTappable: isTappable) {136            stack.glassSurface(glass, in: theme.shape(\.large))137        } else {138            stack.glassOpaqueSurface(in: theme.shape(\.large))139        }140    }141}142143extension GlassCard where Header == EmptyView, Footer == EmptyView {144    /// Creates a card with content only.145    public init(146        style: GlassCardStyle = .opaque,147        padding: GlassSpacingStep = .md,148        action: (@MainActor () -> Void)? = nil,149        @ViewBuilder content: () -> Content150    ) {151        self.init(style: style, padding: padding, action: action, content: content, header: { EmptyView() }, footer: { EmptyView() })152    }153}154155extension GlassCard where Header == Text, Footer == EmptyView {156    /// Creates a card with a localised title as its header.157    public init(158        _ titleKey: LocalizedStringKey,159        style: GlassCardStyle = .opaque,160        padding: GlassSpacingStep = .md,161        action: (@MainActor () -> Void)? = nil,162        @ViewBuilder content: () -> Content163    ) {164        self.init(style: style, padding: padding, action: action, content: content, header: { Text(titleKey) }, footer: { EmptyView() })165    }166167    /// Creates a card whose header is a title that is already a string (data, user content).168    public init<S: StringProtocol>(169        verbatim title: S,170        style: GlassCardStyle = .opaque,171        padding: GlassSpacingStep = .md,172        action: (@MainActor () -> Void)? = nil,173        @ViewBuilder content: () -> Content174    ) {175        self.init(style: style, padding: padding, action: action, content: content, header: { Text(verbatim: String(title)) }, footer: { EmptyView() })176    }177}178
Files main
Menu

Theme