Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Feedback/GlassBanner.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 SwiftUI4243/// Severity of a ``GlassBanner``. Each has its own symbol and its own word, so colour is never the only cue.44public enum GlassBannerSeverity: String, CaseIterable, Sendable {45 case information, success, warning, error4647 /// SF Symbol of the severity. The four shapes differ, not only their colour.48 public var systemImage: String {49 switch self {50 case .information: "info.circle.fill"51 case .success: "checkmark.circle.fill"52 case .warning: "exclamationmark.triangle.fill"53 case .error: "xmark.octagon.fill"54 }55 }5657 /// Theme colour role that tints the banner.58 public var colorToken: GlassColorToken {59 switch self {60 case .information: .accent61 case .success: .success62 case .warning: .warning63 case .error: .destructive64 }65 }6667 /// Localisation key of the severity word.68 public var labelKey: GlassL10n.Key {69 switch self {70 case .information: .info71 case .success: .success72 case .warning: .warning73 case .error: .error74 }75 }7677 /// The localised severity word ("Warning").78 public var label: String { GlassL10n.string(labelKey) }79}8081/// The optional button of a ``GlassBanner``.82public struct GlassBannerAction {83 let title: Text84 let handler: @MainActor () -> Void8586 /// Creates an action with a localised title.87 public init(_ titleKey: LocalizedStringKey, handler: @escaping @MainActor () -> Void) {88 self.title = Text(titleKey)89 self.handler = handler90 }9192 /// Creates an action whose title is already a string.93 public init<S: StringProtocol>(verbatim title: S, handler: @escaping @MainActor () -> Void) {94 self.title = Text(verbatim: String(title))95 self.handler = handler96 }97}9899/// An inline message that stays in the layout until it is resolved or dismissed.100///101/// ```swift102/// GlassBanner(.warning, title: "Payment method expires soon",103/// message: "Update your card before 31 March.",104/// action: GlassBannerAction("Update") { openBilling() },105/// onDismiss: { hideBanner() })106/// ```107///108/// The banner is content: an opaque surface lightly tinted with the severity colour. The severity is shown by109/// a symbol, by its name in text and by the tint.110public struct GlassBanner: View {111 @GlassThemed private var theme112 private let severity: GlassBannerSeverity113 private let title: Text114 private let message: Text?115 private let action: GlassBannerAction?116 private let onDismiss: (@MainActor () -> Void)?117118 /// Creates a banner with a localised title.119 ///120 /// - Parameters:121 /// - message: optional detail, a data string shown as is.122 /// - onDismiss: when set, a dismiss button is shown.123 public init(124 _ severity: GlassBannerSeverity,125 title: LocalizedStringKey,126 message: String? = nil,127 action: GlassBannerAction? = nil,128 onDismiss: (@MainActor () -> Void)? = nil129 ) {130 self.init(severity, title: Text(title), message: message.map { Text(verbatim: $0) }, action: action, onDismiss: onDismiss)131 }132133 /// Creates a banner whose title is already a string (an error description, server text).134 public init<S: StringProtocol>(135 _ severity: GlassBannerSeverity,136 verbatim title: S,137 message: String? = nil,138 action: GlassBannerAction? = nil,139 onDismiss: (@MainActor () -> Void)? = nil140 ) {141 self.init(severity, title: Text(verbatim: String(title)), message: message.map { Text(verbatim: $0) }, action: action, onDismiss: onDismiss)142 }143144 /// Creates a banner from `Text` values.145 public init(146 _ severity: GlassBannerSeverity,147 title: Text,148 message: Text? = nil,149 action: GlassBannerAction? = nil,150 onDismiss: (@MainActor () -> Void)? = nil151 ) {152 self.severity = severity153 self.title = title154 self.message = message155 self.action = action156 self.onDismiss = onDismiss157 }158159 public var body: some View {160 let palette = GlassTintedPalette(severity.colorToken, theme: theme)161 let shape = theme.shape(\.medium)162 HStack(alignment: .top, spacing: theme.spacing.sm) {163 Image(systemName: severity.systemImage)164 .font(theme.font(.headline))165 .foregroundStyle(palette.accent)166 .accessibilityHidden(true)167 VStack(alignment: .leading, spacing: theme.spacing.xxs) {168 VStack(alignment: .leading, spacing: theme.spacing.xxs) {169 Text(verbatim: severity.label)170 .font(theme.font(.caption).weight(.semibold))171 .textCase(.uppercase)172 title.font(theme.font(.headline))173 if let message {174 message.font(theme.font(.callout))175 }176 }177 .accessibilityElement(children: .combine)178 if let action {179 Button(action: action.handler) {180 action.title.font(theme.font(.headline)).underline()181 }182 .buttonStyle(.plain)183 .glassMinimumHitTarget()184 }185 }186 .foregroundStyle(palette.foreground)187 .frame(maxWidth: .infinity, alignment: .leading)188 if let onDismiss {189 Button(action: onDismiss) {190 Label(GlassL10n.string(.dismiss), systemImage: "xmark")191 .labelStyle(.iconOnly)192 .foregroundStyle(palette.foreground)193 }194 .buttonStyle(.plain)195 .glassMinimumHitTarget()196 }197 }198 .padding(theme.spacing.md)199 .background(palette.background, in: shape)200 .overlay(shape.stroke(palette.accent.opacity(0.5), lineWidth: 1))201 .accessibilityElement(children: .contain)202 }203}204