main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const {app, BrowserWindow, BrowserView, Menu} = require('electron')
  2. const path = require('path')
  3. const eggLauncher = require('./electron/lib/lanucher')
  4. const setup = require('./electron/setup')
  5. const electronConfig = require('./electron/config')
  6. const storage = require('./electron/lib/storage')
  7. const preferences = require('./electron/preferences')
  8. const helper = require('./electron/lib/helper')
  9. // main window
  10. global.MAIN_WINDOW = null;
  11. global.APP_TRAY = null;
  12. global.CAN_QUIT = false;
  13. // argv
  14. let ENV = 'prod'
  15. for (let i = 0; i < process.argv.length; i++) {
  16. const tmpArgv = process.argv[i]
  17. if (tmpArgv.indexOf('--env=') !== -1) {
  18. ENV = tmpArgv.substring(6)
  19. }
  20. }
  21. process.env.EE_SERVER_ENV = ENV;
  22. // Initialize
  23. setup()
  24. const eggConfig = electronConfig.get('egg', process.env.EE_SERVER_ENV)
  25. eggConfig.env = process.env.EE_SERVER_ENV
  26. // eLogger
  27. const eLogger = require('./electron/lib/eLogger').get()
  28. async function initialize () {
  29. // 限制一个窗口
  30. const gotTheLock = app.requestSingleInstanceLock()
  31. if (!gotTheLock) {
  32. helper.appQuit()
  33. }
  34. app.on('second-instance', (event) => {
  35. if (MAIN_WINDOW) {
  36. if (MAIN_WINDOW.isMinimized()) {
  37. MAIN_WINDOW.restore()
  38. }
  39. MAIN_WINDOW.focus()
  40. }
  41. })
  42. app.whenReady().then(() => {
  43. createWindow()
  44. app.on('activate', function () {
  45. if (BrowserWindow.getAllWindows().length === 0) {
  46. createWindow()
  47. }
  48. })
  49. })
  50. app.on('window-all-closed', function () {
  51. if (process.platform !== 'darwin') {
  52. eLogger.info('[main] [initialize] window-all-closed quit')
  53. helper.appQuit()
  54. }
  55. })
  56. }
  57. async function createWindow () {
  58. const winOptions = electronConfig.get('windowsOption')
  59. MAIN_WINDOW = new BrowserWindow(winOptions)
  60. // loading html
  61. loadingView(winOptions)
  62. if (eggConfig.env === 'prod') {
  63. // hidden menu
  64. Menu.setApplicationMenu(null)
  65. // dynamic port
  66. await storage.setDynamicPort()
  67. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  68. }
  69. // options register
  70. await preferences()
  71. // egg server
  72. await startServer(eggConfig)
  73. return MAIN_WINDOW
  74. }
  75. async function startServer (options) {
  76. eLogger.info('[main] [startServer] options', options)
  77. const protocol = 'http://'
  78. let startRes = null
  79. let url = null
  80. const remoteConfig = electronConfig.get('remoteUrl');
  81. if (remoteConfig.enable) {
  82. url = remoteConfig.url
  83. loadMainUrl(url)
  84. return true
  85. }
  86. if (ENV === 'prod') {
  87. url = protocol + options.hostname + ':' + options.port
  88. } else {
  89. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  90. const selectMode = developmentModeConfig.default
  91. const modeInfo = developmentModeConfig.mode[selectMode]
  92. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  93. }
  94. eLogger.info('[main] [url]:', url)
  95. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  96. eLogger.info('[main] [startServer] startRes:', startRes)
  97. if (startRes === 'success') {
  98. loadMainUrl(url)
  99. return true
  100. }
  101. app.relaunch()
  102. }
  103. /**
  104. * White screen optimization
  105. */
  106. function loadingView (winOptions) {
  107. const loadingBrowserView = new BrowserView()
  108. MAIN_WINDOW.setBrowserView(loadingBrowserView)
  109. loadingBrowserView.setBounds({
  110. x: 0,
  111. y: 0,
  112. width: winOptions.width,
  113. height: winOptions.height
  114. });
  115. // loading html
  116. const loadingHtml = path.join('file://', __dirname, '/asset/loading.html')
  117. loadingBrowserView.webContents.loadURL(loadingHtml)
  118. MAIN_WINDOW.webContents.on('dom-ready', async (event) => {
  119. MAIN_WINDOW.removeBrowserView(loadingBrowserView)
  120. });
  121. }
  122. function loadMainUrl (url) {
  123. MAIN_WINDOW.loadURL(url)
  124. }
  125. /**
  126. * Catch exception
  127. */
  128. process.on('uncaughtException', function(err) {
  129. eLogger.error(err);
  130. });
  131. initialize()