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