Cotton code
308 lines · 12.5 KB · SwiftRaw
// The sheets over Story's screens, each in Ink: Your scripts (behind the// title, wireframes 1), naming a new script, All views (wireframes 13), and// the short list of acts a press and hold or ⋯ opens. No system lists or// grouped tables: rows under thin dotted rules, words in the two faces.#if os(iOS)import SwiftUI/// Wireframes 1: every script, the current one first; a tap makes it home;/// New script names one.struct InkScriptsSheet: View {    let model: InkStory    @Environment(\.closeCard) private var closeCard    /// Closes the panel this stands in.    private func dismiss() { closeCard?() }    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 18) {                InkSheetTop(title: InkWords.line("home.scripts"), close: InkWords.line("home.close")) { dismiss() }                VStack(spacing: 0) {                    let scripts = model.scripts?.stories ?? []                    ForEach(Array(scripts.enumerated()), id: \.element.id) { at, script in                        Button { Task { await model.choose(script.id) } } label: {                            VStack(alignment: .leading, spacing: 3) {                                Text(script.title.isEmpty ? InkWords.line("fan.untitled") : script.title)                                    .font(InkType.title(22, relativeTo: .title2))                                    .foregroundStyle(script.current ? Ink.blue : Ink.ink)                                    .fixedSize(horizontal: false, vertical: true)                                Text(script.summary)                                    .font(InkType.count)                                    .foregroundStyle(Ink.quiet)                                    .fixedSize(horizontal: false, vertical: true)                            }                            .frame(maxWidth: .infinity, alignment: .leading)                            .padding(.vertical, 14)                            .contentShape(Rectangle())                        }                        .buttonStyle(InkPress())                        .accessibilityElement(children: .combine)                        .accessibilityAddTraits(script.current ? [.isButton, .isSelected] : .isButton)                        if at < scripts.count - 1 { InkRule() }                    }                }                InkFlow(spacing: 10) {                    InkButton(words: InkWords.line("home.new_script")) { model.sheet = .naming }                    InkButton(words: InkWords.line("home.bring_in"), ghost: true) {                        model.sheet = nil                        model.bringing = true                    }                }            }            .padding(.horizontal, Ink.gutter)            .padding(.top, 22)            .padding(.bottom, 26)        }        .scrollIndicators(.hidden)    }}/// A new script's title, asked once; everything else can come later.struct InkNamingSheet: View {    let model: InkStory    @State private var title = ""    @FocusState private var focused: Bool    @Environment(\.closeCard) private var closeCard    /// Closes the panel this stands in.    private func dismiss() { closeCard?() }    var body: some View {        VStack(alignment: .leading, spacing: 18) {            InkSheetTop(title: InkWords.line("home.new_script"), close: InkWords.line("home.close")) { dismiss() }            Text(InkWords.line("stories.first_hint"))                .font(InkType.count)                .foregroundStyle(Ink.quiet)                .fixedSize(horizontal: false, vertical: true)            InkField(hint: InkWords.line("stories.title_hint"), text: $title, focused: $focused, font: InkType.title(22, relativeTo: .title2)) { start() }            HStack {                Spacer()                InkButton(words: InkWords.line("stories.first")) { start() }                    .disabled(title.trimmingCharacters(in: .whitespaces).isEmpty)                    .opacity(title.trimmingCharacters(in: .whitespaces).isEmpty ? 0.4 : 1)            }            Spacer(minLength: 0)        }        .padding(.horizontal, Ink.gutter)        .padding(.top, 22)        .onAppear { focused = true }    }    private func start() {        let named = title.trimmingCharacters(in: .whitespacesAndNewlines)        guard !named.isEmpty else { return }        Task { await model.start(named) }    }}/// Wireframes 13: the one door to every other way of looking at the/// script, titled with its name: the story as a whole (Read), then what is/// in it (Places). A row stands only once its view works.struct InkViewsSheet: View {    let model: InkStory    @Environment(\.closeCard) private var closeCard    /// Closes the panel this stands in.    private func dismiss() { closeCard?() }    var body: some View {        let title = model.home?.story.title ?? ""        ScrollView {            VStack(alignment: .leading, spacing: 18) {                InkSheetTop(title: InkWords.line("ink.views_title", ["title": title.isEmpty ? InkWords.line("fan.untitled") : title]),                            close: InkWords.line("home.close")) { dismiss() }                if let story = model.story, !(model.home?.scenes.isEmpty ?? true) {                    section(InkWords.line("views.story")) {                        InkViewRow(words: InkWords.line("home.read"), detail: model.home?.story.summary) {                            model.sheet = nil                            model.path.append(.read(story: story, at: nil))                        }                    }                }                if let places = model.home?.doors?.first(where: { $0.key == "places" }), let story = model.story {                    section(InkWords.line("ink.views_in_it")) {                        InkViewRow(words: places.words, detail: nil) {                            model.sheet = nil                            model.path.append(.places(story: story, title: places.words))                        }                    }                }            }            .padding(.horizontal, Ink.gutter)            .padding(.top, 22)            .padding(.bottom, 26)        }    }    private func section<Rows: View>(_ caption: String, @ViewBuilder rows: () -> Rows) -> some View {        VStack(alignment: .leading, spacing: 4) {            Text(caption)                .font(InkType.caption)                .foregroundStyle(Ink.quiet)                .accessibilityAddTraits(.isHeader)            rows()        }    }}struct InkViewRow: View {    let words: String    let detail: String?    /// The detail under the words (a sentence), not beside them (a count).    var below = false    let open: () -> Void    /// A detail stands beside the words.    private var beside: Bool { !below && detail != nil }    var body: some View {        Button(action: open) {            HStack(alignment: .firstTextBaseline, spacing: 8) {                VStack(alignment: .leading, spacing: 4) {                    Text(words)                        .font(InkType.sceneTitle)                        .foregroundStyle(Ink.ink)                        .fixedSize(horizontal: false, vertical: true)                    if below, let detail {                        Text(detail)                            .font(InkType.count)                            .foregroundStyle(Ink.quiet)                            .fixedSize(horizontal: false, vertical: true)                    }                }                // A detail beside the words takes the room the words leave: the                // words standing as wide as they could once crushed it to no                // width, so All views' Read and About's Drafts stood as tall                // blank rows with their count unseen.                .frame(maxWidth: beside ? nil : .infinity, alignment: .leading)                .layoutPriority(1)                if beside, let detail {                    Text(detail)                        .font(InkType.sceneLength)                        .foregroundStyle(Ink.quiet)                        .multilineTextAlignment(.trailing)                        .frame(maxWidth: .infinity, alignment: .trailing)                }                Text("›")                    .font(InkType.sceneTitle)                    .foregroundStyle(Ink.quiet)                    .accessibilityHidden(true)            }            .padding(.vertical, 14)            .contentShape(Rectangle())        }        .buttonStyle(InkPress())        .accessibilityElement(children: .combine)        .accessibilityAddTraits(.isButton)    }}/// One act in a short list.struct InkAct: Identifiable {    let id = UUID()    let words: String    /// A taking-away act (Remove, Delete): set in the quiet ink, last.    var quiet = false    /// A quiet line under the words, saying what the act will do.    var detail: String? = nil    /// An identifier for the row, where a test names it.    var tag: String? = nil    let run: () -> Void}private struct InkActTag: ViewModifier {    let tag: String?    func body(content: Content) -> some View {        if let tag { content.accessibilityIdentifier(tag) } else { content }    }}/// What a press and hold or ⋯ opens: a title, a quiet line if it needs one,/// then the acts as rows; a list taller than the screen scrolls.struct InkActsSheet: View {    let title: String    let acts: [InkAct]    /// A sentence under the title saying what choosing will do.    var line: String? = nil    var body: some View {        ViewThatFits(in: .vertical) {            rows            ScrollView { rows }.scrollIndicators(.hidden)        }    }    private var rows: some View {        VStack(alignment: .leading, spacing: 0) {            Text(title)                .font(InkType.sceneTitle)                .foregroundStyle(Ink.quiet)                .fixedSize(horizontal: false, vertical: true)                .padding(.bottom, line == nil ? 6 : 4)                .accessibilityAddTraits(.isHeader)            if let line {                Text(line)                    .font(InkType.count)                    .foregroundStyle(Ink.quiet)                    .fixedSize(horizontal: false, vertical: true)                    .padding(.bottom, 8)            }            ForEach(Array(acts.enumerated()), id: \.element.id) { at, act in                Button(action: act.run) {                    VStack(alignment: .leading, spacing: 4) {                        Text(act.words)                            .font(InkType.mono(16, relativeTo: .body))                            .foregroundStyle(act.quiet ? Ink.quiet : Ink.ink)                            .fixedSize(horizontal: false, vertical: true)                        if let detail = act.detail {                            Text(detail)                                .font(InkType.count)                                .foregroundStyle(Ink.quiet)                                .fixedSize(horizontal: false, vertical: true)                        }                    }                    .frame(maxWidth: .infinity, alignment: .leading)                    .padding(.vertical, 14)                    .contentShape(Rectangle())                }                .buttonStyle(InkPress())                .accessibilityElement(children: .combine)                .accessibilityAddTraits(.isButton)                .modifier(InkActTag(tag: act.tag))                if at < acts.count - 1 { InkRule() }            }        }        .padding(.horizontal, Ink.gutter)        .padding(.top, 24)        .padding(.bottom, 12)        .fixedSize(horizontal: false, vertical: true)    }}/// A one-line field in Ink: the words on a dotted rule, the hint in the rule's colour.struct InkField: View {    let hint: String    @Binding var text: String    var focused: FocusState<Bool>.Binding    var font: Font = InkType.line    let submit: () -> Void    var body: some View {        VStack(alignment: .leading, spacing: 6) {            TextField("", text: $text, prompt: Text(hint).foregroundColor(Ink.rule), axis: .vertical)                .font(font)                .foregroundStyle(Ink.ink)                .focused(focused)                .accessibilityLabel(hint)                .submitLabel(.done)                .onChange(of: text) { _, now in                    // Return on a one-line field ends it.                    if now.contains("\n") {                        text = now.replacingOccurrences(of: "\n", with: "")                        submit()                    }                }            InkRule()        }    }}#endif