main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const yaml = require('js-yaml')
  2. const path = require('path')
  3. const fs = require('fs')
  4. const Config = require('electron-config')
  5. const electron = require('electron')
  6. const platform = require('os').platform()
  7. require('electron-debug')({enabled: true, showDevTools: process.argv.indexOf('--debug') != -1})
  8. let app = electron.app
  9. let windowConfig = new Config({name: 'window'})
  10. setupWindowManagement = () => {
  11. let windowCloseable
  12. app.window.on('show', () => {
  13. electron.ipcMain.send('window-shown')
  14. })
  15. app.window.on('close', (e) => {
  16. windowConfig.set('windowBoundaries', app.window.getBounds())
  17. if (!windowCloseable) {
  18. app.window.minimize()
  19. e.preventDefault()
  20. }
  21. })
  22. app.window.on('closed', () => {
  23. app.window = null
  24. })
  25. electron.ipcMain.on('window-closeable', (event, flag) => {
  26. windowCloseable = flag
  27. })
  28. electron.ipcMain.on('window-focus', () => {
  29. app.window.focus()
  30. })
  31. electron.ipcMain.on('window-toggle-focus', () => {
  32. if (app.window.isFocused()) {
  33. app.window.minimize()
  34. } else {
  35. app.window.focus()
  36. }
  37. })
  38. electron.ipcMain.on('window-maximize', () => {
  39. if (app.window.isMaximized()) {
  40. app.window.unmaximize()
  41. } else {
  42. app.window.maximize()
  43. }
  44. })
  45. electron.ipcMain.on('window-minimize', () => {
  46. app.window.minimize()
  47. })
  48. electron.ipcMain.on('window-set-bounds', (event, bounds) => {
  49. app.window.setBounds(bounds, true)
  50. })
  51. app.on('before-quit', () => windowCloseable = true)
  52. }
  53. setupMenu = () => {
  54. var template = [{
  55. label: "Application",
  56. submenu: [
  57. { type: "separator" },
  58. { label: "Quit", accelerator: "CmdOrCtrl+Q", click: () => {
  59. app.window.webContents.send('host:quit-request')
  60. }}
  61. ]
  62. },
  63. {
  64. label: "Edit",
  65. submenu: [
  66. { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
  67. { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
  68. { type: "separator" },
  69. { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
  70. { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
  71. { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
  72. { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
  73. ]
  74. }]
  75. electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template))
  76. }
  77. start = () => {
  78. let t0 = Date.now()
  79. let secondInstance = app.makeSingleInstance((argv) => {
  80. app.window.focus()
  81. })
  82. if (secondInstance) {
  83. app.quit()
  84. return
  85. }
  86. let configPath = path.join(electron.app.getPath('userData'), 'config.yaml')
  87. let configData
  88. if (fs.existsSync(configPath)) {
  89. configData = yaml.safeLoad(fs.readFileSync(configPath, 'utf8'))
  90. } else {
  91. configData = {}
  92. }
  93. let options = {
  94. width: 800,
  95. height: 400,
  96. //icon: `${app.getAppPath()}/assets/img/icon.png`,
  97. title: 'Term',
  98. minWidth: 300,
  99. minHeight: 100,
  100. 'web-preferences': {'web-security': false},
  101. //- background to avoid the flash of unstyled window
  102. backgroundColor: '#1D272D',
  103. frame: false,
  104. }
  105. Object.assign(options, windowConfig.get('windowBoundaries'))
  106. if (platform == 'darwin') {
  107. options.titleBarStyle = 'hidden'
  108. }
  109. if ((configData.appearance || {}).useNativeFrame) {
  110. options.frame = true
  111. }
  112. app.commandLine.appendSwitch('disable-http-cache')
  113. app.window = new electron.BrowserWindow(options)
  114. app.window.loadURL(`file://${app.getAppPath()}/assets/webpack/index.html`, {extraHeaders: "pragma: no-cache\n"})
  115. if (platform != 'darwin') {
  116. app.window.setMenu(null)
  117. }
  118. app.window.show()
  119. app.window.focus()
  120. setupWindowManagement()
  121. if (platform == 'darwin') {
  122. setupMenu()
  123. } else {
  124. app.window.setMenu(null)
  125. }
  126. console.info(`Host startup: ${Date.now() - t0}ms`)
  127. t0 = Date.now()
  128. electron.ipcMain.on('app:ready', () => {
  129. console.info(`App startup: ${Date.now() - t0}ms`)
  130. })
  131. }
  132. app.on('ready', start)
  133. app.on('activate', () => {
  134. if (!app.window)
  135. start()
  136. else {
  137. app.window.show()
  138. app.window.focus()
  139. }
  140. })
  141. process.on('uncaughtException', function(err) {
  142. console.log(err)
  143. app.window.webContents.send('uncaughtException', err)
  144. })