|
| 1 | +import { |
| 2 | + app, |
| 3 | + systemPreferences, |
| 4 | + BrowserWindow, |
| 5 | + globalShortcut, |
| 6 | +} from "electron"; |
| 7 | +import { initializeDatabase } from "../../db/config"; |
| 8 | +import { logger } from "../logger"; |
| 9 | +import { WindowManager } from "./window-manager"; |
| 10 | +import { setupApplicationMenu } from "../menu"; |
| 11 | +import { ServiceManager } from "../managers/service-manager"; |
| 12 | +import { createIPCHandler } from "electron-trpc-experimental/main"; |
| 13 | +import { router } from "../../trpc/router"; |
| 14 | +import { EventHandlers } from "./event-handlers"; |
| 15 | + |
| 16 | +export class AppManager { |
| 17 | + private windowManager: WindowManager; |
| 18 | + private serviceManager: ServiceManager; |
| 19 | + |
| 20 | + constructor() { |
| 21 | + this.windowManager = new WindowManager(); |
| 22 | + this.serviceManager = ServiceManager.createInstance(); |
| 23 | + this.windowManager.setMainWindowCreatedCallback( |
| 24 | + this.onMainWindowCreated.bind(this), |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + async initialize(): Promise<void> { |
| 29 | + try { |
| 30 | + await this.initializeDatabase(); |
| 31 | + await this.requestPermissions(); |
| 32 | + await this.serviceManager.initialize(this.windowManager); |
| 33 | + this.exposeGlobalServices(); |
| 34 | + await this.setupWindows(); |
| 35 | + await this.setupMenu(); |
| 36 | + |
| 37 | + // Setup event handlers |
| 38 | + const eventHandlers = new EventHandlers(this); |
| 39 | + eventHandlers.setupEventHandlers(); |
| 40 | + |
| 41 | + // Schedule auto-update check after startup |
| 42 | + this.scheduleAutoUpdateCheck(); |
| 43 | + |
| 44 | + logger.main.info("Application initialized successfully"); |
| 45 | + } catch (error) { |
| 46 | + logger.main.error("Error initializing app:", error); |
| 47 | + throw error; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private async initializeDatabase(): Promise<void> { |
| 52 | + await initializeDatabase(); |
| 53 | + logger.db.info( |
| 54 | + "Database initialized and migrations completed successfully", |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + private async requestPermissions(): Promise<void> { |
| 59 | + if (process.platform === "darwin") { |
| 60 | + const accessibilityEnabled = |
| 61 | + systemPreferences.isTrustedAccessibilityClient(false); |
| 62 | + if (!accessibilityEnabled) { |
| 63 | + logger.main.debug( |
| 64 | + "Please enable accessibility permissions in System Preferences > Security & Privacy > Privacy > Accessibility", |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const microphoneEnabled = |
| 70 | + systemPreferences.getMediaAccessStatus("microphone"); |
| 71 | + logger.main.info("Microphone access status:", { |
| 72 | + status: microphoneEnabled, |
| 73 | + }); |
| 74 | + |
| 75 | + if (microphoneEnabled !== "granted") { |
| 76 | + await systemPreferences.askForMediaAccess("microphone"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private async setupWindows(): Promise<void> { |
| 81 | + this.windowManager.createWidgetWindow(); |
| 82 | + this.setupTRPCHandler(); |
| 83 | + |
| 84 | + if (process.platform === "darwin" && app.dock) { |
| 85 | + app.dock.show(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private setupTRPCHandler(): Promise<void> { |
| 90 | + const windows = this.windowManager |
| 91 | + .getAllWindows() |
| 92 | + .filter((w): w is BrowserWindow => w !== null); |
| 93 | + createIPCHandler({ router, windows }); |
| 94 | + return Promise.resolve(); |
| 95 | + } |
| 96 | + |
| 97 | + updateTRPCHandler(): void { |
| 98 | + const windows = this.windowManager |
| 99 | + .getAllWindows() |
| 100 | + .filter((w): w is BrowserWindow => w !== null); |
| 101 | + createIPCHandler({ router, windows }); |
| 102 | + } |
| 103 | + |
| 104 | + private async setupMenu(): Promise<void> { |
| 105 | + setupApplicationMenu( |
| 106 | + () => this.windowManager.createOrShowMainWindow(), |
| 107 | + () => { |
| 108 | + const autoUpdaterService = this.serviceManager.getAutoUpdaterService(); |
| 109 | + if (autoUpdaterService) { |
| 110 | + autoUpdaterService.checkForUpdates(true); |
| 111 | + } |
| 112 | + }, |
| 113 | + () => this.windowManager.openAllDevTools(), |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + private exposeGlobalServices(): void { |
| 118 | + // Make services available globally for tRPC (temporary solution) |
| 119 | + const transcriptionService = this.serviceManager.getTranscriptionService(); |
| 120 | + const autoUpdaterService = this.serviceManager.getAutoUpdaterService(); |
| 121 | + const settingsService = this.serviceManager.getSettingsService(); |
| 122 | + const swiftBridge = this.serviceManager.getSwiftIOBridge(); |
| 123 | + |
| 124 | + (globalThis as any).modelManagerService = |
| 125 | + this.serviceManager.getModelManagerService(); |
| 126 | + (globalThis as any).transcriptionService = transcriptionService; |
| 127 | + (globalThis as any).settingsService = settingsService; |
| 128 | + (globalThis as any).logger = logger; |
| 129 | + (globalThis as any).autoUpdaterService = autoUpdaterService; |
| 130 | + (globalThis as any).swiftBridge = swiftBridge; |
| 131 | + } |
| 132 | + |
| 133 | + getWindowManager(): WindowManager { |
| 134 | + return this.windowManager; |
| 135 | + } |
| 136 | + |
| 137 | + getServiceManager(): ServiceManager { |
| 138 | + return this.serviceManager; |
| 139 | + } |
| 140 | + |
| 141 | + getTranscriptionService(): any { |
| 142 | + return this.serviceManager.getTranscriptionService(); |
| 143 | + } |
| 144 | + |
| 145 | + getSwiftIOBridge(): any { |
| 146 | + return this.serviceManager.getSwiftIOBridge(); |
| 147 | + } |
| 148 | + |
| 149 | + getAutoUpdaterService(): any { |
| 150 | + return this.serviceManager.getAutoUpdaterService(); |
| 151 | + } |
| 152 | + |
| 153 | + private scheduleAutoUpdateCheck(): void { |
| 154 | + // Check for updates on startup (after a brief delay) |
| 155 | + setTimeout(() => { |
| 156 | + try { |
| 157 | + const autoUpdaterService = this.serviceManager.getAutoUpdaterService(); |
| 158 | + autoUpdaterService.checkForUpdatesAndNotify(); |
| 159 | + } catch (error) { |
| 160 | + logger.main.warn("Auto-update check failed during startup", { |
| 161 | + error: error instanceof Error ? error.message : String(error), |
| 162 | + }); |
| 163 | + } |
| 164 | + }, 5000); // Wait 5 seconds after startup |
| 165 | + } |
| 166 | + |
| 167 | + private onMainWindowCreated(window: BrowserWindow): void { |
| 168 | + this.updateTRPCHandler(); |
| 169 | + } |
| 170 | + |
| 171 | + async cleanup(): Promise<void> { |
| 172 | + globalShortcut.unregisterAll(); |
| 173 | + await this.serviceManager.cleanup(); |
| 174 | + if (this.windowManager) { |
| 175 | + this.windowManager.cleanup(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + handleActivate(): void { |
| 180 | + const allWindows = this.windowManager.getAllWindows(); |
| 181 | + |
| 182 | + if (allWindows.every((w) => !w || w.isDestroyed())) { |
| 183 | + this.windowManager.createWidgetWindow(); |
| 184 | + } else { |
| 185 | + const widgetWindow = this.windowManager.getWidgetWindow(); |
| 186 | + if (!widgetWindow || widgetWindow.isDestroyed()) { |
| 187 | + this.windowManager.createWidgetWindow(); |
| 188 | + } else { |
| 189 | + widgetWindow.show(); |
| 190 | + } |
| 191 | + this.windowManager.createOrShowMainWindow(); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments