Sources/OrgstarMobile/CommandList.swift
89 lines · 3794 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 /// Called with the chosen command before the list closes.
21 let choose: (String) -> Void
22 @State private var query = ""
23 @State private var keyboard = GCKeyboard.coalesced != nil
24 @Environment(\.dismiss) private var dismiss
25
26 var body: some View {
27 let ranked = CommandRanking.rank(rows, query: query, id: \.id, titles: { [$0.title] })
28 NavigationStack {
29 List {
30 if query.allSatisfy(\.isWhitespace) {
31 if !ranked.recent.isEmpty {
32 Section("Recent") { ForEach(ranked.recent) { row($0) } }
33 }
34 ForEach(sections(ranked.rest), id: \.group) { section in
35 Section {
36 ForEach(section.rows) { row($0) }
37 } header: {
38 if let group = section.group { Text(CommandGroups.commandList[group].title) }
39 }
40 }
41 } else {
42 // Those that run here first, each part in rank order.
43 ForEach(ranked.rest.filter { $0.reason == nil } + ranked.rest.filter { $0.reason != nil }) { row($0) }
44 }
45 }
46 .overlay {
47 if !query.allSatisfy(\.isWhitespace), ranked.rest.isEmpty { ContentUnavailableView.search(text: query) }
48 }
49 .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always))
50 .navigationTitle("Commands")
51 .navigationBarTitleDisplayMode(.inline)
52 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } }
53 }
54 .onReceive(NotificationCenter.default.publisher(for: .GCKeyboardDidConnect)) { _ in keyboard = true }
55 .onReceive(NotificationCenter.default.publisher(for: .GCKeyboardDidDisconnect)) { _ in keyboard = GCKeyboard.coalesced != nil }
56 }
57
58 /// The rows by `CommandGroups.commandList`, those in no group first, in the list's order.
59 private func sections(_ rows: [CommandRow]) -> [(group: Int?, rows: [CommandRow])] {
60 let grouped = Dictionary(grouping: rows) { CommandGroups.group(of: $0.id, in: CommandGroups.commandList) }
61 return ([nil] + CommandGroups.commandList.indices.map(Optional.some)).compactMap { group in
62 grouped[group].map { (group, $0) }
63 }
64 }
65
66 private func row(_ command: CommandRow) -> some View {
67 Button {
68 CommandUsage.shared.record(command.id)
69 choose(command.id)
70 dismiss()
71 } label: {
72 HStack {
73 VStack(alignment: .leading, spacing: 2) {
74 Text(command.title)
75 if let reason = command.reason {
76 Text(reason).font(.caption).foregroundStyle(.secondary)
77 }
78 }
79 Spacer()
80 if keyboard, let keys = command.keys {
81 Text(keys).font(.caption.monospaced()).foregroundStyle(.secondary)
82 }
83 }
84 }
85 .tint(.primary)
86 .disabled(command.reason != nil)
87 }
88}
89#endif