Cotton code
236 lines · 9.9 KB · SwiftRaw
// The + fan — the dock's DETERMINISTIC lane, as a sheet.//// The rows themselves are `AddFanActions.swift`; this file is only how they are// drawn and what a tap does.//// The pill is the natural-language lane: you say the thought and a model// decides what it becomes. This is the other half — you already know what you// are making, so you say so, and nothing is inferred. Eleven entries, each onto// a creation surface the app already has.//// A compact GRID rather than a list: short nouns read faster side by side than// stacked, so the fan stays one glance and one tap instead of becoming a menu.// Three columns, a 48pt disc and the noun under it. The glyphs are lucide names// in ink — no emoji, no colour (spec, Vocabulary & glyphs).//// A row usually PUSHES. One kind of row opens a sheet the HOST owns instead,// because not every creation surface is a route: a project's memory sentence is// written where the project is, and a host that cannot open that sheet is never// offered the row at all (`canAddMemory`).//// ## What did NOT port, and why the sheet is smaller here//// The component that came before was a `BottomSheetModal` wrapped in a// form-sheet helper, and the WHOLE BODY of that wrapper was// a list of gorhom defects being held down — an unmounted-while-closed backdrop,// a keyboard that is not blurred on close, an `accessible` container that// collapses the content into one VoiceOver element, theme variables re-applied// because a portal escapes the provider. None of them exist here: `cottonSheet`// is a SwiftUI `.sheet` in the app's own dress, so the wrapper retires with the// library it was written for (`Chrome/Sheets.swift` says the same thing from// the other side).//// Two more of that file's decisions are gorhom's rather than the product's, and// are deliberately absent:////   • **The modal-vs-inline choice.** The old fan had to be a `BottomSheetModal`//     because the dock is a thumb-height strip inside a transform that rides the//     keyboard, and an inline sheet would lay itself out inside THAT and open a//     few points tall. A SwiftUI sheet is presented by the window, so there is//     no such choice to make.////   • **The 400 ms post-`pathname` orphan sweep.** In gorhom a `dismiss()`//     issued while the present animation is still running is SWALLOWED, so a//     fast tap could navigate and leave the sheet standing over the destination//     with `open` already false; the old file watched the route and swept that//     orphan up a frame later. That is a defect workaround, not behaviour —//     SwiftUI has no swallowed-dismiss race, and porting the timer would import//     a bug's shadow into an app that never had the bug. It is NOT ported.//// WHAT DOES survive is the ordering that file was careful about: the fan// closes BEFORE it acts. A sheet still standing over the screen it sent you to// is the surface refusing to let go of a decision already made.import CottonKitimport SwiftUI/// Three across. Named because the number is the reason the fan is a grid at/// all — see the header.public let addFanColumns = 3/// The disc behind a row's glyph, and the glyph's own size.public let addFanDiscDiameter: CGFloat = 48public let addFanGlyphSize: CGFloat = 20/// The fan's content — the sheet's body, without the sheet.////// A view of its own rather than a closure inside the modifier below, so a/// gallery can draw it and a test can build it without presenting anything.public struct AddFanSheet: View {    private let actions: [AddFanAction]    private let onChoose: (AddFanDestination) -> Void    /// The accessibility-identifier stem — the dock's own stem with `-fan`    /// appended, which for the one host that exists is this default.    private let identifier: String    @Environment(\.cottonHaptics) private var haptics    @Environment(\.dismiss) private var dismiss    @Environment(\.closeCard) private var closeCard    /// - Parameters:    ///   - today: the day key the Event row anchors on.    ///   - canAddMemory: the host can open its own creation sheet — which is    ///     what earns the Memory row. Absent, the fan offers ten entries, so a    ///     row is never drawn with nothing behind it.    ///   - onChoose: what to do with the chosen row. The fan has already shut    ///     by the time this is called.    ///   - canAttachPictures: the host holds a draft a picture can go on — the    ///     Picture row, and the Camera row where this machine has one.    public init(        today: String,        canAddMemory: Bool = false,        canAttachPictures: Bool = false,        identifier: String = "dock-fan",        onChoose: @escaping (AddFanDestination) -> Void    ) {        self.actions = addFanActions(            today: today,            canAddMemory: canAddMemory,            canAttachPictures: canAttachPictures,            // A capability, read where the rows are built — never a platform            // branch (`Camera`'s own rule).            hasCamera: canAttachPictures && Camera.isAvailable        )        self.identifier = identifier        self.onChoose = onChoose    }    public var body: some View {        VStack(alignment: .leading, spacing: 16) {            Text(ShellLines.line("capture", "composer.make_something"))                .cottonType(.screenTitle)                .accessibilityAddTraits(.isHeader)            LazyVGrid(                columns: Array(                    repeating: GridItem(.flexible(), spacing: 0, alignment: .top),                    count: addFanColumns                ),                spacing: 12            ) {                ForEach(actions) { action in                    cell(action)                }            }        }        .padding(.vertical, 8)        .accessibilityIdentifier(identifier)    }    private func cell(_ action: AddFanAction) -> some View {        Button {            haptics.select()            // Shut FIRST, then act — the ordering that matters, and the one            // decision here that was never about the library.            CardClosing.close(closeCard, else: dismiss)            onChoose(action.destination)        } label: {            VStack(spacing: 8) {                ZStack {                    Circle()                        .fill(CottonColors.chipGround)                        .frame(width: addFanDiscDiameter, height: addFanDiscDiameter)                    // An unmapped lucide name draws NOTHING rather than a wrong                    // picture — `Glyph`'s own rule, and its test is what keeps                    // this branch unreachable.                    if let symbol = Glyph.symbol(action.icon) {                        Image(systemName: symbol)                            .font(.system(size: addFanGlyphSize, weight: .regular))                            .foregroundStyle(CottonColors.inkSecondary)                    }                }                Text(action.label)                    .cottonType(.caption, ink: .ink, weight: .medium)                    .multilineTextAlignment(.center)            }            .frame(maxWidth: .infinity)            .padding(.vertical, 8)        }        .buttonStyle(MotionButtonStyle())        .accessibilityLabel(action.label)        .accessibilityIdentifier("\(identifier)-\(action.key)")    }}extension View {    /// Present the + fan in the app's own sheet dress.    ///    /// `cottonSheet` with the default `.content` sizing: the fan is as tall as    /// its rows and no taller, which is what `enableDynamicSizing` said on the    /// phone.    ///    /// **`onChoose` fires when the fan is OFF THE SCREEN, not when the row is    /// tapped.** A row's whole job is to take the person somewhere else, and    /// several of those somewheres raise a system sheet of their own the moment    /// they arrive — the recordings picker is one (`/meetings?import=1`). iOS    /// refuses a presentation over a sheet that is still dismissing, silently,    /// and never calls the refused presentation back: the walk of 15 Sep 2026    /// got no picker at all, and then no picker from the plus circle either,    /// until the app was restarted. So the choice is REMEMBERED, the fan comes    /// down, and the row acts once the screen is clear.    public func addFanSheet(        isPresented: Binding<Bool>,        today: String,        canAddMemory: Bool = false,        canAttachPictures: Bool = false,        identifier: String = "dock-fan",        onChoose: @escaping (AddFanDestination) -> Void    ) -> some View {        modifier(            AddFanSheetModifier(                isPresented: isPresented,                today: today,                canAddMemory: canAddMemory,                canAttachPictures: canAttachPictures,                identifier: identifier,                onChoose: onChoose            )        )    }}/// The fan, and the one piece of state that makes "after it is down" possible.private struct AddFanSheetModifier: ViewModifier {    @Binding var isPresented: Bool    let today: String    let canAddMemory: Bool    let canAttachPictures: Bool    let identifier: String    let onChoose: (AddFanDestination) -> Void    /// What was tapped, held until the sheet has gone. Cleared as it is taken,    /// so a fan opened and swiped away acts on nothing.    @State private var chosen: AddFanDestination?    func body(content: Content) -> some View {        content.cottonCard(            isPresented: $isPresented,            onDismiss: {                guard let destination = chosen else { return }                chosen = nil                onChoose(destination)            }        ) {            AddFanSheet(                today: today,                canAddMemory: canAddMemory,                canAttachPictures: canAttachPictures,                identifier: identifier,                onChoose: { destination in                    chosen = destination                    isPresented = false                }            )        }    }}