Sources/OrgstarMobile/CommandList.swift

b043ff99fba99e1bab2208d64a2e1cd8fbf80c87
orgstar/Sources/OrgstarMobile/CommandList.swift

92 lines · 3956 bytes

 1#if os(iOS)
 2import GameController
 3import OrgApp
 4import SwiftUI
 5
 6/// A command in the Commands list.
 7struct CommandRow: Identifiable {
 8    let id: String
 9    let title: String
10    /// Its keys in the current keymap, shown while a hardware keyboard is attached.
11    var keys: String? = nil
12    /// Why it doesn't apply here; nil when it runs. The row is listed greyed with this.
13    var reason: String? = nil
14}
15
16/// Commands, on every screen: with an empty query the recent ones, then every command by
17/// subject; a query matches as the Mac's palette does.
18struct CommandList: View {
19    let rows: [CommandRow]
20    var title = String(localized: "Commands")
21    /// Choosing a command counts as running it, for Recent and the ranking.
22    var recordsUse = true
23    /// Called with the chosen command before the list closes.
24    let choose: (String) -> Void
25    @State private var query = ""
26    @State private var keyboard = GCKeyboard.coalesced != nil
27    @Environment(\.dismiss) private var dismiss
28
29    var body: some View {
30        let ranked = CommandRanking.rank(rows, query: query, id: \.id, titles: { [$0.title] })
31        NavigationStack {
32            List {
33                if query.allSatisfy(\.isWhitespace) {
34                    if !ranked.recent.isEmpty {
35                        Section("Recent") { ForEach(ranked.recent) { row($0) } }
36                    }
37                    ForEach(sections(ranked.rest), id: \.group) { section in
38                        Section {
39                            ForEach(section.rows) { row($0) }
40                        } header: {
41                            if let group = section.group { Text(CommandGroups.commandList[group].title) }
42                        }
43                    }
44                } else {
45                    // Those that run here first, each part in rank order.
46                    ForEach(ranked.rest.filter { $0.reason == nil } + ranked.rest.filter { $0.reason != nil }) { row($0) }
47                }
48            }
49            .overlay {
50                if !query.allSatisfy(\.isWhitespace), ranked.rest.isEmpty { ContentUnavailableView.search(text: query) }
51            }
52            .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always))
53            .navigationTitle(title)
54            .navigationBarTitleDisplayMode(.inline)
55            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } }
56        }
57        .onReceive(NotificationCenter.default.publisher(for: .GCKeyboardDidConnect)) { _ in keyboard = true }
58        .onReceive(NotificationCenter.default.publisher(for: .GCKeyboardDidDisconnect)) { _ in keyboard = GCKeyboard.coalesced != nil }
59    }
60
61    /// The rows by `CommandGroups.commandList`, those in no group first, in the list's order.
62    private func sections(_ rows: [CommandRow]) -> [(group: Int?, rows: [CommandRow])] {
63        let grouped = Dictionary(grouping: rows) { CommandGroups.group(of: $0.id, in: CommandGroups.commandList) }
64        return ([nil] + CommandGroups.commandList.indices.map(Optional.some)).compactMap { group in
65            grouped[group].map { (group, $0) }
66        }
67    }
68
69    private func row(_ command: CommandRow) -> some View {
70        Button {
71            if recordsUse { CommandUsage.shared.record(command.id) }
72            choose(command.id)
73            dismiss()
74        } label: {
75            HStack {
76                VStack(alignment: .leading, spacing: 2) {
77                    Text(command.title)
78                    if let reason = command.reason {
79                        Text(reason).font(.caption).foregroundStyle(.secondary)
80                    }
81                }
82                Spacer()
83                if keyboard, let keys = command.keys {
84                    Text(keys).font(.caption.monospaced()).foregroundStyle(.secondary)
85                }
86            }
87        }
88        .tint(.primary)
89        .disabled(command.reason != nil)
90    }
91}
92#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form