lifecycle.ts 1.9 KB

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