lifecycle.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { Event } from '../utils/Event'
  6. import { AppEvent } from '../event/AppEvent'
  7. class Lifecycle {
  8. /**
  9. * Core app has been loaded
  10. */
  11. async ready(): Promise<void> {
  12. logger.info('[lifecycle] ready');
  13. }
  14. /**
  15. * Electron app is ready
  16. */
  17. async electronAppReady(): Promise<void> {
  18. logger.info('[lifecycle] electron-app-ready');
  19. // When double clicking the icon, display the opened window
  20. electronApp.on('second-instance', () => {
  21. const win = getMainWindow();
  22. if (win.isMinimized()) {
  23. win.restore();
  24. }
  25. win.show();
  26. win.focus();
  27. });
  28. }
  29. /**
  30. * Main window has been loaded
  31. */
  32. async windowReady(): Promise<void> {
  33. logger.info('[lifecycle] window-ready');
  34. const win = getMainWindow();
  35. // The window is centered and scaled proportionally
  36. // Obtain the size information of the main screen, calculate the width and height of the window as a percentage of the screen,
  37. // and calculate the coordinates of the upper left corner when the window is centered
  38. const mainScreen = screen.getPrimaryDisplay();
  39. const { width, height } = mainScreen.workAreaSize;
  40. const windowWidth = Math.floor(width * 0.6);
  41. const windowHeight = Math.floor(height * 0.8);
  42. const x = Math.floor((width - windowWidth) / 2);
  43. const y = Math.floor((height - windowHeight) / 2);
  44. win.setBounds({ x, y, width: windowWidth, height: windowHeight });
  45. // Delay loading, no white screen
  46. const config = getConfig();
  47. const { windowsOption } = config;
  48. if (windowsOption?.show == false) {
  49. win.once('ready-to-show', () => {
  50. Event.emit(AppEvent.APP_START,{});
  51. win.show();
  52. win.focus();
  53. });
  54. }
  55. }
  56. /**
  57. * Before app close
  58. */
  59. async beforeClose(): Promise<void> {
  60. logger.info('[lifecycle] before-close');
  61. }
  62. }
  63. Lifecycle.toString = () => '[class Lifecycle]';
  64. export { Lifecycle };