electron.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const fs = require('fs')
  2. const path = require('path')
  3. const electron = require('electron')
  4. const ipcMain = electron.ipcMain
  5. const dialog = electron.dialog
  6. const app = electron.app
  7. const BrowserWindow = electron.BrowserWindow
  8. let windowsRegistry = []
  9. function createWindow (opt = {}) {
  10. let options = Object.assign({
  11. width: 1600,
  12. height: 1200,
  13. 'web-security': false,
  14. }, opt)
  15. let mainWindow = new BrowserWindow(options)
  16. windowsRegistry.push(mainWindow)
  17. console.log('createWindow', opt)
  18. // and load the index.html of the app.
  19. mainWindow.loadURL(
  20. `file://${__dirname}/index.html?dev=1&test=1&db=0&gapi=0&od=0&analytics=0&picker=0&mode=device&browser=0&p=electron`)
  21. // Open the DevTools.
  22. mainWindow.webContents.openDevTools()
  23. mainWindow.on('close', (event/*:WindowEvent*/) => {
  24. const win = event.sender
  25. const index = windowsRegistry.indexOf(win)
  26. console.log('Window on close idx:%d', index)
  27. const contents = win.webContents
  28. if (contents != null) {
  29. contents.executeJavaScript(`global.__emt_isModified()`, true,
  30. isModified => {
  31. console.log('__emt_isModified', isModified)
  32. if (isModified) {
  33. var choice = dialog.showMessageBox(
  34. win,
  35. {
  36. type: 'question',
  37. buttons: ['Yes', 'No'],
  38. title: 'Confirm',
  39. message: 'All Changes will be lost' //mxResources.get('allChangesLost')
  40. })
  41. if (choice === 0) {
  42. win.destroy()
  43. }
  44. } else {
  45. win.destroy()
  46. }
  47. })
  48. event.preventDefault()
  49. }
  50. })
  51. // Emitted when the window is closed.
  52. mainWindow.on('closed', (event/*:WindowEvent*/) => {
  53. const index = windowsRegistry.indexOf(event.sender)
  54. console.log('Window closed idx:%d', index)
  55. windowsRegistry.splice(index, 1)
  56. })
  57. return mainWindow.id
  58. }
  59. // This method will be called when Electron has finished
  60. // initialization and is ready to create browser windows.
  61. // Some APIs can only be used after this event occurs.
  62. app.on('ready', e => {
  63. //asynchronous
  64. ipcMain.on('asynchronous-message', (event, arg) => {
  65. console.log(arg) // prints "ping"
  66. event.sender.send('asynchronous-reply', 'pong')
  67. })
  68. //synchronous
  69. ipcMain.on('winman', (event, arg) => {
  70. console.log('ipcMain.on winman', arg)
  71. if (arg.action === 'newfile') {
  72. event.returnValue = createWindow(arg.opt)
  73. return
  74. }
  75. event.returnValue = 'pong'
  76. })
  77. createWindow()
  78. })
  79. // Quit when all windows are closed.
  80. app.on('window-all-closed', function () {
  81. console.log('window-all-closed', windowsRegistry.length)
  82. // On OS X it is common for applications and their menu bar
  83. // to stay active until the user quits explicitly with Cmd + Q
  84. if (process.platform !== 'darwin') {
  85. app.quit()
  86. }
  87. })
  88. app.on('activate', function () {
  89. console.log('app on activate', windowsRegistry.length)
  90. // On OS X it's common to re-create a window in the app when the
  91. // dock icon is clicked and there are no other windows open.
  92. if (windowsRegistry.length === 0) {
  93. createWindow()
  94. }
  95. })
  96. // In this file you can include the rest of your app's specific main process
  97. // code. You can also put them in separate files and require them here.