Jotley / app / macOS / Views / NoteDetailPlaceholderView+Body.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | |
| 4 | enum MacNoteHeaderLayout { |
| 5 | static let topInset: CGFloat = 16 |
| 6 | static let controlHeight: CGFloat = 36 |
| 7 | static let menuTopInset = topInset + controlHeight + 8 |
| 8 | } |
| 9 | |
| 10 | // MARK: - View Components |
| 11 | |
| 12 | extension NoteDetailPlaceholderView { |
| 13 | // Computed property to check if we're recording this specific note |
| 14 | var isRecordingThisNote: Bool { |
| 15 | viewModel.recorder.isRecording && |
| 16 | viewModel.recorder.isRecordingNewNote && |
| 17 | viewModel.newNoteBeingRecorded == note |
| 18 | } |
| 19 | |
| 20 | var isRecordingOnAnotherNote: Bool { |
| 21 | guard let activeRecordingNote = viewModel.activeRecordingNote else { return false } |
| 22 | return activeRecordingNote.objectID != note.objectID |
| 23 | } |
| 24 | |
| 25 | var hasActiveRecordingSession: Bool { |
| 26 | viewModel.recorder.isRecording || viewModel.isTranscriptionPaused |
| 27 | } |
| 28 | |
| 29 | // Check if there's content available to summarize (transcription) |
| 30 | var hasContentToSummarize: Bool { |
| 31 | let hasTranscription = note.transcription != nil && !note.transcription!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 32 | return hasTranscription |
| 33 | } |
| 34 | |
| 35 | var hasSummary: Bool { |
| 36 | guard let summary = note.summary?.trimmingCharacters(in: .whitespacesAndNewlines), |
| 37 | !summary.isEmpty else { |
| 38 | return false |
| 39 | } |
| 40 | return true |
| 41 | } |
| 42 | |
| 43 | var detailScrollTopAnchorID: String { |
| 44 | "note-detail-summary-top-anchor" |
| 45 | } |
| 46 | |
| 47 | var detailScrollBottomAnchorID: String { |
| 48 | "note-detail-live-bottom-anchor" |
| 49 | } |
| 50 | |
| 51 | // Keep the final transcript above the entire footer fade, while the scroll |
| 52 | // view and its scrollbar continue to reach the bottom of the window. |
| 53 | var detailFooterFadeHeight: CGFloat { |
| 54 | selectedTab == DetailTab.summary.rawValue ? 160 : 120 |
| 55 | } |
| 56 | |
| 57 | var detailBottomPadding: CGFloat { |
| 58 | if viewModel.recorder.isRecording { return 88 } |
| 59 | if selectedTab == DetailTab.transcript.rawValue || note.hasMeaningfulSummaryText { |
| 60 | return detailFooterFadeHeight + 16 |
| 61 | } |
| 62 | return 16 |
| 63 | } |
| 64 | |
| 65 | var bodyContentView: some View { |
| 66 | GeometryReader { geometry in |
| 67 | VStack(spacing: 0) { |
| 68 | detailHeaderControls |
| 69 | |
| 70 | // Main content wrapped in ScrollView |
| 71 | ScrollViewReader { scrollProxy in |
| 72 | ScrollView { |
| 73 | VStack(alignment: .leading, spacing: 0) { |
| 74 | headerContent |
| 75 | .padding(.horizontal, 16) |
| 76 | .padding(.top, 12) |
| 77 | .padding(.bottom, 12) |
| 78 | dateAndTagsContent |
| 79 | .padding(.horizontal, 16) |
| 80 | |
| 81 | contentArea |
| 82 | // Follow the actual speech, not a viewport-sized |
| 83 | // empty area that pushes new notes off the top. |
| 84 | .frame(maxWidth: .infinity, |
| 85 | minHeight: isRecordingThisNote || isEditingTranscript ? 0 : max(400, geometry.size.height - 150), |
| 86 | alignment: .topLeading) |
| 87 | .padding(.top, 8) |
| 88 | } |
| 89 | .padding(.bottom, detailBottomPadding) |
| 90 | .id(detailScrollTopAnchorID) |
| 91 | |
| 92 | Color.clear |
| 93 | .frame(height: 1) |
| 94 | .id(detailScrollBottomAnchorID) |
| 95 | } |
| 96 | .coordinateSpace(name: "scroll") |
| 97 | .modifier(JotleyTranscriptEdgeFade(showsBottomFade: false, |
| 98 | isEnabled: isRecordingThisNote && selectedTab == DetailTab.transcript.rawValue)) |
| 99 | .onScrollPhaseChange { _, phase in |
| 100 | transcriptFollow.scrollPhaseChanged(isUserScrolling: phase != .idle && phase != .animating) |
| 101 | } |
| 102 | .onScrollGeometryChange(for: Bool.self) { geometry in |
| 103 | geometry.contentOffset.y + geometry.containerSize.height >= |
| 104 | geometry.contentSize.height + geometry.contentInsets.bottom - 40 |
| 105 | } action: { _, atBottom in |
| 106 | transcriptFollow.bottomChanged(atBottom) |
| 107 | } |
| 108 | .task(id: [viewModel.recorder.transcription, String(selectedTab), note.objectID.uriRepresentation().absoluteString, String(isRecordingThisNote)]) { |
| 109 | guard isRecordingThisNote, selectedTab == DetailTab.transcript.rawValue, |
| 110 | transcriptFollow.shouldFollow else { return } |
| 111 | await Task.yield() |
| 112 | guard !Task.isCancelled, transcriptFollow.shouldFollow else { return } |
| 113 | scrollProxy.scrollTo(detailScrollBottomAnchorID, anchor: .bottom) |
| 114 | } |
| 115 | |
| 116 | // Reveal the beginning once when generation opens. Streaming |
| 117 | // updates must not pull the reader down to the growing end. |
| 118 | .task(id: [String(isGeneratingThisSummary), String(selectedTab), note.objectID.uriRepresentation().absoluteString]) { |
| 119 | guard isGeneratingThisSummary, selectedTab == DetailTab.summary.rawValue else { return } |
| 120 | await Task.yield() |
| 121 | guard !Task.isCancelled else { return } |
| 122 | scrollProxy.scrollTo(detailScrollTopAnchorID, anchor: .top) |
| 123 | } |
| 124 | } |
| 125 | .scrollIndicators(.visible) // Make scrollbars visible |
| 126 | |
| 127 | } // End of VStack |
| 128 | } // End of GeometryReader |
| 129 | .overlay(alignment: .bottom) { |
| 130 | detailOverlay5.animation(.smooth(duration: 0.25), value: viewModel.recorder.isRecording) |
| 131 | } |
| 132 | .animation(.spring(response: 0.3, dampingFraction: 0.8), value: isRecordingThisNote) |
| 133 | .onAppear { |
| 134 | showActionsPopover = false |
| 135 | // Load templates when view appears |
| 136 | if availableTemplates.isEmpty { |
| 137 | loadTemplates() |
| 138 | } |
| 139 | } |
| 140 | .sheet(item: $templatePresentation) { presentation in |
| 141 | VStack(alignment: .trailing, spacing: 0) { |
| 142 | Button("Done") { templatePresentation = nil } |
| 143 | .jotleyDismissButtonStyle() |
| 144 | .padding() |
| 145 | if presentation == .create { |
| 146 | NewSummaryTemplateView(onSaved: { templatePresentation = nil }, onUse: { definition in |
| 147 | templatePresentation = nil |
| 148 | selectSummaryTemplate(definition) |
| 149 | }) |
| 150 | } else { |
| 151 | SummaryTemplatesView(onUse: { definition in |
| 152 | templatePresentation = nil |
| 153 | selectSummaryTemplate(definition) |
| 154 | }) |
| 155 | } |
| 156 | } |
| 157 | .frame(width: 900, height: 720) |
| 158 | .environment(\.managedObjectContext, viewModel.persistenceController.context) |
| 159 | } |
| 160 | .onChange(of: note.objectID) { _, _ in |
| 161 | transcriptFollow = MacTranscriptFollowState() |
| 162 | isEditingTranscript = false |
| 163 | showActionsPopover = false |
| 164 | showsSummaryTemplatePicker = false |
| 165 | } |
| 166 | .onChange(of: viewModel.recorder.isRecording) { _, isRecordingNow in |
| 167 | |
| 168 | if isRecordingNow { |
| 169 | showActionsPopover = false |
| 170 | } |
| 171 | } |
| 172 | .onChange(of: viewModel.isTranscriptionPaused) { _, isPaused in |
| 173 | |
| 174 | if isPaused { |
| 175 | showActionsPopover = false |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | @ViewBuilder var detailHeaderControls: some View { |
| 181 | |
| 182 | // Top toolbar with tab buttons |
| 183 | HStack(spacing: 0) { |
| 184 | Spacer() |
| 185 | |
| 186 | // Center - Tab buttons (hidden during recording) |
| 187 | HStack(spacing: 6) { |
| 188 | // Summary tab: show only when a real summary exists |
| 189 | // (or one is actively being generated). Recovery path |
| 190 | // for empty states lives on the Transcript tab via |
| 191 | // its "Generate Summary" footer button. |
| 192 | let isGeneratingSummaryForThisNote = |
| 193 | (viewModel.isGeneratingSummary && viewModel.generatingSummaryNoteID == note.objectID) || |
| 194 | note.summary?.trimmingCharacters(in: .whitespacesAndNewlines) == "Generating summary..." |
| 195 | if !isRecordingThisNote && (hasSummary || isGeneratingSummaryForThisNote || hasContentToSummarize) { |
| 196 | NoteContentSwitch(selection: Binding( |
| 197 | get: { selectedTab == DetailTab.summary.rawValue ? "Summary" : "Transcript" }, |
| 198 | set: { selectedTab = $0 == "Summary" ? DetailTab.summary.rawValue : DetailTab.transcript.rawValue }), |
| 199 | showsSummary: hasSummary || isGeneratingSummaryForThisNote, |
| 200 | showsTranscript: hasContentToSummarize, summaryIsGenerating: isGeneratingSummaryForThisNote) |
| 201 | } |
| 202 | |
| 203 | if isRecordingOnAnotherNote { |
| 204 | jumpToRecordingTabButton |
| 205 | } |
| 206 | } |
| 207 | .fixedSize() // Prevent tabs from wrapping |
| 208 | |
| 209 | Spacer() |
| 210 | |
| 211 | // Right side - More menu |
| 212 | moreMenuButton |
| 213 | } |
| 214 | .frame(minHeight: MacNoteHeaderLayout.controlHeight) |
| 215 | .padding(.horizontal, 16) |
| 216 | .padding(.top, MacNoteHeaderLayout.topInset) |
| 217 | .padding(.bottom, 12) |
| 218 | .background(Color.themeContentBackground) |
| 219 | |
| 220 | } |
| 221 | |
| 222 | @ViewBuilder var detailOverlay5: some View { |
| 223 | if viewModel.recorder.isRecording { |
| 224 | MacCompactTranscriptionBar(viewModel: viewModel) |
| 225 | .matchedGeometryEffect(id: "transcription-footer", in: transcriptionFooterNamespace) |
| 226 | .modifier(MacNoteFooterPlacement()) |
| 227 | .transition(.opacity) |
| 228 | } |
| 229 | |
| 230 | if selectedTab == DetailTab.summary.rawValue && note.hasMeaningfulSummaryText && |
| 231 | !viewModel.recorder.isRecording && !isGeneratingThisSummary && !viewModel.operationInProgress { |
| 232 | MacSummaryActionsFooter( |
| 233 | isEnabled: !hasActiveRecordingSession && !viewModel.recorder.isBusy && |
| 234 | !viewModel.isGeneratingSummary && !LocalAppModel.shared.busy, |
| 235 | onAsk: { showingAskAI = true } |
| 236 | ) |
| 237 | .modifier(MacNoteFooterPlacement()) |
| 238 | |
| 239 | } |
| 240 | |
| 241 | // Transcript actions remain on the Transcript tab. |
| 242 | if selectedTab == DetailTab.transcript.rawValue && !isEditingTranscript && !viewModel.recorder.isRecording && !isGeneratingThisSummary && !viewModel.operationInProgress { |
| 243 | let transcript = editableTranscript |
| 244 | // Per-note saved-audio loading flag (not a persisted placeholder) so the |
| 245 | // action footer stays hidden while saved audio is being transcribed. |
| 246 | let isLoading = viewModel.isTranscriptLoading(for: note) |
| 247 | let hasContent = !transcript.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 248 | let isGenerating = viewModel.isGeneratingSummary && viewModel.generatingSummaryNoteID == note.objectID |
| 249 | |
| 250 | if !isLoading { |
| 251 | VStack(spacing: 10) { |
| 252 | if hasContent && !note.hasMeaningfulSummaryText { |
| 253 | HStack(spacing: 8) { |
| 254 | summaryTemplateButton |
| 255 | Button { |
| 256 | Task { await handleSummarizeAction(templateKey: summaryTemplateKey) } |
| 257 | } label: { |
| 258 | Text("Generate Summary") |
| 259 | .font(.system(size: 13, weight: .semibold)) |
| 260 | } |
| 261 | .buttonStyle(JotleyGlassButtonStyle(floatsOverContent: true, usesAccentForeground: true)) |
| 262 | .disabled(isGenerating || (try? templateCatalog.resolve(summaryTemplateKey)) == nil) |
| 263 | .accessibilityIdentifier("noteDetailTranscriptSummarizeButton") |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | Button { |
| 268 | Task { @MainActor in |
| 269 | await viewModel.resumeTranscriptionOnNote(note: note) |
| 270 | } |
| 271 | } label: { |
| 272 | Label { |
| 273 | Text("Continue Transcription").modifier(JotleyNoteFooterTextStyle()) |
| 274 | } icon: { |
| 275 | Image(systemName: "waveform") |
| 276 | .foregroundStyle(Color.themePrimaryAccent) |
| 277 | } |
| 278 | .font(.system(size: 13, weight: .medium)) |
| 279 | } |
| 280 | .buttonStyle(JotleyGlassButtonStyle(floatsOverContent: true)) |
| 281 | .matchedGeometryEffect(id: "transcription-footer", in: transcriptionFooterNamespace) |
| 282 | .accessibilityIdentifier("noteDetailTranscriptResumeButton") |
| 283 | } |
| 284 | .modifier(MacNoteFooterPlacement()) |
| 285 | |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | |
| 291 | private var summaryTemplateKey: String { note.summaryType ?? LocalSummaryTemplate.auto.key } |
| 292 | |
| 293 | private var summaryTemplateButton: some View { |
| 294 | let title = templateCatalog.title(for: summaryTemplateKey) |
| 295 | return Button { |
| 296 | showsSummaryTemplatePicker.toggle() |
| 297 | } label: { |
| 298 | Label { |
| 299 | Text(title).modifier(JotleyNoteFooterTextStyle()) |
| 300 | } icon: { |
| 301 | Image(systemName: "sparkles") |
| 302 | .foregroundStyle(Color.themePrimaryAccent) |
| 303 | } |
| 304 | .font(.system(size: 13, weight: .medium)) |
| 305 | .lineLimit(1) |
| 306 | .frame(maxWidth: 160) |
| 307 | .fixedSize(horizontal: true, vertical: false) |
| 308 | } |
| 309 | .buttonStyle(JotleyGlassButtonStyle(floatsOverContent: true)) |
| 310 | .accessibilityLabel("Templates: \(title)") |
| 311 | .accessibilityIdentifier("noteDetailTemplatesButton") |
| 312 | .popover(isPresented: $showsSummaryTemplatePicker, arrowEdge: .bottom) { |
| 313 | TemplateSubmenu( |
| 314 | note: note, |
| 315 | availableTemplates: availableTemplates, |
| 316 | onSelectTemplate: { key in |
| 317 | showsSummaryTemplatePicker = false |
| 318 | if let definition = try? templateCatalog.resolve(key) { |
| 319 | selectSummaryTemplate(definition) |
| 320 | } |
| 321 | }, |
| 322 | onManageTemplates: { create in |
| 323 | showsSummaryTemplatePicker = false |
| 324 | templatePresentation = create ? .create : .manage |
| 325 | } |
| 326 | ) |
| 327 | .padding(6) |
| 328 | .presentationBackground(Color.themeContentBackground) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | private func selectSummaryTemplate(_ template: SummaryTemplateDefinition) { |
| 333 | guard summaryTemplateKey != template.key else { return } |
| 334 | if note.hasMeaningfulSummaryText { |
| 335 | Task { await handleSummarizeAction(templateKey: template.key) } |
| 336 | } else { |
| 337 | note.summaryType = template.key |
| 338 | saveContext() |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | struct MacSummaryActionsFooter: View { |
| 345 | let isEnabled: Bool |
| 346 | let onAsk: () -> Void |
| 347 | |
| 348 | var body: some View { |
| 349 | Button(action: onAsk) { |
| 350 | Label { |
| 351 | Text("Ask AI").modifier(JotleyNoteFooterTextStyle()) |
| 352 | } icon: { |
| 353 | Image(systemName: "bubble.left.and.bubble.right") |
| 354 | .foregroundStyle(Color.themePrimaryAccent) |
| 355 | } |
| 356 | .font(.system(size: 13, weight: .medium)) |
| 357 | } |
| 358 | .accessibilityIdentifier("noteDetailSummaryAskAIButton") |
| 359 | .buttonStyle(JotleyGlassButtonStyle(floatsOverContent: true)) |
| 360 | .disabled(!isEnabled) |
| 361 | } |
| 362 | } |