electron.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. const fs = require('fs')
  2. const path = require('path')
  3. const url = require('url')
  4. const electron = require('electron')
  5. const ipcMain = electron.ipcMain
  6. const dialog = electron.dialog
  7. const app = electron.app
  8. const BrowserWindow = electron.BrowserWindow
  9. const autoUpdater = require('electron-updater').autoUpdater
  10. const log = require('electron-log')
  11. autoUpdater.logger = log
  12. autoUpdater.logger.transports.file.level = 'info'
  13. // autoUpdater.autoDownload = false
  14. autoUpdater.autoDownload = true
  15. const __DEV__ = process.env.NODE_ENV === 'development'
  16. let windowsRegistry = []
  17. function createWindow (opt = {}) {
  18. let options = Object.assign({
  19. width: 1600,
  20. height: 1200,
  21. 'web-security': false,
  22. webPreferences: {
  23. // preload: path.resolve('./preload.js'),
  24. },
  25. }, opt)
  26. let mainWindow = new BrowserWindow(options)
  27. windowsRegistry.push(mainWindow)
  28. console.log('createWindow', opt)
  29. let wurl = url.format({
  30. pathname: `${__dirname}/index.html`,
  31. protocol: 'file:',
  32. query: {
  33. 'dev': __DEV__ ? 1 : 0,
  34. 'test': 1,
  35. 'db': 0,
  36. 'gapi': 0,
  37. 'od': 0,
  38. 'analytics': 0,
  39. 'picker': 0,
  40. 'mode': 'device',
  41. 'browser': 0,
  42. 'p': 'electron',
  43. },
  44. slashes: true,
  45. })
  46. //`file://${__dirname}/index.html?dev=1&test=1&db=0&gapi=0&od=0&analytics=0&picker=0&mode=device&browser=0&p=electron`
  47. // and load the index.html of the app.
  48. mainWindow.loadURL(wurl)
  49. // Open the DevTools.
  50. mainWindow.webContents.openDevTools()
  51. mainWindow.on('close', (event/*:WindowEvent*/) => {
  52. const win = event.sender
  53. const index = windowsRegistry.indexOf(win)
  54. console.log('Window on close idx:%d', index)
  55. const contents = win.webContents
  56. if (contents != null) {
  57. contents.executeJavaScript(`global.__emt_isModified()`, true,
  58. isModified => {
  59. console.log('__emt_isModified', isModified)
  60. if (isModified) {
  61. var choice = dialog.showMessageBox(
  62. win,
  63. {
  64. type: 'question',
  65. buttons: ['Yes', 'No'],
  66. title: 'Confirm',
  67. message: 'All Changes will be lost' //mxResources.get('allChangesLost')
  68. })
  69. if (choice === 0) {
  70. win.destroy()
  71. }
  72. } else {
  73. win.destroy()
  74. }
  75. })
  76. event.preventDefault()
  77. }
  78. })
  79. // Emitted when the window is closed.
  80. mainWindow.on('closed', (event/*:WindowEvent*/) => {
  81. const index = windowsRegistry.indexOf(event.sender)
  82. console.log('Window closed idx:%d', index)
  83. windowsRegistry.splice(index, 1)
  84. })
  85. return mainWindow.id
  86. }
  87. // This method will be called when Electron has finished
  88. // initialization and is ready to create browser windows.
  89. // Some APIs can only be used after this event occurs.
  90. app.on('ready', e => {
  91. //asynchronous
  92. ipcMain.on('asynchronous-message', (event, arg) => {
  93. console.log(arg) // prints "ping"
  94. event.sender.send('asynchronous-reply', 'pong')
  95. })
  96. //synchronous
  97. ipcMain.on('winman', (event, arg) => {
  98. console.log('ipcMain.on winman', arg)
  99. if (arg.action === 'newfile') {
  100. event.returnValue = createWindow(arg.opt)
  101. return
  102. }
  103. event.returnValue = 'pong'
  104. })
  105. createWindow()
  106. // checkUpdate()
  107. autoUpdater.checkForUpdates()
  108. })
  109. // Quit when all windows are closed.
  110. app.on('window-all-closed', function () {
  111. console.log('window-all-closed', windowsRegistry.length)
  112. // On OS X it is common for applications and their menu bar
  113. // to stay active until the user quits explicitly with Cmd + Q
  114. if (process.platform !== 'darwin') {
  115. app.quit()
  116. }
  117. })
  118. app.on('activate', function () {
  119. console.log('app on activate', windowsRegistry.length)
  120. // On OS X it's common to re-create a window in the app when the
  121. // dock icon is clicked and there are no other windows open.
  122. if (windowsRegistry.length === 0) {
  123. createWindow()
  124. }
  125. })
  126. function checkUpdate () {
  127. autoUpdater.checkForUpdates().then(UpdateCheckResult => {
  128. if (UpdateCheckResult) {
  129. let idx = dialog.showMessageBox({
  130. type: 'question',
  131. buttons: ['Ok', 'Cancel'],
  132. title: 'Confirm Update',
  133. message: 'Update available.\n\nWould you like to download and install new version?',
  134. detail: 'Application will automatically restart to apply update after download',
  135. })
  136. if (idx === 0) return autoUpdater.downloadUpdate()
  137. }
  138. }).then((a, b) => {
  139. log.info('@cfu update-downloaded@\n', a, b)
  140. }).catch(e => {
  141. log.error('@cfu then error@\n', e)
  142. })
  143. }
  144. autoUpdater.on('error', e => log.error('@error@\n', e))
  145. autoUpdater.on('update-available',
  146. (a, b) => log.info('@update-available@\n', a, b))
  147. /**/
  148. autoUpdater.on('update-downloaded', (event, info) => {
  149. log.info('@update-downloaded@\n', info, event)
  150. // Ask user to update the app
  151. dialog.showMessageBox({
  152. type: 'question',
  153. buttons: ['Install and Relaunch', 'Later'],
  154. defaultId: 0,
  155. message: 'A new version of ' + app.getName() + ' has been downloaded',
  156. detail: 'It will be installed the next time you restart the application',
  157. }, response => {
  158. if (response === 0) {
  159. setTimeout(() => autoUpdater.quitAndInstall(), 1)
  160. }
  161. })
  162. })
  163. /**/