SPB Git

Repository Public MIT

GlassSurface.swift 6.3 KB 168 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassEffects/GlassSurface.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 SPGlassTokens44import SPGlassThemes4546extension View {47    /// Draws this view on a Liquid Glass surface of the given shape.48    ///49    /// Apply it after the padding and frame modifiers that define the surface. With Reduce Transparency50    /// (real or simulated) the surface becomes opaque: the theme's surface colour with a separator outline.51    public func glassSurface(_ style: GlassSurfaceStyle = .regular, in shape: some Shape = Capsule()) -> some View {52        modifier(GlassSurfaceModifier(style: style, shape: shape))53    }5455    /// Draws this view on an opaque themed surface. Use it for dense content, which glass would make hard to read.56    public func glassOpaqueSurface(in shape: some Shape, elevated: Bool = false) -> some View {57        modifier(GlassOpaqueSurfaceModifier(shape: shape, elevated: elevated))58    }59}6061struct GlassSurfaceModifier<S: Shape>: ViewModifier {62    @GlassThemed private var theme63    @GlassAccessibility private var accessibility64    let style: GlassSurfaceStyle65    let shape: S6667    func body(content: Content) -> some View {68        switch GlassRendering.resolve(reduceTransparency: accessibility.reduceTransparency) {69        case .liquidGlass:70            content.glassEffect(style.glass(in: theme), in: shape)71        case .opaque:72            content73                .background(opaqueFill, in: shape)74                .overlay(shape.stroke(theme.color(.separator), lineWidth: 1))75        }76    }7778    private var opaqueFill: AnyShapeStyle {79        if let tint = style.resolvedTint(in: theme) {80            AnyShapeStyle(tint.mix(with: theme.color(.surface), by: 0.8))81        } else {82            AnyShapeStyle(theme.color(.surface))83        }84    }85}8687/// The finish shared by every opaque content surface of the kit: the theme surface, a hairline and a88/// restrained shadow. A resting surface sits on the page (hairline, a contact shadow in light appearance);89/// an elevated one floats above it (softer hairline, an ambient shadow and a contact shadow).90struct GlassOpaqueSurfaceModifier<S: Shape>: ViewModifier {91    @GlassThemed private var theme92    let shape: S93    let elevated: Bool9495    func body(content: Content) -> some View {96        content97            .background(theme.color(.surface), in: shape)98            // In dark appearance depth is read from a lighter top edge rather than from a shadow.99            .overlay {100                if theme.isDark {101                    shape102                        .stroke(LinearGradient(colors: [.white.opacity(0.10), .white.opacity(0)], startPoint: .top, endPoint: .center), lineWidth: 1)103                        .allowsHitTesting(false)104                }105            }106            .overlay(shape.stroke(theme.color(.separator).opacity(hairlineOpacity), lineWidth: hairlineWidth).allowsHitTesting(false))107            .shadow(color: .black.opacity(contactOpacity), radius: 1.5, y: 1)108            .shadow(color: .black.opacity(ambientOpacity), radius: elevated ? 18 : 0, y: elevated ? 8 : 0)109    }110111    private var hairlineWidth: CGFloat {112        theme.increasedContrast ? 1.5 : (elevated ? 0.5 : 1)113    }114115    private var hairlineOpacity: Double {116        if theme.increasedContrast { return 1 }117        return elevated ? 0.7 : (theme.isDark ? 0.9 : 0.75)118    }119120    private var contactOpacity: Double {121        if theme.isDark { return elevated ? 0.35 : 0 }122        return elevated ? 0.07 : 0.045123    }124125    private var ambientOpacity: Double {126        guard elevated else { return 0 }127        return theme.isDark ? 0.45 : 0.11128    }129}130131/// A padded Liquid Glass surface around arbitrary content.132///133/// ```swift134/// GlassSurface { Label("Offline", systemImage: "wifi.slash") }135/// ```136public struct GlassSurface<Content: View, S: Shape>: View {137    @GlassThemed private var theme138    private let style: GlassSurfaceStyle139    private let shape: S140    private let padding: GlassSpacingStep141    private let content: Content142143    public init(144        _ style: GlassSurfaceStyle = .regular,145        in shape: S,146        padding: GlassSpacingStep = .md,147        @ViewBuilder content: () -> Content148    ) {149        self.style = style150        self.shape = shape151        self.padding = padding152        self.content = content()153    }154155    public var body: some View {156        content157            .padding(theme.spacing[padding])158            .glassSurface(style, in: shape)159    }160}161162extension GlassSurface where S == RoundedRectangle {163    /// A surface with a continuous rounded rectangle of 22 points.164    public init(_ style: GlassSurfaceStyle = .regular, padding: GlassSpacingStep = .md, @ViewBuilder content: () -> Content) {165        self.init(style, in: RoundedRectangle(cornerRadius: 22, style: .continuous), padding: padding, content: content)166    }167}168
Files main
Menu

Theme