Sources/Orgstar/Keys.swift

397c9ad69a596cfff75844141c5e537217661eb3
orgstar/Sources/Orgstar/Keys.swift history · blame · raw

93 lines · 3530 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
17    var body: some View {
18        VStack(alignment: .leading, spacing: 0) {
19            if showingHints, commands.pendingKeys != nil, !commands.keyHints.isEmpty {
20                Divider()
21                LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), alignment: .leading)], alignment: .leading, spacing: 4) {
22                    ForEach(commands.keyHints, id: \.self) { hint in
23                        HStack(spacing: 8) {
24                            Text(hint.key).font(.system(.body, design: .monospaced)).foregroundStyle(.tint)
25                            Text(hint.title).foregroundStyle(hint.title == "+prefix" ? .secondary : .primary).lineLimit(1)
26                        }
27                    }
28                }
29                .padding(10)
30            }
31            if let text = commands.pendingKeys.map({ $0 + "-" }) ?? commands.message {
32                Divider()
33                Text(text)
34                    .font(.system(.callout, design: .monospaced))
35                    .lineLimit(1)
36                    .padding(.horizontal, 10)
37                    .padding(.vertical, 4)
38                    .frame(maxWidth: .infinity, alignment: .leading)
39            }
40        }
41        .background(.bar)
42        .task(id: commands.pendingKeys) {
43            showingHints = false
44            guard commands.pendingKeys != nil else { return }
45            try? await Task.sleep(for: .milliseconds(600))
46            if !Task.isCancelled { showingHints = true }
47        }
48    }
49}
50
51/// M-x and ⇧⌘P: every command by title, with its keys.
52struct PaletteView: View {
53    let keymap: Keymap
54    let run: (String) -> Void
55    @State private var query = ""
56    @Environment(\.dismiss) private var dismiss
57
58    private var matches: [EditorCommand] {
59        let scored: [(command: EditorCommand, score: Int)] = EditorCommands.all.filter(\.inPalette).compactMap { command in
60            guard let score = FuzzyMatch.score(query, command.title) else { return nil }
61            return (command, score)
62        }
63        return scored.sorted { $0.score != $1.score ? $0.score > $1.score : $0.command.title < $1.command.title }.map(\.command)
64    }
65
66    var body: some View {
67        let matches = matches
68        VStack(spacing: 0) {
69            TextField("Run command", text: $query)
70                .textFieldStyle(.plain)
71                .font(.title3)
72                .padding(12)
73                .onSubmit { if let first = matches.first { run(first.id) } }
74            Divider()
75            List(matches, id: \.id) { command in
76                Button {
77                    run(command.id)
78                } label: {
79                    HStack {
80                        Text(command.title)
81                        Spacer()
82                        if let keys = keymap.keys(for: command.id) {
83                            Text(KeySequence.format(keys)).font(.system(.caption, design: .monospaced)).foregroundStyle(.secondary)
84                        }
85                    }
86                }
87                .buttonStyle(.plain)
88            }
89        }
90        .frame(width: 520, height: 360)
91        .onExitCommand { dismiss() }
92    }
93}