main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // 限制一个窗口
  29. const gotTheLock = app.requestSingleInstanceLock()
  30. if (!gotTheLock) {
  31. helper.appQuit()
  32. }
  33. app.on('second-instance', (event) => {
  34. if (MAIN_WINDOW) {
  35. if (MAIN_WINDOW.isMinimized()) {
  36. MAIN_WINDOW.restore()
  37. }
  38. MAIN_WINDOW.focus()
  39. }
  40. })
  41. app.whenReady().then(() => {
  42. createWindow()
  43. app.on('activate', function () {
  44. if (BrowserWindow.getAllWindows().length === 0) {
  45. createWindow()
  46. }
  47. })
  48. })
  49. app.on('window-all-closed', function () {
  50. if (process.platform !== 'darwin') {
  51. eLogger.info('[main] [initialize] window-all-closed quit')
  52. helper.appQuit()
  53. }
  54. })
  55. }
  56. async function createWindow () {
  57. const winOptions = electronConfig.get('windowsOption')
  58. MAIN_WINDOW = new BrowserWindow(winOptions)
  59. // loading html
  60. loadingView(winOptions)
  61. if (eggConfig.env === 'prod') {
  62. // hidden menu
  63. Menu.setApplicationMenu(null)
  64. // dynamic port
  65. await storage.setDynamicPort()
  66. eggConfig.port = electronConfig.get('egg', eggConfig.env).port
  67. }
  68. // options register
  69. await preferences()
  70. // egg server
  71. await startServer(eggConfig)
  72. return MAIN_WINDOW
  73. }
  74. async function startServer (options) {
  75. eLogger.info('[main] [startServer] options', options)
  76. const protocol = 'http://'
  77. let startRes = null
  78. let url = null
  79. const remoteConfig = electronConfig.get('remoteUrl');
  80. if (remoteConfig.enable) {
  81. url = remoteConfig.url
  82. loadMainUrl(url)
  83. return true
  84. }
  85. if (ENV === 'prod') {
  86. url = protocol + options.hostname + ':' + options.port
  87. } else {
  88. const developmentModeConfig = electronConfig.get('developmentMode', ENV)
  89. const selectMode = developmentModeConfig.default
  90. const modeInfo = developmentModeConfig.mode[selectMode]
  91. url = protocol + modeInfo.hostname + ':' + modeInfo.port
  92. }
  93. eLogger.info('[main] [url]:', url)
  94. startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
  95. eLogger.info('[main] [startServer] startRes:', startRes)
  96. if (startRes === 'success') {
  97. loadMainUrl(url)
  98. return true
  99. }
  100. app.relaunch()
  101. }
  102. /**
  103. * White screen optimization
  104. */
  105. function loadingView (winOptions) {
  106. const loadingBrowserView = new BrowserView()
  107. MAIN_WINDOW.setBrowserView(loadingBrowserView)
  108. loadingBrowserView.setBounds({
  109. x: 0,
  110. y: 0,
  111. width: winOptions.width,
  112. height: winOptions.height
  113. });
  114. // loading html
  115. const loadingHtml = path.join('file://', __dirname, '/asset/loading.html')
  116. loadingBrowserView.webContents.loadURL(loadingHtml)
  117. MAIN_WINDOW.webContents.on('dom-ready', async (event) => {
  118. MAIN_WINDOW.removeBrowserView(loadingBrowserView)
  119. });
  120. }
  121. function loadMainUrl (url) {
  122. MAIN_WINDOW.loadURL(url)
  123. }
  124. initialize()