electron.js 4.7 KB

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