SPB Git

Repository Public MIT

GlassGroup.swift 4.3 KB 115 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 SwiftUI4243/// Groups glass surfaces so that the system renders them together and lets neighbours blend and morph.44///45/// Glass cannot sample other glass. Surfaces that sit close to each other, or that morph into one another,46/// belong in one group. With Reduce Transparency the group is a plain container.47public struct GlassGroup<Content: View>: View {48    @GlassAccessibility private var accessibility49    private let spacing: CGFloat?50    private let content: Content5152    /// - Parameter spacing: distance below which neighbouring surfaces start to blend. `nil` keeps the system default.53    public init(spacing: CGFloat? = nil, @ViewBuilder content: () -> Content) {54        self.spacing = spacing55        self.content = content()56    }5758    public var body: some View {59        if accessibility.reduceTransparency {60            content61        } else {62            GlassEffectContainer(spacing: spacing) { content }63        }64    }65}6667/// Transitions available to a glass surface that appears, disappears or changes identity.68public enum GlassSurfaceTransition: String, CaseIterable, Sendable {69    /// The surface morphs from and to surfaces sharing its namespace.70    case morph71    /// The surface materialises in place, without travel.72    case materialize73    /// No glass transition.74    case none7576    /// The transition actually used: Reduce Motion replaces morphing with materialising in place.77    public func effective(reduceMotion: Bool) -> GlassSurfaceTransition {78        reduceMotion && self == .morph ? .materialize : self79    }8081    var system: GlassEffectTransition {82        switch self {83        case .morph: .matchedGeometry84        case .materialize: .materialize85        case .none: .identity86        }87    }88}8990extension View {91    /// Gives a glass surface a stable identity inside a ``GlassGroup`` so that it can morph between states.92    ///93    /// Keep the namespace stable for the lifetime of the group (`@Namespace` on the owning view).94    public func glassMorph(id: some Hashable & Sendable, in namespace: Namespace.ID, transition: GlassSurfaceTransition = .morph) -> some View {95        modifier(GlassMorphModifier(id: id, namespace: namespace, transition: transition))96    }97}9899private struct GlassMorphModifier<ID: Hashable & Sendable>: ViewModifier {100    @GlassAccessibility private var accessibility101    let id: ID102    let namespace: Namespace.ID103    let transition: GlassSurfaceTransition104105    func body(content: Content) -> some View {106        if accessibility.reduceTransparency {107            content108        } else {109            content110                .glassEffectID(id, in: namespace)111                .glassEffectTransition(transition.effective(reduceMotion: accessibility.reduceMotion).system)112        }113    }114}115
Files main
Menu

Theme