-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemePreviewGenerator.swift
More file actions
191 lines (160 loc) · 5.73 KB
/
ThemePreviewGenerator.swift
File metadata and controls
191 lines (160 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
nonisolated public struct ThemePreviewGenerator: Sendable {
nonisolated public init() {}
nonisolated public func generate(from config: ThemeConfig) -> GeneratedFile {
var sections: [String] = []
var components: [String] = []
// Generate color swatches if colors present
if config.categories.contains(.colors) {
let tokens = ThemeCategory.colors.tokens(from: config)
sections.append(colorSwatchesSection(tokens: tokens))
components.append(colorSwatchComponent())
}
// Generate gradient strips if gradients present
if config.categories.contains(.gradients) {
let tokens = ThemeCategory.gradients.tokens(from: config)
sections.append(gradientStripsSection(tokens: tokens))
components.append(gradientStripComponent())
}
// Generate shadow showcase if shadows present
if config.categories.contains(.shadows) {
let tokens = ThemeCategory.shadows.tokens(from: config)
sections.append(shadowShowcaseSection(tokens: tokens))
components.append(shadowCardComponent())
}
let bodySections = sections.joined(separator: "\n\n")
var componentsSuffix = ""
if !components.isEmpty {
componentsSuffix = "\n\n// MARK: - Preview Components\n\n" + components.joined(separator: "\n\n")
}
let content = """
// Generated by ThemeKit — do not edit
import SwiftUI
import ThemeKit
/// A SwiftUI view that renders all theme tokens as a visual palette.
public struct ThemePreview: View {
public var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 24) {
\(bodySections.split(separator: "\n").map { " \($0)" }.joined(separator: "\n"))
}
.padding()
}
}
}
#Preview {
ThemePreview()
}\(componentsSuffix)
"""
return GeneratedFile(name: "Theme+Preview.swift", content: content)
}
// MARK: - Sections
nonisolated func colorSwatchesSection(tokens: [ThemeToken]) -> String {
let swatches = tokens.map { token in
"""
ColorSwatch(name: "\(token.name)", style: .\(token.style))
"""
}.joined(separator: "\n")
return """
// MARK: - Colors
VStack(alignment: .leading, spacing: 12) {
Text("Colors")
.font(.headline)
LazyVGrid(columns: [GridItem(.adaptive(minimum: 96))], spacing: 12) {
\(swatches)
}
}
"""
}
nonisolated func gradientStripsSection(tokens: [ThemeToken]) -> String {
let strips = tokens.map { token in
"""
GradientStrip(name: "\(token.name)", style: .\(token.style))
"""
}.joined(separator: "\n")
return """
// MARK: - Gradients
VStack(alignment: .leading, spacing: 12) {
Text("Gradients")
.font(.headline)
LazyVGrid(columns: [GridItem(.adaptive(minimum: 96))], spacing: 12) {
\(strips)
}
}
"""
}
nonisolated func shadowShowcaseSection(tokens: [ThemeToken]) -> String {
let cards = tokens.map { token in
"""
ShadowCard(name: "\(token.name)", style: .background.\(token.style))
"""
}.joined(separator: "\n")
return """
// MARK: - Shadows
VStack(alignment: .leading, spacing: 12) {
Text("Shadows")
.font(.headline)
LazyVGrid(columns: [GridItem(.adaptive(minimum: 120))], spacing: 12) {
\(cards)
}
}
"""
}
// MARK: - Components
nonisolated func colorSwatchComponent() -> String {
"""
private struct ColorSwatch<Style: ShapeStyle>: View {
let name: String
let style: Style
var body: some View {
VStack(alignment: .leading, spacing: 4) {
RoundedRectangle(cornerRadius: 8)
.fill(style)
.frame(height: 32)
Text(name)
.font(.caption)
.lineLimit(1)
.foregroundStyle(.secondary)
}
}
}
"""
}
nonisolated func gradientStripComponent() -> String {
"""
private struct GradientStrip<Style: ShapeStyle>: View {
let name: String
let style: Style
var body: some View {
VStack(alignment: .leading, spacing: 4) {
RoundedRectangle(cornerRadius: 8)
.fill(style)
.frame(height: 32)
Text(name)
.font(.caption)
.lineLimit(1)
.foregroundStyle(.secondary)
}
}
}
"""
}
nonisolated func shadowCardComponent() -> String {
"""
private struct ShadowCard<Style: ShapeStyle>: View {
let name: String
let style: Style
var body: some View {
VStack(alignment: .leading, spacing: 4) {
RoundedRectangle(cornerRadius: 12)
.fill(style)
.frame(height: 48)
Text(name)
.font(.caption)
.lineLimit(1)
.foregroundStyle(.secondary)
}
}
}
"""
}
}