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 { focusSmartWindow } from '../service/window/smartWindow';
  6. class Lifecycle {
  7. /**
  8. * core app have been loaded
  9. */
  10. async ready(): Promise<void> {
  11. logger.info('[lifecycle] ready');
  12. }
  13. /**
  14. * electron app ready
  15. */
  16. async electronAppReady(): Promise<void> {
  17. logger.info('[lifecycle] electron-app-ready');
  18. // When double clicking the icon, display the opened windows
  19. electronApp.on('second-instance', () => {
  20. const win = getMainWindow();
  21. if (win.isMinimized()) {
  22. win.restore();
  23. }
  24. win.show();
  25. // 智能工作台开着时优先把用户拉回工作台,否则聚焦主窗口
  26. if (!focusSmartWindow()) {
  27. win.focus();
  28. }
  29. });
  30. }
  31. /**
  32. * main window have been loaded
  33. */
  34. async windowReady(): Promise<void> {
  35. logger.info('[lifecycle] window-ready');
  36. const win = getMainWindow();
  37. // The window is centered and scaled proportionally
  38. // Obtain the size information of the main screen, calculate the width and height of the window as a percentage of the screen,
  39. // and calculate the coordinates of the upper left corner when the window is centered
  40. const mainScreen = screen.getPrimaryDisplay();
  41. const { width, height } = mainScreen.workAreaSize;
  42. const windowWidth = Math.floor(width * 0.6);
  43. const windowHeight = Math.floor(height * 0.7);
  44. const x = Math.floor((width - windowWidth) / 2);
  45. const y = Math.floor((height - windowHeight) / 2);
  46. win.setBounds({ x, y, width: windowWidth, height: windowHeight })
  47. // Delayed loading, no white screen
  48. const { windowsOption } = getConfig();
  49. if (windowsOption.show == false) {
  50. win.once('ready-to-show', () => {
  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. export {
  64. Lifecycle
  65. };