electron.js 4.9 KB

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