Cotton code
239 lines · 11.1 KB · SwiftRaw
// Home's tiles, the figures under them, and the places found by name.//// The app ships no tool (owner's ruling, 21 Oct 2026: tools are records// only). Every tool it shows is a package record (`ToolDefinition`, from// `tools.list`), drawn as a data tile; `builtin` on a table definition says// only that its schema is code, never that it is a tool the app ships. The// few places found by name that are no table's tool — Projects, About, the// Tools screen — are said in `Resources/Places/places.json`.//// Every figure obeys **figure or silence**: a count appears only when it is// the number that would make you open the thing, and it says what it counts// (`TableToolFigure.words`). Every zero is silence.import CottonKitimport Foundation/// One row Find matches: a package tool or a place.////// `path` is a route PATH STRING, not a `Route` case, and deliberately: the/// design-system gallery renders/// them with nothing to navigate, and a value type that cannot be built without/// the router is a value type the tests have to launch an app to see.public struct FindTool: Sendable, Equatable {    public let key: String    /// The proper noun the user reads.    public let name: String    /// kebab-case lucide glyph — never an emoji.    public let icon: String    public let path: String    /// The figure sentence, or `nil` for silence.    public let figure: String?    public init(key: String, name: String, icon: String, path: String, figure: String? = nil) {        self.key = key        self.name = name        self.icon = icon        self.path = path        self.figure = figure    }}extension FindTool: Identifiable {    /// The tool's own key. A row is a TOOL, not a position: the arrange grid    /// re-seats the same objects rather than replacing them, and a `ForEach`    /// keyed by offset would give a moved object a new identity mid-drag.    public var id: String { key }}/// One tile on Home: a VIEW of a tool (owner's ruling, 12 Oct 2026 — "a/// tile points at a view"), and whether it has nothing to show. An empty tile/// is drawn faint, after the ones that hold something, until it holds a record.public struct HomeTile: Sendable, Equatable, Identifiable {    /// The view's id: `tool/<key>` (a tool's list), `<tool>/<table>` (one    /// table of a tool with several), `ready/today`, `chart/mood`,    /// `<table>/<groupId>` (a group), or `ready/everything`.    public let id: String    public let empty: Bool    /// The tool whose picture the tile wears; nil for Everything.    public let tool: String?    /// The view's own name ("Today", a group's); nil names it by its tool.    public let name: String?    /// The path the tile opens, when the view's definition names one.    public let opens: String?    /// The table whose list the tile opens — one table of a tool with    /// several. Said by the core, never parsed from the id, which has a    /// group's shape.    public let table: String?    public init(id: String, empty: Bool = false, tool: String? = nil, name: String? = nil, opens: String? = nil, table: String? = nil) {        self.id = id        self.empty = empty        self.tool = tool ?? HomeTiles.toolKey(of: id)        self.name = name        self.opens = opens        self.table = table    }    /// The key the tile is drawn under: its tool's for the tool's own list,    /// Everything's, else the view itself — one tile per view, so two views of    /// one tool are two tiles.    public var key: String { HomeTiles.tileKey(of: id) ?? id }}/// Home's tiles for the shown database, as `read.home`'s `arrangement`/// answers them (`crates/cotton-core/src/rules/home_arrangement.rs`).////// There is no "put away" (owner's ruling, 12 Oct 2026): a tool is in a/// database or it is not, and Home is an ordered list of view ids. The crate/// answers `tiles` ALREADY in draw order — the ones with something to show in/// the person's order, then the empty ones — and `viewIds`, the person's own/// order. The app only reads it: the arrangement is set over MCP alone/// (owner, 16 Oct 2026), never by a control in the app.public struct HomeTiles: Sendable, Equatable {    /// The tiles in draw order.    public let tiles: [HomeTile]    /// The person's order, as MCP last set it.    public let viewIds: [String]    /// The database's tools, by key, in the person's order.    public let tools: [String]    public init(tiles: [HomeTile], viewIds: [String]? = nil, tools: [String]? = nil) {        self.tiles = tiles        self.viewIds = viewIds ?? tiles.map(\.id)        self.tools = tools ?? tiles.compactMap { Self.toolKey(of: $0.id) }    }    /// Everything's view: a shipped view over every table, not a tool.    public static let everything = "ready/everything"    private static let toolPrefix = "tool/"    /// A tool's own view — its list.    public static func view(ofTool key: String) -> String { toolPrefix + key }    /// The tool whose list `view` is; nil for any other view.    public static func toolKey(of view: String) -> String? {        guard view.hasPrefix(toolPrefix) else { return nil }        let key = String(view.dropFirst(toolPrefix.count))        return key.isEmpty ? nil : key    }    /// The key a tool's list or Everything is drawn under — the tool's own,    /// or Everything's; nil for any other view, which is drawn under its id.    public static func tileKey(of view: String) -> String? {        toolKey(of: view) ?? (view == everything ? HomeData.everythingKey : nil)    }    /// The tools Home's tiles wear (Everything's key for Everything), each    /// once, in draw order — what the board lays out before it places a tile    /// per view.    public var keys: [String] {        var seen: Set<String> = []        return tiles.compactMap { tile in            let key = tile.id == Self.everything ? HomeData.everythingKey : tile.tool            return key.flatMap { seen.insert($0).inserted ? $0 : nil }        }    }    /// Of the tiles `drawn`, by key, the ones hidden from the fan: drawn    /// grey. Grey says one thing — this tool is not on the fan — never that    /// it holds nothing yet; a tool on the fan is never grey, empty or not    /// (walker pass 4, 16 Oct 2026: two tools greyed on the    /// Tools grid while their fan cards stood in colour).    public func hiddenFromFan(_ drawn: [String]) -> Set<String> {        let onFan = Set(tiles.map(\.key) + keys)        return Set(drawn.filter { !onFan.contains($0) })    }    /// Whether `view` is a tile on Home.    public func shows(_ view: String) -> Bool { viewIds.contains(view) }    /// The `arrangement` block of `read.home`; nil, or a block with no    /// `tiles`, holds no tile. There is no order of the app's own to fall    /// back on: a database's tiles are its answer's, or none (295 A, walker294:    /// the bundle's built-ins by name were drawn as an arrangement, then    /// re-dealt).    public init(answer arrangement: JSONValue?) {        self = HomeTiles(arranged: arrangement) ?? HomeTiles(tiles: [])    }    /// The `arrangement` block of `read.home`, or nil when there is none or    /// it has no `tiles` — no arrangement to draw. Keys the app does not draw    /// (the core's `offers`) are passed over.    public init?(arranged arrangement: JSONValue?) {        guard let arrangement, arrangement["tiles"]?.array != nil else { return nil }        let tiles = arrangement.list("tiles").compactMap { tile -> HomeTile? in            guard let id = tile.text("id") else { return nil }            return HomeTile(                id: id, empty: tile.flag("empty"), tool: tile.text("tool"), name: tile.text("name"),                opens: tile.text("opens"), table: tile.text("table"))        }        self.init(tiles: tiles, viewIds: arrangement.strings("viewIds"), tools: arrangement.strings("tools"))    }}public enum HomeData {    // ── Where a place is said ─────────────────────────────────────────────    /// The places found by name that are no table's tool    /// (`Resources/Places/places.json`), in that file's order. A place is    /// the app's own screen; a tool is a package record and never one of    /// these (owner's ruling, 21 Oct 2026: tools are records only).    static let places: [TableTool] = places(from: CottonUIResources.bundle)    static func places(from bundle: Bundle?) -> [TableTool] {        guard let url = bundle?.url(forResource: "places", withExtension: "json", subdirectory: "Places"),              let data = try? FixtureDefinitions.readBundledJSON(url),              let tools = try? JSONDecoder().decode([TableTool].self, from: data)        else { return [] }        return tools    }    /// A place as a row: its key, its name, its glyph, its route.    static func row(of place: TableTool) -> FindTool {        FindTool(            key: place.key ?? place.name, name: place.name, icon: place.icon ?? "table",            path: place.route ?? "/")    }    // ── What Find matches ─────────────────────────────────────────────────    /// The shown database's package tools, then the places — what Find    /// matches a typed name against. A tool's row is its record: the key is    /// its id, the name its name, the glyph its home table's `tool` icon and    /// the path the one route rule a tile tap follows    /// (`BoardLayout.route(for:in:)`). The app ships no tool of its own, so    /// with no tools in hand Find matches the places alone.    public static func findTools(_ tools: [ToolDefinition], definitions: TableDefinitions) -> [FindTool] {        let rows = tools.map { tool in            FindTool(                key: tool.id, name: tool.name, icon: BoardLayout.home(for: tool, in: definitions)?.tool?.icon ?? "table",                path: BoardLayout.route(for: tool, in: definitions, tools: tools).path)        }        return rows + places.map { row(of: $0) }    }    /// The one place with this key, or `nil` — a key from a later build, or    /// one that names no place. A tool is found among the package records    /// the caller holds (`findTools`), never here.    public static func toolPlace(_ key: String) -> FindTool? {        places.first { ($0.key ?? $0.name) == key }.map { row(of: $0) }    }    /// The families no Home tile counts: a write that moved only these never    /// re-reads Home (architect 2335 item 6 — only the tiles' tables).    public static let noTileFamilies: Set<ScreenFamily> = [        .activity, .devices, .dictation, .feedback, .storage, .summary, .bin, .projects,    ]    /// Whether an own write that moved `families` moved a table a tile counts.    public static func tileReads(_ families: Set<ScreenFamily>) -> Bool {        !families.subtracting(noTileFamilies).isEmpty    }    /// The key Everything's tile is drawn under (`everything.json`'s tool key).    /// Everything is no tool (owner's ruling, 12 Oct 2026): it is a shipped    /// view, `ready/everything`.    public static let everythingKey = "everything"}