electron.js 4.7 KB

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