// A spoken command's answer, carried out by the shell: a record that // resolved opens where a tap would open it; words that named none open the // deck's Find with them, the same search the ink feeds. import CottonKit import CottonVoice import Foundation import SwiftUI @MainActor public enum VoiceCommands { /// Posted with `userInfo["query"]`: the deck shown opens Find with it. public static let search = Notification.Name("cotton.voice.search") /// `search`: where the command was said holds a search of its own /// (Home's, `voiceSearch`); without one the deck shown opens Find. public static func run(_ command: VoiceCommand, navigator: Navigator?, search: (@MainActor (String) -> Void)? = nil) { if command.kind == "open", let table = command.table, let id = command.id { navigator?.open(table: table, id: id, definition: nil) return } guard !command.query.isEmpty else { return } if let search { return search(command.query) } NotificationCenter.default.post(name: Self.search, object: nil, userInfo: ["query": command.query]) } } /// Home's search, handed to the fan Home draws: a spoken `search` command /// said there opens it with the words. Nil under a deck's fan. private struct VoiceSearchKey: EnvironmentKey { static let defaultValue: (@MainActor (String) -> Void)? = nil } extension EnvironmentValues { var voiceSearch: (@MainActor (String) -> Void)? { get { self[VoiceSearchKey.self] } set { self[VoiceSearchKey.self] = newValue } } }