Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Apps/GlassStudio/Kit/Views/InspectorView.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/// Edits typed values. Each field becomes a native control; there is no free-form code entry.45public struct InspectorView: View {46 @Binding private var values: InspectorValues47 @Environment(\.self) private var environment4849 public init(values: Binding<InspectorValues>) {50 _values = values51 }5253 public var body: some View {54 Form {55 if values.fields.isEmpty {56 Text(L("This component has no adjustable parameter."))57 .foregroundStyle(.secondary)58 }59 ForEach(values.fields) { field in60 control(for: field)61 .accessibilityIdentifier("inspector-\(field.id)")62 }63 if !values.fields.isEmpty {64 Button(L("Reset parameters"), systemImage: "arrow.counterclockwise") { values.reset() }65 .disabled(values.isDefault)66 }67 }68 .formStyle(.grouped)69 }7071 @ViewBuilder72 private func control(for field: InspectorField) -> some View {73 let title = L(field.title)74 switch field.kind {75 case .toggle:76 Toggle(title, isOn: Binding(get: { values.bool(field.id) }, set: { values[field.id] = .bool($0) }))77 case .choice(let options, _):78 Picker(title, selection: Binding(get: { values.choice(field.id) }, set: { values[field.id] = .choice($0) })) {79 ForEach(options, id: \.id) { option in Text(L(option.title)).tag(option.id) }80 }81 case .number(let range, let step, _):82 let binding = Binding(get: { values.number(field.id) }, set: { values[field.id] = .number($0) })83 LabeledContent(title) {84 HStack {85 if (range.upperBound - range.lowerBound) / max(step, 0.0001) > 12 {86 Slider(value: binding, in: range, step: step) { Text(title) }87 .frame(minWidth: 100)88 } else {89 Stepper(title, value: binding, in: range, step: step).labelsHidden()90 }91 Text(binding.wrappedValue, format: .number.precision(.fractionLength(step < 1 ? 2 : 0)))92 .monospacedDigit()93 .foregroundStyle(.secondary)94 }95 }96 case .text(_, let maxLength):97 TextField(title, text: Binding(get: { values.text(field.id) }, set: { values[field.id] = .text(String($0.prefix(maxLength))) }))98 #if os(iOS)99 .textInputAutocapitalization(.never)100 #endif101 .autocorrectionDisabled()102 case .color:103 ColorPicker(title, selection: Binding(104 get: { Color(glass: values.color(field.id)) },105 set: { values[field.id] = .color(GlassRGBA($0, in: environment)) }106 ), supportsOpacity: false)107 case .size:108 Picker(title, selection: Binding(get: { values.size(field.id) }, set: { values[field.id] = .size($0) })) {109 Text(L("Small")).tag(GlassControlSize.small)110 Text(L("Regular")).tag(GlassControlSize.regular)111 Text(L("Large")).tag(GlassControlSize.large)112 }113 }114 }115}116117/// Monospaced, selectable code with a copy button.118public struct CodeView: View {119 private let code: String120 private let title: String121 @State private var copied = false122123 public init(_ code: String, title: String) {124 self.code = code125 self.title = title126 }127128 public var body: some View {129 VStack(alignment: .leading, spacing: 8) {130 HStack {131 Text(title).font(.subheadline.weight(.semibold))132 Spacer()133 Button(copied ? L("Copied") : L("Copy"), systemImage: copied ? "checkmark" : "doc.on.doc") {134 StudioPasteboard.copy(code)135 copied = true136 }137 .buttonStyle(.bordered)138 .controlSize(.small)139 .accessibilityIdentifier("copy-code")140 }141 ScrollView(.horizontal) {142 Text(verbatim: code)143 .font(.system(.callout, design: .monospaced))144 .textSelection(.enabled)145 .padding(12)146 .frame(maxWidth: .infinity, alignment: .leading)147 }148 .background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 10, style: .continuous))149 .accessibilityLabel(Text(title))150 .accessibilityValue(Text(verbatim: code))151 }152 .task(id: copied) {153 guard copied else { return }154 try? await Task.sleep(for: .seconds(2))155 copied = false156 }157 .onChange(of: code) { copied = false }158 }159}160161public enum StudioPasteboard {162 @MainActor163 public static func copy(_ text: String) {164 #if os(macOS)165 NSPasteboard.general.clearContents()166 NSPasteboard.general.setString(text, forType: .string)167 #else168 UIPasteboard.general.string = text169 #endif170 }171}172