SPB Git

Repository Public MIT

GlassSurface.swift 6.2 KB 165 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 SwiftUI4243extension View {44    /// Draws this view on a Liquid Glass surface of the given shape.45    ///46    /// Apply it after the padding and frame modifiers that define the surface. With Reduce Transparency47    /// (real or simulated) the surface becomes opaque: the theme's surface colour with a separator outline.48    public func glassSurface(_ style: GlassSurfaceStyle = .regular, in shape: some Shape = Capsule()) -> some View {49        modifier(GlassSurfaceModifier(style: style, shape: shape))50    }5152    /// Draws this view on an opaque themed surface. Use it for dense content, which glass would make hard to read.53    public func glassOpaqueSurface(in shape: some Shape, elevated: Bool = false) -> some View {54        modifier(GlassOpaqueSurfaceModifier(shape: shape, elevated: elevated))55    }56}5758struct GlassSurfaceModifier<S: Shape>: ViewModifier {59    @GlassThemed private var theme60    @GlassAccessibility private var accessibility61    let style: GlassSurfaceStyle62    let shape: S6364    func body(content: Content) -> some View {65        switch GlassRendering.resolve(reduceTransparency: accessibility.reduceTransparency) {66        case .liquidGlass:67            content.glassEffect(style.glass(in: theme), in: shape)68        case .opaque:69            content70                .background(opaqueFill, in: shape)71                .overlay(shape.stroke(theme.color(.separator), lineWidth: 1))72        }73    }7475    private var opaqueFill: AnyShapeStyle {76        if let tint = style.resolvedTint(in: theme) {77            AnyShapeStyle(tint.mix(with: theme.color(.surface), by: 0.8))78        } else {79            AnyShapeStyle(theme.color(.surface))80        }81    }82}8384/// The finish shared by every opaque content surface of the kit: the theme surface, a hairline and a85/// restrained shadow. A resting surface sits on the page (hairline, a contact shadow in light appearance);86/// an elevated one floats above it (softer hairline, an ambient shadow and a contact shadow).87struct GlassOpaqueSurfaceModifier<S: Shape>: ViewModifier {88    @GlassThemed private var theme89    let shape: S90    let elevated: Bool9192    func body(content: Content) -> some View {93        content94            .background(theme.color(.surface), in: shape)95            // In dark appearance depth is read from a lighter top edge rather than from a shadow.96            .overlay {97                if theme.isDark {98                    shape99                        .stroke(LinearGradient(colors: [.white.opacity(0.10), .white.opacity(0)], startPoint: .top, endPoint: .center), lineWidth: 1)100                        .allowsHitTesting(false)101                }102            }103            .overlay(shape.stroke(theme.color(.separator).opacity(hairlineOpacity), lineWidth: hairlineWidth).allowsHitTesting(false))104            .shadow(color: .black.opacity(contactOpacity), radius: 1.5, y: 1)105            .shadow(color: .black.opacity(ambientOpacity), radius: elevated ? 18 : 0, y: elevated ? 8 : 0)106    }107108    private var hairlineWidth: CGFloat {109        theme.increasedContrast ? 1.5 : (elevated ? 0.5 : 1)110    }111112    private var hairlineOpacity: Double {113        if theme.increasedContrast { return 1 }114        return elevated ? 0.7 : (theme.isDark ? 0.9 : 0.75)115    }116117    private var contactOpacity: Double {118        if theme.isDark { return elevated ? 0.35 : 0 }119        return elevated ? 0.07 : 0.045120    }121122    private var ambientOpacity: Double {123        guard elevated else { return 0 }124        return theme.isDark ? 0.45 : 0.11125    }126}127128/// A padded Liquid Glass surface around arbitrary content.129///130/// ```swift131/// GlassSurface { Label("Offline", systemImage: "wifi.slash") }132/// ```133public struct GlassSurface<Content: View, S: Shape>: View {134    @GlassThemed private var theme135    private let style: GlassSurfaceStyle136    private let shape: S137    private let padding: GlassSpacingStep138    private let content: Content139140    public init(141        _ style: GlassSurfaceStyle = .regular,142        in shape: S,143        padding: GlassSpacingStep = .md,144        @ViewBuilder content: () -> Content145    ) {146        self.style = style147        self.shape = shape148        self.padding = padding149        self.content = content()150    }151152    public var body: some View {153        content154            .padding(theme.spacing[padding])155            .glassSurface(style, in: shape)156    }157}158159extension GlassSurface where S == RoundedRectangle {160    /// A surface with a continuous rounded rectangle of 22 points.161    public init(_ style: GlassSurfaceStyle = .regular, padding: GlassSpacingStep = .md, @ViewBuilder content: () -> Content) {162        self.init(style, in: RoundedRectangle(cornerRadius: 22, style: .continuous), padding: padding, content: content)163    }164}165
Files main
Menu

Theme