// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Feedback/GlassBanner.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 /// Severity of a ``GlassBanner``. Each has its own symbol and its own word, so colour is never the only cue. public enum GlassBannerSeverity: String, CaseIterable, Sendable { case information, success, warning, error /// SF Symbol of the severity. The four shapes differ, not only their colour. public var systemImage: String { switch self { case .information: "info.circle.fill" case .success: "checkmark.circle.fill" case .warning: "exclamationmark.triangle.fill" case .error: "xmark.octagon.fill" } } /// Theme colour role that tints the banner. public var colorToken: GlassColorToken { switch self { case .information: .accent case .success: .success case .warning: .warning case .error: .destructive } } /// Localisation key of the severity word. public var labelKey: GlassL10n.Key { switch self { case .information: .info case .success: .success case .warning: .warning case .error: .error } } /// The localised severity word ("Warning"). public var label: String { GlassL10n.string(labelKey) } } /// The optional button of a ``GlassBanner``. public struct GlassBannerAction { let title: Text let handler: @MainActor () -> Void /// Creates an action with a localised title. public init(_ titleKey: LocalizedStringKey, handler: @escaping @MainActor () -> Void) { self.title = Text(titleKey) self.handler = handler } /// Creates an action whose title is already a string. public init(verbatim title: S, handler: @escaping @MainActor () -> Void) { self.title = Text(verbatim: String(title)) self.handler = handler } } /// An inline message that stays in the layout until it is resolved or dismissed. /// /// ```swift /// GlassBanner(.warning, title: "Payment method expires soon", /// message: "Update your card before 31 March.", /// action: GlassBannerAction("Update") { openBilling() }, /// onDismiss: { hideBanner() }) /// ``` /// /// The banner is content: an opaque surface barely tinted with the severity colour, a hairline and an accent /// edge. The severity is shown by a symbol in a tinted tile, by its name in text and by the tint. public struct GlassBanner: View { @GlassThemed private var theme private let severity: GlassBannerSeverity private let title: Text private let message: Text? private let action: GlassBannerAction? private let onDismiss: (@MainActor () -> Void)? /// Creates a banner with a localised title. /// /// - Parameters: /// - message: optional detail, a data string shown as is. /// - onDismiss: when set, a dismiss button is shown. public init( _ severity: GlassBannerSeverity, title: LocalizedStringKey, message: String? = nil, action: GlassBannerAction? = nil, onDismiss: (@MainActor () -> Void)? = nil ) { self.init(severity, title: Text(title), message: message.map { Text(verbatim: $0) }, action: action, onDismiss: onDismiss) } /// Creates a banner whose title is already a string (an error description, server text). public init( _ severity: GlassBannerSeverity, verbatim title: S, message: String? = nil, action: GlassBannerAction? = nil, onDismiss: (@MainActor () -> Void)? = nil ) { self.init(severity, title: Text(verbatim: String(title)), message: message.map { Text(verbatim: $0) }, action: action, onDismiss: onDismiss) } /// Creates a banner from `Text` values. public init( _ severity: GlassBannerSeverity, title: Text, message: Text? = nil, action: GlassBannerAction? = nil, onDismiss: (@MainActor () -> Void)? = nil ) { self.severity = severity self.title = title self.message = message self.action = action self.onDismiss = onDismiss } public var body: some View { let palette = GlassTintedPalette(severity.colorToken, theme: theme, fraction: 0.07) let shape = theme.shape(\.medium) HStack(alignment: .top, spacing: theme.spacing.sm) { Image(systemName: severity.systemImage) .font(.system(.subheadline, weight: .semibold)) .foregroundStyle(palette.accent) .frame(width: 32, height: 32) .background(palette.accent.opacity(0.16), in: RoundedRectangle(cornerRadius: 9, style: .continuous)) .accessibilityHidden(true) VStack(alignment: .leading, spacing: theme.spacing.xs) { VStack(alignment: .leading, spacing: 2) { Text(verbatim: severity.label) .font(.system(.caption2, weight: .semibold)) .textCase(.uppercase) .tracking(0.7) .foregroundStyle(theme.color(.textSecondary)) title .font(theme.font(.headline)) .foregroundStyle(palette.foreground) if let message { message .font(theme.font(.callout)) .foregroundStyle(theme.color(.textSecondary)) .padding(.top, 1) } } .accessibilityElement(children: .combine) if let action { Button(action: action.handler) { action.title .font(theme.font(.callout).weight(.semibold)) .foregroundStyle(palette.foreground) .padding(.horizontal, theme.spacing.sm) .padding(.vertical, theme.spacing.xxs + 2) .background(palette.accent.opacity(0.14), in: Capsule()) .overlay(Capsule().stroke(palette.accent.opacity(0.45), lineWidth: 1)) } .buttonStyle(.plain) .glassMinimumHitTarget() } } .frame(maxWidth: .infinity, alignment: .leading) if let onDismiss { Button(action: onDismiss) { Label(GlassL10n.string(.dismiss), systemImage: "xmark") .labelStyle(.iconOnly) .font(.system(.footnote, weight: .bold)) .foregroundStyle(theme.color(.textSecondary)) .frame(width: 32, height: 32) } .buttonStyle(.plain) .glassMinimumHitTarget() .padding(.trailing, -theme.spacing.xs) .padding(.top, -6) } } .padding(.vertical, theme.spacing.sm) .padding(.leading, theme.spacing.sm + 4) .padding(.trailing, theme.spacing.md) .background(palette.background) .overlay(alignment: .leading) { Rectangle().fill(palette.accent).frame(width: 4).accessibilityHidden(true) } .clipShape(shape) .overlay(shape.stroke(palette.accent.opacity(theme.increasedContrast ? 1 : 0.32), lineWidth: 1)) .accessibilityElement(children: .contain) } }