Cotton code
140 lines · 5.3 KB · SwiftRaw
// A picture, a film, a document or a recording that is not on this phone yet,// drawn the one way everywhere it can stand (walker 604bcce6 and its offline// pass): a paper slip with the kind's picture, the file's name and the one// sentence, and Fetch when it can be fetched — over the poster dimmed under a// scrim when there is one. Never grey words over the picture, never a bare// "Photo". Every word is the media definition's `viewer.words`.import Foundationimport SwiftUI/// What kind of thing is missing: the picture the slip draws for it.enum AbsentKind {    case photo, video, document, recording, file    var symbol: String {        switch self {        case .photo: return "photo"        case .video: return "film"        case .document: return "doc.text"        case .recording: return "waveform"        case .file: return "doc"        }    }    /// The kind a file's content type names, else its name's extension.    static func of(contentType: String? = nil, name: String? = nil) -> AbsentKind {        let type = (contentType ?? "").lowercased()        if type.hasPrefix("image/") { return .photo }        if type.hasPrefix("video/") { return .video }        if type.hasPrefix("audio/") { return .recording }        if type == "application/pdf" || type.hasPrefix("text/") { return .document }        switch ((name ?? "") as NSString).pathExtension.lowercased() {        case "jpg", "jpeg", "png", "heic", "gif", "webp": return .photo        case "mov", "mp4", "m4v": return .video        case "m4a", "mp3", "wav", "aac", "caf": return .recording        case "pdf", "txt", "md", "doc", "docx", "pages", "rtf": return .document        default: return .file        }    }}/// The slip: the kind's picture, the name, the sentence, Fetch.struct AbsentSlip: View {    let kind: AbsentKind    var name: String? = nil    let words: ViewerWords?    var removed = false    var fetching = false    /// nil when nothing here can fetch it.    var onFetch: (() -> Void)? = nil    /// Small, in a row or a bubble: the picture and the sentence beside each other.    var compact = false    @Environment(\.theme) private var theme    private var sentence: String {        // The definition's line, never the core's own sentence for why a        // fetch failed: an engine composes no sentence for a slip.        (removed ? words?.removed : words?.absent) ?? ""    }    var body: some View {        Paper(dye: .cream, edge: .cut, seed: name ?? kind.symbol, tilt: false, shadow: .lifted, inset: 0) {            Group {                if compact {                    HStack(alignment: .center, spacing: AbsentSlipKnobs.gap) {                        glyph                        VStack(alignment: .leading, spacing: AbsentSlipKnobs.lineGap) { lines }                        Spacer(minLength: 0)                    }                } else {                    VStack(spacing: AbsentSlipKnobs.gap) {                        glyph                        VStack(spacing: AbsentSlipKnobs.lineGap) { lines }.multilineTextAlignment(.center)                    }                }            }            .padding(.horizontal, AbsentSlipKnobs.inset)            .padding(.vertical, compact ? AbsentSlipKnobs.compactVertical : AbsentSlipKnobs.inset)        }        .frame(maxWidth: compact ? .infinity : AbsentSlipKnobs.maxWidth)        .accessibilityElement(children: .combine)        .accessibilityIdentifier("absent-slip")    }    private var glyph: some View {        Image(systemName: kind.symbol)            .font(theme.type.interface(compact ? AbsentSlipKnobs.compactGlyph : AbsentSlipKnobs.glyph, weight: .light))            .foregroundStyle(theme.print(.pencil))            .accessibilityHidden(true)    }    @ViewBuilder private var lines: some View {        if let name, !name.isEmpty {            Text(name)                .paperMeta(theme.scale.metaSize, ink: .ink)                .lineLimit(2)                .fixedSize(horizontal: false, vertical: true)        }        Text(sentence)            .paperHand(theme.scale.handSize, ink: .pencil)            .fixedSize(horizontal: false, vertical: true)            .accessibilityIdentifier("viewer-absent")        if !removed, let onFetch {            PaperTypedButton(fetching ? (words?.fetching ?? "") : (words?.fetch ?? ""), size: theme.scale.metaSize, action: onFetch)                .disabled(fetching)                .accessibilityIdentifier("absent-fetch")        }    }}/// The poster dimmed under the page's own ground, the slip on it — or the/// slip alone when there is no poster.struct AbsentOverPoster<Poster: View>: View {    let slip: AbsentSlip    @ViewBuilder let poster: Poster    @Environment(\.theme) private var theme    var body: some View {        ZStack {            poster                .overlay(theme.colors.ground.color.opacity(AbsentSlipKnobs.scrim))                .accessibilityHidden(true)            slip.padding(AbsentSlipKnobs.inset)        }    }}enum AbsentSlipKnobs {    static let gap: CGFloat = 10    static let lineGap: CGFloat = 4    static let inset: CGFloat = 16    static let compactVertical: CGFloat = 10    static let glyph: CGFloat = 30    static let compactGlyph: CGFloat = 20    static let maxWidth: CGFloat = 300    /// How much of the ground lies over a poster the slip stands on.    static let scrim: Double = 0.55}