SPB Git

Repository Public License

HelpPanel.swift 6.6 KB 139 lines swift

1import LunarDesign2import NavigatorCore3import SwiftUI45/// Rows of the Help panel: every registered command, with its shortcut, grouped by category and filtered by a6/// search text. Pure, so that it can be tested.7enum HelpIndex {8    struct Row: Identifiable, Equatable {9        var id: String10        var title: String11        var shortcut: CommandShortcut?12    }1314    struct Section: Identifiable, Equatable {15        var id: String16        var title: String17        var rows: [Row]18    }1920    @MainActor static func sections(registry: FeatureRegistry, query: String, shortcutsOnly: Bool) -> [Section] {21        let needle = query.trimmingCharacters(in: .whitespacesAndNewlines).folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)22        let tokens = needle.split(whereSeparator: \.isWhitespace).map(String.init)23        return registry.commandsByCategory().compactMap { category, commands in24            let rows = commands.compactMap { command -> Row? in25                if shortcutsOnly, command.shortcut == nil { return nil }26                let title = command.title.string27                if !tokens.isEmpty {28                    let haystack = ([title, category.title.string, command.shortcut?.description ?? ""] + command.keywords)29                        .joined(separator: " ").folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)30                    guard tokens.allSatisfy(haystack.contains) else { return nil }31                }32                return Row(id: command.id, title: title, shortcut: command.shortcut)33            }34            .sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending }35            return rows.isEmpty ? nil : Section(id: category.id, title: category.title.string, rows: rows)36        }37    }38}3940/// "ORIX Navigator Help": what the browser can do and the key that does it, generated from the command41/// registry — a command a feature adds appears here without anyone writing its documentation twice.42struct HelpPanel: View {43    @Environment(\.lunarTheme) private var theme44    @Environment(BrowserStore.self) private var store45    let window: WindowModel46    @State private var query = ""47    @State private var shortcutsOnly = HelpPanel.hasHardwareKeyboardByDefault4849    /// The Mac always has a keyboard: the list opens on shortcuts. On iPhone and iPad it opens on everything.50    static var hasHardwareKeyboardByDefault: Bool {51        #if os(macOS)52        true53        #else54        false55        #endif56    }5758    var body: some View {59        let sections = HelpIndex.sections(registry: store.registry, query: query, shortcutsOnly: shortcutsOnly)60        let context = window.commandContext(source: .help)61        VStack(spacing: LunarSpacing.xs) {62            LunarSearchField(text: $query, prompt: L("Search commands and shortcuts"), leading: .symbol("magnifyingglass"))63                #if os(iOS)64                .textInputAutocapitalization(.never)65                .autocorrectionDisabled()66                #endif67                .accessibilityIdentifier("help.search")68                .padding(.horizontal, LunarSpacing.sm)69            LunarSegmentedControl(L("Show"), selection: $shortcutsOnly, segments: [70                LunarSegment(L("Keyboard shortcuts"), value: true),71                LunarSegment(L("All commands"), value: false),72            ])73            .padding(.horizontal, LunarSpacing.sm)74            .accessibilityIdentifier("help.filter")75            if sections.isEmpty {76                LunarEmptyState(icon: .symbol("questionmark.circle"), title: L("No command found"),77                                message: L("Try another word, or show all commands."))78                    .frame(maxHeight: .infinity)79            } else {80                ScrollView {81                    LazyVStack(alignment: .leading, spacing: LunarSpacing.sm) {82                        ForEach(sections) { section in83                            VStack(alignment: .leading, spacing: 2) {84                                Text(section.title).textCase(.uppercase).lunarFont(.overline).foregroundStyle(theme.textSecondary)85                                    .padding(.horizontal, LunarSpacing.sm)86                                    .padding(.bottom, 2)87                                    .accessibilityAddTraits(.isHeader)88                                ForEach(section.rows) { row in rowView(row, context: context) }89                            }90                        }91                        Text(L("Selecting a command runs it in this window."))92                            .lunarFont(.footnote).foregroundStyle(theme.textTertiary)93                            .padding(.horizontal, LunarSpacing.sm)94                            .padding(.top, LunarSpacing.xs)95                    }96                    .padding(.horizontal, LunarSpacing.xs)97                    .padding(.bottom, LunarSpacing.sm)98                }99                .scrollIndicators(.never)100            }101        }102        .padding(.top, LunarSpacing.xs)103        .accessibilityElement(children: .contain)104        .accessibilityIdentifier("help.list")105    }106107    private func rowView(_ row: HelpIndex.Row, context: CommandContext) -> some View {108        let command = store.registry.command(row.id)109        let enabled = command.map { $0.isAvailable(context) } ?? false110        return Button {111            // The Help panel itself is a command: running it from its own list would only close the list.112            guard let command, command.id != ShellCommandID.showHelp else { return }113            Task { await command.perform(context) }114        } label: {115            LunarHoverRow {116                HStack(spacing: LunarSpacing.sm) {117                    if let icon = command?.icon {118                        LunarIconView(icon, size: 16).foregroundStyle(theme.textSecondary).frame(width: 22)119                    } else {120                        Color.clear.frame(width: 22, height: 1)121                    }122                    Text(row.title).lunarFont(.body).foregroundStyle(enabled ? theme.textPrimary : theme.textSecondary)123                        .lineLimit(2).multilineTextAlignment(.leading)124                    Spacer(minLength: LunarSpacing.xs)125                    if let shortcut = row.shortcut { LunarShortcut(shortcut.keyCaps) }126                }127                .padding(.horizontal, LunarSpacing.sm)128                .frame(minHeight: 38)129            }130        }131        .buttonStyle(LunarPressStyle())132        .disabled(!enabled)133        .accessibilityElement(children: .ignore)134        .accessibilityLabel(row.title)135        .accessibilityValue(row.shortcut.map { L("Shortcut %@", $0.keyCaps.joined(separator: " ")) } ?? "")136        .accessibilityIdentifier("help.command." + row.id)137    }138}139
Files main
Menu

Theme