Cotton code
97 lines · 3.7 KB · SwiftRaw
// HomeInput mounts one voice dock; this says, over the fan, what became of// the words the box sent: a kept thought's receipt with its Undo.import CottonKitimport CottonPaperimport SwiftUIstruct FanRecord: View {    let dock: DockModel    /// The strip's lines.    let words: VoiceWords    /// The thought the last send kept, while its receipt stands.    let kept: ThoughtFiled?    let undone: Bool    /// Takes the kept thought back; nil once it is taken back or while busy.    let undo: (() -> Void)?    /// The receipt's time is up.    let onDismiss: () -> Void    var body: some View {        Color.clear            .frame(width: 0, height: 0)            .preference(key: FanToastKey.self, value: toast)            // A receipt stands for a receipt's time.            .task(id: kept?.id) {                guard kept != nil else { return }                try? await Task.sleep(for: .seconds(ToastWindow.receipt))                if !Task.isCancelled { onDismiss() }            }    }    /// What the toast says: nothing while the dock listens; a kept    /// thought's receipt, with its Undo, for a toast's time; else nothing.    private var toast: FanToast? {        guard !dock.isListening, kept != nil else { return nil }        let act = undo.map { FanToast.Act(label: words.undo, run: $0) }        return FanToast(words: undone ? words.undone : words.saved, act: act)    }}/// The hub closes the open tier; at the root it is the fan.enum FanButton: Equatable {    case closesEdit, closesWhole, fan    static func of(editing: Bool, inWhole: Bool) -> FanButton {        if editing { return .closesEdit }        if inWhole { return .closesWhole }        return .fan    }}/// A nested fan's close: the acts' on the cream paper with the ink X, a/// value row's on the red with the paper's X. The record button is it while/// a card is edited or a whole's parts are dealt (`FanButton`); a fan with no/// record button under it — a customer's (`CustomerShell`) — stands the/// edit's just above the fan (`FanHome.closesEdits`).struct FanClose: View {    let size: CGFloat    /// The fan holds a card's acts or a whole's parts, not a value row.    let acts: Bool    let close: () -> Void    /// The hub's own id where the X is the fan's root close (`shell.fan`),    /// not a tier's.    var identifier = "fan-record-close"    @Environment(\.theme) private var theme    var body: some View {        Button { withAnimation(.easeOut(duration: Double(theme.scale[.fanTurnMsMin]) / 1000)) { close() } } label: {            ZStack {                Circle()                    .fill(acts ? NavChrome.circleFill : theme.colors.accent.color)                    .frame(width: size, height: size)                    .softShadow()                FanCloseMark()                    .stroke(acts ? NavChrome.glyphInk : theme.colors.accentContrast.color,                            style: StrokeStyle(lineWidth: size * 0.035, lineCap: .round))                    .frame(width: size * theme.scale[.fanRecordGlyph] * 0.75,                           height: size * theme.scale[.fanRecordGlyph] * 0.75)            }            .contentShape(Circle())        }        .buttonStyle(MotionButtonStyle())        .accessibilityLabel(CottonStrings.close)        .accessibilityIdentifier(identifier)    }}private struct FanCloseMark: Shape {    func path(in rect: CGRect) -> Path {        Path { path in            path.move(to: CGPoint(x: rect.minX, y: rect.minY))            path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))            path.move(to: CGPoint(x: rect.maxX, y: rect.minY))            path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))        }    }}