main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Initialize
  14. setup()
  15. // argv
  16. let ENV = 'prod'
  17. for (let i = 0; i < process.argv.length; i++) {
  18. const tmpArgv = process.argv[i]
  19. if (tmpArgv.indexOf('--env=') !== -1) {
  20. ENV = tmpArgv.substr(6)
  21. }
  22. }
  23. const eggConfig = electronConfig.get('egg', ENV)
  24. eggConfig.env = ENV
  25. // eLogger
  26. const eLogger = require('./electron/lib/eLogger').get()
  27. async function initialize () {
  28. app.whenReady().then(() => {
  29. createWindow()
  30. app.on('activate', function () {
  31. if (BrowserWindow.getAllWindows().length === 0) {
  32. createWindow()
  33. }
  34. })
  35. })
  36. app.on('window-all-closed', function () {
  37. if (process.platform !== 'darwin') {
  38. eLogger.info('[main] [initialize] window-all-closed quit')
  39. helper.appQuit()
  40. }
  41. })
  42. }
  43. async function createWindow () {
  44. const winOptions = electronConfig.get('windowsOption')
  45. MAIN_WINDOW = new BrowserWindow(winOptions)
  46. // loading html
  47. loadingView(winOptions)
  48. if (eggConfig.env === 'prod') {
  49. // hidden menu
  50. Menu.setApplicationMenu(null)
  51. // dynamic port
  52. await storage.setDynamicPort()
  53. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  54. }
  55. // options register
  56. await preferences()
  57. // egg server
  58. await startServer(eggConfig)
  59. return MAIN_WINDOW
  60. }
  61. async function startServer (options) {
  62. eLogger.info('[main] [startServer] options', options)
  63. const protocol = 'http://'
  64. let startRes = null
  65. let url = null
  66. const remoteConfig = electronConfig.get('remoteUrl');
  67. if (remoteConfig.enable) {
  68. url = remoteConfig.url
  69. loadMainUrl(url)
  70. return true
  71. }
  72. if (ENV === 'prod') {
  73. url = protocol + options.hostname + ':' + options.port
  74. } else {
  75. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  76. const selectMode = developmentModeConfig.default
  77. const modeInfo = developmentModeConfig.mode[selectMode]
  78. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  79. }
  80. eLogger.info('[main] [url]:', url)
  81. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  82. eLogger.info('[main] [startServer] startRes:', startRes)
  83. if (startRes === 'success') {
  84. loadMainUrl(url)
  85. return true
  86. }
  87. app.relaunch()
  88. }
  89. /**
  90. * White screen optimization
  91. */
  92. function loadingView (winOptions) {
  93. const loadingBrowserView = new BrowserView()
  94. MAIN_WINDOW.setBrowserView(loadingBrowserView)
  95. loadingBrowserView.setBounds({
  96. x: 0,
  97. y: 0,
  98. width: winOptions.width,
  99. height: winOptions.height
  100. });
  101. // loading html
  102. const loadingHtml = path.join('file://', __dirname, '/asset/loading.html')
  103. loadingBrowserView.webContents.loadURL(loadingHtml)
  104. MAIN_WINDOW.webContents.on('dom-ready', async (event) => {
  105. MAIN_WINDOW.removeBrowserView(loadingBrowserView)
  106. });
  107. }
  108. function loadMainUrl (url) {
  109. MAIN_WINDOW.loadURL(url)
  110. }
  111. initialize()