Branchwell

Jotley / app / macOS / Views / NoteDetailPlaceholderView+Content.swift

1import SwiftUI
2import AppKit
3
4extension NoteDetailPlaceholderView {
5 var contentWithAlerts: some View {
6 contentWithAlertsBase
7 .sheet(isPresented: $showingAskAI) {
8 LocalMacAskNoteView(note: note).environmentObject(persistenceController)
9 }
10 .safeAreaInset(edge: .bottom) {
11 if isGeneratingThisSummary {
12 MacSummaryProgressFooter(progress: viewModel.summaryProgress) { viewModel.cancelInference() }
13 }
14 }
15 .onAppear(perform: handleOnAppear)
16 .onDisappear {
17 NotificationCenter.default.removeObserver(self, name: Notification.Name("JotVibeSaveCurrentNote"), object: nil)
18 // Clean up timers
19 summarySaveTimer?.invalidate()
20 summarySaveTimer = nil
21 transcriptSaveTimer?.invalidate()
22 transcriptSaveTimer = nil
23 // Only flush when the user was actively editing — an unconditional
24 // save here can race the SwiftUI binding update for editable*
25 // and overwrite real content with an empty @State buffer.
26 if isSummaryEditorFocused {
27 saveSummaryChanges()
28 }
29 if isTranscriptEditorFocused {
30 saveTranscriptChanges()
31 }
32 }
33 .onChange(of: viewModel.isTranscriptionPaused, handleTranscriptionPausedChanged)
34 .onChange(of: viewModel.recorder.isRecording, handleRecordingChanged)
35 .onChange(of: isTranscriptEditorFocused, handleTranscriptFocusChanged)
36 .onChange(of: note.objectID, handleNoteChanged)
37 .onChange(of: selectedTab, handleTabChanged)
38 .onChange(of: note.summary, handleSummaryChanged)
39 .onChange(of: note.title, handleTitleChanged)
40 .onChange(of: note.transcription) { oldValue, newValue in
41 guard !isTranscriptEditorFocused else { return }
42
43 // Guard against a CloudKit store-trump merge echoing the import-time
44 // "Loading transcript..." sentinel back over a real transcript while
45 // the auto-summary is still running. When that happens for THIS note
46 // (owner id matches) and we still hold real content, refuse the
47 // downgrade and re-assert the real transcript so Core Data — and the
48 // next CloudKit export — converge back to the truth. Without this the
49 // transcript tab snaps back to the import spinner and the real
50 // transcript is lost: the reported transcript persistence regression.
51 if Note.isTranscriptionLoadingPlaceholder(newValue),
52 editableTranscriptNoteID == note.objectID,
53 !Note.isTranscriptionLoadingPlaceholder(editableTranscript),
54 !editableTranscript.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
55
56 let realTranscript = editableTranscript
57 if note.transcription != realTranscript {
58 note.transcription = realTranscript
59 if note.transcriptUtterances.isEmpty {
60 note.setTranscriptUtterances(TranscriptUtterance.fallbackUtterances(from: realTranscript))
61 }
62 do {
63 try persistenceController.save()
64 } catch {
65
66 }
67 }
68 return
69 }
70
71 // If viewing raw transcript or cleaned is not preferred/available, update editableTranscript.
72 if editableTranscript == savedTranscript {
73 editableTranscript = newValue ?? ""
74 savedTranscript = editableTranscript
75 editableTranscriptNoteID = note.objectID
76 }
77
78 }
79 .onChange(of: isSummaryEditorFocused, handleSummaryFocusChanged)
80 .onChange(of: editableSummary) { oldValue, newValue in
81 if oldValue != newValue {
82
83 scheduleSummarySave()
84 }
85 }
86 .onChange(of: editableTranscript) { oldValue, newValue in
87 if oldValue != newValue {
88
89 scheduleTranscriptSave()
90 }
91 }
92 .onReceive(NotificationCenter.default.publisher(for: NSWindow.didResignKeyNotification)) { _ in
93 // When window loses focus, save any pending changes
94 if isSummaryEditorFocused {
95
96 saveSummaryChanges()
97 }
98 if isTranscriptEditorFocused {
99
100 saveTranscriptChanges()
101 }
102 }
103 .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("NoteTagsUpdated"))) { notification in
104 // Refresh tags when they are updated asynchronously.
105 if let userInfo = notification.userInfo,
106 let noteID = userInfo["noteID"] as? NSManagedObjectID,
107 noteID == note.objectID {
108
109 currentTags = note.tagNames
110 viewRefreshID = UUID() // Force view refresh
111 }
112 }
113 }
114
115 var contentWithAlertsBase: some View {
116 bodyContentView
117 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
118 .background(Color.themeContentBackground.ignoresSafeArea())
119 .coordinateSpace(name: "contentView")
120
121 .overlay {
122 ZStack(alignment: .topLeading) {
123
124 if showActionsPopover {
125 CustomMoreMenuOverlay(
126 isPresented: $showActionsPopover,
127 content: actionPopoverContentView
128 )
129 }
130 }
131 }
132 .alert(obsidianAlertTitle, isPresented: $showObsidianSaveAlert) {
133 Button("OK", role: .cancel) {}
134 .frame(minWidth: 70)
135 } message: {
136 Text(obsidianAlertMessage)
137 }
138
139 }
140}
141
142
143/// Center the status itself over Cancel, rather than expanding a leading-aligned row.
144struct MacSummaryProgressFooter: View {
145 let progress: LocalSummaryProgress
146 let cancel: () -> Void
147 var body: some View {
148 VStack(spacing: 10) {
149 HStack(spacing: 10) {
150 ProgressView().progressViewStyle(MacThemedCircularProgressStyle()).controlSize(.small)
151 Text(progress.title).font(.system(size: 15, weight: .semibold))
152 }.frame(maxWidth: .infinity, alignment: .center)
153 .accessibilityIdentifier("noteDetailSummaryProgress")
154 if let detail = progress.detail {
155 Text(detail).font(.system(size: 13)).foregroundStyle(Color.themeTextSecondary)
156 .multilineTextAlignment(.center).fixedSize(horizontal: false, vertical: true)
157 }
158 if let fraction = progress.fraction { ProgressView(value: fraction).tint(.themePrimaryAccent) }
159 Button(action: cancel) {
160 Label {
161 Text("Cancel Generation").modifier(JotleyNoteFooterTextStyle())
162 } icon: {
163 Image(systemName: "xmark.circle.fill")
164 }
165 }.jotleyDismissButtonStyle()
166 .keyboardShortcut(.cancelAction).accessibilityIdentifier("noteDetailCancelGenerationButton")
167 }.foregroundStyle(Color.themeTextPrimary)
168 .frame(maxWidth: 480)
169 .padding(.top, 12)
170 .padding([.horizontal, .bottom], MacNoteFooterPlacement.edgeInset)
171 .frame(maxWidth: .infinity)
172 .background(Color.themeBackground)
173 }
174}