Sources/Orgstar/InspectorPane.swift
cmc One capitalisation rule: title case for titles and buttons, sentence case for settings
7c1313ad9f 20 hours ago178 lines · 7052 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 /// The selected row's heading start, in each tab.
18 @State private var selectedRow: Int?
19 @State private var selectedClock: Int?
20
21 /// A column view row, identified by its heading.
22 private struct ColumnRow: Identifiable {
23 let row: ColumnView.Row
24 var id: Int { row.start }
25 }
26
27 enum Tab: String, CaseIterable {
28 case columns = "Columns", clock = "Clock"
29
30 var title: LocalizedStringKey {
31 switch self {
32 case .columns: "Columns"
33 case .clock: "Clock"
34 }
35 }
36 }
37
38 enum Span: String, CaseIterable {
39 case today = "Today", week = "This Week", all = "All"
40
41 var title: LocalizedStringKey {
42 switch self {
43 case .today: "Today"
44 case .week: "This Week"
45 case .all: "All"
46 }
47 }
48 }
49
50 private struct Key: Equatable {
51 var shown: Bool
52 var path: String?
53 var version: Int
54 var caret: Int?
55 var subtree: Bool
56 var span: Span
57 }
58
59 /// The heading at the caret, for a subtree's columns and the current row.
60 private var heading: Int? { session.outline.last { $0.start <= commands.selection.location }?.start }
61
62 var body: some View {
63 VStack(spacing: 0) {
64 Picker("", selection: $tab) {
65 ForEach(Tab.allCases, id: \.self) { Text($0.title) }
66 }
67 .pickerStyle(.segmented)
68 .labelsHidden()
69 .padding(8)
70 Divider()
71 switch tab {
72 case .columns: columnsView
73 case .clock: clockView
74 }
75 }
76 .task(id: Key(shown: shown, path: session.path, version: shown ? session.editVersion : 0, caret: shown && subtree ? heading : nil, subtree: subtree, span: span)) {
77 if shown { await refresh() }
78 }
79 }
80
81 @ViewBuilder
82 private var columnsView: some View {
83 Picker("Scope", selection: $subtree) {
84 Text("File").tag(false)
85 Text("Subtree").tag(true)
86 }
87 .pickerStyle(.segmented)
88 .padding(.horizontal, 8)
89 .padding(.bottom, 6)
90 if let columns, !columns.rows.isEmpty {
91 // A table, so the arrow keys move through the rows; selecting one goes to it.
92 Table(columns.rows.map(ColumnRow.init), selection: $selectedRow) {
93 TableColumnForEach(columns.specs.indices, id: \.self) { i in
94 TableColumn(columns.specs[i].title) { (item: ColumnRow) in
95 let row = item.row
96 Text(columns.specs[i].property == "ITEM" ? String(repeating: "*", count: row.level) + " " + row.values[i] : row.values[i])
97 .lineLimit(1)
98 .fontWeight(row.start == heading ? .semibold : .regular)
99 }
100 }
101 }
102 .font(.system(.callout, design: .monospaced))
103 .onChange(of: selectedRow) { if let selectedRow { session.reveal(selectedRow) } }
104 .contextMenu(forSelectionType: Int.self) { _ in } primaryAction: { starts in
105 if let start = starts.first { session.reveal(start) }
106 }
107 } else {
108 ContentUnavailableView("No Entries", systemImage: "tablecells")
109 }
110 }
111
112 @ViewBuilder
113 private var clockView: some View {
114 Picker("Span", selection: $span) {
115 ForEach(Span.allCases, id: \.self) { Text($0.title) }
116 }
117 .pickerStyle(.segmented)
118 .padding(.horizontal, 8)
119 .padding(.bottom, 6)
120 if clocked.isEmpty {
121 ContentUnavailableView("No Clocked Time", systemImage: "clock")
122 } else {
123 // A selectable list, so the arrow keys move through the headings; selecting one goes to it.
124 List(selection: $selectedClock) {
125 ForEach(clocked, id: \.start) { entry in
126 HStack {
127 Text(entry.heading.isEmpty ? String(localized: "(untitled)") : entry.heading).lineLimit(1)
128 Spacer()
129 Text(ClockReport.hours(entry.minutes)).monospacedDigit()
130 }
131 .fontWeight(entry.start == heading ? .semibold : .regular)
132 .accessibilityElement(children: .combine)
133 .tag(entry.start)
134 }
135 HStack {
136 Text("Total").bold()
137 Spacer()
138 Text(ClockReport.hours(clocked.reduce(0) { $0 + $1.minutes })).bold().monospacedDigit()
139 }
140 }
141 .onChange(of: selectedClock) { if let selectedClock { session.reveal(selectedClock) } }
142 .contextMenu(forSelectionType: Int.self) { _ in } primaryAction: { starts in
143 if let start = starts.first { session.reveal(start) }
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}