Jotley / app / macOS / Views / NoteDetailPlaceholderView+ActionsPopover.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | |
| 4 | extension NoteDetailPlaceholderView { |
| 5 | // Action popover content |
| 6 | var actionPopoverContentView: some View { |
| 7 | CustomMoreMenu(parent: self, viewModel: viewModel, note: note, isPresented: $showActionsPopover) |
| 8 | } |
| 9 | |
| 10 | struct CustomMoreMenu: View { |
| 11 | let parent: NoteDetailPlaceholderView |
| 12 | @ObservedObject var viewModel: NotesListViewModel |
| 13 | let note: Note |
| 14 | @Binding var isPresented: Bool |
| 15 | @AppStorage(SummaryDisplayStyle.preferenceKey) private var summaryStyle: SummaryDisplayStyle = .cards |
| 16 | @State private var showTemplateSubmenu = false |
| 17 | @State private var showExportSubmenu = false |
| 18 | @State private var templateMenuFrame: CGRect = .zero |
| 19 | private let mainMenuWidth: CGFloat = 260 |
| 20 | private let submenuGap: CGFloat = 2 |
| 21 | |
| 22 | // Get the current template name from note.summaryType |
| 23 | private var currentTemplateName: String { |
| 24 | parent.templateCatalog.title(for: note.summaryType) |
| 25 | } |
| 26 | |
| 27 | private var hasSummary: Bool { |
| 28 | guard let summary = note.summary else { return false } |
| 29 | let trimmedSummary = summary.trimmingCharacters(in: .whitespacesAndNewlines) |
| 30 | guard !trimmedSummary.isEmpty else { return false } |
| 31 | |
| 32 | return trimmedSummary != "Transcription in progress..." && trimmedSummary != "Generating summary..." |
| 33 | } |
| 34 | |
| 35 | private var showsSummaryActions: Bool { |
| 36 | hasSummary && !isRecordingThisNote |
| 37 | } |
| 38 | |
| 39 | private var showsTemplateActions: Bool { |
| 40 | parent.hasContentToSummarize && !isRecordingThisNote |
| 41 | } |
| 42 | |
| 43 | private var isRecordingThisNote: Bool { |
| 44 | viewModel.recorder.isRecording && |
| 45 | viewModel.recorder.isRecordingNewNote && |
| 46 | viewModel.newNoteBeingRecorded == note |
| 47 | } |
| 48 | |
| 49 | private var liveTranscriptForCopy: String { |
| 50 | let finalized = viewModel.recorder.accumulatedTranscript.trimmingCharacters(in: .whitespacesAndNewlines) |
| 51 | let interim = viewModel.recorder.latestTranscriptDelta.trimmingCharacters(in: .whitespacesAndNewlines) |
| 52 | |
| 53 | if !finalized.isEmpty && !interim.isEmpty { |
| 54 | return "\(finalized)\n\n\(interim)" |
| 55 | } |
| 56 | |
| 57 | if !finalized.isEmpty { |
| 58 | return finalized |
| 59 | } |
| 60 | |
| 61 | if !interim.isEmpty { |
| 62 | return interim |
| 63 | } |
| 64 | |
| 65 | return (note.transcription ?? "").trimmingCharacters(in: .whitespacesAndNewlines) |
| 66 | } |
| 67 | |
| 68 | private func applyTemplateAndRegenerate(_ templateKey: String) { |
| 69 | isPresented = false |
| 70 | guard note.summaryType ?? "default-summary" != templateKey else { return } |
| 71 | parent.selectedTemplateKeyForAction = templateKey |
| 72 | parent.currentSelectedTemplate = parent.availableTemplates.first { $0.key == templateKey } ?? |
| 73 | Template( |
| 74 | key: "default-summary", |
| 75 | title: "Auto", |
| 76 | description: "General content with structured key points", |
| 77 | category: nil |
| 78 | ) |
| 79 | |
| 80 | Task { |
| 81 | await parent.handleSummarizeAction(templateKey: templateKey) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | var body: some View { |
| 86 | VStack(spacing: 0) { |
| 87 | if !isRecordingThisNote { |
| 88 | HStack(spacing: 10) { |
| 89 | Text("Summary Style") |
| 90 | .accessibilityHidden(true) |
| 91 | Spacer(minLength: 0) |
| 92 | Menu { |
| 93 | SummaryDisplayStylePicker() |
| 94 | .pickerStyle(.inline) |
| 95 | } label: { |
| 96 | HStack(spacing: 8) { |
| 97 | Text(summaryStyle.title) |
| 98 | .foregroundStyle(.primary) |
| 99 | .lineLimit(1) |
| 100 | Image(systemName: "chevron.up.chevron.down") |
| 101 | .font(.system(size: 10, weight: .semibold)) |
| 102 | .foregroundStyle(.secondary) |
| 103 | .accessibilityHidden(true) |
| 104 | } |
| 105 | .padding(.horizontal, 12) |
| 106 | .frame(minHeight: 30) |
| 107 | .modifier(JotleyGlassMenuLabel(shape: Capsule())) |
| 108 | .contentShape(Capsule()) |
| 109 | } |
| 110 | .menuStyle(.button) |
| 111 | .buttonStyle(.plain) |
| 112 | .menuIndicator(.hidden) |
| 113 | .accessibilityLabel("Summary Style") |
| 114 | .accessibilityValue(summaryStyle.title) |
| 115 | .accessibilityIdentifier("noteDetailSummaryStyleMenu") |
| 116 | } |
| 117 | .font(.system(size: 13)) |
| 118 | .padding(.horizontal, 12) |
| 119 | .padding(.vertical, 6) |
| 120 | } |
| 121 | if isRecordingThisNote { |
| 122 | MenuItemView( |
| 123 | icon: "doc.on.doc", |
| 124 | title: "Copy", |
| 125 | disabled: liveTranscriptForCopy.isEmpty |
| 126 | ) { |
| 127 | if !liveTranscriptForCopy.isEmpty { |
| 128 | NSPasteboard.general.clearContents() |
| 129 | NSPasteboard.general.setString(liveTranscriptForCopy, forType: .string) |
| 130 | } |
| 131 | isPresented = false |
| 132 | } |
| 133 | } else { |
| 134 | if showsTemplateActions { |
| 135 | // Click to open/close template submenu. |
| 136 | MenuItemView( |
| 137 | icon: "sparkles", |
| 138 | title: "Template", |
| 139 | subtitle: currentTemplateName, |
| 140 | hasSubmenu: true, |
| 141 | submenuExpanded: showTemplateSubmenu |
| 142 | ) { |
| 143 | if showTemplateSubmenu { |
| 144 | showTemplateSubmenu = false |
| 145 | } else { |
| 146 | if parent.availableTemplates.isEmpty { |
| 147 | parent.loadTemplates() |
| 148 | } |
| 149 | showTemplateSubmenu = true |
| 150 | } |
| 151 | } |
| 152 | .background( |
| 153 | GeometryReader { geometry in |
| 154 | Color.clear |
| 155 | .onAppear { |
| 156 | templateMenuFrame = geometry.frame(in: .local) |
| 157 | } |
| 158 | } |
| 159 | ) |
| 160 | } |
| 161 | |
| 162 | if showsTemplateActions { |
| 163 | if note.hasMeaningfulSummaryText { |
| 164 | MenuItemView( |
| 165 | icon: "arrow.clockwise", |
| 166 | title: String(localized: "Regenerate Summary"), |
| 167 | disabled: parent.hasActiveRecordingSession || viewModel.recorder.isBusy || |
| 168 | viewModel.operationInProgress || viewModel.isGeneratingSummary || LocalAppModel.shared.busy |
| 169 | ) { |
| 170 | isPresented = false |
| 171 | Task { |
| 172 | await parent.handleSummarizeAction(templateKey: note.summaryType ?? LocalSummaryTemplate.auto.key) |
| 173 | } |
| 174 | } |
| 175 | .accessibilityIdentifier("noteDetailRegenerateSummaryMenuButton") |
| 176 | } |
| 177 | Divider() |
| 178 | .padding(.vertical, 4) |
| 179 | } |
| 180 | |
| 181 | MenuItemView(icon: "bubble.left.and.bubble.right", title: "Ask AI", |
| 182 | disabled: (!parent.hasContentToSummarize && !hasSummary) || parent.hasActiveRecordingSession || viewModel.isGeneratingSummary) { |
| 183 | isPresented = false |
| 184 | parent.showingAskAI = true |
| 185 | }.accessibilityIdentifier("noteDetailAskAIMenuItem") |
| 186 | |
| 187 | // Copy — context-aware, copies current tab content |
| 188 | MenuItemView( |
| 189 | icon: "doc.on.doc", |
| 190 | title: "Copy", |
| 191 | subtitle: DetailTab(rawValue: parent.selectedTab)?.title |
| 192 | ) { |
| 193 | parent.copyCurrentTabContent() |
| 194 | isPresented = false |
| 195 | } |
| 196 | |
| 197 | // Save to File — inline-expanding submenu |
| 198 | MenuItemView( |
| 199 | icon: "square.and.arrow.up", |
| 200 | title: "Save to File...", |
| 201 | hasSubmenu: true, |
| 202 | submenuExpanded: showExportSubmenu |
| 203 | ) { |
| 204 | withAnimation(.easeInOut(duration: 0.15)) { |
| 205 | showExportSubmenu.toggle() |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if showExportSubmenu { |
| 210 | if showsSummaryActions { |
| 211 | MenuItemView( |
| 212 | icon: "doc.richtext", |
| 213 | title: "Summary as PDF" |
| 214 | ) { |
| 215 | parent.exportSummaryPDF() |
| 216 | isPresented = false |
| 217 | } |
| 218 | .accessibilityIdentifier("exportSummaryPDFButton") |
| 219 | |
| 220 | MenuItemView( |
| 221 | icon: "text.badge.checkmark", |
| 222 | title: "Summary Only" |
| 223 | ) { |
| 224 | parent.exportSummary() |
| 225 | isPresented = false |
| 226 | } |
| 227 | .accessibilityIdentifier("exportSummaryButton") |
| 228 | } |
| 229 | |
| 230 | if let transcription = note.transcription, |
| 231 | !transcription.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { |
| 232 | MenuItemView( |
| 233 | icon: "text.badge.checkmark", |
| 234 | title: "Transcript Only" |
| 235 | ) { |
| 236 | parent.exportTranscript() |
| 237 | isPresented = false |
| 238 | } |
| 239 | .accessibilityIdentifier("exportTranscriptButton") |
| 240 | } |
| 241 | |
| 242 | MenuItemView( |
| 243 | icon: "doc.plaintext", |
| 244 | title: "Full Note (Plain Text)" |
| 245 | ) { |
| 246 | parent.exportAsPlainText() |
| 247 | isPresented = false |
| 248 | } |
| 249 | |
| 250 | MenuItemView( |
| 251 | icon: "text.badge.checkmark", |
| 252 | title: "Full Note (Formatted)" |
| 253 | ) { |
| 254 | parent.exportAsMarkdown() |
| 255 | isPresented = false |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if InkspringIdeaLink.isInstalled { |
| 260 | MenuItemView(icon: "lightbulb", title: "Post Idea to Inkspring", |
| 261 | disabled: !parent.hasContentToSummarize && !showsSummaryActions) { |
| 262 | isPresented = false |
| 263 | InkspringIdeaLink.send(title: note.title, summary: note.summary, transcript: note.transcription) |
| 264 | }.accessibilityIdentifier("noteDetailPostIdeaToInkspringMenuItem") |
| 265 | } |
| 266 | |
| 267 | Divider() |
| 268 | .padding(.vertical, 4) |
| 269 | |
| 270 | if showsSummaryActions { |
| 271 | MenuItemView( |
| 272 | icon: "pencil", |
| 273 | title: "Edit Summary", |
| 274 | disabled: parent.hasActiveRecordingSession || viewModel.isGeneratingSummary |
| 275 | ) { |
| 276 | parent.requestSummaryInlineEdit() |
| 277 | isPresented = false |
| 278 | } |
| 279 | .accessibilityIdentifier("noteDetailEditSummaryMenuItem") |
| 280 | } |
| 281 | |
| 282 | if parent.hasContentToSummarize { |
| 283 | MenuItemView(icon: "pencil", title: "Edit Transcript", |
| 284 | disabled: parent.hasActiveRecordingSession || viewModel.recorder.isBusy || |
| 285 | viewModel.operationInProgress || viewModel.isGeneratingSummary || LocalAppModel.shared.busy) { |
| 286 | parent.selectedTab = DetailTab.transcript.rawValue |
| 287 | parent.isEditingTranscript = true |
| 288 | isPresented = false |
| 289 | }.accessibilityIdentifier("noteDetailEditTranscriptMenuItem") |
| 290 | } |
| 291 | |
| 292 | // Delete |
| 293 | MenuItemView( |
| 294 | icon: "trash", |
| 295 | title: String(localized: "Move to Trash"), |
| 296 | isDestructive: true |
| 297 | ) { |
| 298 | isPresented = false |
| 299 | viewModel.performContextualDelete(for: note) |
| 300 | }.accessibilityIdentifier("noteDetailMoveToTrash") |
| 301 | } |
| 302 | } |
| 303 | .padding(6) |
| 304 | .frame(width: mainMenuWidth) |
| 305 | .modifier(MacNoteMenuSurface()) |
| 306 | .overlay(alignment: .topLeading) { |
| 307 | if showTemplateSubmenu && showsTemplateActions { |
| 308 | TemplateSubmenu( |
| 309 | note: note, |
| 310 | availableTemplates: parent.availableTemplates, |
| 311 | onSelectTemplate: { templateKey in |
| 312 | applyTemplateAndRegenerate(templateKey) |
| 313 | }, onManageTemplates: { create in |
| 314 | isPresented = false |
| 315 | parent.templatePresentation = create ? .create : .manage |
| 316 | } |
| 317 | ) |
| 318 | // The overlay only offers the shorter main menu's height; without this the rows are squeezed. |
| 319 | .fixedSize(horizontal: false, vertical: true) |
| 320 | .modifier(MacNoteMenuSurface()) |
| 321 | .offset( |
| 322 | x: -(TemplateSubmenu.width + submenuGap), |
| 323 | y: max(0, templateMenuFrame.minY - 8) |
| 324 | ) |
| 325 | } |
| 326 | } |
| 327 | .zIndex(1) |
| 328 | .onChange(of: parent.selectedTab) { _, _ in |
| 329 | showTemplateSubmenu = false |
| 330 | showExportSubmenu = false |
| 331 | } |
| 332 | .onDisappear { |
| 333 | showTemplateSubmenu = false |
| 334 | showExportSubmenu = false |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | } |