Cotton code
145 lines · 6.1 KB · SwiftRaw
// About this script (wireframes 7): the facts about the script and the things// done to it as a whole. Title, Written by and In one line are written in// place (kept as typed and on leaving; a blank title is never kept); Drafts,// with the latest one beside it; Send hands it on as a PDF, a Final Draft or// a Fountain file; Bring in a script makes a new script from a file; Delete// sends it to Deleted (it can come back from there). Film only.#if os(iOS)import CottonKitimport CottonValuesimport SwiftUIstruct InkAboutScreen: View {    let story: InkStory    let script: String    @State private var title = ""    @State private var writtenBy = ""    @State private var logline = ""    @State private var loaded = false    @State private var sending = false    @State private var deleting = false    /// The newest draft, for the Drafts row's detail.    @State private var latest: StoryDraftMark?    @FocusState private var focused: Field?    @Environment(\.dismiss) private var dismiss    enum Field: Hashable { case title, writtenBy, logline }    var body: some View {        VStack(spacing: 0) {            HStack(spacing: 10) {                InkGlyph(symbol: "chevron.left", label: InkWords.line("writer.back")) { save(); dismiss() }                Text(InkWords.line("about.title"))                    .font(InkType.barTitle)                    .foregroundStyle(Ink.ink)                    .multilineTextAlignment(.center)                    .frame(maxWidth: .infinity)                    .accessibilityAddTraits(.isHeader)                Color.clear.frame(width: 44, height: 44)            }            .padding(.horizontal, 8)            .padding(.top, 6)            ScrollView {                VStack(alignment: .leading, spacing: 22) {                    fact(InkWords.line("about.title_label"), text: $title, field: .title, font: InkType.title(24, relativeTo: .title2))                    fact(InkWords.line("script.title_page_by"), text: $writtenBy, field: .writtenBy, font: InkType.line)                    fact(InkWords.line("about.logline"), text: $logline, field: .logline, font: InkType.line)                    VStack(spacing: 0) {                        InkViewRow(words: InkWords.line("ink.drafts"), detail: latest.map(InkDraftsScreen.latest)) {                            save()                            story.path.append(.drafts(script))                        }                        InkRule()                        InkViewRow(words: InkWords.line("about.send"), detail: nil) {                            save()                            sending = true                        }                        InkRule()                        InkViewRow(words: InkWords.line("home.bring_in"), detail: InkWords.line("ink.bring_in_line"), below: true) {                            save()                            story.bringing = true                        }                    }                    InkButton(words: InkWords.line("about.delete"), ghost: true) { deleting = true }                        .padding(.top, 8)                }                .padding(.horizontal, Ink.gutter)                .padding(.top, 16)                .padding(.bottom, 26)            }        }        .inkGround()        .toolbar(.hidden, for: .navigationBar)        .onChange(of: focused) { _, _ in save() }        .onChange(of: title) { _, _ in keepSoon() }        .onChange(of: writtenBy) { _, _ in keepSoon() }        .onChange(of: logline) { _, _ in keepSoon() }        .onDisappear { save() }        .lookPanel(isPresented: $sending, style: Ink.panel) {            InkSendSheet(story: story, script: script, title: story.home?.story.title ?? title)        }        .lookPanel(isPresented: $deleting, style: Ink.panel) {            InkActsSheet(title: InkWords.line("about.delete_ask"), acts: [                InkAct(words: InkWords.line("about.delete"), quiet: true) {                    deleting = false                    Task { await story.deleteScript(script) }                },            ])        }        .task {            if !loaded, let facts = story.home?.story, facts.id == script {                title = facts.title                writtenBy = facts.writtenBy ?? ""                logline = facts.logline ?? ""                loaded = true            }            latest = (try? await story.shelf.drafts(story: script))?.first        }    }    private func fact(_ label: String, text: Binding<String>, field: Field, font: Font) -> some View {        VStack(alignment: .leading, spacing: 6) {            Text(label)                .font(InkType.caption)                .foregroundStyle(Ink.quiet)                .accessibilityHidden(true)            TextField("", text: text, axis: .vertical)                .font(font)                .foregroundStyle(Ink.ink)                .focused($focused, equals: field)                .accessibilityLabel(label)            InkRule()        }    }    /// Typing keeps the facts a moment after the last key.    @State private var typing: Task<Void, Never>?    private func keepSoon() {        guard loaded else { return }        typing?.cancel()        typing = Task {            try? await Task.sleep(for: .milliseconds(700))            guard !Task.isCancelled else { return }            save()        }    }    private func save() {        guard loaded, let shown = story.home?.story, shown.id == script else { return }        let named = title.trimmingCharacters(in: .whitespacesAndNewlines)        // A blank title is never kept: the one shown stays.        let keptTitle = named.isEmpty ? shown.title : named        let by = writtenBy.trimmingCharacters(in: .whitespacesAndNewlines)        let line = logline.trimmingCharacters(in: .whitespacesAndNewlines)        // Nothing changed: nothing written.        guard keptTitle != shown.title || by != (shown.writtenBy ?? "") || line != (shown.logline ?? "") else { return }        let facts = StoryFacts(title: keptTitle, writtenBy: by, logline: line)        Task {            await story.attempt { try await story.book.writeFacts(story: script, facts) }            await story.reloadHome()        }    }}#endif