Sources/Orgstar/InspectorPane.swift
cmc Mac: route echo-area, alert, inspector and modeline strings through the catalog
8835742e44 19 hours ago178 lines · 6957 bytes
1import OrgApp
2import OrgCore
3import SwiftUI
4
5/// The inspector beside the editor: column view and clocked time for the open file, kept
6/// current as it changes.
7struct InspectorPane: View {
8 let session: DocumentSession
9 let commands: AppCommands
10 /// Whether the inspector is on screen; nothing is computed while it isn't.
11 let shown: Bool
12 @AppStorage("inspectorTab") private var tab = Tab.columns
13 @AppStorage("inspectorColumnsScope") private var subtree = false
14 @AppStorage("inspectorClockSpan") private var span = Span.week
15 @State private var columns: ColumnView.View?
16 @State private var clocked: [(start: Int, heading: String, minutes: Int)] = []
17
18 enum Tab: String, CaseIterable {
19 case columns = "Columns", clock = "Clock"
20
21 var title: LocalizedStringKey {
22 switch self {
23 case .columns: "Columns"
24 case .clock: "Clock"
25 }
26 }
27 }
28
29 enum Span: String, CaseIterable {
30 case today = "Today", week = "This Week", all = "All"
31
32 var title: LocalizedStringKey {
33 switch self {
34 case .today: "Today"
35 case .week: "This Week"
36 case .all: "All"
37 }
38 }
39 }
40
41 private struct Key: Equatable {
42 var shown: Bool
43 var path: String?
44 var version: Int
45 var caret: Int?
46 var subtree: Bool
47 var span: Span
48 }
49
50 /// The heading at the caret, for a subtree's columns and the current row.
51 private var heading: Int? { session.outline.last { $0.start <= commands.selection.location }?.start }
52
53 var body: some View {
54 VStack(spacing: 0) {
55 Picker("", selection: $tab) {
56 ForEach(Tab.allCases, id: \.self) { Text($0.title) }
57 }
58 .pickerStyle(.segmented)
59 .labelsHidden()
60 .padding(8)
61 Divider()
62 switch tab {
63 case .columns: columnsView
64 case .clock: clockView
65 }
66 }
67 .task(id: Key(shown: shown, path: session.path, version: shown ? session.editVersion : 0, caret: shown && subtree ? heading : nil, subtree: subtree, span: span)) {
68 if shown { await refresh() }
69 }
70 }
71
72 @ViewBuilder
73 private var columnsView: some View {
74 Picker("Scope", selection: $subtree) {
75 Text("File").tag(false)
76 Text("Subtree").tag(true)
77 }
78 .pickerStyle(.segmented)
79 .padding(.horizontal, 8)
80 .padding(.bottom, 6)
81 if let columns, !columns.rows.isEmpty {
82 ScrollView([.horizontal, .vertical]) {
83 Grid(alignment: .leading, horizontalSpacing: 10, verticalSpacing: 3) {
84 GridRow {
85 ForEach(columns.specs.indices, id: \.self) { i in
86 Text(columns.specs[i].title).bold().foregroundStyle(.secondary)
87 }
88 }
89 Divider()
90 ForEach(columns.rows.indices, id: \.self) { r in
91 let row = columns.rows[r]
92 GridRow {
93 ForEach(columns.specs.indices, id: \.self) { i in
94 Text(columns.specs[i].property == "ITEM" ? String(repeating: "*", count: row.level) + " " + row.values[i] : row.values[i])
95 .lineLimit(1)
96 .fontWeight(row.start == heading ? .semibold : .regular)
97 }
98 }
99 .contentShape(Rectangle())
100 .onTapGesture { session.reveal(row.start) }
101 .accessibilityAddTraits(.isButton)
102 .accessibilityAction { session.reveal(row.start) }
103 }
104 }
105 .font(.system(.callout, design: .monospaced))
106 .padding(8)
107 }
108 .defaultScrollAnchor(.topLeading)
109 } else {
110 ContentUnavailableView("No entries", systemImage: "tablecells")
111 }
112 }
113
114 @ViewBuilder
115 private var clockView: some View {
116 Picker("Span", selection: $span) {
117 ForEach(Span.allCases, id: \.self) { Text($0.title) }
118 }
119 .pickerStyle(.segmented)
120 .padding(.horizontal, 8)
121 .padding(.bottom, 6)
122 if clocked.isEmpty {
123 ContentUnavailableView("No clocked time", systemImage: "clock")
124 } else {
125 List {
126 ForEach(clocked, id: \.start) { entry in
127 HStack {
128 Text(entry.heading.isEmpty ? String(localized: "(untitled)") : entry.heading).lineLimit(1)
129 Spacer()
130 Text(ClockReport.hours(entry.minutes)).monospacedDigit()
131 }
132 .fontWeight(entry.start == heading ? .semibold : .regular)
133 .contentShape(Rectangle())
134 .onTapGesture { session.reveal(entry.start) }
135 .accessibilityElement(children: .combine)
136 .accessibilityAddTraits(.isButton)
137 .accessibilityAction { session.reveal(entry.start) }
138 }
139 HStack {
140 Text("Total").bold()
141 Spacer()
142 Text(ClockReport.hours(clocked.reduce(0) { $0 + $1.minutes })).bold().monospacedDigit()
143 }
144 }
145 }
146 }
147
148 private func refresh() async {
149 guard session.isOrg, let document = session.document else {
150 columns = nil
151 clocked = []
152 return
153 }
154 let found = await Self.content(text: document.text, tree: document.tree, heading: heading, subtree: subtree, span: span)
155 guard !Task.isCancelled else { return }
156 (columns, clocked) = found
157 }
158
159 @concurrent
160 private nonisolated static func content(
161 text: String, tree: OrgTree, heading: Int?, subtree: Bool, span: Span
162 ) async -> (columns: ColumnView.View?, clocked: [(start: Int, heading: String, minutes: Int)]) {
163 let columns = (try? ColumnView.capture(text: text, tree: tree, at: heading ?? 0, global: !subtree || heading == nil))?.view
164 let formatter = DateFormatter()
165 formatter.locale = Locale(identifier: "en_US_POSIX")
166 formatter.dateFormat = "yyyy-MM-dd"
167 // Weeks start on Monday, as org's do.
168 var calendar = Calendar.current
169 calendar.firstWeekday = 2
170 let today = Date()
171 let from: Date? = switch span {
172 case .today: today
173 case .week: calendar.dateInterval(of: .weekOfYear, for: today)?.start
174 case .all: nil
175 }
176 return (columns, ClockReport.byHeading(ClockReport.entries(text), from: from.map(formatter.string), to: span == .all ? nil : formatter.string(from: today)))
177 }
178}