main.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. if (process.platform == 'win32' && require('electron-squirrel-startup')) process.exit(0)
  2. const electron = require('electron')
  3. let electronVibrancy
  4. if (process.platform != 'linux') {
  5. electronVibrancy = require('electron-vibrancy')
  6. }
  7. if (process.argv.indexOf('--debug') !== -1) {
  8. require('electron-debug')({enabled: true, showDevTools: 'undocked'})
  9. }
  10. let app = electron.app
  11. let secondInstance = app.makeSingleInstance((argv, cwd) => {
  12. app.window.webContents.send('host:second-instance', argv, cwd)
  13. })
  14. if (secondInstance) {
  15. app.quit()
  16. return
  17. }
  18. const yaml = require('js-yaml')
  19. const path = require('path')
  20. const fs = require('fs')
  21. const Config = require('electron-config')
  22. let windowConfig = new Config({name: 'window'})
  23. if (!process.env.TERMINUS_PLUGINS) {
  24. process.env.TERMINUS_PLUGINS = ''
  25. }
  26. setWindowVibrancy = (enabled) => {
  27. if (enabled && !app.window.vibrancyViewID) {
  28. app.window.vibrancyViewID = electronVibrancy.SetVibrancy(app.window, 0)
  29. } else if (!enabled && app.window.vibrancyViewID) {
  30. electronVibrancy.RemoveView(app.window, app.window.vibrancyViewID)
  31. app.window.vibrancyViewID = null
  32. }
  33. }
  34. setupWindowManagement = () => {
  35. app.window.on('show', () => {
  36. app.window.webContents.send('host:window-shown')
  37. if (app.tray) {
  38. app.tray.destroy()
  39. app.tray = null
  40. }
  41. })
  42. app.window.on('hide', (e) => {
  43. if (!app.tray) {
  44. setupTray()
  45. }
  46. })
  47. app.window.on('enter-full-screen', () => app.window.webContents.send('host:window-enter-full-screen'))
  48. app.window.on('leave-full-screen', () => app.window.webContents.send('host:window-leave-full-screen'))
  49. app.window.on('close', (e) => {
  50. windowConfig.set('windowBoundaries', app.window.getBounds())
  51. })
  52. app.window.on('closed', () => {
  53. app.window = null
  54. })
  55. electron.ipcMain.on('window-focus', () => {
  56. app.window.focus()
  57. })
  58. electron.ipcMain.on('window-maximize', () => {
  59. app.window.maximize()
  60. })
  61. electron.ipcMain.on('window-unmaximize', () => {
  62. app.window.unmaximize()
  63. })
  64. electron.ipcMain.on('window-toggle-maximize', () => {
  65. if (app.window.isMaximized()) {
  66. app.window.unmaximize()
  67. } else {
  68. app.window.maximize()
  69. }
  70. })
  71. electron.ipcMain.on('window-minimize', () => {
  72. app.window.minimize()
  73. })
  74. electron.ipcMain.on('window-set-bounds', (event, bounds) => {
  75. app.window.setBounds(bounds)
  76. })
  77. electron.ipcMain.on('window-set-always-on-top', (event, flag) => {
  78. app.window.setAlwaysOnTop(flag)
  79. })
  80. electron.ipcMain.on('window-set-vibrancy', (event, enabled) => {
  81. setWindowVibrancy(enabled)
  82. })
  83. }
  84. setupTray = () => {
  85. if (process.platform == 'darwin') {
  86. app.tray = new electron.Tray(`${app.getAppPath()}/assets/tray-darwinTemplate.png`)
  87. app.tray.setPressedImage(`${app.getAppPath()}/assets/tray-darwinHighlightTemplate.png`)
  88. } else {
  89. app.tray = new electron.Tray(`${app.getAppPath()}/assets/tray.png`)
  90. }
  91. app.tray.on('click', () => {
  92. app.window.show()
  93. app.window.focus()
  94. })
  95. const contextMenu = electron.Menu.buildFromTemplate([{
  96. label: 'Show',
  97. click () {
  98. app.window.show()
  99. app.window.focus()
  100. }
  101. }])
  102. if (process.platform != 'darwin') {
  103. app.tray.setContextMenu(contextMenu)
  104. }
  105. app.tray.setToolTip(`Terminus ${app.getVersion()}`)
  106. }
  107. setupMenu = () => {
  108. let template = [{
  109. label: "Application",
  110. submenu: [
  111. { role: 'about', label: 'About Terminus' },
  112. { type: 'separator' },
  113. {
  114. label: 'Preferences',
  115. accelerator: 'Cmd+,',
  116. click () {
  117. app.window.webContents.send('host:preferences-menu')
  118. }
  119. },
  120. { type: 'separator' },
  121. { role: 'services', submenu: [] },
  122. { type: 'separator' },
  123. { role: 'hide' },
  124. { role: 'hideothers' },
  125. { role: 'unhide' },
  126. { type: 'separator' },
  127. {
  128. label: 'Quit',
  129. accelerator: 'Cmd+Q',
  130. click () {
  131. app.quit()
  132. }
  133. }
  134. ]
  135. },
  136. {
  137. label: "Edit",
  138. submenu: [
  139. {role: 'undo'},
  140. {role: 'redo'},
  141. {type: 'separator'},
  142. {role: 'cut'},
  143. {role: 'copy'},
  144. {role: 'paste'},
  145. {role: 'pasteandmatchstyle'},
  146. {role: 'delete'},
  147. {role: 'selectall'}
  148. ]
  149. },
  150. {
  151. label: 'View',
  152. submenu: [
  153. {role: 'reload'},
  154. {role: 'forcereload'},
  155. {role: 'toggledevtools'},
  156. {type: 'separator'},
  157. {role: 'resetzoom'},
  158. {role: 'zoomin'},
  159. {role: 'zoomout'},
  160. {type: 'separator'},
  161. {role: 'togglefullscreen'}
  162. ]
  163. },
  164. {
  165. role: 'window',
  166. submenu: [
  167. {role: 'minimize'},
  168. {role: 'zoom'},
  169. {type: 'separator'},
  170. {role: 'front'}
  171. ]
  172. },
  173. {
  174. role: 'help',
  175. submenu: [
  176. {
  177. label: 'Website',
  178. click () { electron.shell.openExternal('https://eugeny.github.io/terminus') }
  179. }
  180. ]
  181. }]
  182. electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template))
  183. }
  184. start = () => {
  185. let t0 = Date.now()
  186. let configPath = path.join(electron.app.getPath('userData'), 'config.yaml')
  187. let configData
  188. if (fs.existsSync(configPath)) {
  189. configData = yaml.safeLoad(fs.readFileSync(configPath, 'utf8'))
  190. } else {
  191. configData = {}
  192. }
  193. let options = {
  194. width: 800,
  195. height: 600,
  196. title: 'Terminus',
  197. minWidth: 400,
  198. minHeight: 300,
  199. webPreferences: {webSecurity: false},
  200. frame: false,
  201. show: false,
  202. }
  203. Object.assign(options, windowConfig.get('windowBoundaries'))
  204. if ((configData.appearance || {}).frame == 'native') {
  205. options.frame = true
  206. } else {
  207. if (process.platform == 'darwin') {
  208. options.titleBarStyle = 'hiddenInset'
  209. }
  210. }
  211. if (process.platform == 'linux') {
  212. options.backgroundColor = '#131d27'
  213. }
  214. app.commandLine.appendSwitch('disable-http-cache')
  215. app.window = new electron.BrowserWindow(options)
  216. app.window.once('ready-to-show', () => {
  217. if (process.platform == 'darwin') {
  218. app.window.setVibrancy('dark')
  219. } else if (process.platform == 'windows') {
  220. setWindowVibrancy(true)
  221. }
  222. app.window.show()
  223. app.window.focus()
  224. })
  225. app.window.loadURL(`file://${app.getAppPath()}/dist/index.html`, {extraHeaders: "pragma: no-cache\n"})
  226. if (process.platform != 'darwin') {
  227. app.window.setMenu(null)
  228. }
  229. setupWindowManagement()
  230. if (process.platform == 'darwin') {
  231. setupMenu()
  232. } else {
  233. app.window.setMenu(null)
  234. }
  235. console.info(`Host startup: ${Date.now() - t0}ms`)
  236. t0 = Date.now()
  237. electron.ipcMain.on('app:ready', () => {
  238. console.info(`App startup: ${Date.now() - t0}ms`)
  239. })
  240. }
  241. app.on('ready', start)
  242. app.on('activate', () => {
  243. if (!app.window)
  244. start()
  245. else {
  246. app.window.show()
  247. app.window.focus()
  248. }
  249. })
  250. process.on('uncaughtException', function(err) {
  251. console.log(err)
  252. app.window.webContents.send('uncaughtException', err)
  253. })