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 barely tinted with the severity colour, a hairline and an accent109/// edge. The severity is shown by a symbol in a tinted tile, 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, fraction: 0.07)161 let shape = theme.shape(\.medium)162 HStack(alignment: .top, spacing: theme.spacing.sm) {163 Image(systemName: severity.systemImage)164 .font(.system(.subheadline, weight: .semibold))165 .foregroundStyle(palette.accent)166 .frame(width: 32, height: 32)167 .background(palette.accent.opacity(0.16), in: RoundedRectangle(cornerRadius: 9, style: .continuous))168 .accessibilityHidden(true)169 VStack(alignment: .leading, spacing: theme.spacing.xs) {170 VStack(alignment: .leading, spacing: 2) {171 Text(verbatim: severity.label)172 .font(.system(.caption2, weight: .semibold))173 .textCase(.uppercase)174 .tracking(0.7)175 .foregroundStyle(theme.color(.textSecondary))176 title177 .font(theme.font(.headline))178 .foregroundStyle(palette.foreground)179 if let message {180 message181 .font(theme.font(.callout))182 .foregroundStyle(theme.color(.textSecondary))183 .padding(.top, 1)184 }185 }186 .accessibilityElement(children: .combine)187 if let action {188 Button(action: action.handler) {189 action.title190 .font(theme.font(.callout).weight(.semibold))191 .foregroundStyle(palette.foreground)192 .padding(.horizontal, theme.spacing.sm)193 .padding(.vertical, theme.spacing.xxs + 2)194 .background(palette.accent.opacity(0.14), in: Capsule())195 .overlay(Capsule().stroke(palette.accent.opacity(0.45), lineWidth: 1))196 }197 .buttonStyle(.plain)198 .glassMinimumHitTarget()199 }200 }201 .frame(maxWidth: .infinity, alignment: .leading)202 if let onDismiss {203 Button(action: onDismiss) {204 Label(GlassL10n.string(.dismiss), systemImage: "xmark")205 .labelStyle(.iconOnly)206 .font(.system(.footnote, weight: .bold))207 .foregroundStyle(theme.color(.textSecondary))208 .frame(width: 32, height: 32)209 }210 .buttonStyle(.plain)211 .glassMinimumHitTarget()212 .padding(.trailing, -theme.spacing.xs)213 .padding(.top, -6)214 }215 }216 .padding(.vertical, theme.spacing.sm)217 .padding(.leading, theme.spacing.sm + 4)218 .padding(.trailing, theme.spacing.md)219 .background(palette.background)220 .overlay(alignment: .leading) {221 Rectangle().fill(palette.accent).frame(width: 4).accessibilityHidden(true)222 }223 .clipShape(shape)224 .overlay(shape.stroke(palette.accent.opacity(theme.increasedContrast ? 1 : 0.32), lineWidth: 1))225 .accessibilityElement(children: .contain)226 }227}228