Cotton code
174 lines · 7.0 KB · SwiftRaw
// The one empty state: a blank object of the screen's own kind, and two typed// lines.//// It replaced the accent disc with a glyph in it (the retired// `EmptyStateMedallion`), which was the old design system's answer and is a// badge rather than a thing.// A screen of notes that has no notes shows an empty SHEET; a screen of people// with nobody in it shows an empty PRINT; a board with no projects shows a bare// cap — each of which says what the screen would hold without anybody reading a// word. The words are then short, typed, and about what to do next.//// The object is passed in rather than chosen here, because only the screen knows// what it holds; `ObjectEmpty.sheet` and `.print` are the two the kit can make// on its own.import SwiftUIpublic struct ObjectEmpty<Object: View>: View {    @Environment(\.theme) private var theme    private let object: Object    private let line1: String    private let line2: String?    private let actionLabel: String?    private let actionIdentifier: String?    private let action: (() -> Void)?    /// Under a project scope the first line says what the list is empty OF    /// (Mac redesign spec §4) — "Nothing in Garden yet" — rather than the    /// screen's own words for an empty workspace. Empty on the phone.    @Environment(\.cottonProjectScope) private var scope    /// - Parameters:    ///   - actionLabel: the one forward verb. With no `action` it is a    ///     STATEMENT — typed under the lines, not a control ("Sent").    ///   - actionIdentifier: the verb's own accessibility identifier, separate    ///     from the state's, so a test can press it by name.    public init(        object: Object,        line1: String,        line2: String? = nil,        actionLabel: String? = nil,        actionIdentifier: String? = nil,        action: (() -> Void)? = nil    ) {        self.object = object        self.line1 = line1        self.line2 = line2        self.actionLabel = actionLabel        self.actionIdentifier = actionIdentifier        self.action = action    }    public var body: some View {        VStack(spacing: ObjectEmptyKnobs.objectEmptyActionGap) {            // The object and its words are ONE element to a screen reader; the            // verb under them stays its own, or nobody could press it by name.            VStack(spacing: ObjectEmptyKnobs.objectEmptyObjectGap) {                object                    .frame(maxWidth: ObjectEmptyKnobs.objectEmptyObjectMaxWidth)                    .opacity(Double(ObjectEmptyKnobs.objectEmptyObjectOpacity))                // **BOTH LINES ARE THE SECONDARY INK.** The second was the                // tertiary (#ABABAB), which on the paper ground (#EEE3D6) is                // under 2:1 — the Mac walk read the /code empty state's two                // lines as "very low contrast" (finding 23), and every empty                // state in the app draws through here. What separates the                // lines is the SIZE; the tertiary ink is for a mark beside                // something, not for a sentence somebody has to read.                VStack(spacing: ObjectEmptyKnobs.objectEmptyLineGap) {                    Text(ScopeWording.nothingIn(scope.map(\.name)) ?? line1)                        .font(theme.type.body(theme.scale.bodySize + 1))                        .foregroundStyle(theme.colors.inkSecondary.color)                    if let line2 {                        Text(line2)                            .font(theme.type.body(theme.scale.bodySize - 1))                            .foregroundStyle(theme.colors.inkSecondary.color)                    }                }                .multilineTextAlignment(.center)            }            .accessibilityElement(children: .combine)            if let actionLabel {                if let action {                    Button(actionLabel, action: action)                        .buttonStyle(MotionButtonStyle())                        .font(theme.type.body(theme.scale.bodySize))                        .foregroundStyle(theme.colors.accent.color)                        .frame(minHeight: ObjectEmptyKnobs.objectEmptyActionMinHeight)                        .accessibilityIdentifier(actionIdentifier ?? "")                } else {                    Text(actionLabel)                        .font(theme.type.body(theme.scale.bodySize))                        .foregroundStyle(theme.colors.inkSecondary.color)                }            }        }        .frame(maxWidth: .infinity)        .padding(.vertical, ObjectEmptyKnobs.objectEmptyVerticalPadding)    }}/// The blank sheet an empty paper screen shows. Its own view rather than an/// erased one: `AnyView` is the route table's alone (`NavigationTests`), and a/// concrete type is what lets `ObjectEmpty.init(sheet:)` below be a constrained/// initialiser rather than a factory that throws away its type.struct EmptySheet: View {    @Environment(\.theme) private var theme    private let dye: PaperDye    private let edge: PaperEdge    public init(dye: PaperDye = .cream, edge: PaperEdge = .cut) {        self.dye = dye        self.edge = edge    }    public var body: some View {        PaperSurface(dye: dye, edge: edge, seed: "empty")            .frame(height: ObjectEmptyKnobs.objectEmptySheetHeight)            .modifier(PaperLift(shadow: PaperShadow.resting))    }}extension ObjectEmpty where Object == EmptySheet {    /// An empty sheet — for a screen that holds paper.    init(        sheet dye: PaperDye = .cream,        edge: PaperEdge = .cut,        line1: String,        line2: String? = nil,        actionLabel: String? = nil,        actionIdentifier: String? = nil,        action: (() -> Void)? = nil    ) {        self.init(            object: EmptySheet(dye: dye, edge: edge),            line1: line1,            line2: line2,            actionLabel: actionLabel,            actionIdentifier: actionIdentifier,            action: action        )    }}extension ObjectEmpty where Object == EmptyPrint {    /// An empty print — for a screen that holds photographs.    public init(        print seed: String = "empty",        line1: String,        line2: String? = nil,        actionLabel: String? = nil,        action: (() -> Void)? = nil    ) {        self.init(            object: EmptyPrint(seed: seed, size: .grid),            line1: line1,            line2: line2,            actionLabel: actionLabel,            action: action        )    }}/// The sizes, counts and timings ObjectEmpty draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum ObjectEmptyKnobs {    static let objectEmptyActionGap: CGFloat = 18    static let objectEmptyActionMinHeight: CGFloat = 44    static let objectEmptyLineGap: CGFloat = 6    static let objectEmptyObjectGap: CGFloat = 18    static let objectEmptyObjectMaxWidth: CGFloat = 168    static let objectEmptyObjectOpacity: CGFloat = 0.85    static let objectEmptySheetHeight: CGFloat = 108    static let objectEmptyVerticalPadding: CGFloat = 34}