Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassAI/AI/GlassThinkingIndicator.swift8// Module : SPGlassAI9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0313//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 Accessibility43import SPGlassCore44import SPGlassTokens45import SPGlassThemes4647/// The kind of activity a ``GlassThinkingIndicator`` shows. It only selects the symbol; the words come from the host.48public enum GlassActivityKind: Hashable, Sendable {49 case thinking, searching, reading, writing, waiting50 /// Any other activity, with its own SF Symbol.51 case custom(systemImage: String)5253 /// The built-in kinds, in a stable order.54 public static let standard: [GlassActivityKind] = [.thinking, .searching, .reading, .writing, .waiting]5556 public var symbolName: String {57 switch self {58 case .thinking: "sparkles"59 case .searching: "magnifyingglass"60 case .reading: "doc.text.magnifyingglass"61 case .writing: "pencil.line"62 case .waiting: "hourglass"63 case .custom(let systemImage): systemImage64 }65 }66}6768/// Wording and announcement rules of ``GlassThinkingIndicator``.69public enum GlassActivityAnnouncement {70 /// The label shown and spoken: the host's label, or "Working" when it is missing or blank.71 public static func resolvedLabel(_ label: String?) -> String {72 let trimmed = label?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""73 return trimmed.isEmpty ? GlassL10n.string(.working) : trimmed74 }7576 /// A change is announced only when the resolved label really changes.77 public static func shouldAnnounce(from old: String?, to new: String?) -> Bool {78 resolvedLabel(old) != resolvedLabel(new)79 }8081 /// The announcement, with low priority so that it never interrupts what is being spoken.82 public static func attributed(_ label: String?) -> AttributedString {83 var announcement = AttributedString(resolvedLabel(label))84 announcement.accessibilitySpeechAnnouncementPriority = .low85 return announcement86 }87}8889/// The form of a ``GlassThinkingIndicator``.90public enum GlassThinkingIndicatorStyle: String, CaseIterable, Sendable {91 /// A chip on an opaque surface: a tinted symbol tile, the label and three activity dots. For a92 /// conversation, where the indicator stands in for the reply that is coming.93 case chip94 /// The symbol and the label alone, in secondary text. For a status line or a toolbar.95 case plain96}9798/// An explicit activity indicator: a symbol and the label the host supplies.99///100/// ```swift101/// GlassThinkingIndicator(.searching, label: "Searching the documentation")102/// GlassThinkingIndicator() // "Working"103/// GlassThinkingIndicator(.waiting, label: "Waiting for the server", style: .plain)104/// ```105///106/// The indicator states what the application is doing in the host's own words. It never displays reasoning,107/// intermediate thoughts or any text the host did not pass as `label`. The symbol pulses and the dots run108/// while the indicator is on screen; both are static with Reduce Motion. A change of label is announced109/// politely to assistive technologies. Remove the view when the activity ends.110public struct GlassThinkingIndicator: View {111 @GlassThemed private var theme112 @GlassAccessibility private var accessibility113 private let kind: GlassActivityKind114 private let label: String?115 private let announcesChanges: Bool116 private let style: GlassThinkingIndicatorStyle117118 /// - Parameters:119 /// - kind: selects the symbol.120 /// - label: what is happening, in the host's words. "Working" when `nil`.121 /// - announcesChanges: posts a low-priority accessibility announcement when the label changes.122 /// - style: ``GlassThinkingIndicatorStyle/chip`` (default) or ``GlassThinkingIndicatorStyle/plain``.123 public init(_ kind: GlassActivityKind = .thinking, label: String? = nil, announcesChanges: Bool = true, style: GlassThinkingIndicatorStyle = .chip) {124 self.kind = kind125 self.label = label126 self.announcesChanges = announcesChanges127 self.style = style128 }129130 public var body: some View {131 Group {132 switch style {133 case .chip: chip134 case .plain: plain135 }136 }137 .accessibilityElement(children: .combine)138 .accessibilityAddTraits(.updatesFrequently)139 .onChange(of: label) { old, new in140 guard announcesChanges, GlassActivityAnnouncement.shouldAnnounce(from: old, to: new) else { return }141 AccessibilityNotification.Announcement(GlassActivityAnnouncement.attributed(new)).post()142 }143 }144145 private var plain: some View {146 Label {147 Text(verbatim: GlassActivityAnnouncement.resolvedLabel(label))148 } icon: {149 Image(systemName: kind.symbolName)150 .symbolEffect(.pulse, isActive: !accessibility.reduceMotion)151 .accessibilityHidden(true)152 }153 .font(theme.font(.callout))154 .foregroundStyle(theme.color(.textSecondary))155 }156157 private var chip: some View {158 HStack(spacing: theme.spacing.xs) {159 Image(systemName: kind.symbolName)160 .font(.system(size: 13, weight: .semibold))161 .foregroundStyle(theme.color(.primary))162 .symbolEffect(.pulse, isActive: !accessibility.reduceMotion)163 .frame(width: 28, height: 28)164 .background(theme.color(.primary).opacity(theme.isDark ? 0.26 : 0.13), in: Circle())165 .accessibilityHidden(true)166 Text(verbatim: GlassActivityAnnouncement.resolvedLabel(label))167 .font(theme.font(.callout).weight(.medium))168 .foregroundStyle(theme.color(.textPrimary))169 .lineLimit(2)170 Image(systemName: "ellipsis")171 .font(.system(size: 15, weight: .bold))172 .foregroundStyle(theme.color(.textSecondary))173 .symbolEffect(.variableColor.iterative, isActive: !accessibility.reduceMotion)174 .accessibilityHidden(true)175 }176 .padding(.leading, 6)177 .padding(.trailing, theme.spacing.sm)178 .padding(.vertical, 6)179 .glassOpaqueSurface(in: Capsule())180 }181}182