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 SwiftUI4243/// Meaning of a ``GlassBadge``; it selects the colour and, for a status badge, the symbol.44public enum GlassBadgeTone: String, CaseIterable, Sendable {45 case neutral, accent, success, warning, critical4647 /// Theme colour role of the fill; `nil` for the neutral surface.48 public var colorToken: GlassColorToken? {49 switch self {50 case .neutral: nil51 case .accent: .accent52 case .success: .success53 case .warning: .warning54 case .critical: .destructive55 }56 }5758 /// Symbol of a status badge. Every tone has a different shape, so the status survives without colour.59 public var statusSystemImage: String {60 switch self {61 case .neutral: "circle.fill"62 case .accent: "info.circle.fill"63 case .success: "checkmark.circle.fill"64 case .warning: "exclamationmark.triangle.fill"65 case .critical: "xmark.octagon.fill"66 }67 }68}6970/// Pure formatting of badge counts.71public enum GlassBadgeFormatter {72 /// The text of a count badge, or `nil` when the badge is hidden.73 ///74 /// - Negative counts are treated as zero.75 /// - Zero is hidden unless `showsZero` is true.76 /// - A count above `cap` reads as the cap followed by a plus sign ("99+"). A cap below 1 counts as 1.77 public static func countText(_ count: Int, cap: Int = 99, showsZero: Bool = false, locale: Locale = .current) -> String? {78 let value = max(0, count)79 if value == 0, !showsZero { return nil }80 let limit = max(1, cap)81 let style = IntegerFormatStyle<Int>.number.locale(locale)82 return value > limit ? limit.formatted(style) + "+" : value.formatted(style)83 }8485 /// What VoiceOver reads for a count: the exact number of items, or "More than 99" above the cap.86 public static func countAccessibilityLabel(_ count: Int, cap: Int = 99) -> String {87 let value = max(0, count)88 let limit = max(1, cap)89 return value > limit ? GlassL10n.string(.badgeMoreThan, limit) : GlassL10n.string(.badgeCount, value)90 }91}9293/// A small marker: a count, a short value or a status.94///95/// ```swift96/// GlassBadge(count: unread) // "12", "99+", hidden at zero97/// GlassBadge("New", tone: .accent) // short value98/// GlassBadge(status: "Online", tone: .success) // symbol + text99/// ```100///101/// A badge is content, drawn on a solid fill whose text colour comes from `theme.onColor`. A status badge102/// always pairs its colour with a symbol and a label; with `showsLabel: false` the label remains the103/// accessibility label and the symbol still differs per tone.104public struct GlassBadge: View {105 private enum Kind {106 case text(String, accessibilityLabel: String?)107 case status(String, showsLabel: Bool)108 case hidden109 }110111 @GlassThemed private var theme112 private let kind: Kind113 private let tone: GlassBadgeTone114115 /// Creates a count badge. See ``GlassBadgeFormatter/countText(_:cap:showsZero:locale:)`` for the rules.116 public init(count: Int, cap: Int = 99, showsZero: Bool = false, tone: GlassBadgeTone = .critical) {117 if let text = GlassBadgeFormatter.countText(count, cap: cap, showsZero: showsZero) {118 kind = .text(text, accessibilityLabel: GlassBadgeFormatter.countAccessibilityLabel(count, cap: cap))119 } else {120 kind = .hidden121 }122 self.tone = tone123 }124125 /// Creates a badge showing a short value ("New", "Beta", "3 min").126 public init(_ value: String, tone: GlassBadgeTone = .neutral) {127 kind = value.isEmpty ? .hidden : .text(value, accessibilityLabel: nil)128 self.tone = tone129 }130131 /// Creates a status badge: a symbol in the tone's colour followed by the status name.132 ///133 /// - Parameter showsLabel: `false` shows the symbol alone; `label` is then read by VoiceOver only.134 public init(status label: String, tone: GlassBadgeTone, showsLabel: Bool = true) {135 kind = .status(label, showsLabel: showsLabel)136 self.tone = tone137 }138139 public var body: some View {140 switch kind {141 case .hidden:142 EmptyView()143 case .text(let text, let accessibilityLabel):144 pill(text)145 .accessibilityLabel(accessibilityLabel ?? text)146 case .status(let label, let showsLabel):147 HStack(spacing: theme.spacing.xxs) {148 Image(systemName: tone.statusSystemImage)149 .foregroundStyle(statusColor)150 .accessibilityHidden(true)151 if showsLabel {152 Text(verbatim: label).foregroundStyle(theme.color(.textPrimary))153 }154 }155 .font(theme.font(.caption))156 .accessibilityElement(children: .ignore)157 .accessibilityLabel(label)158 }159 }160161 private func pill(_ text: String) -> some View {162 let colors = pillColors163 return Text(verbatim: text)164 .font(theme.font(.caption).weight(.semibold).monospacedDigit())165 .foregroundStyle(colors.content)166 .padding(.horizontal, theme.spacing.xs)167 .padding(.vertical, theme.spacing.xxs / 2)168 .frame(minWidth: 20)169 .background(colors.fill, in: Capsule())170 .overlay(Capsule().stroke(tone == .neutral ? theme.color(.separator) : .clear, lineWidth: 1))171 }172173 private var pillColors: (fill: Color, content: Color) {174 guard let token = tone.colorToken else { return (theme.color(.surface), theme.color(.textPrimary)) }175 let palette = GlassSolidPalette(token, theme: theme)176 return (palette.fill, palette.content)177 }178179 private var statusColor: Color {180 tone.colorToken.map { theme.color($0) } ?? theme.color(.textSecondary)181 }182}183