app.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { app, ipcMain, Menu, Tray, shell } from 'electron'
  2. // eslint-disable-next-line no-duplicate-imports
  3. import * as electron from 'electron'
  4. import { loadConfig } from './config'
  5. import { Window, WindowOptions } from './window'
  6. export class Application {
  7. private tray: Tray
  8. private windows: Window[] = []
  9. constructor () {
  10. ipcMain.on('app:config-change', () => {
  11. this.broadcast('host:config-change')
  12. })
  13. const configData = loadConfig()
  14. if (process.platform === 'linux') {
  15. app.commandLine.appendSwitch('no-sandbox')
  16. if (((configData.appearance || {}).opacity || 1) !== 1) {
  17. app.commandLine.appendSwitch('enable-transparent-visuals')
  18. app.disableHardwareAcceleration()
  19. }
  20. }
  21. app.commandLine.appendSwitch('disable-http-cache')
  22. app.commandLine.appendSwitch('lang', 'EN')
  23. app.allowRendererProcessReuse = false
  24. for (const flag of configData.flags || [['force_discrete_gpu', '0']]) {
  25. app.commandLine.appendSwitch(flag[0], flag[1])
  26. }
  27. }
  28. init () {
  29. electron.screen.on('display-metrics-changed', () => this.broadcast('host:display-metrics-changed'))
  30. }
  31. async newWindow (options?: WindowOptions): Promise<Window> {
  32. let window = new Window(options)
  33. this.windows.push(window)
  34. window.visible$.subscribe(visible => {
  35. if (visible) {
  36. this.disableTray()
  37. } else {
  38. this.enableTray()
  39. }
  40. })
  41. if (process.platform === 'darwin') {
  42. this.setupMenu()
  43. }
  44. await window.ready
  45. return window
  46. }
  47. broadcast (event, ...args) {
  48. for (let window of this.windows) {
  49. window.send(event, ...args)
  50. }
  51. }
  52. async send (event, ...args) {
  53. if (!this.hasWindows()) {
  54. await this.newWindow()
  55. }
  56. this.windows.filter(w => !w.isDestroyed())[0].send(event, ...args)
  57. }
  58. enableTray () {
  59. if (this.tray) {
  60. return
  61. }
  62. if (process.platform === 'darwin') {
  63. this.tray = new Tray(`${app.getAppPath()}/assets/tray-darwinTemplate.png`)
  64. this.tray.setPressedImage(`${app.getAppPath()}/assets/tray-darwinHighlightTemplate.png`)
  65. } else {
  66. this.tray = new Tray(`${app.getAppPath()}/assets/tray.png`)
  67. }
  68. this.tray.on('click', () => setTimeout(() => this.focus()))
  69. const contextMenu = Menu.buildFromTemplate([{
  70. label: 'Show',
  71. click: () => this.focus(),
  72. }])
  73. if (process.platform !== 'darwin') {
  74. this.tray.setContextMenu(contextMenu)
  75. }
  76. this.tray.setToolTip(`Terminus ${app.getVersion()}`)
  77. }
  78. disableTray () {
  79. if (this.tray) {
  80. this.tray.destroy()
  81. this.tray = null
  82. }
  83. }
  84. hasWindows () {
  85. return !!this.windows.length
  86. }
  87. focus () {
  88. for (let window of this.windows) {
  89. window.show()
  90. }
  91. }
  92. private setupMenu () {
  93. let template: Electron.MenuItemConstructorOptions[] = [
  94. {
  95. label: 'Application',
  96. submenu: [
  97. { role: 'about', label: 'About Terminus' },
  98. { type: 'separator' },
  99. {
  100. label: 'Preferences',
  101. accelerator: 'Cmd+,',
  102. click: async () => {
  103. if (!this.hasWindows()) {
  104. await this.newWindow()
  105. }
  106. this.windows[0].send('host:preferences-menu')
  107. },
  108. },
  109. { type: 'separator' },
  110. { role: 'services', submenu: [] },
  111. { type: 'separator' },
  112. { role: 'hide' },
  113. { role: 'hideOthers' },
  114. { role: 'unhide' },
  115. { type: 'separator' },
  116. {
  117. label: 'Quit',
  118. accelerator: 'Cmd+Q',
  119. click () {
  120. app.quit()
  121. },
  122. },
  123. ],
  124. },
  125. {
  126. label: 'Edit',
  127. submenu: [
  128. { role: 'undo' },
  129. { role: 'redo' },
  130. { type: 'separator' },
  131. { role: 'cut' },
  132. { role: 'copy' },
  133. { role: 'paste' },
  134. { role: 'pasteAndMatchStyle' },
  135. { role: 'delete' },
  136. { role: 'selectAll' },
  137. ],
  138. },
  139. {
  140. label: 'View',
  141. submenu: [
  142. { role: 'reload' },
  143. { role: 'forceReload' },
  144. { role: 'toggleDevTools' },
  145. { type: 'separator' },
  146. { role: 'resetZoom' },
  147. { role: 'zoomIn' },
  148. { role: 'zoomOut' },
  149. { type: 'separator' },
  150. { role: 'togglefullscreen' },
  151. ],
  152. },
  153. {
  154. role: 'window',
  155. submenu: [
  156. { role: 'minimize' },
  157. { role: 'zoom' },
  158. { type: 'separator' },
  159. { role: 'front' },
  160. ],
  161. },
  162. {
  163. role: 'help',
  164. submenu: [
  165. {
  166. label: 'Website',
  167. click () {
  168. shell.openExternal('https://eugeny.github.io/terminus')
  169. },
  170. },
  171. ],
  172. },
  173. ]
  174. Menu.setApplicationMenu(Menu.buildFromTemplate(template))
  175. }
  176. }