Sources/Orgstar/Keys.swift
178 lines · 7098 bytes
1import OrgApp
2import OrgCore
3import OrgEditorAppKit
4import SwiftUI
5
6struct KeyHint: Hashable {
7 let key: String
8 let title: String
9}
10
11/// The minibuffer line: a pending prefix, or the last message for a few seconds. After a
12/// short pause on a prefix, the keys that can follow it.
13struct EchoArea: View {
14 let commands: AppCommands
15 @Environment(\.orgTheme) private var theme
16 @State private var showingHints = false
17 @State private var answer = ""
18 @FocusState private var answerFocused: Bool
19
20 var body: some View {
21 VStack(alignment: .leading, spacing: 0) {
22 if showingHints, commands.pendingKeys != nil, !commands.keyHints.isEmpty {
23 Divider()
24 LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), alignment: .leading)], alignment: .leading, spacing: 4) {
25 ForEach(commands.keyHints, id: \.self) { hint in
26 HStack(spacing: 8) {
27 Text(hint.key).font(.system(.body, design: .monospaced)).foregroundStyle(.tint)
28 Text(hint.title).foregroundStyle(hint.title == "+prefix" ? .secondary : .primary).lineLimit(1)
29 }
30 }
31 }
32 .padding(10)
33 }
34 if let request = commands.prompt {
35 if !request.prompt.choices.isEmpty {
36 Divider()
37 let matches = filteredChoices(request.prompt.choices)
38 ScrollView {
39 VStack(alignment: .leading, spacing: 2) {
40 ForEach(matches.prefix(12), id: \.self) { choice in
41 Button(choice) { commands.answer(choice) }
42 .buttonStyle(.plain)
43 .font(.system(.callout, design: .monospaced))
44 .foregroundStyle(choice == matches.first ? .primary : .secondary)
45 }
46 }
47 .frame(maxWidth: .infinity, alignment: .leading)
48 .padding(10)
49 }
50 .frame(maxHeight: 200)
51 }
52 Divider()
53 HStack(spacing: 6) {
54 Text(request.prompt.message).font(.system(.callout, design: .monospaced)).foregroundStyle(.secondary)
55 TextField("", text: $answer)
56 .textFieldStyle(.plain)
57 .font(.system(.callout, design: .monospaced))
58 .focused($answerFocused)
59 .onSubmit {
60 if request.prompt.choices.isEmpty {
61 commands.answer(answer)
62 } else if let first = filteredChoices(request.prompt.choices).first {
63 commands.answer(first)
64 }
65 }
66 .onExitCommand { commands.answer(nil) }
67 }
68 .padding(.horizontal, 10)
69 .padding(.vertical, 4)
70 .onAppear {
71 answer = request.prompt.initial
72 answerFocused = true
73 }
74 .id(request.id)
75 } else if let text = commands.pendingKeys.map({ $0 + "-" }) ?? commands.message {
76 Text(text)
77 .font(.system(.callout, design: .monospaced))
78 .lineLimit(1)
79 .padding(.horizontal, 10)
80 .padding(.vertical, 4)
81 .frame(maxWidth: .infinity, alignment: .leading)
82 }
83 }
84 .foregroundStyle(theme.color("modeline-foreground") ?? .primary)
85 .background { if let bg = theme.color("modeline-background") { bg } else { Rectangle().fill(.bar) } }
86 .task(id: commands.pendingKeys) {
87 showingHints = false
88 guard commands.pendingKeys != nil else { return }
89 try? await Task.sleep(for: .milliseconds(600))
90 if !Task.isCancelled { showingHints = true }
91 }
92 }
93}
94
95/// The evil state, as Doom's modeline shows it.
96struct StateTag: View {
97 let state: String
98 @Environment(\.orgTheme) private var theme
99
100 var body: some View {
101 Text(label)
102 .font(.system(.caption, design: .monospaced).bold())
103 .foregroundStyle(.white)
104 .padding(.horizontal, 6)
105 .padding(.vertical, 1)
106 .background(color, in: RoundedRectangle(cornerRadius: 4))
107 .accessibilityLabel("\(label.capitalized) state")
108 }
109
110 private var label: String {
111 switch state {
112 case "visual-line": "V-LINE"
113 default: state.uppercased()
114 }
115 }
116
117 private var color: Color {
118 switch state {
119 case "insert": theme.color("state-insert") ?? .green
120 case "visual", "visual-line": theme.color("state-visual") ?? .orange
121 default: theme.color("state-normal") ?? .blue
122 }
123 }
124}
125
126extension EchoArea {
127 /// Choices matching what's typed, best first.
128 func filteredChoices(_ choices: [String]) -> [String] {
129 guard !answer.isEmpty else { return choices }
130 return choices.compactMap { choice in FuzzyMatch.score(answer, choice).map { (choice, $0) } }
131 .sorted { $0.1 > $1.1 }
132 .map(\.0)
133 }
134}
135
136/// M-x and ⇧⌘P: every command by title, with its keys.
137struct PaletteView: View {
138 let commands: AppCommands
139 let run: (String) -> Void
140 @State private var query = ""
141 @Environment(\.dismiss) private var dismiss
142
143 private var matches: [EditorCommand] {
144 let scored: [(command: EditorCommand, score: Int)] = EditorCommands.all.filter(\.inPalette).compactMap { command in
145 guard let score = FuzzyMatch.score(query, command.title) else { return nil }
146 return (command, score)
147 }
148 return scored.sorted { $0.score != $1.score ? $0.score > $1.score : $0.command.title < $1.command.title }.map(\.command)
149 }
150
151 var body: some View {
152 let matches = matches
153 VStack(spacing: 0) {
154 TextField("Run command", text: $query)
155 .textFieldStyle(.plain)
156 .font(.title3)
157 .padding(12)
158 .onSubmit { if let first = matches.first { run(first.id) } }
159 Divider()
160 List(matches, id: \.id) { command in
161 Button {
162 run(command.id)
163 } label: {
164 HStack {
165 Text(command.title)
166 Spacer()
167 if let keys = commands.keyLabel(for: command.id) {
168 Text(keys).font(.system(.caption, design: .monospaced)).foregroundStyle(.secondary)
169 }
170 }
171 }
172 .buttonStyle(.plain)
173 }
174 }
175 .frame(width: 520, height: 360)
176 .onExitCommand { dismiss() }
177 }
178}