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