Commit 6ca4dbeb3e

6ca4dbeb3ecc6404ebe8b36d426926aeb8dcfa30

parent: 9e9d587ae8

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 12:36 UTC

Compute the inspector's content off the main actor, only while it is shown

Closes #194

Layout: unified · split

Sources/Orgstar/ContentView.swift +1 −1
@@ -282,7 +282,7 @@ struct ContentView: View {
282282 detail
283283 .frame(maxWidth: .infinity)
284284 .inspector(isPresented: $inspectorShown) {
285 InspectorPane(session: session, commands: commands)
285 InspectorPane(session: session, commands: commands, shown: inspectorShown)
286286 .inspectorColumnWidth(min: 240, ideal: 340, max: 600)
287287 }
288288 }
Sources/Orgstar/InspectorPane.swift +18 −5
@@ -7,6 +7,8 @@ import SwiftUI
77struct InspectorPane: View {
88 let session: DocumentSession
99 let commands: AppCommands
10 /// Whether the inspector is on screen; nothing is computed while it isn't.
11 let shown: Bool
1012 @AppStorage("inspectorTab") private var tab = Tab.columns
1113 @AppStorage("inspectorColumnsScope") private var subtree = false
1214 @AppStorage("inspectorClockSpan") private var span = Span.week
@@ -17,6 +19,7 @@ struct InspectorPane: View {
1719 enum Span: String, CaseIterable { case today = "Today", week = "This Week", all = "All" }
1820
1921 private struct Key: Equatable {
22 var shown: Bool
2023 var path: String?
2124 var version: Int
2225 var caret: Int?
@@ -41,7 +44,9 @@ struct InspectorPane: View {
4144 case .clock: clockView
4245 }
4346 }
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 }
4550 }
4651
4752 @ViewBuilder
@@ -120,14 +125,22 @@ struct InspectorPane: View {
120125 }
121126 }
122127
123 private func refresh() {
128 private func refresh() async {
124129 guard session.isOrg, let document = session.document else {
125130 columns = nil
126131 clocked = []
127132 return
128133 }
129 let text = document.text
130 columns = (try? ColumnView.capture(text: text, tree: document.tree, at: heading ?? 0, global: !subtree || heading == nil))?.view
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
131144 let formatter = DateFormatter()
132145 formatter.locale = Locale(identifier: "en_US_POSIX")
133146 formatter.dateFormat = "yyyy-MM-dd"
@@ -140,6 +153,6 @@ struct InspectorPane: View {
140153 case .week: calendar.dateInterval(of: .weekOfYear, for: today)?.start
141154 case .all: nil
142155 }
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)))
144157 }
145158}