Cotton code
162 lines · 6.2 KB · SwiftRaw
import SwiftUI/// The panel: 360 × 560, one of three screens (sign in, one more step, working) and the account at its foot/// (build/panel.html; the core decides which, `status.panel.screen`).struct PopoverView: View {    @Bindable var model: AppModel    var body: some View {        VStack(alignment: .leading, spacing: 12) {            switch model.screen {            case .starting:                Spacer(minLength: 0)            case .signIn:                SignInView(model: model)            case .cards:                if let error = model.openError {                    Text(error).font(Faces.small).foregroundStyle(Palette.warnText).fixedSize(horizontal: false, vertical: true)                }                if let panel = model.status?.panel {                    switch panel.screen {                    case "working": WorkingView(model: model, panel: panel)                    case "signIn": SignInView(model: model)                    default: StepView(model: model)                    }                } else {                    Spacer(minLength: 0)                }            }            AccountFooter(model: model, account: model.status?.panel?.account, signedIn: model.screen == .cards)        }        .padding(.top, 14)        .padding(.horizontal, 14)        .padding(.bottom, 10)        .frame(width: 360, height: 560, alignment: .top)        .foregroundStyle(Palette.ink)    }}/// Signed in, not yet able to open the person's things: the getting-in page. This Mac asks by itself and shows/// the square code, then the six digits once the other device opens the request; No other device? and Sign out/// are on the card (GETTING-IN-CONTRACT.md, the Mac panel). It moves on by itself when the other device answers.struct StepView: View {    @Bindable var model: AppModel    var body: some View {        ScrollView(.vertical) {            VStack(alignment: .leading, spacing: 8) {                if let asking = model.asking {                    AskCardView(card: asking, model: model)                } else if model.status?.vault?.state == "waiting" && model.signInError == nil {                    ProgressView().controlSize(.small).frame(maxWidth: .infinity)                        .task { if model.asking == nil && !model.signInBusy { model.askApprovalHere() } }                } else {                    if let vault = model.status?.vault {                        Text(vault.detail)                            .font(Faces.small)                            .foregroundStyle(vault.state == "trouble" ? Palette.warnText : Palette.quiet)                            .fixedSize(horizontal: false, vertical: true)                    }                    if let error = model.signInError {                        Text(error).font(Faces.small).foregroundStyle(Palette.warnText).fixedSize(horizontal: false, vertical: true)                    }                    if let ask = model.status?.vault?.ask ?? model.status?.askButton {                        Button(ask) { model.askApprovalHere() }                            .buttonStyle(PrimaryButtonStyle(enabled: !model.signInBusy))                            .disabled(model.signInBusy)                            .accessibilityIdentifier("vault-ask-approval")                    }                }            }            .frame(maxWidth: .infinity, alignment: .leading)        }        .scrollIndicators(.never)        .frame(maxHeight: .infinity)    }}// MARK: Shared pieces/// The mock's card: white, 12-point corners, a hairline shadow under it.struct CardBackground: ViewModifier {    var fill: Color = Palette.card    var ring = false    func body(content: Content) -> some View {        content            .padding(.vertical, 12)            .padding(.horizontal, 14)            .frame(maxWidth: .infinity, alignment: .leading)            .background(                RoundedRectangle(cornerRadius: 12, style: .continuous)                    .fill(fill)                    .shadow(color: ring ? .clear : Palette.cardShadow, radius: 0, x: 0, y: 1)            )            .overlay {                if ring {                    RoundedRectangle(cornerRadius: 13.5, style: .continuous)                        .strokeBorder(Palette.ink, lineWidth: 1.5)                        .padding(-1.5)                }            }    }}extension View {    func card(fill: Color = Palette.card, ring: Bool = false) -> some View {        modifier(CardBackground(fill: fill, ring: ring))    }}/// The mock's black button.struct PrimaryButtonStyle: ButtonStyle {    var enabled = true    func makeBody(configuration: Configuration) -> some View {        configuration.label            .font(Faces.button)            .foregroundStyle(Palette.buttonText)            .frame(maxWidth: .infinity)            .padding(8)            .background(                RoundedRectangle(cornerRadius: 8, style: .continuous)                    .fill(Color.accentColor.opacity(enabled ? (configuration.isPressed ? 0.8 : 1) : 0.4))            )            .contentShape(Rectangle())    }}/// The mock's input: paper-white, a hairline border, 8-point corners.struct FieldBox<Field: View>: View {    var font: Font = Faces.field    var tracking: CGFloat = 0    /// Drawn in the quiet colour while the field is empty.    var placeholder: String = ""    var isEmpty = false    @ViewBuilder var field: Field    var body: some View {        field            .textFieldStyle(.plain)            .background(alignment: .leading) {                if isEmpty {                    Text(placeholder)                        .font(font)                        .tracking(tracking)                        .foregroundStyle(Palette.quiet.opacity(0.6))                        .allowsHitTesting(false)                }            }            .font(font)            .tracking(tracking)            .foregroundStyle(Palette.ink)            .padding(.vertical, 10)            .padding(.horizontal, 12)            .background(                RoundedRectangle(cornerRadius: 8, style: .continuous).fill(Palette.field)            )            .overlay(                RoundedRectangle(cornerRadius: 8, style: .continuous).strokeBorder(Palette.fieldBorder, lineWidth: 1)            )    }}