hostApp.service.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Injectable, NgZone, Injector } from '@angular/core'
  2. import { isWindowsBuild, WIN_BUILD_FLUENT_BG_SUPPORTED, HostAppService, Platform, CLIHandler } from 'tabby-core'
  3. import { ElectronService } from '../services/electron.service'
  4. @Injectable({ providedIn: 'root' })
  5. export class ElectronHostAppService extends HostAppService {
  6. get platform (): Platform {
  7. return this.configPlatform
  8. }
  9. get configPlatform (): Platform {
  10. return {
  11. win32: Platform.Windows,
  12. darwin: Platform.macOS,
  13. linux: Platform.Linux,
  14. }[process.platform]
  15. }
  16. constructor (
  17. private zone: NgZone,
  18. private electron: ElectronService,
  19. injector: Injector,
  20. ) {
  21. super(injector)
  22. electron.ipcRenderer.on('host:preferences-menu', () => this.zone.run(() => this.settingsUIRequest.next()))
  23. electron.ipcRenderer.on('cli', (_$event, argv: any, cwd: string, secondInstance: boolean) => this.zone.run(async () => {
  24. const event = { argv, cwd, secondInstance }
  25. this.logger.info('CLI arguments received:', event)
  26. const cliHandlers = injector.get(CLIHandler) as unknown as CLIHandler[]
  27. cliHandlers.sort((a, b) => b.priority - a.priority)
  28. let handled = false
  29. for (const handler of cliHandlers) {
  30. if (handled && handler.firstMatchOnly) {
  31. continue
  32. }
  33. if (await handler.handle(event)) {
  34. this.logger.info('CLI handler matched:', handler.constructor.name)
  35. handled = true
  36. }
  37. }
  38. }))
  39. electron.ipcRenderer.on('host:config-change', () => this.zone.run(() => {
  40. this.configChangeBroadcast.next()
  41. }))
  42. if (isWindowsBuild(WIN_BUILD_FLUENT_BG_SUPPORTED)) {
  43. electron.ipcRenderer.send('window-set-disable-vibrancy-while-dragging', true)
  44. }
  45. }
  46. newWindow (): void {
  47. this.electron.ipcRenderer.send('app:new-window')
  48. }
  49. async saveConfig (data: string): Promise<void> {
  50. await this.electron.ipcRenderer.invoke('app:save-config', data)
  51. }
  52. emitReady (): void {
  53. this.electron.ipcRenderer.send('app:ready')
  54. }
  55. relaunch (): void {
  56. const isPortable = !!process.env.PORTABLE_EXECUTABLE_FILE
  57. if (isPortable) {
  58. this.electron.app.relaunch({ execPath: process.env.PORTABLE_EXECUTABLE_FILE })
  59. } else {
  60. let args: string[] = []
  61. if (this.platform === Platform.Linux) {
  62. args = ['--no-sandbox']
  63. }
  64. this.electron.app.relaunch({ args })
  65. }
  66. this.electron.app.exit()
  67. }
  68. quit (): void {
  69. this.logger.info('Quitting')
  70. this.electron.app.quit()
  71. }
  72. }