Cotton code
135 lines · 6.8 KB · SwiftRaw
#if canImport(UIKit)import ImageIOimport SwiftUIimport UniformTypeIdentifiersimport XCTestimport CottonPaper@testable import CottonUI/// Deliberate design captures: tools with a peek, views, and opened acts./// `TEST_RUNNER_FAN_SHOTS_OUT` selects the output directory through xcodebuild.@MainActorfinal class FanDesignShotsTests: XCTestCase {    private struct Shot {        let flavour: String        let name: String        let design: String        let tiles: [String]        let acts: [String]    }    private let shots = [        Shot(flavour: "nancy", name: "nan", design: "nan-dish-2.png",             tiles: ["Plans", "Chef", "Menu", "Kitchen", "Orders"], acts: ["Take an order", "When", "Bin"]),    ]    func testDrawTheBusinessFans() throws {        let env = ProcessInfo.processInfo.environment        guard let dir = env["FAN_SHOTS_OUT"], !dir.isEmpty else {            throw XCTSkip("set FAN_SHOTS_OUT=<dir> to draw the business fans")        }        let out = URL(fileURLWithPath: dir, isDirectory: true)        try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)        let designs = env["FAN_DESIGNS"].map { URL(fileURLWithPath: $0, isDirectory: true) }        for shot in shots {            let after = GalleryFaces.kitchen.theme            var before = after            before.cards.currentTileFill = nil            before.cards.primaryActFill = nil            let codex = designs.flatMap { UIImage(contentsOfFile: $0.appendingPathComponent(shot.design).path) }            let page = HStack(alignment: .center, spacing: 28) {                if let codex { Image(uiImage: codex).resizable().scaledToFit().frame(height: 720) }                VStack(spacing: 24) {                    fan(shot, label: "before", theme: before)                    fan(shot, label: "after", theme: after)                }            }            .padding(24)            .environment(\.colorScheme, .light)            .background(after.colors.ground.color)            write(page, to: out.appendingPathComponent("tools-peek-\(shot.name).png"))            write(tierShot(shot, acts: false, theme: after), to: out.appendingPathComponent("views-tier-\(shot.name).png"))            write(tierShot(shot, acts: true, theme: after), to: out.appendingPathComponent("opened-acts-\(shot.name).png"))        }    }    /// The tiles on a shallow arc, the middle one current and lifted, and    /// the acts row under them.    private func fan(_ shot: Shot, label: String, theme: Theme) -> some View {        let geometry = FanGeometry(scale: theme.scale).withHub(diameter: Double(theme.scale[.fanRecordSize]))        let middle = shot.tiles.count / 2        let tier = FanTier(id: shot.flavour, cards: shot.tiles.map { .init(id: $0, label: $0, style: .object) }, raised: shot.tiles[middle])        let views = FanTier(id: "views", cards: shot.acts.enumerated().map { .init(id: $0.element, label: $0.element, count: $0.offset + 1) }, raised: shot.acts.first)        return VStack(spacing: 18) {            HStack {                Text(label).font(.system(size: 14, weight: .semibold))                Spacer()                NavCircle(symbol: "magnifyingglass", label: "Search") {}                NavCircle(symbol: "plus", label: "Add") {}            }            ZStack(alignment: .topLeading) {                ForEach(Array(tier.cards.enumerated()), id: \.element.id) { index, card in                    if let place = geometry.placement(index: index, theta: Double(middle) * geometry.step, width: 400) {                        FanTierCard(look: .init(card: card, raised: card.id == tier.raised,                                                width: geometry.tileWidth, height: geometry.tileHeight))                            .scaleEffect(place.scale)                            .rotationEffect(.degrees(place.angle))                            .position(place.centre)                            .zIndex(place.order)                    }                }                // Deliberate replacement: only the open tier is on the arc.                FanPeekCards(cards: FanPeek.cards(next: views, centred: true, moving: false), geometry: geometry)                    .scaleEffect(geometry.settledScale)                    .position(geometry.peekCentre(attachedTo: geometry.placement(angle: 0, width: 400)!))                Circle().fill(NavChrome.circleFill)                    .frame(width: theme.scale[.fanRecordSize], height: theme.scale[.fanRecordSize])                    .position(geometry.hub(width: 400, diameter: Double(theme.scale[.fanRecordSize])))            }            .frame(width: 400, height: geometry.height)        }        .padding(20)        .background(theme.colors.ground.color)        .environment(\.theme, theme)    }    private func tierShot(_ shot: Shot, acts: Bool, theme: Theme) -> some View {        let geometry = FanGeometry(scale: theme.scale).withHub(diameter: Double(theme.scale[.fanRecordSize]))        let cards = shot.acts.enumerated().map { index, label in            if acts {                return FanTier.Card(item: EditingItem(id: label, word: label, does: .offered(label)),                                    object: nil, style: .object,                                    red: index == shot.acts.count - 1, primary: index == 0)            }            return FanTier.Card(id: label, label: label, count: index + 1)        }        return ZStack(alignment: .topLeading) {            ForEach(Array(cards.enumerated()), id: \.element.id) { index, card in                if let place = geometry.placement(index: index, theta: 0, width: 400) {                    FanTierCard(look: .init(card: card, raised: index == 0, width: geometry.tileWidth, height: geometry.tileHeight))                        .scaleEffect(place.scale).rotationEffect(.degrees(place.angle))                        .position(place.centre).zIndex(place.order)                }            }            FanClose(size: theme.scale[.fanRecordSize], acts: true) {}                .position(geometry.hub(width: 400, diameter: Double(theme.scale[.fanRecordSize])))        }        .frame(width: 400, height: geometry.height + 70)        .padding(.top, 60)        .background(theme.colors.ground.color)        .environment(\.theme, theme)    }    private func write(_ view: some View, to url: URL) {        let renderer = ImageRenderer(content: view)        renderer.scale = 2        guard let image = renderer.cgImage,              let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)        else { return XCTFail("\(url.lastPathComponent) drew nothing") }        CGImageDestinationAddImage(dest, image, nil)        XCTAssertTrue(CGImageDestinationFinalize(dest), "\(url.lastPathComponent) was not written")    }}#endif