SPB Git

Repository Public License

ReferenceRenderTests.swift 5.0 KB 107 lines swift

1import Foundation2import SwiftUI3import Testing4@testable import LunarDesign56/// Offscreen rendering of the gallery and of the reference mockups to `DesignSystem/reference/`, and the token7/// export. These write files, so they only run when asked:8///9///     LUNAR_RENDER_REFERENCE=1 swift test … --filter LunarReferenceRender     (see scripts/render-design-reference.sh)10///     LUNAR_EXPORT_TOKENS=1    swift test … --filter LunarReferenceRender/exportTokens11enum ReferenceRun {12    static let renderEnabled = ProcessInfo.processInfo.environment["LUNAR_RENDER_REFERENCE"] == "1"13    static let tokensEnabled = renderEnabled || ProcessInfo.processInfo.environment["LUNAR_EXPORT_TOKENS"] == "1"1415    /// The themes rendered for reference: the default and two others, each in dark and light.16    static let themes: [LunarTheme] = [LunarThemes.lunar, LunarThemes.aurora, LunarThemes.solar]17        .flatMap { [$0.dark, $0.light] }18}1920@MainActor21@Suite(.serialized) struct LunarReferenceRender {2223    @Test(.enabled(if: ReferenceRun.tokensEnabled))24    func exportTokens() throws {25        try FileManager.default.createDirectory(at: ReferencePaths.designSystem, withIntermediateDirectories: true)26        try LunarTokenDocument.current.jsonData()27            .write(to: ReferencePaths.designSystem.appendingPathComponent("tokens.json"), options: .atomic)28    }2930    @Test(.enabled(if: ReferenceRun.renderEnabled), arguments: ReferenceRun.themes)31    func gallery(theme: LunarTheme) throws {32        let view = LunarGalleryContent(layout: .wide)33            .padding(LunarSpacing.xl)34            .frame(width: 1440)35            .background { Rectangle().fill(theme.windowGradient) }36        // The gallery is tall: 1.5x keeps the file size reasonable while staying sharp.37        try ReferenceRenderer.write(view, theme: theme, name: "gallery", scale: 1.5)38    }3940    @Test(.enabled(if: ReferenceRun.renderEnabled), arguments: ReferenceRun.themes)41    func mockups(theme: LunarTheme) throws {42        try ReferenceRenderer.write(MacWindowMockup(), theme: theme, name: "mac-window")43        try ReferenceRenderer.write(PhoneMockup(), theme: theme, name: "iphone")44        try ReferenceRenderer.write(PadMockup(), theme: theme, name: "ipad")45        try ReferenceRenderer.write(OrbitMockup(), theme: theme, name: "orbit")46        try ReferenceRenderer.write(NewTabMockup(), theme: theme, name: "new-tab")47    }4849    /// A contact sheet of every glyph at 16, 24 and 48 pt, to judge legibility at small sizes.50    @Test(.enabled(if: ReferenceRun.renderEnabled))51    func glyphSheet() throws {52        for theme in [LunarThemes.lunar.dark, LunarThemes.lunar.light] {53            try ReferenceRenderer.write(GlyphSheet(), theme: theme, name: "glyphs")54        }55    }56}5758@MainActor59enum ReferenceRenderer {60    enum Failure: Error { case noImage(String), encoding(String) }6162    static func write(_ view: some View, theme: LunarTheme, name: String, scale: CGFloat = 2) throws {63        let content = view64            .lunarTheme(theme)65            .environment(\.lunarRenderMode, .snapshot)66        let renderer = ImageRenderer(content: content)67        renderer.scale = scale68        renderer.isOpaque = true69        guard let image = renderer.cgImage else { throw Failure.noImage(name) }70        let rep = NSBitmapImageRep(cgImage: image)71        guard let data = rep.representation(using: .png, properties: [:]) else { throw Failure.encoding(name) }72        let directory = ReferencePaths.reference73        try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)74        let url = directory.appendingPathComponent("\(name)--\(theme.familyID)-\(theme.variant.rawValue).png")75        try data.write(to: url, options: .atomic)76    }77}7879struct GlyphSheet: View {80    @Environment(\.lunarTheme) private var theme8182    var body: some View {83        VStack(alignment: .leading, spacing: LunarSpacing.md) {84            ForEach([16, 24, 48] as [CGFloat], id: \.self) { size in85                HStack(spacing: LunarSpacing.md) {86                    Text("\(Int(size))").lunarFont(.caption).lunarTabularDigits()87                        .foregroundStyle(theme.textSecondary).frame(width: 24)88                    LunarMarkShape().fill(theme.accent).frame(width: size, height: size)89                    ForEach(LunarGlyph.allCases) { glyph in90                        LunarGlyphView(glyph, size: size).foregroundStyle(theme.textPrimary)91                    }92                }93            }94            HStack(spacing: 0) {95                Color.clear.frame(width: 24 + LunarSpacing.md + 48 + LunarSpacing.md, height: 1)96                ForEach(LunarGlyph.allCases) { glyph in97                    Text(glyph.displayName).font(.system(size: 9, weight: .medium))98                        .foregroundStyle(theme.textSecondary)99                        .frame(width: 48 + LunarSpacing.md, alignment: .leading)100                }101            }102        }103        .padding(LunarSpacing.xl)104        .background { Rectangle().fill(theme.windowGradient) }105    }106}107
Files main
Menu

Theme