Cotton code
112 lines · 4.1 KB · SwiftRaw
// A screen's menu, as the tray.//// The architect's ruling of 13 Oct 2026: the three-dots menu is removed from// every page. The stack of cards at the top corner (`TrayStackButton`) is the// one page menu, and what a screen offers besides its records — Select, a// related list, its settings, an export — is a card in the tray it raises,// coloured by what it does like every act. A screen keeps its menu as the// list of `PopoverMenuItem`s it always built; this turns them into cards and// runs the one that fires.import SwiftUI/// One line of a screen's menu, which the tray draws as an act card.public struct PopoverMenuItem: Identifiable {    public let id: String    /// The object its card stands, by name (`shell.list`'s `menu`, or the    /// screen that built the line); a link where it names none.    public let object: String?    /// What the line does, which its card is coloured by.    public let does: CommandDoes?    public let label: String    public let isDestructive: Bool    public let isDisabled: Bool    public let action: () -> Void    public init(        id: String,        object: String? = nil,        does: CommandDoes? = .moves,        label: String,        destructive: Bool = false,        disabled: Bool = false,        action: @escaping () -> Void    ) {        self.id = id        self.object = object        self.does = does        self.label = label        self.isDestructive = destructive        self.isDisabled = disabled        self.action = action    }}extension ActCardReading {    /// A menu line as an act card: the object and colour the line says, a    /// bin held to fire where it takes something away.    static func item(_ item: PopoverMenuItem, prefix: String = MenuTray.prefix) -> ActCardReading {        ActCardReading(            id: prefix + item.id, verb: item.label.replacingOccurrences(of: "\u{2026}", with: ""),            object: .enamel(item.isDestructive ? .bin : CardEnamel.named(item.object) ?? .link),            gesture: item.isDestructive ? .holds : .fires,            does: item.isDestructive ? .destroys : item.does)    }}enum MenuTray {    static let prefix = "menu-"    /// The menu line a fired card stands for.    static func line(_ card: ActCardReading, in items: [PopoverMenuItem], prefix: String = prefix) -> PopoverMenuItem? {        items.first { prefix + $0.id == card.id && !$0.isDisabled }    }}extension View {    /// The screen's menu as the tray the stack button raises.    func menuTray(state: Binding<TrayState>, items: [PopoverMenuItem], title: String? = nil) -> some View {        tray(state: state) {            Tray(                state: state, acts: items.filter { !$0.isDisabled }.map { ActCardReading.item($0) }, title: title,                onFire: { card in MenuTray.line(card, in: items)?.action() })        }    }}extension View {    /// The screen's menu as the tray, held open by a flag: the stack button    /// sets it, and the tray clears it as it drops.    func menuTray(isOpen: Binding<Bool>, items: [PopoverMenuItem], title: String? = nil) -> some View {        modifier(FlagMenuTray(isOpen: isOpen, items: items, title: title))    }}/// What a tray held open by a flag stands at. The flag says raised or not;/// a row that must be held raises its card alone (`.offering`), which a flag/// cannot say, so the offer is kept here.struct TrayFlagState: Equatable {    var offer: TrayOffer?    func state(isOpen: Bool) -> TrayState {        offer.map(TrayState.offering) ?? (isOpen ? .pulled : .rest)    }    mutating func set(_ state: TrayState, isOpen: inout Bool) {        if case .offering(let offered) = state { offer = offered } else { offer = nil }        isOpen = state.isRaised    }}private struct FlagMenuTray: ViewModifier {    @Binding var isOpen: Bool    let items: [PopoverMenuItem]    let title: String?    @State private var flag = TrayFlagState()    func body(content: Content) -> some View {        content.menuTray(            state: Binding(get: { flag.state(isOpen: isOpen) }, set: { flag.set($0, isOpen: &isOpen) }),            items: items, title: title)    }}