Sources/Orgstar/InspectorPane.swift

daac3706d5aa523bc907d6118edd28cbc0767283
orgstar/Sources/Orgstar/InspectorPane.swift

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

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form