index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict'
  2. import pkg from '../../package.json'
  3. import usbffi from './main-process/usb-ffi'
  4. import downloads from './main-process/downloads'
  5. import updateInstall from './main-process/updateInstall'
  6. import fileselect from './main-process/file-select'
  7. import db from '../database/index'
  8. const path = require('path')
  9. // const glob = require('glob')
  10. const electron = require('electron')
  11. const app = electron.app
  12. const BrowserWindow = electron.BrowserWindow
  13. const Menu = electron.Menu
  14. const autoUpdater = require('./software-update')
  15. /**
  16. * Set `__static` path to static files in production
  17. * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
  18. */
  19. if (process.env.NODE_ENV !== 'development') {
  20. global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
  21. }
  22. let mainWindow
  23. let winURL = process.env.NODE_ENV === 'development'
  24. ? `http://localhost:9080`
  25. : `file://${__dirname}/index.html`
  26. let firsturl = db.read().get('sc_hadInstall.first').value()
  27. winURL += firsturl ? '/#/firstopen' : '/#' + db.read().get('sc_hadInstall.url').value()
  28. function initialize () {
  29. function createWindow () {
  30. Menu.setApplicationMenu(null)
  31. /**
  32. * Initial window options
  33. */
  34. const windowOptions = {
  35. // width: 900,
  36. width: 1450,
  37. height: 564,
  38. resizable: false,
  39. show: true,
  40. frame: false,
  41. fullscreenable: false,
  42. center: true,
  43. transparent: true,
  44. titleBarStyle: 'hidden',
  45. backgroundColor: '#fff',
  46. webPreferences: {
  47. backgroundThrottling: false
  48. }
  49. }
  50. mainWindow = new BrowserWindow(windowOptions)
  51. mainWindow.loadURL(winURL)
  52. // mainWindow.webContents.openDevTools()
  53. mainWindow.on('closed', () => {
  54. mainWindow = null
  55. })
  56. }
  57. let shouldQuit = app.makeSingleInstance(function (commandLine, workingDirectory) {
  58. // 当另一个实例运行的时候,这里将会被调用,我们需要激活应用的窗口
  59. if (mainWindow) {
  60. if (mainWindow.isMinimized()) mainWindow.restore()
  61. mainWindow.focus()
  62. }
  63. return true
  64. })
  65. // 这个实例是多余的实例,需要退出
  66. if (shouldQuit) {
  67. app.quit()
  68. return
  69. }
  70. app.on('ready', function () {
  71. createWindow()
  72. loadJS(mainWindow)
  73. autoUpdater.initialize(mainWindow)
  74. })
  75. app.on('window-all-closed', () => {
  76. if (process.platform !== 'darwin') {
  77. app.quit()
  78. }
  79. })
  80. app.on('activate', () => {
  81. if (mainWindow === null) {
  82. createWindow()
  83. }
  84. })
  85. function loadJS (win) {
  86. // 无法使用require调用含异步的方法,只能采用import方式,原因不明,坑!
  87. // require('./main-process/usb-ffi').initialize(win)
  88. usbffi(win)
  89. downloads(win)
  90. updateInstall(win)
  91. fileselect(win)
  92. // require('./main-process/downloads').initialize(win)
  93. // let files = glob.sync(path.join(__dirname, './main-process/*.js'))
  94. // if (files !== []) {
  95. // files.forEach(function (file) {
  96. // require(file)
  97. // })
  98. // }
  99. // require('./main-process/download')
  100. }
  101. }
  102. initialize()
  103. if (process.platform === 'win32') {
  104. app.setAppUserModelId(pkg.build.appId)
  105. }