lifecycle.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { app as electronApp, screen } from 'electron';
  2. import { logger } from 'ee-core/log';
  3. import { getConfig } from 'ee-core/config';
  4. import { getMainWindow } from 'ee-core/electron';
  5. import { disposeCodex } from '../service/codex';
  6. import { locateCodexBinary } from '../service/codex/codexLocator';
  7. import { focusSmartWindow } from '../service/window/smartWindow';
  8. class Lifecycle {
  9. /**
  10. * core app have been loaded
  11. */
  12. async ready(): Promise<void> {
  13. logger.info('[lifecycle] ready');
  14. try {
  15. // 只解析路径、不 spawn:打包后 asar / asar.unpacked 的定位问题一眼可见
  16. logger.info('[lifecycle] codex 二进制:', await locateCodexBinary());
  17. } catch (error) {
  18. logger.error('[lifecycle] codex 二进制不可用:', error instanceof Error ? error.message : String(error));
  19. }
  20. }
  21. /**
  22. * electron app ready
  23. */
  24. async electronAppReady(): Promise<void> {
  25. logger.info('[lifecycle] electron-app-ready');
  26. // When double clicking the icon, display the opened windows
  27. electronApp.on('second-instance', () => {
  28. const win = getMainWindow();
  29. if (win.isMinimized()) {
  30. win.restore();
  31. }
  32. win.show();
  33. // 智能工作台开着时优先把用户拉回工作台,否则聚焦主窗口
  34. if (!focusSmartWindow()) {
  35. win.focus();
  36. }
  37. });
  38. }
  39. /**
  40. * main window have been loaded
  41. */
  42. async windowReady(): Promise<void> {
  43. logger.info('[lifecycle] window-ready');
  44. const win = getMainWindow();
  45. // The window is centered and scaled proportionally
  46. // Obtain the size information of the main screen, calculate the width and height of the window as a percentage of the screen,
  47. // and calculate the coordinates of the upper left corner when the window is centered
  48. const mainScreen = screen.getPrimaryDisplay();
  49. const { width, height } = mainScreen.workAreaSize;
  50. const windowWidth = Math.floor(width * 0.6);
  51. const windowHeight = Math.floor(height * 0.7);
  52. const x = Math.floor((width - windowWidth) / 2);
  53. const y = Math.floor((height - windowHeight) / 2);
  54. win.setBounds({ x, y, width: windowWidth, height: windowHeight })
  55. // Delayed loading, no white screen
  56. const { windowsOption } = getConfig();
  57. if (windowsOption.show == false) {
  58. win.once('ready-to-show', () => {
  59. win.show();
  60. win.focus();
  61. })
  62. }
  63. }
  64. /**
  65. * before app close
  66. */
  67. async beforeClose(): Promise<void> {
  68. logger.info('[lifecycle] before-close');
  69. try {
  70. // 回收 codex app-server 子进程,避免退出后留下孤儿进程
  71. await disposeCodex();
  72. } catch (error) {
  73. logger.error('[lifecycle] dispose codex failed:', error instanceof Error ? error.message : String(error));
  74. }
  75. }
  76. }
  77. export {
  78. Lifecycle
  79. };