Cotton code
57 lines · 2.3 KB · SwiftRaw
// The voice split on Home: a line spoken to the record button, run (`SpokenHome` says where it// goes; this does it): the fan turns, a page opens, another database is// chosen, the record on top comes down, or a command's record opens. False// when the line goes nowhere, and the dock keeps it as a thought.import CottonKitimport SwiftUI@MainActorstruct HomeVoice {    let definitions: TableDefinitions    /// The fan's tiles; empty on a grid Home.    let fan: [(key: String, name: String)]    /// The databases a spoken line may choose, as their caps name them.    let choosable: [(id: String, name: String)]    let core: (any CottonCalling)?    let navigator: Navigator?    /// The tile the fan turns to.    let show: Binding<String?>?    let scene: DatabaseScene?    let databases: DatabasesModel?    /// Runs the line; false when it goes nowhere (a thought).    func run(_ words: String) async -> Bool { await did(words) != nil }    /// Runs the line and says what happened, in the thought definition's    /// voice lines ("Opened People"); nil when the line goes nowhere.    func did(_ words: String) async -> String? {        let lines = VoiceWords.reading(definitions)        switch await SpokenHome.goes(            words, definitions: definitions, fan: fan, databases: choosable,            find: { [core] in await SpokenHome.find($0, core: core) },            command: { [core] in await SpokenHome.command($0, core: core) })        {        case .turn(let key)?:            // Whatever was pushed over Home closes; each card keeps its own stack.            navigator?.popToRoot()            show?.wrappedValue = key            return lines.opened(fan.first { $0.key == key }?.name ?? key)        case .open(let route, let name)?:            navigator?.open(route)            return lines.opened(name)        case .database(let id, let tile)?:            // The tile turns once the chosen database's fan is up.            show?.wrappedValue = tile            await DatabaseCapRow.choose(id, scene: scene, in: databases)            return lines.opened(choosable.first { $0.id == id }?.name ?? id)        case .back?:            navigator?.pop()            return lines.back        case .ran(let made)?:            if let made { navigator?.open(made) }            return lines.ran        case nil: return nil        }    }}