Cotton code
198 lines · 9.4 KB · SwiftRaw
// THE REPORT IN AN APP'S OWN LOOK (owner, 22 Nov 2026): in Words, Days,// Story and Comms a shake raises one question, "What looks wrong?", with one// field (the keyboard comes up, and the keyboard's own dictation works in it)// and Send, live once something is written. Nothing else is asked or chosen:// no project, no picture to look at, no microphone of the app's own (Android's// sheet has none). The picture of the screen and the app's recent log still go,// said by one small line. Send is the paper sheet's own (`ShakeReportModel.send`,// the same `feedback.send` and the same request); what became of it is said in// the app's toast as the panel closes. A pull down, a tap above, or Cancel// closes it, and the picture goes with it.//// The panel is shaped as the app's other panels (`LookPanel`): Story's and// Days' rise from the foot with the screen's 28 pt corner (Days' with its// grabber), Comms' with its 26 pt corner, Words' floats clear of the edges with// its 18 pt corner. The words, the field and the buttons are drawn in the app// look the root hands the shell (`\.vaultLook`, `AppLook.swift`). Apps on paper// keep the paper sheet (`ShakeReportSheet`).#if os(iOS)import SwiftUIimport UIKitextension VaultLook {    /// The panel the report rises in, shaped as this app's other panels, and    /// whether it carries a grabber at its head (Days').    @MainActor func reportPanel(_ scheme: ColorScheme) -> (style: LookPanelStyle, grabber: Bool) {        let ground = colour("sheet", scheme)        if NotesRoot.applies {            return (LookPanelStyle(scrim: Color.black.opacity(0.28), ground: ground, corner: 18, inset: 10), false)        }        if InkStoryRoot.applies { return (Ink.panel, false) }        if DaysRoot.applies { return (Pen.panel, true) }        if ChatRoot.applies {            return (LookPanelStyle(scrim: colour("text", scheme).opacity(0.28), ground: colour("background", scheme), corner: 26), false)        }        return (LookPanelStyle(scrim: colour("scrim", scheme), ground: ground, corner: corner("sheet")), false)    }}/// The root's raise: while a shake is staged in an app with a look, the/// report stands in that look's panel. The root's environment is handed in/// again (`Hosted` reads the open database, the answers and the toast centre),/// as the paper card's is.struct LookReportRaise: ViewModifier {    @Binding var staged: ShakeStaged?    let look: VaultLook?    let core: any CottonCalling    let answers: AnswerCache    @Environment(\.colorScheme) private var scheme    func body(content: Content) -> some View {        // One shape whatever the look: Vault's and Health's arrive after the        // first frame, and a branch here would rebuild the whole app under it.        let panel: (style: LookPanelStyle, grabber: Bool) = look?.reportPanel(scheme) ?? (LookPanelStyle(scrim: .clear, ground: .clear), false)        content.lookPanel(item: Binding(get: { look == nil ? nil : staged }, set: { staged = $0 }),                          style: panel.style, name: ShellLines.line("feedback", "report.title")) { shaken in            if let look {                Hosted(unwired: { core, toasts in                    ShakeReportModel(writer: ScreenWriter(core: core), toasts: toasts, staged: shaken)                }) { model in                    LookReportSheet(model: model, look: look, grabber: panel.grabber) { staged = nil }                }                .id(shaken.id)                .environment(\.cottonCore, core)                .environment(\.cottonAnswers, answers)            }        }    }}/// The question, the field, the line saying what goes with it, Cancel and Send.struct LookReportSheet: View {    @Bindable var model: ShakeReportModel    let look: VaultLook    var grabber = false    let close: () -> Void    @FocusState private var writing: Bool    @Environment(\.colorScheme) private var scheme    @Environment(\.dynamicTypeSize) private var textSize    private var title: String { ShellLines.line("feedback", "report.title") }    /// The written part's own height: the panel is as tall as it, and scrolls    /// past it only where the room above the keyboard is shorter.    @State private var partsHigh: CGFloat = 0    var body: some View {        VStack(spacing: 0) {            if grabber {                // Days' grabber, as its other panels draw it (`daysSheet`).                Capsule().fill(Pen.faint).frame(width: 36, height: 5)                    .padding(.top, 6).padding(.bottom, 5)                    .frame(maxWidth: .infinity)                    .accessibilityHidden(true)            }            ScrollView {                parts.onGeometryChange(for: CGFloat.self) { $0.size.height } action: { partsHigh = $0 }            }            .frame(maxHeight: partsHigh > 0 ? partsHigh : nil)            .scrollBounceBehavior(.basedOnSize)            // Cancel and Send stand under it, always above the keyboard.            HStack(spacing: look.gap("m")) {                VaultButton(label: CottonStrings.cancel, fill: .plain) { close() }                    .disabled(model.stage == .sending)                    .accessibilityIdentifier("report-cancel")                Spacer(minLength: 0)                VaultButton(label: ShellLines.line("feedback", model.stage == .sending ? "report.sending" : "report.send"), tone: .accent) {                    Task { if await model.sendAndSay() { close() } }                }                .disabled(!model.canSend)                .opacity(model.canSend || model.stage == .sending ? 1 : 0.4)                .accessibilityIdentifier("report-send")            }            .padding(.horizontal, look.gap("gutter"))            .padding(.top, look.gap("s"))            .padding(.bottom, look.gap("l"))        }        .modifier(KeyboardClear())        .environment(\.vault, look)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("report-sheet")        .onAppear { writing = true }        // However it closed (Cancel, the pull, a tap above), a report never        // sent leaves nothing behind: the picture taken at the shake goes.        .onDisappear { if model.stage == .writing { model.cancel() } }    }    private var parts: some View {        VStack(alignment: .leading, spacing: look.gap("l")) {            Text(title)                .vault(look, "title")                .fixedSize(horizontal: false, vertical: true)                .accessibilityAddTraits(.isHeader)                .accessibilityIdentifier("report-title")            TextField(text: $model.note, axis: .vertical) { EmptyView() }                .font(look.font("body", at: textSize))                .foregroundStyle(look.colour("text", scheme))                .tint(look.colour("accent", scheme))                .lineLimit(3...8)                .focused($writing)                .padding(.horizontal, look.gap("m"))                .padding(.vertical, look.gap("m"))                .background(RoundedRectangle(cornerRadius: look.corner("field"), style: .continuous).fill(look.colour("field", scheme)))                .disabled(model.stage != .writing)                .accessibilityLabel(title)                .accessibilityIdentifier("report-note")            Text(ShellLines.line("feedback", model.staged.snapshot == nil ? "report.goes_with.log" : "report.goes_with.screen_and_log"))                .vault(look, "row_detail", "secondary")                .fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("report-goes-with")        }        .padding(.horizontal, look.gap("gutter"))        .padding(.top, grabber ? look.gap("s") : look.gap("xl"))        .padding(.bottom, look.gap("s"))        .frame(maxWidth: .infinity, alignment: .leading)    }}/// Keeps the panel's foot above the keyboard. The root the report is raised/// over does not give the keyboard's room to its overlays (Story's home holds/// its own place under the keyboard), so the panel reads the keyboard's top/// and stands on it: no gap where the window already gave the room.private struct KeyboardClear: ViewModifier {    @State private var keyboardTop: CGFloat = .infinity    @State private var foot: CGFloat = 0    func body(content: Content) -> some View {        content            .padding(.bottom, max(0, foot - keyboardTop))            .onGeometryChange(for: CGFloat.self) { $0.frame(in: .global).maxY } action: { foot = $0 }            .ignoresSafeArea(.keyboard)            .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillChangeFrameNotification)) { note in                guard let end = note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }                keyboardTop = end.height > 0 ? end.minY : .infinity            }            .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in                keyboardTop = .infinity            }    }}#endif#if COTTON_DEVimport Foundation/// DEVELOPER TOOL: `-report-sheet-open YES` asks the root to shake once, the/// first time the file is open, so a UI test can open the report.@MainActor enum ReportSeed {    private static var shaken = false    static func asked(_ arguments: [String] = ProcessInfo.processInfo.arguments) -> Bool {        guard !shaken, let at = arguments.firstIndex(of: "-report-sheet-open"), at + 1 < arguments.count,              arguments[at + 1] == "YES" else { return false }        shaken = true        return true    }}#endif