Cotton code
121 lines · 5.5 KB · SwiftRaw
import CottonPaperimport CottonValuesimport SwiftUI/// A preview describes the next tier; settling never enters it.enum FanPeek {    static let most = 3    static func cards(next: FanTier?, centred: Bool, moving: Bool) -> [FanTier.Card] {        guard centred, !moving, let next else { return [] }        let current = next.cards.first { $0.id == next.raised } ?? next.cards.first        guard let current else { return [] }        return Array(([current] + next.cards.filter { $0.id != current.id }).prefix(most))    }}#if os(iOS)/// Only the exposed headers draw, so the preview never paints over its card.struct FanPeekCards: View {    let cards: [FanTier.Card]    let geometry: FanGeometry    /// The next tier's tile: painted, the headers stand on its stock — a    /// letterboard's felt and cream capitals (a series' seasons over their    /// episodes), a courier stock's label band and typed words in ink, a    /// tile naming its words (`FanTileLook.words`) the band they stand in,    /// in their ink — else the shell's cream.    var tile: FanTileLook? = nil    @Environment(\.theme) private var theme    var body: some View {        let painted = tile.flatMap { $0.painted ? $0 : nil }        ZStack {            ForEach(Array(cards.enumerated().reversed()), id: \.element.id) { index, card in                let place = geometry.peekPlacement(index: index)                let words = card.label + (card.count.map { " · \($0)" } ?? "")                ZStack {                    if let painted, painted.letterboard, painted.words == nil {                        RoundedRectangle(cornerRadius: 4).fill(PaintedTileKnobs.felt)                        Text(words.uppercased())                            .font(CottonFonts.letterboard(place.typeSize)).foregroundStyle(PaintedTileKnobs.cream)                            .lineLimit(1).minimumScaleFactor(0.7).padding(.horizontal, 4)                    } else if let painted {                        let type = painted.paintedType                        FanPeekStock(stock: painted.stock.flatMap(ToolPhotographs.image), width: place.width, height: place.height,                                     centre: FanPeekStock.centre(type))                        Text(type.cased(words))                            .font(type.letterboard ? CottonFonts.letterboard(place.typeSize) : CottonFonts.type(place.typeSize))                            .foregroundStyle(type.ink?.color ?? (type.letterboard ? PaintedTileKnobs.cream : theme.print(.ink)))                            .lineLimit(1).minimumScaleFactor(0.7).padding(.horizontal, 4)                    } else {                        RoundedRectangle(cornerRadius: 4).fill(theme.sheet(.cream))                        Text(words)                            .font(CottonFonts.type(place.typeSize)).foregroundStyle(theme.print(.ink))                            .lineLimit(1).minimumScaleFactor(0.7).padding(.horizontal, 4)                    }                }                .frame(width: place.width, height: place.height)                .rotationEffect(.degrees(place.angle))                .softShadow()                .offset(y: geometry.tileWidth * FanGeometry.peekStepRatio - place.rise)            }        }        .frame(width: geometry.tileWidth, height: geometry.peekHeight)    }}/// A painted stock's header in the peek: the band of the stock its words/// stand in (a courier label, a named words' field: `centre`), the stock at/// the header's width; not in hand yet, the shell's cream.struct FanPeekStock: View {    let stock: UIImage?    let width: Double    let height: Double    var centre: CGFloat = FanPeekStock.centre(.courier)    @Environment(\.theme) private var theme    /// The band's centre against the stock's height: the name's line.    static func centre(_ type: PaintedTileType) -> CGFloat { type.nameBaseline - type.nameCap / 2 }    var body: some View {        if let stock {            let tall = width * stock.size.height / max(stock.size.width, 1)            Image(uiImage: stock).resizable().interpolation(.high)                .frame(width: width, height: tall)                .offset(y: height / 2 - Double(centre) * tall)                .frame(width: width, height: height, alignment: .top)                .clipShape(RoundedRectangle(cornerRadius: 4))        } else {            RoundedRectangle(cornerRadius: 4).fill(theme.sheet(.cream))        }    }}struct FanPeekLayer: View {    let next: FanTier?    let owner: String    let motion: FanMotion    let geometry: FanGeometry    let enter: () -> Void    var body: some View {        let cards = FanPeek.cards(next: next, centred: true, moving: motion.moving)        GeometryReader { proxy in            if let first = cards.first,               let place = geometry.placement(index: motion.settled, theta: motion.theta, width: proxy.size.width) {                // The button includes the exposed headers alone. The card's                // own tap remains with the arc, sharing the same entry action.                Button(action: enter) {                    FanPeekCards(cards: cards, geometry: geometry, tile: next?.tile)                        .contentShape(Rectangle())                }                .buttonStyle(.plain)                .scaleEffect(place.scale).rotationEffect(.degrees(place.angle))                .position(geometry.peekCentre(attachedTo: place))                .accessibilityLabel(first.label + (first.count.map { ", \($0)" } ?? ""))                .accessibilityIdentifier("fan-peek-\(owner)")            }        }    }}#endif