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 SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// Severity of a ``GlassBanner``. Each has its own symbol and its own word, so colour is never the only cue.48public enum GlassBannerSeverity: String, CaseIterable, Sendable {49 case information, success, warning, error5051 /// SF Symbol of the severity. The four shapes differ, not only their colour.52 public var systemImage: String {53 switch self {54 case .information: "info.circle.fill"55 case .success: "checkmark.circle.fill"56 case .warning: "exclamationmark.triangle.fill"57 case .error: "xmark.octagon.fill"58 }59 }6061 /// Theme colour role that tints the banner.62 public var colorToken: GlassColorToken {63 switch self {64 case .information: .accent65 case .success: .success66 case .warning: .warning67 case .error: .destructive68 }69 }7071 /// Localisation key of the severity word.72 public var labelKey: GlassL10n.Key {73 switch self {74 case .information: .info75 case .success: .success76 case .warning: .warning77 case .error: .error78 }79 }8081 /// The localised severity word ("Warning").82 public var label: String { GlassL10n.string(labelKey) }83}8485/// The optional button of a ``GlassBanner``.86public struct GlassBannerAction {87 let title: Text88 let handler: @MainActor () -> Void8990 /// Creates an action with a localised title.91 public init(_ titleKey: LocalizedStringKey, handler: @escaping @MainActor () -> Void) {92 self.title = Text(titleKey)93 self.handler = handler94 }9596 /// Creates an action whose title is already a string.97 public init<S: StringProtocol>(verbatim title: S, handler: @escaping @MainActor () -> Void) {98 self.title = Text(verbatim: String(title))99 self.handler = handler100 }101}102103/// An inline message that stays in the layout until it is resolved or dismissed.104///105/// ```swift106/// GlassBanner(.warning, title: "Payment method expires soon",107/// message: "Update your card before 31 March.",108/// action: GlassBannerAction("Update") { openBilling() },109/// onDismiss: { hideBanner() })110/// ```111///112/// The banner is content: an opaque surface lightly tinted with the severity colour. The severity is shown by113/// a symbol, by its name in text and by the tint.114public struct GlassBanner: View {115 @GlassThemed private var theme116 private let severity: GlassBannerSeverity117 private let title: Text118 private let message: Text?119 private let action: GlassBannerAction?120 private let onDismiss: (@MainActor () -> Void)?121122 /// Creates a banner with a localised title.123 ///124 /// - Parameters:125 /// - message: optional detail, a data string shown as is.126 /// - onDismiss: when set, a dismiss button is shown.127 public init(128 _ severity: GlassBannerSeverity,129 title: LocalizedStringKey,130 message: String? = nil,131 action: GlassBannerAction? = nil,132 onDismiss: (@MainActor () -> Void)? = nil133 ) {134 self.init(severity, title: Text(title), message: message.map { Text(verbatim: $0) }, action: action, onDismiss: onDismiss)135 }136137 /// Creates a banner whose title is already a string (an error description, server text).138 public init<S: StringProtocol>(139 _ severity: GlassBannerSeverity,140 verbatim title: S,141 message: String? = nil,142 action: GlassBannerAction? = nil,143 onDismiss: (@MainActor () -> Void)? = nil144 ) {145 self.init(severity, title: Text(verbatim: String(title)), message: message.map { Text(verbatim: $0) }, action: action, onDismiss: onDismiss)146 }147148 /// Creates a banner from `Text` values.149 public init(150 _ severity: GlassBannerSeverity,151 title: Text,152 message: Text? = nil,153 action: GlassBannerAction? = nil,154 onDismiss: (@MainActor () -> Void)? = nil155 ) {156 self.severity = severity157 self.title = title158 self.message = message159 self.action = action160 self.onDismiss = onDismiss161 }162163 public var body: some View {164 let palette = GlassTintedPalette(severity.colorToken, theme: theme)165 let shape = theme.shape(\.medium)166 HStack(alignment: .top, spacing: theme.spacing.sm) {167 Image(systemName: severity.systemImage)168 .font(theme.font(.headline))169 .foregroundStyle(palette.accent)170 .accessibilityHidden(true)171 VStack(alignment: .leading, spacing: theme.spacing.xxs) {172 VStack(alignment: .leading, spacing: theme.spacing.xxs) {173 Text(verbatim: severity.label)174 .font(theme.font(.caption).weight(.semibold))175 .textCase(.uppercase)176 title.font(theme.font(.headline))177 if let message {178 message.font(theme.font(.callout))179 }180 }181 .accessibilityElement(children: .combine)182 if let action {183 Button(action: action.handler) {184 action.title.font(theme.font(.headline)).underline()185 }186 .buttonStyle(.plain)187 .glassMinimumHitTarget()188 }189 }190 .foregroundStyle(palette.foreground)191 .frame(maxWidth: .infinity, alignment: .leading)192 if let onDismiss {193 Button(action: onDismiss) {194 Label(GlassL10n.string(.dismiss), systemImage: "xmark")195 .labelStyle(.iconOnly)196 .foregroundStyle(palette.foreground)197 }198 .buttonStyle(.plain)199 .glassMinimumHitTarget()200 }201 }202 .padding(theme.spacing.md)203 .background(palette.background, in: shape)204 .overlay(shape.stroke(palette.accent.opacity(0.5), lineWidth: 1))205 .accessibilityElement(children: .contain)206 }207}208