Sources/Orgstar/Keys.swift

d3f871e3e72aad75d7fcdc8bd2d828ecec0a4a3f
orgstar/Sources/Orgstar/Keys.swift history · blame · raw

146 lines · 6023 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    @State private var showingHints = false
 16    @State private var answer = ""
 17    @FocusState private var answerFocused: Bool
 18
 19    var body: some View {
 20        VStack(alignment: .leading, spacing: 0) {
 21            if showingHints, commands.pendingKeys != nil, !commands.keyHints.isEmpty {
 22                Divider()
 23                LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), alignment: .leading)], alignment: .leading, spacing: 4) {
 24                    ForEach(commands.keyHints, id: \.self) { hint in
 25                        HStack(spacing: 8) {
 26                            Text(hint.key).font(.system(.body, design: .monospaced)).foregroundStyle(.tint)
 27                            Text(hint.title).foregroundStyle(hint.title == "+prefix" ? .secondary : .primary).lineLimit(1)
 28                        }
 29                    }
 30                }
 31                .padding(10)
 32            }
 33            if let request = commands.prompt {
 34                if !request.prompt.choices.isEmpty {
 35                    Divider()
 36                    let matches = filteredChoices(request.prompt.choices)
 37                    ScrollView {
 38                        VStack(alignment: .leading, spacing: 2) {
 39                            ForEach(matches.prefix(12), id: \.self) { choice in
 40                                Button(choice) { commands.answer(choice) }
 41                                    .buttonStyle(.plain)
 42                                    .font(.system(.callout, design: .monospaced))
 43                                    .foregroundStyle(choice == matches.first ? .primary : .secondary)
 44                            }
 45                        }
 46                        .frame(maxWidth: .infinity, alignment: .leading)
 47                        .padding(10)
 48                    }
 49                    .frame(maxHeight: 200)
 50                }
 51                Divider()
 52                HStack(spacing: 6) {
 53                    Text(request.prompt.message).font(.system(.callout, design: .monospaced)).foregroundStyle(.secondary)
 54                    TextField("", text: $answer)
 55                        .textFieldStyle(.plain)
 56                        .font(.system(.callout, design: .monospaced))
 57                        .focused($answerFocused)
 58                        .onSubmit {
 59                            if request.prompt.choices.isEmpty {
 60                                commands.answer(answer)
 61                            } else if let first = filteredChoices(request.prompt.choices).first {
 62                                commands.answer(first)
 63                            }
 64                        }
 65                        .onExitCommand { commands.answer(nil) }
 66                }
 67                .padding(.horizontal, 10)
 68                .padding(.vertical, 4)
 69                .onAppear {
 70                    answer = request.prompt.initial
 71                    answerFocused = true
 72                }
 73                .id(request.id)
 74            } else if let text = commands.pendingKeys.map({ $0 + "-" }) ?? commands.message {
 75                Divider()
 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        .background(.bar)
 85        .task(id: commands.pendingKeys) {
 86            showingHints = false
 87            guard commands.pendingKeys != nil else { return }
 88            try? await Task.sleep(for: .milliseconds(600))
 89            if !Task.isCancelled { showingHints = true }
 90        }
 91    }
 92}
 93
 94extension EchoArea {
 95    /// Choices matching what's typed, best first.
 96    func filteredChoices(_ choices: [String]) -> [String] {
 97        guard !answer.isEmpty else { return choices }
 98        return choices.compactMap { choice in FuzzyMatch.score(answer, choice).map { (choice, $0) } }
 99            .sorted { $0.1 > $1.1 }
100            .map(\.0)
101    }
102}
103
104/// M-x and ⇧⌘P: every command by title, with its keys.
105struct PaletteView: View {
106    let keymap: Keymap
107    let run: (String) -> Void
108    @State private var query = ""
109    @Environment(\.dismiss) private var dismiss
110
111    private var matches: [EditorCommand] {
112        let scored: [(command: EditorCommand, score: Int)] = EditorCommands.all.filter(\.inPalette).compactMap { command in
113            guard let score = FuzzyMatch.score(query, command.title) else { return nil }
114            return (command, score)
115        }
116        return scored.sorted { $0.score != $1.score ? $0.score > $1.score : $0.command.title < $1.command.title }.map(\.command)
117    }
118
119    var body: some View {
120        let matches = matches
121        VStack(spacing: 0) {
122            TextField("Run command", text: $query)
123                .textFieldStyle(.plain)
124                .font(.title3)
125                .padding(12)
126                .onSubmit { if let first = matches.first { run(first.id) } }
127            Divider()
128            List(matches, id: \.id) { command in
129                Button {
130                    run(command.id)
131                } label: {
132                    HStack {
133                        Text(command.title)
134                        Spacer()
135                        if let keys = keymap.keys(for: command.id) {
136                            Text(KeySequence.format(keys)).font(.system(.caption, design: .monospaced)).foregroundStyle(.secondary)
137                        }
138                    }
139                }
140                .buttonStyle(.plain)
141            }
142        }
143        .frame(width: 520, height: 360)
144        .onExitCommand { dismiss() }
145    }
146}