Jotley / app · jotley.ai / macOS / JotleyAppDelegate.swift
| 1 | import AppKit |
| 2 | import CoreData |
| 3 | import Combine |
| 4 | import Sparkle |
| 5 | let kJotleyObsidianSyncFolderBookmarkDefaultsKey = "jotleyObsidianSyncFolderBookmark" |
| 6 | @MainActor final class JotleyAppDelegate: NSObject, NSApplicationDelegate { |
| 7 | static var shared: JotleyAppDelegate? |
| 8 | override init() { super.init(); Self.shared = self } |
| 9 | private var statusItem: NSStatusItem? |
| 10 | private var statusMenu: NSMenu? |
| 11 | private var primaryRecordingMenuItem: NSMenuItem? |
| 12 | private var recorderStateObserver: NSObjectProtocol? |
| 13 | var recordingNotesViewModel: NotesListViewModel? |
| 14 | func applicationDidFinishLaunching(_ notification: Notification) { |
| 15 | JotleySummaryNotifications.shared.install() |
| 16 | setupStatusItem() |
| 17 | JotleyUpdates.shared.start() |
| 18 | } |
| 19 | weak var activeNotesViewModel: NotesListViewModel? |
| 20 | func bringMainWindowToFrontAndEnsureSingle() { |
| 21 | NSApp.activate(ignoringOtherApps: true) |
| 22 | NSApp.windows.first(where: { $0.canBecomeMain })?.makeKeyAndOrderFront(nil) |
| 23 | } |
| 24 | func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { |
| 25 | NotificationCenter.default.post(name: Notification.Name("JotleyForceAllEditorsSave"), object: nil) |
| 26 | Task { @MainActor in |
| 27 | await (recordingNotesViewModel ?? activeNotesViewModel)?.stopRecording() |
| 28 | do { |
| 29 | try LocalAppModel.shared.store?.save() |
| 30 | await LocalAppModel.shared.mcp.setEnabled(false, persistPreference: false) |
| 31 | sender.reply(toApplicationShouldTerminate: true) |
| 32 | } |
| 33 | catch { sender.reply(toApplicationShouldTerminate: false) } |
| 34 | } |
| 35 | return .terminateLater |
| 36 | } |
| 37 | |
| 38 | func setupStatusItem() { |
| 39 | let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) |
| 40 | |
| 41 | if let button = statusItem.button { |
| 42 | button.image = statusItemImage() |
| 43 | button.toolTip = "Jotley — Start Transcription" |
| 44 | button.target = self |
| 45 | button.action = #selector(handleStatusItemButtonClick(_:)) |
| 46 | button.sendAction(on: [.leftMouseUp, .rightMouseUp]) |
| 47 | } |
| 48 | |
| 49 | let menu = NSMenu() |
| 50 | |
| 51 | let primaryRecordingItem = NSMenuItem(title: "Start Transcription", action: #selector(handlePrimaryRecordingActionFromStatusItem), keyEquivalent: "") |
| 52 | primaryRecordingItem.target = self |
| 53 | menu.addItem(primaryRecordingItem) |
| 54 | primaryRecordingMenuItem = primaryRecordingItem |
| 55 | |
| 56 | menu.addItem(.separator()) |
| 57 | |
| 58 | let openItem = NSMenuItem(title: "Open Jotley", action: #selector(handleOpenFromStatusItem), keyEquivalent: "") |
| 59 | openItem.target = self |
| 60 | menu.addItem(openItem) |
| 61 | |
| 62 | menu.addItem(.separator()) |
| 63 | |
| 64 | let quitItem = NSMenuItem(title: "Quit Jotley", action: #selector(handleQuitFromStatusItem), keyEquivalent: "q") |
| 65 | quitItem.target = self |
| 66 | menu.addItem(quitItem) |
| 67 | |
| 68 | statusMenu = menu |
| 69 | self.statusItem = statusItem |
| 70 | setupRecorderStateObserver() |
| 71 | refreshStatusItemState() |
| 72 | } |
| 73 | |
| 74 | func setupRecorderStateObserver() { |
| 75 | if let recorderStateObserver { |
| 76 | NotificationCenter.default.removeObserver(recorderStateObserver) |
| 77 | } |
| 78 | |
| 79 | recorderStateObserver = NotificationCenter.default.addObserver( |
| 80 | forName: .processTapRecordingStateChanged, |
| 81 | object: nil, |
| 82 | queue: .main |
| 83 | ) { [weak self] _ in |
| 84 | self?.refreshStatusItemState() |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @objc func handleStatusItemButtonClick(_ sender: NSStatusBarButton) { |
| 89 | refreshStatusItemState() |
| 90 | guard let statusItem else { return } |
| 91 | |
| 92 | if let menu = statusMenu { |
| 93 | statusItem.popUpMenu(menu) |
| 94 | } else { |
| 95 | handlePrimaryRecordingActionFromStatusItem() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @objc func handlePrimaryRecordingActionFromStatusItem() { |
| 100 | if LocalAppModel.shared.recorder.isRecording { |
| 101 | Task { @MainActor in |
| 102 | await (recordingNotesViewModel ?? activeNotesViewModel)?.stopRecording() |
| 103 | refreshStatusItemState() |
| 104 | } |
| 105 | } else { |
| 106 | handleQuickRecordFromStatusItem() |
| 107 | refreshStatusItemState() |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @objc func handleQuickRecordFromStatusItem() { |
| 112 | // Call the single recording coordinator directly. (Previously this posted |
| 113 | // a process-wide `.createNewNoteFromMeeting` broadcast that every live |
| 114 | // NotesListViewModel handled, starting one stream each.) |
| 115 | bringMainWindowToFrontAndEnsureSingle() |
| 116 | Task { await activeNotesViewModel?.startNewNote() } |
| 117 | } |
| 118 | |
| 119 | @objc func handleOpenFromStatusItem() { |
| 120 | bringMainWindowToFrontAndEnsureSingle() |
| 121 | } |
| 122 | |
| 123 | @objc func handleQuitFromStatusItem() { |
| 124 | NSApp.terminate(nil) |
| 125 | } |
| 126 | |
| 127 | func refreshStatusItemState() { |
| 128 | let isRecording = LocalAppModel.shared.recorder.isRecording |
| 129 | primaryRecordingMenuItem?.title = isRecording ? "Stop Transcription" : "Start Transcription" |
| 130 | |
| 131 | guard let button = statusItem?.button else { return } |
| 132 | button.toolTip = isRecording ? "Jotley — Transcribing (open menu to stop)" : "Jotley — Start Transcription" |
| 133 | button.setAccessibilityLabel(isRecording ? "Jotley — Transcribing" : "Jotley — Transcription") |
| 134 | } |
| 135 | |
| 136 | private func statusItemImage() -> NSImage? { |
| 137 | let configuration = NSImage.SymbolConfiguration(pointSize: 15, weight: .semibold) |
| 138 | let image = NSImage(systemSymbolName: "note.text", accessibilityDescription: "Jotley Transcription")? |
| 139 | .withSymbolConfiguration(configuration) |
| 140 | image?.isTemplate = true |
| 141 | return image |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | // Sparkle is enabled by the direct-download release configuration, not by |
| 147 | // unsigned source builds or App Store variants. No note/audio content is sent. |
| 148 | @MainActor final class JotleyUpdates: NSObject, ObservableObject, SPUUpdaterDelegate { |
| 149 | static let shared = JotleyUpdates() |
| 150 | @Published private(set) var canCheck = false |
| 151 | @Published private(set) var automaticChecks = false |
| 152 | private var controller: SPUStandardUpdaterController? |
| 153 | private var observations: [NSKeyValueObservation] = [] |
| 154 | private var postponedInstall: (() -> Void)? |
| 155 | private var installTimer: Timer? |
| 156 | var isEnabled: Bool { Bundle.main.object(forInfoDictionaryKey: "JotleyEnableSparkle") as? Bool == true } |
| 157 | static func allowsUpdate(recording: Bool, recorderBusy: Bool, processing: Bool) -> Bool { |
| 158 | !recording && !recorderBusy && !processing |
| 159 | } |
| 160 | var isIdle: Bool { |
| 161 | Self.allowsUpdate(recording: LocalAppModel.shared.recorder.recording, |
| 162 | recorderBusy: LocalAppModel.shared.recorder.isBusy, |
| 163 | processing: LocalAppModel.shared.busy || JotleyProcessing.shared.count > 0) |
| 164 | } |
| 165 | func start() { |
| 166 | guard isEnabled, controller == nil else { return } |
| 167 | let controller = SPUStandardUpdaterController(startingUpdater: false, updaterDelegate: self, userDriverDelegate: nil) |
| 168 | self.controller = controller |
| 169 | observations = [ |
| 170 | controller.updater.observe(\.canCheckForUpdates, options: [.initial, .new]) { [weak self] updater, _ in |
| 171 | Task { @MainActor in self?.canCheck = updater.canCheckForUpdates } |
| 172 | }, |
| 173 | controller.updater.observe(\.automaticallyChecksForUpdates, options: [.initial, .new]) { [weak self] updater, _ in |
| 174 | Task { @MainActor in self?.automaticChecks = updater.automaticallyChecksForUpdates } |
| 175 | } |
| 176 | ] |
| 177 | controller.startUpdater() |
| 178 | } |
| 179 | func check() { guard isIdle else { return }; controller?.updater.checkForUpdates() } |
| 180 | func setAutomaticChecks(_ enabled: Bool) { controller?.updater.automaticallyChecksForUpdates = enabled } |
| 181 | func updater(_ updater: SPUUpdater, mayPerform updateCheck: SPUUpdateCheck) throws { |
| 182 | guard isIdle else { throw LocalError.message("Finish transcription or processing your note before checking for updates.") } |
| 183 | } |
| 184 | func updater(_ updater: SPUUpdater, shouldPostponeRelaunchForUpdate item: SUAppcastItem, untilInvokingBlock installHandler: @escaping () -> Void) -> Bool { |
| 185 | guard !isIdle else { return false } |
| 186 | postponedInstall = installHandler |
| 187 | installTimer?.invalidate() |
| 188 | installTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in |
| 189 | Task { @MainActor in |
| 190 | guard let self, self.isIdle else { return } |
| 191 | self.installTimer?.invalidate(); self.installTimer = nil |
| 192 | let install = self.postponedInstall; self.postponedInstall = nil |
| 193 | install?() |
| 194 | } |
| 195 | } |
| 196 | return true |
| 197 | } |
| 198 | } |