Cotton code
51 lines · 2.2 KB · SwiftRaw
// Search finds the device and database pages (UI direction, Cotton note// 0a22168c §3): a page whose name, or one of whose columns' names, holds the// typed words joins Find's places rail beside the tools, and picking it opens// that page — "notif" finds This phone's Notifications, "colour" the// database's Cap colour.//// The words are the pages' OWN, read from their definitions (`device.json`'s// columns on this device's row, `database.json`'s), so there is no second// list of settings to keep in step with what the pages draw.import CottonKitimport Foundationenum PageFind {    static let keyPrefix = "page-"    static let icon = "settings-2"    /// One place: the words it is found by and the page it opens.    struct Place: Equatable, Sendable {        let key: String        let label: String        let route: Route    }    /// The device page's and the database page's places, each page first by    /// its own name, then its columns by theirs.    static func places(_ definitions: TableDefinitions) -> [Place] {        var places = [Place(key: "device", label: HostNoun.titleCase, route: .device)]        if let device = definitions["device"] {            places += device.columns                .filter { $0.shownWhen?.values == ["this device"] }                .compactMap { column in column.name.map { Place(key: "device-\(column.key)", label: $0, route: .device) } }        }        places.append(Place(key: "database", label: ShellLines.line("search", "place.database"), route: .database))        if let database = definitions["database"] {            places += database.columns.compactMap { column in                column.name.map { Place(key: "database-\(column.key)", label: $0, route: .database) }            }        }        return places    }    /// The places whose label holds `needle` (already lower-cased), as chips.    static func chips(matching needle: String, in places: [Place]) -> [SearchPlaceChip] {        places.filter { $0.label.lowercased().contains(needle) }.map { place in            SearchPlaceChip(                key: keyPrefix + place.key, label: place.label, icon: icon,                tool: FindTool(key: keyPrefix + place.key, name: place.label, icon: icon, path: place.route.path))        }    }}