app.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { app, ipcMain, Menu, Tray, shell, screen, globalShortcut, MenuItemConstructorOptions } from 'electron'
  2. import promiseIpc from 'electron-promise-ipc'
  3. import * as remote from '@electron/remote/main'
  4. import { exec } from 'mz/child_process'
  5. import * as path from 'path'
  6. import * as fs from 'fs'
  7. import { Subject, throttleTime } from 'rxjs'
  8. import { loadConfig } from './config'
  9. import { Window, WindowOptions } from './window'
  10. import { pluginManager } from './pluginManager'
  11. import { PTYManager } from './pty'
  12. /* eslint-disable block-scoped-var */
  13. try {
  14. var wnr = require('windows-native-registry') // eslint-disable-line @typescript-eslint/no-var-requires, no-var
  15. } catch (_) { }
  16. export class Application {
  17. private tray?: Tray
  18. private ptyManager = new PTYManager()
  19. private windows: Window[] = []
  20. private globalHotkey$ = new Subject<void>()
  21. private quitRequested = false
  22. userPluginsPath: string
  23. constructor () {
  24. remote.initialize()
  25. this.useBuiltinGraphics()
  26. this.ptyManager.init(this)
  27. ipcMain.on('app:config-change', (_event, config) => {
  28. this.broadcast('host:config-change', config)
  29. })
  30. ipcMain.on('app:register-global-hotkey', (_event, specs) => {
  31. globalShortcut.unregisterAll()
  32. for (const spec of specs) {
  33. globalShortcut.register(spec, () => this.globalHotkey$.next())
  34. }
  35. })
  36. this.globalHotkey$.pipe(throttleTime(100)).subscribe(() => {
  37. this.onGlobalHotkey()
  38. })
  39. ;(promiseIpc as any).on('plugin-manager:install', (name, version) => {
  40. return pluginManager.install(this.userPluginsPath, name, version)
  41. })
  42. ;(promiseIpc as any).on('plugin-manager:uninstall', (name) => {
  43. return pluginManager.uninstall(this.userPluginsPath, name)
  44. })
  45. ;(promiseIpc as any).on('get-default-mac-shell', async () => {
  46. try {
  47. return (await exec(`/usr/bin/dscl . -read /Users/${process.env.LOGNAME} UserShell`))[0].toString().split(' ')[1].trim()
  48. } catch {
  49. return '/bin/bash'
  50. }
  51. })
  52. const configData = loadConfig()
  53. if (process.platform === 'linux') {
  54. app.commandLine.appendSwitch('no-sandbox')
  55. if (((configData.appearance || {}).opacity || 1) !== 1) {
  56. app.commandLine.appendSwitch('enable-transparent-visuals')
  57. app.disableHardwareAcceleration()
  58. }
  59. }
  60. this.userPluginsPath = path.join(
  61. app.getPath('userData'),
  62. 'plugins',
  63. )
  64. if (!fs.existsSync(this.userPluginsPath)) {
  65. fs.mkdirSync(this.userPluginsPath)
  66. }
  67. app.commandLine.appendSwitch('disable-http-cache')
  68. app.commandLine.appendSwitch('max-active-webgl-contexts', '9000')
  69. app.commandLine.appendSwitch('lang', 'EN')
  70. for (const flag of configData.flags || [['force_discrete_gpu', '0']]) {
  71. app.commandLine.appendSwitch(flag[0], flag[1])
  72. }
  73. app.on('window-all-closed', () => {
  74. if (this.quitRequested || process.platform !== 'darwin') {
  75. app.quit()
  76. }
  77. })
  78. }
  79. init (): void {
  80. screen.on('display-metrics-changed', () => this.broadcast('host:display-metrics-changed'))
  81. screen.on('display-added', () => this.broadcast('host:displays-changed'))
  82. screen.on('display-removed', () => this.broadcast('host:displays-changed'))
  83. }
  84. async newWindow (options?: WindowOptions): Promise<Window> {
  85. const window = new Window(this, options)
  86. this.windows.push(window)
  87. window.visible$.subscribe(visible => {
  88. if (visible) {
  89. this.disableTray()
  90. } else {
  91. this.enableTray()
  92. }
  93. })
  94. window.closed$.subscribe(() => {
  95. this.windows = this.windows.filter(x => x !== window)
  96. })
  97. if (process.platform === 'darwin') {
  98. this.setupMenu()
  99. }
  100. await window.ready
  101. return window
  102. }
  103. onGlobalHotkey (): void {
  104. if (this.windows.some(x => x.isFocused() && x.isVisible())) {
  105. for (const window of this.windows) {
  106. window.hide()
  107. }
  108. } else {
  109. for (const window of this.windows) {
  110. window.present()
  111. }
  112. }
  113. }
  114. presentAllWindows (): void {
  115. for (const window of this.windows) {
  116. window.present()
  117. }
  118. }
  119. broadcast (event: string, ...args: any[]): void {
  120. for (const window of this.windows) {
  121. window.send(event, ...args)
  122. }
  123. }
  124. async send (event: string, ...args: any[]): Promise<void> {
  125. if (!this.hasWindows()) {
  126. await this.newWindow()
  127. }
  128. this.windows.filter(w => !w.isDestroyed())[0].send(event, ...args)
  129. }
  130. enableTray (): void {
  131. if (this.tray || process.platform === 'linux') {
  132. return
  133. }
  134. if (process.platform === 'darwin') {
  135. this.tray = new Tray(`${app.getAppPath()}/assets/tray-darwinTemplate.png`)
  136. this.tray.setPressedImage(`${app.getAppPath()}/assets/tray-darwinHighlightTemplate.png`)
  137. } else {
  138. this.tray = new Tray(`${app.getAppPath()}/assets/tray.png`)
  139. }
  140. this.tray.on('click', () => setTimeout(() => this.focus()))
  141. const contextMenu = Menu.buildFromTemplate([{
  142. label: 'Show',
  143. click: () => this.focus(),
  144. }])
  145. if (process.platform !== 'darwin') {
  146. this.tray.setContextMenu(contextMenu)
  147. }
  148. this.tray.setToolTip(`Tabby ${app.getVersion()}`)
  149. }
  150. disableTray (): void {
  151. if (process.platform === 'linux') {
  152. return
  153. }
  154. this.tray?.destroy()
  155. this.tray = null
  156. }
  157. hasWindows (): boolean {
  158. return !!this.windows.length
  159. }
  160. focus (): void {
  161. for (const window of this.windows) {
  162. window.show()
  163. }
  164. }
  165. handleSecondInstance (argv: string[], cwd: string): void {
  166. this.presentAllWindows()
  167. this.windows[this.windows.length - 1].passCliArguments(argv, cwd, true)
  168. }
  169. private useBuiltinGraphics (): void {
  170. if (process.platform === 'win32') {
  171. const keyPath = 'SOFTWARE\\Microsoft\\DirectX\\UserGpuPreferences'
  172. const valueName = app.getPath('exe')
  173. if (!wnr.getRegistryValue(wnr.HK.CU, keyPath, valueName)) {
  174. wnr.setRegistryValue(wnr.HK.CU, keyPath, valueName, wnr.REG.SZ, 'GpuPreference=1;')
  175. }
  176. }
  177. }
  178. private setupMenu () {
  179. const template: MenuItemConstructorOptions[] = [
  180. {
  181. label: 'Application',
  182. submenu: [
  183. { role: 'about', label: 'About Tabby' },
  184. { type: 'separator' },
  185. {
  186. label: 'Preferences',
  187. accelerator: 'Cmd+,',
  188. click: async () => {
  189. if (!this.hasWindows()) {
  190. await this.newWindow()
  191. }
  192. this.windows[0].send('host:preferences-menu')
  193. },
  194. },
  195. { type: 'separator' },
  196. { role: 'services', submenu: [] },
  197. { type: 'separator' },
  198. { role: 'hide' },
  199. { role: 'hideOthers' },
  200. { role: 'unhide' },
  201. { type: 'separator' },
  202. {
  203. label: 'Quit',
  204. accelerator: 'Cmd+Q',
  205. click: () => {
  206. this.quitRequested = true
  207. app.quit()
  208. },
  209. },
  210. ],
  211. },
  212. {
  213. label: 'Edit',
  214. submenu: [
  215. { role: 'undo' },
  216. { role: 'redo' },
  217. { type: 'separator' },
  218. { role: 'cut' },
  219. { role: 'copy' },
  220. { role: 'paste' },
  221. { role: 'pasteAndMatchStyle' },
  222. { role: 'delete' },
  223. { role: 'selectAll' },
  224. ],
  225. },
  226. {
  227. label: 'View',
  228. submenu: [
  229. { role: 'toggleDevTools' },
  230. { type: 'separator' },
  231. { role: 'togglefullscreen' },
  232. ],
  233. },
  234. {
  235. role: 'window',
  236. submenu: [
  237. { role: 'minimize' },
  238. { role: 'zoom' },
  239. { type: 'separator' },
  240. { role: 'front' },
  241. ],
  242. },
  243. {
  244. role: 'help',
  245. submenu: [
  246. {
  247. label: 'Website',
  248. click () {
  249. shell.openExternal('https://eugeny.github.io/tabby')
  250. },
  251. },
  252. ],
  253. },
  254. ]
  255. if (process.env.TABBY_DEV) {
  256. template[2].submenu['unshift']({ role: 'reload' })
  257. }
  258. Menu.setApplicationMenu(Menu.buildFromTemplate(template))
  259. }
  260. }