Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassComponents/Feedback/GlassBadge.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/// Meaning of a ``GlassBadge``; it selects the colour and, for a status badge, the symbol.48public enum GlassBadgeTone: String, CaseIterable, Sendable {49 case neutral, accent, success, warning, critical5051 /// Theme colour role of the fill; `nil` for the neutral surface.52 public var colorToken: GlassColorToken? {53 switch self {54 case .neutral: nil55 case .accent: .accent56 case .success: .success57 case .warning: .warning58 case .critical: .destructive59 }60 }6162 /// Symbol of a status badge. Every tone has a different shape, so the status survives without colour.63 public var statusSystemImage: String {64 switch self {65 case .neutral: "circle.fill"66 case .accent: "info.circle.fill"67 case .success: "checkmark.circle.fill"68 case .warning: "exclamationmark.triangle.fill"69 case .critical: "xmark.octagon.fill"70 }71 }72}7374/// Pure formatting of badge counts.75public enum GlassBadgeFormatter {76 /// The text of a count badge, or `nil` when the badge is hidden.77 ///78 /// - Negative counts are treated as zero.79 /// - Zero is hidden unless `showsZero` is true.80 /// - A count above `cap` reads as the cap followed by a plus sign ("99+"). A cap below 1 counts as 1.81 public static func countText(_ count: Int, cap: Int = 99, showsZero: Bool = false, locale: Locale = .current) -> String? {82 let value = max(0, count)83 if value == 0, !showsZero { return nil }84 let limit = max(1, cap)85 let style = IntegerFormatStyle<Int>.number.locale(locale)86 return value > limit ? limit.formatted(style) + "+" : value.formatted(style)87 }8889 /// What VoiceOver reads for a count: the exact number of items, or "More than 99" above the cap.90 public static func countAccessibilityLabel(_ count: Int, cap: Int = 99) -> String {91 let value = max(0, count)92 let limit = max(1, cap)93 return value > limit ? GlassL10n.string(.badgeMoreThan, limit) : GlassL10n.string(.badgeCount, value)94 }95}9697/// A small marker: a count, a short value or a status.98///99/// ```swift100/// GlassBadge(count: unread) // "12", "99+", hidden at zero101/// GlassBadge("New", tone: .accent) // short value102/// GlassBadge(status: "Online", tone: .success) // symbol + text103/// ```104///105/// A badge is content, drawn on a solid fill whose text colour comes from `theme.onColor`. A status badge106/// always pairs its colour with a symbol and a label; with `showsLabel: false` the label remains the107/// accessibility label and the symbol still differs per tone.108public struct GlassBadge: View {109 private enum Kind {110 case text(String, accessibilityLabel: String?)111 case status(String, showsLabel: Bool)112 case hidden113 }114115 @GlassThemed private var theme116 private let kind: Kind117 private let tone: GlassBadgeTone118119 /// Creates a count badge. See ``GlassBadgeFormatter/countText(_:cap:showsZero:locale:)`` for the rules.120 public init(count: Int, cap: Int = 99, showsZero: Bool = false, tone: GlassBadgeTone = .critical) {121 if let text = GlassBadgeFormatter.countText(count, cap: cap, showsZero: showsZero) {122 kind = .text(text, accessibilityLabel: GlassBadgeFormatter.countAccessibilityLabel(count, cap: cap))123 } else {124 kind = .hidden125 }126 self.tone = tone127 }128129 /// Creates a badge showing a short value ("New", "Beta", "3 min").130 public init(_ value: String, tone: GlassBadgeTone = .neutral) {131 kind = value.isEmpty ? .hidden : .text(value, accessibilityLabel: nil)132 self.tone = tone133 }134135 /// Creates a status badge: a symbol in the tone's colour followed by the status name.136 ///137 /// - Parameter showsLabel: `false` shows the symbol alone; `label` is then read by VoiceOver only.138 public init(status label: String, tone: GlassBadgeTone, showsLabel: Bool = true) {139 kind = .status(label, showsLabel: showsLabel)140 self.tone = tone141 }142143 public var body: some View {144 switch kind {145 case .hidden:146 EmptyView()147 case .text(let text, let accessibilityLabel):148 pill(text)149 .accessibilityLabel(accessibilityLabel ?? text)150 case .status(let label, let showsLabel):151 HStack(spacing: theme.spacing.xxs) {152 Image(systemName: tone.statusSystemImage)153 .foregroundStyle(statusColor)154 .accessibilityHidden(true)155 if showsLabel {156 Text(verbatim: label).foregroundStyle(theme.color(.textPrimary))157 }158 }159 .font(theme.font(.caption))160 .accessibilityElement(children: .ignore)161 .accessibilityLabel(label)162 }163 }164165 private func pill(_ text: String) -> some View {166 let colors = pillColors167 return Text(verbatim: text)168 .font(theme.font(.caption).weight(.semibold).monospacedDigit())169 .foregroundStyle(colors.content)170 .padding(.horizontal, theme.spacing.xs)171 .padding(.vertical, theme.spacing.xxs / 2)172 .frame(minWidth: 20)173 .background(colors.fill, in: Capsule())174 .overlay(Capsule().stroke(tone == .neutral ? theme.color(.separator) : .clear, lineWidth: 1))175 }176177 private var pillColors: (fill: Color, content: Color) {178 guard let token = tone.colorToken else { return (theme.color(.surface), theme.color(.textPrimary)) }179 let palette = GlassSolidPalette(token, theme: theme)180 return (palette.fill, palette.content)181 }182183 private var statusColor: Color {184 tone.colorToken.map { theme.color($0) } ?? theme.color(.textSecondary)185 }186}187