Sources/Orgstar/FastSelectionView.swift
128 lines · 5200 bytes
1import OrgCore
2import SwiftUI
3
4/// Fast TODO and tag selection in the echo area: each option with its key. TODO keywords take
5/// one key; tags toggle until Return, Tab types a tag, Escape or C-g quits.
6struct FastSelectionView: View {
7 let selection: FastSelection
8 let message: String
9 let answer: (String?) -> Void
10 @Environment(\.orgTheme) private var theme
11 @State private var inputs: [FastSelection.Input] = []
12 @State private var typing = false
13 @State private var typed = ""
14 @FocusState private var focused: Bool
15 @FocusState private var typingFocused: Bool
16
17 var body: some View {
18 let current = selection.multiple ? selection.tags(after: inputs) ?? [] : []
19 VStack(alignment: .leading, spacing: 6) {
20 if selection.multiple {
21 if !selection.inherited.isEmpty { line("Inherited:", selection.inherited) }
22 line("Current:", current)
23 }
24 ForEach(Array(rows.enumerated()), id: \.offset) { _, row in
25 HStack(spacing: 14) {
26 ForEach(Array(row.enumerated()), id: \.offset) { _, cell in
27 switch cell {
28 case .text(let text):
29 Text(text).foregroundStyle(.secondary)
30 case .option(let key, let name):
31 HStack(spacing: 4) {
32 Text("[\(String(key))]").foregroundStyle(.tint)
33 Text(name)
34 .fontWeight(current.contains(name) ? .bold : .regular)
35 .foregroundStyle(current.contains(name) ? theme.color("todo") ?? .primary : .primary)
36 }
37 }
38 }
39 }
40 }
41 if typing {
42 HStack(spacing: 6) {
43 Text("Tag:").foregroundStyle(.secondary)
44 TextField("", text: $typed)
45 .textFieldStyle(.plain)
46 .focused($typingFocused)
47 .onSubmit {
48 inputs.append(.typed(typed))
49 typed = ""
50 typing = false
51 focused = true
52 }
53 .onExitCommand {
54 typing = false
55 focused = true
56 }
57 }
58 }
59 Text(message).foregroundStyle(.secondary)
60 }
61 .font(.system(.callout, design: .monospaced))
62 .frame(maxWidth: .infinity, alignment: .leading)
63 .padding(10)
64 .focusable()
65 .focusEffectDisabled()
66 .focused($focused)
67 .onKeyPress(phases: .down, action: press)
68 .onAppear { focused = true }
69 .onChange(of: typing) { _, now in if now { typingFocused = true } }
70 }
71
72 private func line(_ label: String, _ tags: [String]) -> some View {
73 HStack(spacing: 6) {
74 Text(label).foregroundStyle(.secondary)
75 Text(tags.isEmpty ? "" : ":" + tags.joined(separator: ":") + ":")
76 }
77 }
78
79 private func press(_ key: KeyPress) -> KeyPress.Result {
80 guard !typing else { return .ignored }
81 if key.key == .escape || (key.modifiers.contains(.control) && key.characters == "g") {
82 answer(nil)
83 return .handled
84 }
85 if selection.multiple {
86 if key.key == .return {
87 answer(FastSelection.encode(inputs))
88 } else if key.key == .tab {
89 typing = true
90 } else if let c = key.characters.first, key.characters.count == 1 {
91 if c == "q", !selection.options.contains(where: { $0.key == "q" }) {
92 answer(nil)
93 } else {
94 inputs.append(.key(c))
95 }
96 } else {
97 return .ignored
98 }
99 return .handled
100 }
101 guard let c = key.characters.first, key.characters.count == 1 else { return .ignored }
102 answer(String(c))
103 return .handled
104 }
105
106 private enum Cell {
107 case text(String)
108 case option(Character, String)
109 }
110
111 /// Options in rows, braces and brackets around groups, as org lays them out.
112 private var rows: [[Cell]] {
113 var rows: [[Cell]] = [[]]
114 func newRow() { if !(rows.last?.isEmpty ?? true) { rows.append([]) } }
115 for item in selection.items {
116 switch item {
117 case .startGroup: newRow(); rows[rows.count - 1].append(.text("{"))
118 case .endGroup: rows[rows.count - 1].append(.text("}")); newRow()
119 case .startTagGroup: newRow(); rows[rows.count - 1].append(.text("["))
120 case .endTagGroup: rows[rows.count - 1].append(.text("]")); newRow()
121 case .groupTags: rows[rows.count - 1].append(.text(":"))
122 case .newline: newRow()
123 case .option(let key, let name): rows[rows.count - 1].append(.option(key, name))
124 }
125 }
126 return rows.filter { !$0.isEmpty }
127 }
128}