SPB Git

Repository Public MIT

GlassSurface.swift 4.9 KB 134 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}8384struct GlassOpaqueSurfaceModifier<S: Shape>: ViewModifier {85    @GlassThemed private var theme86    let shape: S87    let elevated: Bool8889    func body(content: Content) -> some View {90        content91            .background(theme.color(.surface), in: shape)92            .overlay(shape.stroke(theme.color(.separator).opacity(elevated ? 0.6 : 1), lineWidth: elevated ? 0.5 : 1))93            .shadow(color: elevated ? .black.opacity(theme.isDark ? 0.4 : 0.12) : .clear, radius: elevated ? 12 : 0, y: elevated ? 4 : 0)94    }95}9697/// A padded Liquid Glass surface around arbitrary content.98///99/// ```swift100/// GlassSurface { Label("Offline", systemImage: "wifi.slash") }101/// ```102public struct GlassSurface<Content: View, S: Shape>: View {103    @GlassThemed private var theme104    private let style: GlassSurfaceStyle105    private let shape: S106    private let padding: GlassSpacingStep107    private let content: Content108109    public init(110        _ style: GlassSurfaceStyle = .regular,111        in shape: S,112        padding: GlassSpacingStep = .md,113        @ViewBuilder content: () -> Content114    ) {115        self.style = style116        self.shape = shape117        self.padding = padding118        self.content = content()119    }120121    public var body: some View {122        content123            .padding(theme.spacing[padding])124            .glassSurface(style, in: shape)125    }126}127128extension GlassSurface where S == RoundedRectangle {129    /// A surface with a continuous rounded rectangle of 22 points.130    public init(_ style: GlassSurfaceStyle = .regular, padding: GlassSpacingStep = .md, @ViewBuilder content: () -> Content) {131        self.init(style, in: RoundedRectangle(cornerRadius: 22, style: .continuous), padding: padding, content: content)132    }133}134
Files main
Menu

Theme