// What only one KIND of machine has, handed in by that machine. // // The Mac holds the database, so it has pages the phone can never draw: the // API keys, what this Mac reads off its own disk, what it is serving, its // backup. Those are `CottonHost` views over `CottonHost`'s own model, and // `CottonHost` imports `CottonUI` — so the shared screens cannot name them, // cannot construct them, and must not learn what a Mac is in order to show // them. // // The seam is the environment, as `HostNoun` is the seam for the word a host // calls itself. The host puts pages and acts in and answers when one is // pressed; the shared screens draw whatever is there, and nothing when there // is nothing, which is the phone. There is no settings screen to hold them // (Spec 10): each lands where it belongs — // // - THIS device's own record page (`/devices/[id]`, `native Device records`) // offers `deviceActs`: one opens a page at `/device/[page]` // (`Route.hostPage`), drawn by `HostPageScreen` from `pages`; another is a // press the host answers (check for updates); // - the database page (`/database`, `DatabaseTableSource`) offers // `databaseActs` — the database's own address, its backup and leaving it // on this machine — each opening its page at `/device/[page]`. // // **Why a page is an `AnyView`.** Type erasure is the cost of a module that may // not be imported. The route carries the page's id and nothing else, so the // wire protocol learns one opaque path, never what a Mac's pages are. // // **Why the slot is also installed.** The device page's acts are read by a // records actor (`SettingsRecords`), which no environment reaches; the host // installs its one slot once (`install`), and that actor asks it on the main // actor. A process with none installed — the phone — offers no acts. import SwiftUI /// One host-only panel, drawn as the whole of `/device/[page]` when this /// device's own page or the database page opens it. public struct HostSettingsPage: Identifiable, Sendable { /// Wire, not words: the row's id, the selection's value, and the tail of /// the accessibility identifier. Never shown. public let id: String /// The panel, built when it is opened rather than when the list is made: a /// page nobody opened should cost nothing. public let content: @MainActor () -> AnyView /// **The one erasure, and it is here.** A host writes its panel as the /// ordinary `View` it is; the type is dropped on the way in, in this file, /// so no page has to say `AnyView` and `NavigationSweepTests` has one /// exemption rather than one per page. public init( id: String, @ViewBuilder content: @escaping @MainActor () -> Content ) { self.id = id self.content = { AnyView(content()) } } } /// One act THIS device's own record page, or the database page, offers, /// named by the host. public struct HostDeviceAct: Identifiable, Equatable, Sendable { /// What pressing it does. public enum Kind: Equatable, Sendable { /// Opens `/device/[page]` at the act's id: a page in `pages`. case page /// A press `onAct` answers, then said in this sentence. case press(said: String) } /// Wire, not words: a page's id, or the press `onAct` is handed. public let id: String /// The button's plain words ("API keys", "Check for updates"). public let label: String public let kind: Kind public init(id: String, label: String, kind: Kind = .page) { self.id = id self.label = label self.kind = kind } } /// What the host adds to the device and database pages, and what answers a /// press. /// /// `@Observable` rather than a plain value: the acts change while the page is /// open — a key saved, an import finishing — and the page has to redraw when /// they do. Read as an OPTIONAL environment object, so the phone, which puts /// none in, is the case where there is nothing. @Observable @MainActor public final class HostSettingsSlot { public nonisolated static let pageActionPrefix = "host-page:" /// What THIS device's own record page offers, in order. public var deviceActs: [HostDeviceAct] = [] /// What the DATABASE page offers on this kind of machine, in order — each /// opens its page at `/device/[page]` (`DatabaseTableSource`). public var databaseActs: [HostDeviceAct] = [] /// The panels behind the acts /// whose kind is `.page`. public var pages: [HostSettingsPage] = [] /// What a `.press` device act is handed to. /// Set by the host after it has /// built the slot, which is why it is a property rather than an init /// argument: the object that answers a press is the one that OWNS the slot, /// and it cannot hand itself in before it exists. @ObservationIgnored public var onAct: ((String) -> Void)? public init() {} /// A press was pressed. The host decides what it means — there is /// nothing here to decide, and a second table of Mac verbs in this module /// is exactly what this seam exists to avoid. public func act(_ id: String) { onAct?(id) } public func page(_ id: String) -> HostSettingsPage? { pages.first { $0.id == id } } // ── The installed slot ───────────────────────────────────────────────── /// RULE: the device page offers host acts from the installed slot alone, and a process that installed none (the phone) offers none. public private(set) static var installed: HostSettingsSlot? /// Makes `slot` the one the device page asks; `nil` takes it away. public static func install(_ slot: HostSettingsSlot?) { installed = slot } /// Presses the installed host's act `id`, answering the sentence that /// says it was done — `nil` when no host offers that press. public static func press(_ id: String) -> String? { guard let slot = installed, let act = slot.deviceActs.first(where: { $0.id == id }), case .press(let said) = act.kind else { return nil } slot.act(id) return said } }