Jotley / app / macOS / Views / NoteDetailPlaceholderView+ActionsPopoverTemplateSubmenu.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | extension NoteDetailPlaceholderView { |
| 4 | struct TemplateSubmenu: View { |
| 5 | static let width: CGFloat = 300 |
| 6 | |
| 7 | let note: Note |
| 8 | let availableTemplates: [Template] |
| 9 | let onSelectTemplate: (String) -> Void |
| 10 | var onManageTemplates: (Bool) -> Void = { _ in } |
| 11 | |
| 12 | @Environment(\.colorScheme) private var colorScheme |
| 13 | @ObservedObject private var theme = ThemeManager.shared |
| 14 | @State private var hoveredItem: String? |
| 15 | |
| 16 | private var accent: Color { theme.currentThemeColor.color(in: colorScheme) } |
| 17 | private var currentTemplateKey: String { |
| 18 | (try? SummaryTemplateStore.shared.resolve(note.summaryType).key) ?? note.summaryType ?? LocalSummaryTemplate.auto.key |
| 19 | } |
| 20 | |
| 21 | var body: some View { |
| 22 | VStack(alignment: .leading, spacing: 10) { |
| 23 | Text("Summary Template") |
| 24 | .font(.headline) |
| 25 | .accessibilityAddTraits(.isHeader) |
| 26 | .padding(.horizontal, 8) |
| 27 | |
| 28 | templateRow( |
| 29 | key: LocalSummaryTemplate.auto.key, |
| 30 | title: LocalSummaryTemplate.auto.title, |
| 31 | icon: "sparkles", |
| 32 | detail: "Choose a format based on this note." |
| 33 | ) |
| 34 | |
| 35 | Divider() |
| 36 | |
| 37 | ScrollView { |
| 38 | VStack(alignment: .leading, spacing: 10) { |
| 39 | section("My Templates", icon: "person.crop.circle", templates: availableTemplates.filter { $0.key.hasPrefix("custom/") }) |
| 40 | section("Notes & Study", icon: "text.alignleft", templates: availableTemplates.filter { |
| 41 | $0.key != LocalSummaryTemplate.auto.key && !$0.key.hasPrefix("custom/") && $0.category == nil |
| 42 | }) |
| 43 | section("Meetings & Projects", icon: "person.2", templates: availableTemplates.filter { $0.category != nil && !$0.key.hasPrefix("custom/") }) |
| 44 | } |
| 45 | } |
| 46 | .scrollBounceBehavior(.basedOnSize) |
| 47 | .frame(height: 286) |
| 48 | |
| 49 | Divider() |
| 50 | |
| 51 | HStack(spacing: 8) { |
| 52 | Button { onManageTemplates(true) } label: { |
| 53 | Label("New Template", systemImage: "plus") |
| 54 | } |
| 55 | Spacer(minLength: 0) |
| 56 | Button { onManageTemplates(false) } label: { |
| 57 | Label("Manage", systemImage: "slider.horizontal.3") |
| 58 | } |
| 59 | .accessibilityLabel("Manage Templates") |
| 60 | } |
| 61 | .font(.callout.weight(.medium)) |
| 62 | .tint(accent) |
| 63 | .padding(.horizontal, 4) |
| 64 | } |
| 65 | .padding(12) |
| 66 | .frame(width: Self.width) |
| 67 | } |
| 68 | |
| 69 | @ViewBuilder private func section(_ title: String, icon: String, templates: [Template]) -> some View { |
| 70 | if !templates.isEmpty { |
| 71 | VStack(alignment: .leading, spacing: 2) { |
| 72 | Label(title, systemImage: icon) |
| 73 | .font(.caption.weight(.semibold)) |
| 74 | .foregroundStyle(.secondary) |
| 75 | .accessibilityAddTraits(.isHeader) |
| 76 | .padding(.horizontal, 8) |
| 77 | .padding(.bottom, 3) |
| 78 | ForEach(templates, id: \.key) { template in |
| 79 | templateRow(key: template.key, title: template.title) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private func templateRow(key: String, title: String, icon: String? = nil, detail: String? = nil) -> some View { |
| 86 | let selected = currentTemplateKey == key |
| 87 | let shape = RoundedRectangle(cornerRadius: 8) |
| 88 | return Button { onSelectTemplate(key) } label: { |
| 89 | HStack(spacing: 8) { |
| 90 | if let icon { |
| 91 | Image(systemName: icon) |
| 92 | .font(.system(size: 16, weight: .medium)) |
| 93 | .foregroundStyle(accent) |
| 94 | } |
| 95 | VStack(alignment: .leading, spacing: 3) { |
| 96 | Text(title).font(.callout.weight(selected || icon != nil ? .semibold : .regular)) |
| 97 | if let detail { |
| 98 | Text(detail).font(.caption).foregroundStyle(.secondary) |
| 99 | } |
| 100 | } |
| 101 | Spacer(minLength: 4) |
| 102 | if selected { |
| 103 | Image(systemName: "checkmark") |
| 104 | .font(.caption.weight(.semibold)) |
| 105 | .foregroundStyle(accent) |
| 106 | .accessibilityHidden(true) |
| 107 | } |
| 108 | } |
| 109 | .foregroundStyle(.primary) |
| 110 | .padding(.horizontal, 8) |
| 111 | .padding(.vertical, icon == nil ? 5 : 8) |
| 112 | .frame(minHeight: 28) |
| 113 | .background(hoveredItem == key || selected ? Color.primary.opacity(0.06) : .clear, in: shape) |
| 114 | .overlay { shape.strokeBorder(selected ? accent : .clear, lineWidth: 1).allowsHitTesting(false) } |
| 115 | .contentShape(shape) |
| 116 | } |
| 117 | .buttonStyle(.plain) |
| 118 | .onHover { hovering in |
| 119 | if hovering { hoveredItem = key } |
| 120 | else if hoveredItem == key { hoveredItem = nil } |
| 121 | } |
| 122 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 123 | .accessibilityIdentifier("noteTemplate-\(key)") |
| 124 | } |
| 125 | } |
| 126 | } |