electron.js 5.0 KB

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