Commit 6ca4dbeb3e
Verified · cmc
Layout: unified · split
Sources/Orgstar/ContentView.swift +1 −1
| @@ -282,7 +282,7 @@ struct ContentView: View { | |||
| 282 | detail | 282 | detail |
| 283 | .frame(maxWidth: .infinity) | 283 | .frame(maxWidth: .infinity) |
| 284 | .inspector(isPresented: $inspectorShown) { | 284 | .inspector(isPresented: $inspectorShown) { |
| 285 | InspectorPane(session: session, commands: commands) | 285 | InspectorPane(session: session, commands: commands, shown: inspectorShown) |
| 286 | .inspectorColumnWidth(min: 240, ideal: 340, max: 600) | 286 | .inspectorColumnWidth(min: 240, ideal: 340, max: 600) |
| 287 | } | 287 | } |
| 288 | } | 288 | } |
Sources/Orgstar/InspectorPane.swift +18 −5
| @@ -7,6 +7,8 @@ import SwiftUI | |||
| 7 | struct InspectorPane: View { | 7 | struct InspectorPane: View { |
| 8 | let session: DocumentSession | 8 | let session: DocumentSession |
| 9 | let commands: AppCommands | 9 | let commands: AppCommands |
| 10 | /// Whether the inspector is on screen; nothing is computed while it isn't. | ||
| 11 | let shown: Bool | ||
| 10 | @AppStorage("inspectorTab") private var tab = Tab.columns | 12 | @AppStorage("inspectorTab") private var tab = Tab.columns |
| 11 | @AppStorage("inspectorColumnsScope") private var subtree = false | 13 | @AppStorage("inspectorColumnsScope") private var subtree = false |
| 12 | @AppStorage("inspectorClockSpan") private var span = Span.week | 14 | @AppStorage("inspectorClockSpan") private var span = Span.week |
| @@ -17,6 +19,7 @@ struct InspectorPane: View { | |||
| 17 | enum Span: String, CaseIterable { case today = "Today", week = "This Week", all = "All" } | 19 | enum Span: String, CaseIterable { case today = "Today", week = "This Week", all = "All" } |
| 18 | 20 | ||
| 19 | private struct Key: Equatable { | 21 | private struct Key: Equatable { |
| 22 | var shown: Bool | ||
| 20 | var path: String? | 23 | var path: String? |
| 21 | var version: Int | 24 | var version: Int |
| 22 | var caret: Int? | 25 | var caret: Int? |
| @@ -41,7 +44,9 @@ struct InspectorPane: View { | |||
| 41 | case .clock: clockView | 44 | case .clock: clockView |
| 42 | } | 45 | } |
| 43 | } | 46 | } |
| 44 | .task(id: Key(path: session.path, version: session.editVersion, caret: subtree ? heading : nil, subtree: subtree, span: span)) { refresh() } | 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 | } | ||
| 45 | } | 50 | } |
| 46 | 51 | ||
| 47 | @ViewBuilder | 52 | @ViewBuilder |
| @@ -120,14 +125,22 @@ struct InspectorPane: View { | |||
| 120 | } | 125 | } |
| 121 | } | 126 | } |
| 122 | 127 | ||
| 123 | private func refresh() { | 128 | private func refresh() async { |
| 124 | guard session.isOrg, let document = session.document else { | 129 | guard session.isOrg, let document = session.document else { |
| 125 | columns = nil | 130 | columns = nil |
| 126 | clocked = [] | 131 | clocked = [] |
| 127 | return | 132 | return |
| 128 | } | 133 | } |
| 129 | let text = document.text | 134 | let found = await Self.content(text: document.text, tree: document.tree, heading: heading, subtree: subtree, span: span) |
| 130 | columns = (try? ColumnView.capture(text: text, tree: document.tree, at: heading ?? 0, global: !subtree || heading == nil))?.view | 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 | ||
| 131 | let formatter = DateFormatter() | 144 | let formatter = DateFormatter() |
| 132 | formatter.locale = Locale(identifier: "en_US_POSIX") | 145 | formatter.locale = Locale(identifier: "en_US_POSIX") |
| 133 | formatter.dateFormat = "yyyy-MM-dd" | 146 | formatter.dateFormat = "yyyy-MM-dd" |
| @@ -140,6 +153,6 @@ struct InspectorPane: View { | |||
| 140 | case .week: calendar.dateInterval(of: .weekOfYear, for: today)?.start | 153 | case .week: calendar.dateInterval(of: .weekOfYear, for: today)?.start |
| 141 | case .all: nil | 154 | case .all: nil |
| 142 | } | 155 | } |
| 143 | clocked = ClockReport.byHeading(ClockReport.entries(text), from: from.map(formatter.string), to: span == .all ? nil : formatter.string(from: today)) | 156 | return (columns, ClockReport.byHeading(ClockReport.entries(text), from: from.map(formatter.string), to: span == .all ? nil : formatter.string(from: today))) |
| 144 | } | 157 | } |
| 145 | } | 158 | } |