SPB Git

Repository Public MIT

GlassGroup.swift 4.3 KB 117 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassEffects/GlassGroup.swift8// Module           : SPGlassEffects9// 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 SwiftUI42import SPGlassCore43import SPGlassThemes4445/// Groups glass surfaces so that the system renders them together and lets neighbours blend and morph.46///47/// Glass cannot sample other glass. Surfaces that sit close to each other, or that morph into one another,48/// belong in one group. With Reduce Transparency the group is a plain container.49public struct GlassGroup<Content: View>: View {50    @GlassAccessibility private var accessibility51    private let spacing: CGFloat?52    private let content: Content5354    /// - Parameter spacing: distance below which neighbouring surfaces start to blend. `nil` keeps the system default.55    public init(spacing: CGFloat? = nil, @ViewBuilder content: () -> Content) {56        self.spacing = spacing57        self.content = content()58    }5960    public var body: some View {61        if accessibility.reduceTransparency {62            content63        } else {64            GlassEffectContainer(spacing: spacing) { content }65        }66    }67}6869/// Transitions available to a glass surface that appears, disappears or changes identity.70public enum GlassSurfaceTransition: String, CaseIterable, Sendable {71    /// The surface morphs from and to surfaces sharing its namespace.72    case morph73    /// The surface materialises in place, without travel.74    case materialize75    /// No glass transition.76    case none7778    /// The transition actually used: Reduce Motion replaces morphing with materialising in place.79    public func effective(reduceMotion: Bool) -> GlassSurfaceTransition {80        reduceMotion && self == .morph ? .materialize : self81    }8283    var system: GlassEffectTransition {84        switch self {85        case .morph: .matchedGeometry86        case .materialize: .materialize87        case .none: .identity88        }89    }90}9192extension View {93    /// Gives a glass surface a stable identity inside a ``GlassGroup`` so that it can morph between states.94    ///95    /// Keep the namespace stable for the lifetime of the group (`@Namespace` on the owning view).96    public func glassMorph(id: some Hashable & Sendable, in namespace: Namespace.ID, transition: GlassSurfaceTransition = .morph) -> some View {97        modifier(GlassMorphModifier(id: id, namespace: namespace, transition: transition))98    }99}100101private struct GlassMorphModifier<ID: Hashable & Sendable>: ViewModifier {102    @GlassAccessibility private var accessibility103    let id: ID104    let namespace: Namespace.ID105    let transition: GlassSurfaceTransition106107    func body(content: Content) -> some View {108        if accessibility.reduceTransparency {109            content110        } else {111            content112                .glassEffectID(id, in: namespace)113                .glassEffectTransition(transition.effective(reduceMotion: accessibility.reduceMotion).system)114        }115    }116}117
Files main
Menu

Theme