Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Apps/GlassStudio/Kit/Views/PreviewCanvas.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/// Shows a view under a preview configuration: theme, appearance, backdrop, text size and simulated45/// accessibility settings. Simulations apply to this subtree only and are labelled as such.46public struct PreviewCanvas<Content: View>: View {47 private let configuration: PreviewConfiguration48 private let theme: GlassTheme49 private let minHeight: CGFloat50 private let content: Content5152 public init(configuration: PreviewConfiguration, theme: GlassTheme, minHeight: CGFloat = 260, @ViewBuilder content: () -> Content) {53 self.configuration = configuration54 self.theme = theme55 self.minHeight = minHeight56 self.content = content()57 }5859 public var body: some View {60 VStack(alignment: .leading, spacing: 6) {61 ZStack {62 PreviewBackdropView(backdrop: configuration.backdrop)63 content64 .padding(24)65 .frame(maxWidth: .infinity, minHeight: minHeight)66 }67 .glassTheme(theme)68 .modifier(PreviewEnvironment(configuration: configuration))69 .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))70 .overlay(RoundedRectangle(cornerRadius: 16, style: .continuous).strokeBorder(.separator))71 .accessibilityElement(children: .contain)72 .accessibilityLabel(Text(L("Preview")))7374 // Outside the canvas, so that it never covers the component being previewed.75 if configuration.hasSimulations {76 Label(L("Simulated settings"), systemImage: "wand.and.sparkles")77 .font(.caption)78 .foregroundStyle(.secondary)79 .accessibilityHint(Text(L("These settings affect this preview only, not your device.")))80 }81 }82 }83}8485private struct PreviewEnvironment: ViewModifier {86 let configuration: PreviewConfiguration87 @Environment(\.colorScheme) private var systemScheme88 @Environment(\.dynamicTypeSize) private var systemTextSize8990 func body(content: Content) -> some View {91 content92 .environment(\.colorScheme, configuration.colorScheme ?? systemScheme)93 .environment(\.dynamicTypeSize, configuration.dynamicTypeSize ?? systemTextSize)94 .environment(\.layoutDirection, configuration.rightToLeft ? .rightToLeft : .leftToRight)95 .glassSimulatedAccessibility(96 reduceTransparency: configuration.simulateReduceTransparency ? true : nil,97 reduceMotion: configuration.simulateReduceMotion ? true : nil98 )99 }100}101102/// What sits behind a previewed component. The "scene" is drawn in code and stands in for a photograph:103/// no image file is bundled.104struct PreviewBackdropView: View {105 let backdrop: PreviewBackdrop106 @GlassThemed private var theme107108 var body: some View {109 switch backdrop {110 case .themed: theme.color(.background)111 case .white: Color.white112 case .black: Color.black113 case .scene: scene114 case .checkerboard: checkerboard115 }116 }117118 private var scene: some View {119 Canvas { context, size in120 context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(Color(red: 0.36, green: 0.62, blue: 0.84)))121 context.fill(Path(CGRect(x: 0, y: size.height * 0.62, width: size.width, height: size.height * 0.38)), with: .color(Color(red: 0.22, green: 0.47, blue: 0.33)))122 var hills = Path()123 hills.move(to: CGPoint(x: 0, y: size.height * 0.66))124 hills.addCurve(to: CGPoint(x: size.width, y: size.height * 0.58), control1: CGPoint(x: size.width * 0.3, y: size.height * 0.36), control2: CGPoint(x: size.width * 0.62, y: size.height * 0.82))125 hills.addLine(to: CGPoint(x: size.width, y: size.height))126 hills.addLine(to: CGPoint(x: 0, y: size.height))127 context.fill(hills, with: .color(Color(red: 0.16, green: 0.36, blue: 0.27)))128 context.fill(Path(ellipseIn: CGRect(x: size.width * 0.68, y: size.height * 0.1, width: size.height * 0.26, height: size.height * 0.26)), with: .color(Color(red: 0.99, green: 0.86, blue: 0.42)))129 for index in 0..<5 {130 let x = size.width * (0.08 + Double(index) * 0.17)131 context.fill(Path(roundedRect: CGRect(x: x, y: size.height * (0.44 + Double(index % 2) * 0.06), width: size.width * 0.09, height: size.height * 0.24), cornerRadius: 3), with: .color(Color(red: 0.85, green: 0.42 + Double(index) * 0.06, blue: 0.3)))132 }133 }134 .accessibilityHidden(true)135 }136137 private var checkerboard: some View {138 Canvas { context, size in139 let side: CGFloat = 22140 for row in 0...Int(size.height / side) {141 for column in 0...Int(size.width / side) {142 let rect = CGRect(x: CGFloat(column) * side, y: CGFloat(row) * side, width: side, height: side)143 context.fill(Path(rect), with: .color((row + column).isMultiple(of: 2) ? .white : .black))144 }145 }146 }147 .accessibilityHidden(true)148 }149}150151/// Menu that edits a ``PreviewConfiguration``.152struct PreviewOptionsMenu: View {153 @Binding var configuration: PreviewConfiguration154 let themes: [GlassTheme]155156 var body: some View {157 Menu {158 Picker(L("Theme"), selection: $configuration.themeID) {159 ForEach(themes) { theme in Text(verbatim: theme.name).tag(theme.id) }160 }161 Picker(L("Appearance"), selection: $configuration.appearance) {162 Text(L("System")).tag(GlassAppearance.system)163 Text(L("Light")).tag(GlassAppearance.light)164 Text(L("Dark")).tag(GlassAppearance.dark)165 }166 Picker(L("Backdrop"), selection: $configuration.backdrop) {167 Text(L("Theme background")).tag(PreviewBackdrop.themed)168 Text(L("White")).tag(PreviewBackdrop.white)169 Text(L("Black")).tag(PreviewBackdrop.black)170 Text(L("Drawn scene")).tag(PreviewBackdrop.scene)171 Text(L("Checkerboard")).tag(PreviewBackdrop.checkerboard)172 }173 Picker(L("Density"), selection: $configuration.density) {174 Text(L("Theme default")).tag(GlassDensity?.none)175 Text(L("Compact")).tag(GlassDensity?.some(.compact))176 Text(L("Regular density")).tag(GlassDensity?.some(.regular))177 Text(L("Comfortable")).tag(GlassDensity?.some(.comfortable))178 }179 Picker(L("Text size"), selection: $configuration.textSizeIndex) {180 Text(L("System")).tag(Int?.none)181 ForEach(PreviewConfiguration.textSizes.indices, id: \.self) { index in182 Text(verbatim: Self.textSizeNames[index]).tag(Int?.some(index))183 }184 }185 Section(L("Simulate in this preview")) {186 Toggle(L("Reduce Transparency"), isOn: $configuration.simulateReduceTransparency)187 Toggle(L("Reduce Motion"), isOn: $configuration.simulateReduceMotion)188 Toggle(L("Right-to-left layout"), isOn: $configuration.rightToLeft)189 }190 Button(L("Reset preview options"), systemImage: "arrow.counterclockwise") {191 let theme = configuration.themeID192 configuration = PreviewConfiguration()193 configuration.themeID = theme194 }195 } label: {196 Label(L("Preview options"), systemImage: "slider.horizontal.3")197 }198 .accessibilityIdentifier("preview-options")199 }200201 static let textSizeNames = ["XS", "S", "M", "L", "XL", "XXL", "XXXL", "AX1", "AX2", "AX3", "AX4", "AX5"]202}203