Jotley / app · jotley.ai / macOS / JotleyApp.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import CoreData |
| 4 | import FoundationModels |
| 5 | |
| 6 | @main enum JotleyMain { |
| 7 | @MainActor static func main() async { |
| 8 | if CommandLine.arguments.contains("--mcp-stdio") { |
| 9 | await JotleyMCPStdioBridge.run() |
| 10 | } else { |
| 11 | #if DEBUG |
| 12 | if let fixture = ProcessInfo.processInfo.environment["JOTLEY_SCREENSHOT_DATA"] { |
| 13 | do { |
| 14 | LocalAppModel.isolatedStoreForTesting = try screenshotLibrary(fixture) |
| 15 | } catch { |
| 16 | fatalError("Could not create the isolated screenshot library: \(error)") |
| 17 | } |
| 18 | } |
| 19 | #endif |
| 20 | JotleyApp.main() |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | #if DEBUG |
| 25 | @MainActor private static func screenshotLibrary(_ fixture: String) throws -> SharedPersistenceController { |
| 26 | struct Sample: Decodable { |
| 27 | let title: String |
| 28 | let folder: String |
| 29 | let tags: [String] |
| 30 | let transcript: String |
| 31 | let summary: String |
| 32 | } |
| 33 | let samples = try JSONDecoder().decode([Sample].self, from: Data(fixture.utf8)) |
| 34 | let store = try SharedPersistenceController(inMemory: true) |
| 35 | for (index, sample) in samples.enumerated() { |
| 36 | guard let note = try store.create(title: sample.title, text: sample.transcript, noteType: "voice") as? Note |
| 37 | else { throw LocalError.message("Could not create a screenshot note.") } |
| 38 | note.timestamp = Date().addingTimeInterval(Double(-index * 3_600)) |
| 39 | note.summary = sample.summary |
| 40 | if !sample.folder.isEmpty { |
| 41 | note.folder = Folder.getOrCreate(named: sample.folder, in: store.context) |
| 42 | } |
| 43 | for tag in sample.tags { try note.addTagByName(tag, in: store.context) } |
| 44 | let utterances = sample.transcript.components(separatedBy: "\n\n").enumerated().map { index, text in |
| 45 | TranscriptUtterance(text: text, start: Double(index * 25), end: Double((index + 1) * 25)) |
| 46 | } |
| 47 | note.setTranscriptUtterances(utterances) |
| 48 | } |
| 49 | try store.save() |
| 50 | return store |
| 51 | } |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | struct JotleyApp: App { |
| 56 | private let openingSize = CGSize(width: 1080, height: 720) |
| 57 | @NSApplicationDelegateAdaptor private var appDelegate: JotleyAppDelegate |
| 58 | @ObservedObject private var local = LocalAppModel.shared |
| 59 | @ObservedObject private var themeManager = ThemeManager.shared |
| 60 | @ObservedObject private var updates = JotleyUpdates.shared |
| 61 | var body: some Scene { |
| 62 | WindowGroup(id: "main") { |
| 63 | if let store = local.store, store.ready { |
| 64 | RootView(recorder: local.recorder) |
| 65 | .environment(\.managedObjectContext, store.context) |
| 66 | .environmentObject(store) |
| 67 | .tint(Color.themeActionText) |
| 68 | .frame(minWidth: 900, minHeight: 600) |
| 69 | } else { Text(local.message).padding().frame(minWidth: 600, minHeight: 300) } |
| 70 | } |
| 71 | .windowStyle(.hiddenTitleBar) |
| 72 | .windowToolbarStyle(.unified(showsTitle: false)) |
| 73 | .defaultSize(width: openingSize.width, height: openingSize.height) |
| 74 | .restorationBehavior(MacScreenshotFixture.isActive ? .disabled : .automatic) |
| 75 | .defaultLaunchBehavior(MacScreenshotFixture.isActive ? .presented : .automatic) |
| 76 | .commands { |
| 77 | if MacScreenshotFixture.isActive { |
| 78 | ScreenshotWindowCommands() |
| 79 | } |
| 80 | CommandGroup(after: .appInfo) { |
| 81 | if updates.isEnabled { |
| 82 | Button("Check for Updates…") { updates.check() } |
| 83 | .disabled(!updates.canCheck || !updates.isIdle) |
| 84 | } |
| 85 | } |
| 86 | CommandGroup(replacing: .newItem) { |
| 87 | Button("New Transcription") { Task { await appDelegate.activeNotesViewModel?.startNewNote() } }.keyboardShortcut("n") |
| 88 | } } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private struct ScreenshotWindowCommands: Commands { |
| 93 | @Environment(\.openWindow) private var openWindow |
| 94 | |
| 95 | var body: some Commands { |
| 96 | CommandGroup(after: .windowArrangement) { |
| 97 | Button("Open Screenshot Window") { openWindow(id: "main") } |
| 98 | .keyboardShortcut("0", modifiers: [.command, .option]) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | enum MacScreenshotFixture { |
| 104 | static var isActive: Bool { |
| 105 | #if DEBUG |
| 106 | ProcessInfo.processInfo.environment["JOTLEY_SCREENSHOT_DATA"] != nil |
| 107 | #else |
| 108 | false |
| 109 | #endif |
| 110 | } |
| 111 | /// Debug fixtures can open Settings at a section, e.g. "general", for screenshots. |
| 112 | static var settingsSection: String? { |
| 113 | #if DEBUG |
| 114 | isActive ? ProcessInfo.processInfo.environment["JOTLEY_SCREENSHOT_SETTINGS"] : nil |
| 115 | #else |
| 116 | nil |
| 117 | #endif |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @MainActor |
| 122 | final class LocalAppModel: ObservableObject { |
| 123 | #if DEBUG |
| 124 | /// Native UI harnesses install an isolated library before shared is first used. |
| 125 | /// They must never open or clean up the user's development library. |
| 126 | static var isolatedStoreForTesting: SharedPersistenceController? |
| 127 | #endif |
| 128 | static let shared = LocalAppModel() |
| 129 | @Published var notes: [NSManagedObject] = [] |
| 130 | @Published var selected: UUID? |
| 131 | @Published var message = "" |
| 132 | @Published var busy = false |
| 133 | @Published var language = "en-US" |
| 134 | @Published var speechStatus = "Checking installed speech models…" |
| 135 | @Published var summaryStatus = "" |
| 136 | let recorder = ProcessTapRecorder() |
| 137 | let mcp = JotleyMCPService() |
| 138 | private(set) var store: SharedPersistenceController? |
| 139 | var current: NSManagedObject? { notes.first { $0.localNoteID == selected } } |
| 140 | var ready: Bool { store?.ready == true } |
| 141 | |
| 142 | init() { |
| 143 | #if DEBUG |
| 144 | if let isolated = Self.isolatedStoreForTesting { |
| 145 | store = isolated |
| 146 | do { try reload() } catch { message = error.localizedDescription } |
| 147 | return |
| 148 | } |
| 149 | #endif |
| 150 | if let reason = JotleyAppleIntelligenceEligibility.unsupportedReason { |
| 151 | message = reason |
| 152 | return |
| 153 | } |
| 154 | do { |
| 155 | store = try SharedPersistenceController(syncMode: NoteSyncMode.application()) |
| 156 | if let reason = store?.errorMessage { message = reason } |
| 157 | try reload() |
| 158 | if let store, store.ready { |
| 159 | do { try LegacyAudioCleanup.remove(in: store.root) } |
| 160 | catch { message = "Could not remove previous audio: \(error.localizedDescription). Your transcript text is preserved." } |
| 161 | } |
| 162 | } catch { message = error.localizedDescription } |
| 163 | Task { |
| 164 | if let store, store.ready { await mcp.attach(store: store) } |
| 165 | await refreshReadiness() |
| 166 | } |
| 167 | } |
| 168 | func reload() throws { |
| 169 | notes = try store?.notes() ?? [] |
| 170 | if selected == nil { selected = notes.first?.localNoteID } |
| 171 | } |
| 172 | func refreshReadiness() async { |
| 173 | guard #available(macOS 26.0, *) else { |
| 174 | speechStatus = "On-device speech requires macOS 26 or later."; return |
| 175 | } |
| 176 | do { |
| 177 | let config = try await LocalSpeech.configuration(locale: Locale(identifier: language)) |
| 178 | speechStatus = "Installed: \(config.engine.rawValue) · \(config.locale.identifier)" |
| 179 | } catch { speechStatus = error.localizedDescription } |
| 180 | summaryStatus = LocalSummary.readiness(locale: Locale(identifier: language)) ?? "On-device summaries ready." |
| 181 | } |
| 182 | func exportObsidian() { |
| 183 | guard let note = current, !busy, !recorder.recording, !recorder.isBusy else { return } |
| 184 | let panel = NSOpenPanel(); panel.canChooseDirectories = true; panel.canChooseFiles = false |
| 185 | panel.prompt = "Export Here"; panel.message = "Choose an Obsidian vault folder for Markdown." |
| 186 | guard panel.runModal() == .OK, let folder = panel.url else { return } |
| 187 | let scoped = folder.startAccessingSecurityScopedResource() |
| 188 | defer { if scoped { folder.stopAccessingSecurityScopedResource() } } |
| 189 | do { |
| 190 | let markdown = "# \(note.text("title"))\n\n\(note.text("summary"))\n\n## Transcript\n\n\(note.text("transcription"))\n\n## Notes\n\n\(note.text("notes"))\n" |
| 191 | try markdown.write(to: folder.appendingPathComponent("Jotley-\(UUID().uuidString).md"), atomically: true, encoding: .utf8) |
| 192 | message = "Markdown exported to the selected folder." |
| 193 | } catch { message = "Export failed: \(error.localizedDescription)" } |
| 194 | } |
| 195 | } |