// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Feedback/GlassToast.swift // Module : SPGlassComponents // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-02 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// Edge of the host where toasts appear. public enum GlassToastPlacement: String, CaseIterable, Sendable { case top, bottom /// Alignment of the toast stack inside the host. public var alignment: Alignment { self == .top ? .top : .bottom } /// Edge the toasts slide in from when motion is allowed. public var edge: Edge { self == .top ? .top : .bottom } } extension View { /// Shows the toasts of a ``GlassToastCenter`` above this view, inside its safe area. /// /// Apply it once, near the root of a scene. The centre is also placed in the environment, so descendants /// can post with `@Environment(GlassToastCenter.self)`. public func glassToastHost(_ center: GlassToastCenter, placement: GlassToastPlacement = .bottom) -> some View { modifier(GlassToastHostModifier(center: center, placement: placement)) } } private struct GlassToastHostModifier: ViewModifier { @GlassThemed private var theme @GlassAccessibility private var accessibility let center: GlassToastCenter let placement: GlassToastPlacement func body(content: Content) -> some View { content .environment(center) .overlay(alignment: placement.alignment) { // One group: neighbouring toasts are glass surfaces that must be rendered together. GlassGroup(spacing: theme.spacing.xs) { VStack(spacing: theme.spacing.xs) { ForEach(center.toasts) { toast in GlassToastView(toast: toast, onAction: { center.performAction(of: toast.id) }, onDismiss: { center.dismiss(toast.id) }) .transition(accessibility.reduceMotion ? .opacity : .move(edge: placement.edge).combined(with: .opacity)) } } } .padding(theme.spacing.md) .animation(theme.animation(.standard, reduceMotion: accessibility.reduceMotion), value: center.toasts.map(\.id)) } } } /// One toast drawn on a glass surface. ``SwiftUICore/View/glassToastHost(_:placement:)`` creates these views; /// use it directly only to show a toast you manage yourself. public struct GlassToastView: View { @GlassThemed private var theme private let toast: GlassToast private let onAction: @MainActor () -> Void private let onDismiss: (@MainActor () -> Void)? /// - Parameters: /// - onAction: called when the toast's action button is activated. /// - onDismiss: when set, a dismiss button is shown. public init(toast: GlassToast, onAction: @escaping @MainActor () -> Void = {}, onDismiss: (@MainActor () -> Void)? = nil) { self.toast = toast self.onAction = onAction self.onDismiss = onDismiss } public var body: some View { HStack(alignment: .center, spacing: theme.spacing.sm) { if let systemImage = toast.systemImage { Image(systemName: systemImage) .font(.system(.subheadline, weight: .semibold)) .foregroundStyle(theme.color(.textPrimary)) .frame(width: 30, height: 30) .background(theme.color(.textPrimary).opacity(0.1), in: Circle()) .accessibilityHidden(true) } VStack(alignment: .leading, spacing: 1) { if let title = toast.title, !title.isEmpty { Text(verbatim: title) .font(theme.font(.callout).weight(.semibold)) Text(verbatim: toast.message) .font(theme.font(.callout)) .opacity(0.8) } else { Text(verbatim: toast.message) .font(theme.font(.callout).weight(.medium)) } } .foregroundStyle(theme.color(.textPrimary)) .frame(maxWidth: .infinity, alignment: .leading) .accessibilityElement(children: .combine) // Plain system buttons: the toast is the glass surface, and glass never sits inside glass. if let action = toast.action { Button(action: onAction) { Text(verbatim: action.title) .font(theme.font(.callout).weight(.semibold)) .foregroundStyle(theme.color(.textPrimary)) .padding(.horizontal, theme.spacing.sm) .padding(.vertical, theme.spacing.xxs + 2) .background(theme.color(.textPrimary).opacity(0.1), in: Capsule()) } .buttonStyle(.plain) .glassMinimumHitTarget() } if let onDismiss { Button(action: onDismiss) { Label(GlassL10n.string(.dismiss), systemImage: "xmark") .labelStyle(.iconOnly) .font(.system(.footnote, weight: .bold)) .foregroundStyle(theme.color(.textSecondary)) } .buttonStyle(.plain) .glassMinimumHitTarget() } } .padding(.leading, theme.spacing.sm) .padding(.trailing, toast.action == nil && onDismiss == nil ? theme.spacing.md : theme.spacing.xxs) .padding(.vertical, theme.spacing.xxs) .frame(minHeight: 52) .frame(maxWidth: 480) // Neutral glass: a theme's glass tint would make the message hard to read. .glassSurface(.regular.tinted(.none), in: theme.shape(\.large)) .accessibilityElement(children: .contain) .accessibilityLabel(GlassL10n.string(.notification)) } }