Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Apps/GlassStudio/Kit/Model/ComponentDescriptor.swift8// Module : GlassStudio9// 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 SPGlassKit4344/// A named preset of inspector values ("Prominent", "Loading", "Destructive").45public struct ComponentVariant: Identifiable, Hashable, Sendable {46 public let id: String47 public let title: String48 public let values: [String: InspectorValue]4950 public init(_ id: String, _ title: String, _ values: [String: InspectorValue]) {51 self.id = id; self.title = title; self.values = values52 }53}5455/// Everything the Studio knows about one component: its catalogue entry, its typed parameters, a compiled56/// preview factory and a typed template that produces the matching Swift snippet.57///58/// The preview is ordinary compiled SwiftUI. Nothing typed by the user is ever compiled or evaluated: the59/// inspector edits typed values, the preview reads them, and the snippet is assembled from the same values60/// with ``SwiftLiteral``.61@MainActor62public struct ComponentDescriptor: Identifiable {63 public let info: GlassComponentInfo64 public let fields: [InspectorField]65 public let variants: [ComponentVariant]66 /// True when the component can be placed in a playground composition.67 public let isComposable: Bool68 private let makePreview: @MainActor (InspectorValues) -> AnyView69 private let makeSnippet: @MainActor (InspectorValues) -> String7071 public nonisolated var id: String { info.id }7273 public init<Preview: View>(74 _ componentID: String,75 fields: [InspectorField] = [],76 variants: [ComponentVariant] = [],77 isComposable: Bool = false,78 @ViewBuilder preview: @escaping @MainActor (InspectorValues) -> Preview,79 snippet: @escaping @MainActor (InspectorValues) -> String80 ) {81 guard let info = GlassComponentCatalog.component(id: componentID) else {82 preconditionFailure("No catalogue entry for \(componentID)")83 }84 self.info = info85 self.fields = fields86 self.variants = variants87 self.isComposable = isComposable88 self.makePreview = { AnyView(preview($0)) }89 self.makeSnippet = snippet90 }9192 public var defaultValues: InspectorValues { InspectorValues(fields: fields) }9394 /// The live preview for a set of values. Values are validated against the fields first.95 public func preview(_ values: InspectorValues) -> AnyView {96 makePreview(InspectorValues(fields: fields, values: values.dictionary))97 }9899 /// The Swift code that produces what the preview shows.100 public func snippet(_ values: InspectorValues) -> String {101 makeSnippet(InspectorValues(fields: fields, values: values.dictionary))102 }103104 public func values(for variant: ComponentVariant) -> InspectorValues {105 defaultValues.applying(variant.values)106 }107}108109/// Filters of the catalogue search.110public struct CatalogFilter: Equatable, Sendable {111 public var query = ""112 public var category: GlassComponentCategory?113 public var kind: GlassComponentKind?114 public var platform: GlassPlatform?115 public var favoritesOnly = false116117 public init() {}118119 public var isActive: Bool {120 !query.trimmingCharacters(in: .whitespaces).isEmpty || category != nil || kind != nil || platform != nil || favoritesOnly121 }122123 /// Case- and diacritic-insensitive match on name, identifier, category, kind and summary. Every word of124 /// the query must match.125 public func matches(_ info: GlassComponentInfo, isFavorite: Bool = false) -> Bool {126 if let category, info.category != category { return false }127 if let kind, info.kind != kind { return false }128 if let platform, !info.platforms.contains(platform) { return false }129 if favoritesOnly, !isFavorite { return false }130 let words = Self.fold(query).split(separator: " ")131 guard !words.isEmpty else { return true }132 let haystack = Self.fold([info.name, info.id, info.category.rawValue, info.kind.rawValue, info.summary, info.module].joined(separator: " "))133 return words.allSatisfy { haystack.contains($0) }134 }135136 static func fold(_ text: String) -> String {137 text.folding(options: [.caseInsensitive, .diacriticInsensitive, .widthInsensitive], locale: Locale(identifier: "en")).lowercased()138 }139}140