Sources/Orgstar/Keys.swift
186 lines · 7537 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 .lineLimit(1)
56 .truncationMode(.middle)
57 TextField("", text: $answer)
58 .frame(minWidth: 240)
59 .textFieldStyle(.plain)
60 .font(.system(.callout, design: .monospaced))
61 .focused($answerFocused)
62 .onSubmit {
63 if request.prompt.choices.isEmpty || !request.prompt.requireMatch {
64 commands.answer(answer)
65 } else if let first = filteredChoices(request.prompt.choices).first {
66 commands.answer(first)
67 }
68 }
69 .onKeyPress(.tab) {
70 guard let first = filteredChoices(request.prompt.choices).first else { return .ignored }
71 answer = first
72 return .handled
73 }
74 .onExitCommand { commands.answer(nil) }
75 }
76 .padding(.horizontal, 10)
77 .padding(.vertical, 4)
78 .onAppear {
79 answer = request.prompt.initial
80 answerFocused = true
81 }
82 .id(request.id)
83 } else if let text = commands.pendingKeys.map({ $0 + "-" }) ?? commands.message {
84 Text(text)
85 .font(.system(.callout, design: .monospaced))
86 .lineLimit(1)
87 .padding(.horizontal, 10)
88 .padding(.vertical, 4)
89 .frame(maxWidth: .infinity, alignment: .leading)
90 }
91 }
92 .foregroundStyle(theme.color("modeline-foreground") ?? .primary)
93 .background { if let bg = theme.color("modeline-background") { bg } else { Rectangle().fill(.bar) } }
94 .task(id: commands.pendingKeys) {
95 showingHints = false
96 guard commands.pendingKeys != nil else { return }
97 try? await Task.sleep(for: .milliseconds(600))
98 if !Task.isCancelled { showingHints = true }
99 }
100 }
101}
102
103/// The evil state, as Doom's modeline shows it.
104struct StateTag: View {
105 let state: String
106 @Environment(\.orgTheme) private var theme
107
108 var body: some View {
109 Text(label)
110 .font(.system(.caption, design: .monospaced).bold())
111 .foregroundStyle(.white)
112 .padding(.horizontal, 6)
113 .padding(.vertical, 1)
114 .background(color, in: RoundedRectangle(cornerRadius: 4))
115 .accessibilityLabel("\(label.capitalized) state")
116 }
117
118 private var label: String {
119 switch state {
120 case "visual-line": "V-LINE"
121 default: state.uppercased()
122 }
123 }
124
125 private var color: Color {
126 switch state {
127 case "insert": theme.color("state-insert") ?? .green
128 case "visual", "visual-line": theme.color("state-visual") ?? .orange
129 default: theme.color("state-normal") ?? .blue
130 }
131 }
132}
133
134extension EchoArea {
135 /// Choices matching what's typed, best first.
136 func filteredChoices(_ choices: [String]) -> [String] {
137 guard !answer.isEmpty else { return choices }
138 return choices.compactMap { choice in FuzzyMatch.score(answer, choice).map { (choice, $0) } }
139 .sorted { $0.1 > $1.1 }
140 .map(\.0)
141 }
142}
143
144/// M-x and ⇧⌘P: every command by title, with its keys.
145struct PaletteView: View {
146 let commands: AppCommands
147 let run: (String) -> Void
148 @State private var query = ""
149 @Environment(\.dismiss) private var dismiss
150
151 private var matches: [EditorCommand] {
152 let scored: [(command: EditorCommand, score: Int)] = EditorCommands.all.filter(\.inPalette).compactMap { command in
153 guard let score = FuzzyMatch.score(query, command.title) else { return nil }
154 return (command, score)
155 }
156 return scored.sorted { $0.score != $1.score ? $0.score > $1.score : $0.command.title < $1.command.title }.map(\.command)
157 }
158
159 var body: some View {
160 let matches = matches
161 VStack(spacing: 0) {
162 TextField("Run command", text: $query)
163 .textFieldStyle(.plain)
164 .font(.title3)
165 .padding(12)
166 .onSubmit { if let first = matches.first { run(first.id) } }
167 Divider()
168 List(matches, id: \.id) { command in
169 Button {
170 run(command.id)
171 } label: {
172 HStack {
173 Text(command.title)
174 Spacer()
175 if let keys = commands.keyLabel(for: command.id) {
176 Text(keys).font(.system(.caption, design: .monospaced)).foregroundStyle(.secondary)
177 }
178 }
179 }
180 .buttonStyle(.plain)
181 }
182 }
183 .frame(width: 520, height: 360)
184 .onExitCommand { dismiss() }
185 }
186}