SPB Git

Repository Public License

Commands.swift 2.9 KB 77 lines swift

1import LunarDesign2import NavigatorCore3import SwiftUI45/// The menu bar (Mac) and hardware-keyboard commands (iPad), generated from the command registry: each menu6/// lists the commands placed in it, the shell's and the features' alike, with their shortcuts. Every item acts7/// on the focused window and is disabled when its command is unavailable.8public struct BrowserCommands: Commands {9    @FocusedValue(\.browserWindow) private var window10    private let environment: AppEnvironment1112    public init(environment: AppEnvironment = .shared) { self.environment = environment }1314    public var body: some Commands {15        CommandGroup(replacing: .newItem) { items(.file) }16        // The system's Save and Print items do not apply; the File menu carries the registry's own.17        CommandGroup(replacing: .saveItem) {}18        CommandGroup(replacing: .printItem) {}19        CommandGroup(after: .textEditing) {20            Divider()21            items(.edit)22        }23        CommandGroup(before: .toolbar) {24            items(.view)25            Divider()26        }27        CommandMenu(L("History")) { items(.history) }28        CommandMenu(L("Bookmarks")) { items(.bookmarks) }29        CommandMenu(L("Tools")) { ToolsMenuItems(menu: .tools, window: window ?? nil, environment: environment) }30        CommandGroup(before: .windowArrangement) {31            items(.window)32            Divider()33        }34        #if os(macOS)35        CommandGroup(replacing: .appSettings) { items(.app) }36        CommandGroup(replacing: .help) { items(.help) }37        #endif38    }3940    private func items(_ menu: MenuBarMenu) -> RegistryMenuItems {41        RegistryMenuItems(menu: menu, window: window ?? nil, environment: environment)42    }43}4445/// The items of one menu. A view, so that it follows the observable state the commands read (selected tab,46/// loading, panel shown…).47struct RegistryMenuItems: View {48    let menu: MenuBarMenu49    let window: WindowModel?50    let environment: AppEnvironment5152    var body: some View {53        CommandMenuItems(groups: environment.registry.commands(in: menu), context: context, showsShortcuts: true)54    }5556    /// `nil` until the store exists: every item is then disabled.57    private var context: CommandContext? {58        guard let store = environment.store else { return nil }59        return CommandContext(environment: environment, store: store, window: window, source: .menuBar)60    }61}6263/// The Tools menu holds what features place there. Until one does, it says so rather than being empty.64private struct ToolsMenuItems: View {65    let menu: MenuBarMenu66    let window: WindowModel?67    let environment: AppEnvironment6869    var body: some View {70        if environment.registry.commands(in: menu).isEmpty {71            Button(L("No Tools Installed")) {}.disabled(true)72        } else {73            RegistryMenuItems(menu: menu, window: window, environment: environment)74        }75    }76}77
Files main
Menu

Theme