electron.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. {
  96. //asynchronous
  97. ipcMain.on('asynchronous-message', (event, arg) =>
  98. {
  99. //console.log(arg) // prints "ping"
  100. event.sender.send('asynchronous-reply', 'pong')
  101. })
  102. //synchronous
  103. ipcMain.on('winman', (event, arg) =>
  104. {
  105. //console.log('ipcMain.on winman', arg)
  106. if (arg.action === 'newfile')
  107. {
  108. event.returnValue = createWindow(arg.opt)
  109. return
  110. }
  111. event.returnValue = 'pong'
  112. })
  113. createWindow()
  114. if (process.platform !== 'linux')
  115. {
  116. checkUpdate()
  117. }
  118. })
  119. // Quit when all windows are closed.
  120. app.on('window-all-closed', function ()
  121. {
  122. // console.log('window-all-closed', windowsRegistry.length)
  123. // On OS X it is common for applications and their menu bar
  124. // to stay active until the user quits explicitly with Cmd + Q
  125. if (process.platform !== 'darwin')
  126. {
  127. app.quit()
  128. }
  129. })
  130. app.on('activate', function ()
  131. {
  132. // console.log('app on activate', windowsRegistry.length)
  133. // On OS X it's common to re-create a window in the app when the
  134. // dock icon is clicked and there are no other windows open.
  135. if (windowsRegistry.length === 0)
  136. {
  137. createWindow()
  138. }
  139. })
  140. function checkUpdate ()
  141. {
  142. autoUpdater.checkForUpdates().then(UpdateCheckResult =>
  143. {
  144. if (UpdateCheckResult)
  145. {
  146. let idx = dialog.showMessageBox({
  147. type: 'question',
  148. buttons: ['Ok', 'Cancel'],
  149. title: 'Confirm Update',
  150. message: 'Update available.\n\nWould you like to download and install new version?',
  151. detail: 'Application will automatically restart to apply update after download',
  152. })
  153. if (idx === 0)
  154. {
  155. return autoUpdater.downloadUpdate()
  156. }
  157. }
  158. }).then((a, b) => {
  159. log.info('@cfu update-downloaded@\n', a, b)
  160. }).catch(e => {
  161. log.error('@cfu then error@\n', e)
  162. })
  163. }
  164. autoUpdater.on('error', e => log.error('@error@\n', e))
  165. autoUpdater.on('update-available',
  166. (a, b) => log.info('@update-available@\n', a, b))
  167. /**/
  168. autoUpdater.on('update-downloaded', (event, info) =>
  169. {
  170. log.info('@update-downloaded@\n', info, event)
  171. // Ask user to update the app
  172. dialog.showMessageBox(
  173. {
  174. type: 'question',
  175. buttons: ['Install and Relaunch', 'Later'],
  176. defaultId: 0,
  177. message: 'A new version of ' + app.getName() + ' has been downloaded',
  178. detail: 'It will be installed the next time you restart the application',
  179. }, response =>
  180. {
  181. if (response === 0)
  182. {
  183. setTimeout(() => autoUpdater.quitAndInstall(), 1)
  184. }
  185. })
  186. })
  187. /**/