Cotton code
82 lines · 3.0 KB · SwiftRaw
// A project's cap, small, wherever a project is named on paper.//// Home's board draws caps at ~80pt. A todo card, a person's page, a search// result and a project header want the same object at 28, 36, 44 and hero — and// the cap already scales its lettering as a fraction of its side// (`ProjectCaps.swift`), so this is a frame and an accessible name, not a second// design. It exists so nobody writes `ProjectCap(...).frame(width: 32)` in ten// screens and gets ten different answers about what small means.//// A cap chip is never a label with a coloured dot: the whole point of the cap is// that a project is recognised by its enamel before its name is read.import SwiftUIpublic struct CapChip: View {    @Environment(\.theme) private var theme    /// The sizes a cap is worn at away from Home's board.    public enum Size: String, CaseIterable, Sendable {        /// Beside a line of type — a todo card's project, a search result.        case small        /// A row's leading object — a person's page, a project list.        case medium        /// A screen's own subject — a project page's header.        case large        /// The side this size is worn at, as `scale` sets it.        public func side(in scale: ThemeScale) -> CGFloat {            switch self {            case .small: return CapChipKnobs.capChipSmallSide            case .medium: return CapChipKnobs.capChipMediumSide            case .large: return CapChipKnobs.capChipLargeSide            }        }    }    private let name: String    private let colour: CapColour    /// The size the caller named, read off the theme when the chip is drawn;    /// `fixedSide` wins when the caller gave points instead.    private let size: Size    private let fixedSide: CGFloat?    public init(name: String, colour: CapColour, size: Size = .medium) {        self.name = name        self.colour = colour        self.size = size        self.fixedSide = nil    }    public init(name: String, colour: CapColour, side: CGFloat) {        self.name = name        self.colour = colour        self.size = .medium        self.fixedSide = side    }    private var side: CGFloat { fixedSide ?? size.side(in: theme.scale) }    public var body: some View {        ProjectCap(name: name, colour: colour)            .frame(width: side, height: side)            .shadow(                color: theme.colors.shadow.color.opacity(Double(CapChipKnobs.capChipShadowOpacity)),                radius: CapChipKnobs.capChipShadowRadius,                x: 0,                y: 1            )            .accessibilityElement()            .accessibilityLabel(name)    }}/// The sizes, counts and timings CapChip draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum CapChipKnobs {    static let capChipLargeSide: CGFloat = 44    static let capChipMediumSide: CGFloat = 36    static let capChipShadowOpacity: CGFloat = 0.34    static let capChipShadowRadius: CGFloat = 1.5    static let capChipSmallSide: CGFloat = 28}