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