()
| 303 | |
| 304 | // 主应用程序事件 |
| 305 | mainAppEvents() { |
| 306 | app.whenReady().then(async () => { |
| 307 | console.log('[Main] App is ready') |
| 308 | // 设置Windows应用程序用户模型id |
| 309 | if (process.platform === 'win32') app.setAppUserModelId(app.getName()) |
| 310 | |
| 311 | // 启动 Internal API Server(硬依赖:失败则退出应用) |
| 312 | try { |
| 313 | await startInternalServer(getPathProvider()) |
| 314 | registerInternalApiIpc() |
| 315 | console.log('[Main] Internal API Server ready') |
| 316 | } catch (err) { |
| 317 | console.error('[Main] Internal API Server failed to start:', err) |
| 318 | dialog.showErrorBox( |
| 319 | 'ChatLab Internal Server Error', |
| 320 | `Internal API Server failed to start. The application cannot continue.\n\n${err instanceof Error ? err.message : String(err)}` |
| 321 | ) |
| 322 | app.quit() |
| 323 | return |
| 324 | } |
| 325 | |
| 326 | // 创建主窗口 |
| 327 | console.log('[Main] Creating window...') |
| 328 | await this.createWindow() |
| 329 | console.log('[Main] Window created') |
| 330 | |
| 331 | // 检查更新逻辑 |
| 332 | checkUpdate(this.mainWindow) |
| 333 | |
| 334 | // 引入主进程ipcMain |
| 335 | if (this.mainWindow) { |
| 336 | console.log('[Main] Registering IPC handlers...') |
| 337 | mainIpcMain(this.mainWindow) |
| 338 | console.log('[Main] IPC handlers registered') |
| 339 | } |
| 340 | |
| 341 | // 开发环境下 F12 打开控制台 |
| 342 | app.on('browser-window-created', (_, window) => { |
| 343 | optimizer.watchWindowShortcuts(window) |
| 344 | }) |
| 345 | |
| 346 | app.on('activate', () => { |
| 347 | // 在 macOS 上,当单击 Dock 图标且没有其他窗口时,通常会重新创建窗口 |
| 348 | if (BrowserWindow.getAllWindows().length === 0) { |
| 349 | this.createWindow() |
| 350 | return |
| 351 | } |
| 352 | |
| 353 | if (platform.isMacOS) { |
| 354 | this.mainWindow?.show() |
| 355 | } |
| 356 | }) |
| 357 | |
| 358 | // 监听渲染进程崩溃 |
| 359 | app.on('render-process-gone', (_event, w, d) => { |
| 360 | if (d.reason == 'crashed') { |
| 361 | w.reload() |
| 362 | } |
no test coverage detected