// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Feedback/GlassBadge.swift // Module : SPGlassComponents // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-02 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// Weight of a ``GlassBadge``. public enum GlassBadgeStyle: String, CaseIterable, Sendable { /// A solid fill in the tone's colour: the strongest, for counts and the one badge that must be seen. case solid /// A light tint of the tone with dark text: for labels repeated down a list or a table. case soft /// A hairline in the tone's colour on a clear background: the quietest. case outline } /// Pure formatting of badge counts. public enum GlassBadgeFormatter { /// The text of a count badge, or `nil` when the badge is hidden. /// /// - Negative counts are treated as zero. /// - Zero is hidden unless `showsZero` is true. /// - A count above `cap` reads as the cap followed by a plus sign ("99+"). A cap below 1 counts as 1. public static func countText(_ count: Int, cap: Int = 99, showsZero: Bool = false, locale: Locale = .current) -> String? { let value = max(0, count) if value == 0, !showsZero { return nil } let limit = max(1, cap) let style = IntegerFormatStyle.number.locale(locale) return value > limit ? limit.formatted(style) + "+" : value.formatted(style) } /// What VoiceOver reads for a count: the exact number of items, or "More than 99" above the cap. public static func countAccessibilityLabel(_ count: Int, cap: Int = 99) -> String { let value = max(0, count) let limit = max(1, cap) return value > limit ? GlassL10n.string(.badgeMoreThan, limit) : GlassL10n.string(.badgeCount, value) } } /// A small marker: a count, a short value or a status. /// /// ```swift /// GlassBadge(count: unread) // "12", "99+", hidden at zero /// GlassBadge("New", tone: .accent) // short value /// GlassBadge("Draft", tone: .warning, style: .soft) // quieter, for lists and tables /// GlassBadge(status: "Online", tone: .success) // symbol + text /// Image(systemName: "bell").glassBadge(count: unread) // a count on the corner of an icon /// ``` /// /// A badge is content, drawn on an opaque fill whose text colour is measured against it. A status badge /// always pairs its colour with a symbol and a label; with `showsLabel: false` the label remains the /// accessibility label and the symbol still differs per tone. public struct GlassBadge: View { private enum Kind { case text(String, accessibilityLabel: String?) case status(String, showsLabel: Bool) case hidden } @GlassThemed private var theme @Environment(\.self) private var environment private let kind: Kind private let tone: GlassBadgeTone private let style: GlassBadgeStyle? /// Creates a count badge. See ``GlassBadgeFormatter/countText(_:cap:showsZero:locale:)`` for the rules. public init(count: Int, cap: Int = 99, showsZero: Bool = false, tone: GlassBadgeTone = .critical, style: GlassBadgeStyle = .solid) { if let text = GlassBadgeFormatter.countText(count, cap: cap, showsZero: showsZero) { kind = .text(text, accessibilityLabel: GlassBadgeFormatter.countAccessibilityLabel(count, cap: cap)) } else { kind = .hidden } self.tone = tone self.style = style } /// Creates a badge showing a short value ("New", "Beta", "3 min"). public init(_ value: String, tone: GlassBadgeTone = .neutral, style: GlassBadgeStyle = .solid) { kind = value.isEmpty ? .hidden : .text(value, accessibilityLabel: nil) self.tone = tone self.style = style } /// Creates a status badge: a symbol in the tone's colour followed by the status name. /// /// - Parameters: /// - showsLabel: `false` shows the symbol alone; `label` is then read by VoiceOver only. /// - style: `nil` draws the symbol and the label inline, without a surface; a style encloses them in a pill. public init(status label: String, tone: GlassBadgeTone, showsLabel: Bool = true, style: GlassBadgeStyle? = nil) { kind = .status(label, showsLabel: showsLabel) self.tone = tone self.style = style } public var body: some View { switch kind { case .hidden: EmptyView() case .text(let text, let accessibilityLabel): pill(style ?? .solid) { Text(verbatim: text) } .accessibilityLabel(accessibilityLabel ?? text) case .status(let label, let showsLabel): Group { if let style, showsLabel { pill(style) { statusContent(label, showsLabel: true, onPill: style) } } else { statusContent(label, showsLabel: showsLabel, onPill: nil) .font(theme.font(.caption).weight(.medium)) } } .accessibilityElement(children: .ignore) .accessibilityLabel(label) } } private func statusContent(_ label: String, showsLabel: Bool, onPill: GlassBadgeStyle?) -> some View { HStack(spacing: theme.spacing.xxs) { if onPill == .solid { Image(systemName: tone.statusSystemImage).accessibilityHidden(true) } else { Image(systemName: tone.statusSystemImage) .foregroundStyle(statusColor) .accessibilityHidden(true) } if showsLabel { if onPill == nil { Text(verbatim: label).foregroundStyle(theme.color(.textPrimary)) } else { Text(verbatim: label) } } } } private func pill(_ style: GlassBadgeStyle, @ViewBuilder content: () -> some View) -> some View { let colors = pillColors(style) return content() .font(theme.font(.caption).weight(.semibold).monospacedDigit()) .lineLimit(1) .foregroundStyle(colors.content) .padding(.horizontal, 7) .padding(.vertical, 2.5) .frame(minWidth: 21, minHeight: 21) .background(colors.fill, in: Capsule()) .overlay(Capsule().strokeBorder(colors.stroke, lineWidth: 1)) } private func pillColors(_ style: GlassBadgeStyle) -> (fill: Color, content: Color, stroke: Color) { guard let token = tone.colorToken else { // Neutral: the ink of the theme, never a status colour. switch style { case .solid: return (theme.color(.textPrimary).opacity(0.1), theme.color(.textPrimary), .clear) case .soft: return (theme.color(.textPrimary).opacity(0.06), theme.color(.textSecondary), .clear) case .outline: return (.clear, theme.color(.textPrimary), theme.color(.separator)) } } switch style { case .solid: let palette = GlassSolidPalette(token, theme: theme, environment: environment) return (palette.fill, palette.content, .clear) case .soft: let palette = GlassTintedPalette(token, theme: theme, fraction: 0.18) return (palette.background, palette.foreground, palette.accent.opacity(0.3)) case .outline: let palette = GlassTintedPalette(token, theme: theme, fraction: 0) return (.clear, theme.color(.textPrimary), palette.accent.opacity(0.75)) } } private var statusColor: Color { tone.colorToken.map { GlassTintedPalette($0, theme: theme, fraction: 0).accent } ?? theme.color(.textSecondary) } } extension View { /// Places a count badge on the top trailing corner of this view, typically an icon. /// /// ```swift /// Image(systemName: "bell").glassBadge(count: unread) /// ``` /// /// Nothing is drawn while the count is hidden (zero, by default). The badge keeps its own accessibility /// label ("7 items"); combine it with the icon's label when the pair should read as one element. public func glassBadge(count: Int, cap: Int = 99, showsZero: Bool = false, tone: GlassBadgeTone = .critical) -> some View { overlay(alignment: .topTrailing) { GlassBadge(count: count, cap: cap, showsZero: showsZero, tone: tone) .fixedSize() .alignmentGuide(.top) { $0[VerticalAlignment.center] - 2 } .alignmentGuide(.trailing) { $0[HorizontalAlignment.center] + 2 } } } }