// A record's declared acts (`parts.acts`): the main one, the more, the away// one, and how a source-owned row's own acts sit beside them — what a tile// that acts on a record (an arrival, a join request) leads with.importCottonValuesimportFoundationstructRecordPageLayout {let definition: TableDefinitionlet record: TableRecordvar parts: PageParts { definition.resolvedParts }func command(_ id: String?) -> RecordCommand? {guard definition.canRunCommands, let id, id != definition.onOpen else { returnnil }return definition.commands?.first { $0.id == id && $0.sheet == nil && $0.applies(to: record) } }func actions(_ source: [RecordAction]) -> [RecordAction] { source.map { action inlet key = action.commandID ?? action.idif key == parts.acts.main { return action.placed(.main) }if key == parts.acts.away { return action.placed(.away) }return action } }structFoot {var main: RecordCommand?var more: [RecordCommand]var away: RecordCommand?var source: [RecordAction]/// The source's act in the main place when no declared command holds/// it: one the source tagged with the definition's main act whose/// `when` does not cover this record (a code host's Sign in, under/// the place of a browser's "Sign in on the Mac"), or one the source/// placed main itself. It leads as the declared one would.var mainSource: RecordAction? { main == nil ? source.first { $0.placement == .main } : nil } }/// A source-owned draft/row decides which acts exist. When its act has a/// declared command, execute that one native contract (receipt and Undo/// included), retaining the source adapter for its other capabilities.func foot(actions source: [RecordAction], sourceOwned: Bool) -> Foot {let offered = Set(source.map { $0.commandID ?? $0.id })let allows: (RecordCommand) -> Bool = { !sourceOwned || offered.contains($0.id) }func retainingQuestion(_ command: RecordCommand) -> RecordCommand {guard sourceOwned, let question = source.first(where: { ($0.commandID ?? $0.id) == command.id })?.confirmation else { return command }var command = command command.confirm = question.message command.destructive = question.destructivereturn command }let main = self.main.flatMap { allows($0) ? retainingQuestion($0) : nil }let more = self.more.filter(allows).map(retainingQuestion)let away = self.away.flatMap { allows($0) ? retainingQuestion($0) : nil }let used = Set(([main, away].compactMap { $0 } + more).map(\.id))returnFoot(main: main, more: more, away: away, source: actions(source).filter { !used.contains($0.commandID ?? $0.id) }) }var main: RecordCommand? { command(parts.acts.main) }var more: [RecordCommand] { parts.acts.more.compactMap(command) }var away: RecordCommand? { command(parts.acts.away) }func asksFirst(_ command: RecordCommand) -> Bool { command.asksFirst || command.id == parts.acts.away }}extensionPageParts.List {/// Match a child column against this row's value, never its record id./// Missing, blank and non-text parent values draw no list, as records.page does.func parentFilter(_ parent: TableRecord?) -> [String: RecordValue]? {guardlet matching else { return [:] }guardlet parent, case .text(let raw) = parent[matching.parent] else { returnnil }let value = raw.trimmingCharacters(in: .whitespacesAndNewlines)guard !value.isEmpty else { returnnil }return [matching.column: .text(value)] }func matches(_ section: SectionList, parent: TableRecord?) -> Bool {guard table == section.table, relation == section.link, let filter = parentFilter(parent) else { returnfalse }return filter.allSatisfy { section.where[$0.key] == $0.value } }}extensionTableDefinition {/// Whether the record is drawn as a conversation: its parts name the/// messages and it says where they are. An ordinary page with a/// conversation list keeps its own head.var drawsConversation: Bool { pageParts.contains(.messages) && conversation != nil }}