Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Feedback/GlassToast.swift8// Module : SPGlassComponents9// 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 SPGlassThemes45import SPGlassEffects4647/// Edge of the host where toasts appear.48public enum GlassToastPlacement: String, CaseIterable, Sendable {49 case top, bottom5051 /// Alignment of the toast stack inside the host.52 public var alignment: Alignment {53 self == .top ? .top : .bottom54 }5556 /// Edge the toasts slide in from when motion is allowed.57 public var edge: Edge {58 self == .top ? .top : .bottom59 }60}6162extension View {63 /// Shows the toasts of a ``GlassToastCenter`` above this view, inside its safe area.64 ///65 /// Apply it once, near the root of a scene. The centre is also placed in the environment, so descendants66 /// can post with `@Environment(GlassToastCenter.self)`.67 public func glassToastHost(_ center: GlassToastCenter, placement: GlassToastPlacement = .bottom) -> some View {68 modifier(GlassToastHostModifier(center: center, placement: placement))69 }70}7172private struct GlassToastHostModifier: ViewModifier {73 @GlassThemed private var theme74 @GlassAccessibility private var accessibility75 let center: GlassToastCenter76 let placement: GlassToastPlacement7778 func body(content: Content) -> some View {79 content80 .environment(center)81 .overlay(alignment: placement.alignment) {82 // One group: neighbouring toasts are glass surfaces that must be rendered together.83 GlassGroup(spacing: theme.spacing.xs) {84 VStack(spacing: theme.spacing.xs) {85 ForEach(center.toasts) { toast in86 GlassToastView(toast: toast, onAction: { center.performAction(of: toast.id) }, onDismiss: { center.dismiss(toast.id) })87 .transition(accessibility.reduceMotion ? .opacity : .move(edge: placement.edge).combined(with: .opacity))88 }89 }90 }91 .padding(theme.spacing.md)92 .animation(theme.animation(.standard, reduceMotion: accessibility.reduceMotion), value: center.toasts.map(\.id))93 }94 }95}9697/// One toast drawn on a glass surface. ``SwiftUICore/View/glassToastHost(_:placement:)`` creates these views;98/// use it directly only to show a toast you manage yourself.99public struct GlassToastView: View {100 @GlassThemed private var theme101 private let toast: GlassToast102 private let onAction: @MainActor () -> Void103 private let onDismiss: (@MainActor () -> Void)?104105 /// - Parameters:106 /// - onAction: called when the toast's action button is activated.107 /// - onDismiss: when set, a dismiss button is shown.108 public init(toast: GlassToast, onAction: @escaping @MainActor () -> Void = {}, onDismiss: (@MainActor () -> Void)? = nil) {109 self.toast = toast110 self.onAction = onAction111 self.onDismiss = onDismiss112 }113114 public var body: some View {115 HStack(alignment: .center, spacing: theme.spacing.sm) {116 if let systemImage = toast.systemImage {117 Image(systemName: systemImage)118 .font(theme.font(.headline))119 .foregroundStyle(theme.color(.textPrimary))120 .accessibilityHidden(true)121 }122 VStack(alignment: .leading, spacing: theme.spacing.xxs) {123 if let title = toast.title, !title.isEmpty {124 Text(verbatim: title)125 .font(theme.font(.headline))126 }127 Text(verbatim: toast.message)128 .font(theme.font(.callout))129 }130 .foregroundStyle(theme.color(.textPrimary))131 .frame(maxWidth: .infinity, alignment: .leading)132 .accessibilityElement(children: .combine)133 // Plain system buttons: the toast is the glass surface, and glass never sits inside glass.134 if let action = toast.action {135 Button(action: onAction) {136 Text(verbatim: action.title).font(theme.font(.headline))137 }138 .buttonStyle(.borderless)139 .tint(theme.color(.accent))140 .glassMinimumHitTarget()141 }142 if let onDismiss {143 Button(action: onDismiss) {144 Label(GlassL10n.string(.dismiss), systemImage: "xmark")145 .labelStyle(.iconOnly)146 }147 .buttonStyle(.borderless)148 .glassMinimumHitTarget()149 }150 }151 .padding(.horizontal, theme.spacing.md)152 .padding(.vertical, theme.spacing.sm)153 .frame(maxWidth: 480)154 .glassSurface(.regular, in: theme.shape(\.large))155 .accessibilityElement(children: .contain)156 .accessibilityLabel(GlassL10n.string(.notification))157 }158}159