2020-12-24 16:03:14 +03:00
|
|
|
import { app, ipcMain, Menu, Tray, shell, screen, globalShortcut, MenuItemConstructorOptions } from 'electron'
|
2021-12-06 21:13:26 +03:00
|
|
|
import promiseIpc from 'electron-promise-ipc'
|
2021-03-20 19:12:39 +03:00
|
|
|
import * as remote from '@electron/remote/main'
|
2021-08-20 00:29:44 +03:00
|
|
|
import { exec } from 'mz/child_process'
|
2021-06-19 02:36:25 +03:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as fs from 'fs'
|
2021-08-07 11:25:49 +03:00
|
|
|
import { Subject, throttleTime } from 'rxjs'
|
2021-03-20 19:12:39 +03:00
|
|
|
|
2022-05-28 13:35:32 +03:00
|
|
|
import { saveConfig } from './config'
|
2018-11-11 15:24:27 +03:00
|
|
|
import { Window, WindowOptions } from './window'
|
2021-01-01 20:25:54 +03:00
|
|
|
import { pluginManager } from './pluginManager'
|
2021-04-04 21:07:57 +03:00
|
|
|
import { PTYManager } from './pty'
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2021-05-24 18:48:12 +03:00
|
|
|
/* eslint-disable block-scoped-var */
|
|
|
|
|
|
|
|
try {
|
|
|
|
var wnr = require('windows-native-registry') // eslint-disable-line @typescript-eslint/no-var-requires, no-var
|
|
|
|
} catch (_) { }
|
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
export class Application {
|
2021-01-02 22:10:00 +03:00
|
|
|
private tray?: Tray
|
2021-04-04 21:07:57 +03:00
|
|
|
private ptyManager = new PTYManager()
|
2018-08-31 16:41:28 +03:00
|
|
|
private windows: Window[] = []
|
2021-08-07 11:25:49 +03:00
|
|
|
private globalHotkey$ = new Subject<void>()
|
2021-08-27 19:26:57 +03:00
|
|
|
private quitRequested = false
|
2021-06-19 02:36:25 +03:00
|
|
|
userPluginsPath: string
|
2018-08-31 16:41:28 +03:00
|
|
|
|
2022-05-28 13:35:32 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
constructor (private configStore: any) {
|
2021-03-20 19:12:39 +03:00
|
|
|
remote.initialize()
|
2021-05-24 18:48:12 +03:00
|
|
|
this.useBuiltinGraphics()
|
2021-04-04 21:07:57 +03:00
|
|
|
this.ptyManager.init(this)
|
2021-03-20 19:12:39 +03:00
|
|
|
|
2020-04-19 12:47:31 +03:00
|
|
|
ipcMain.on('app:config-change', (_event, config) => {
|
|
|
|
this.broadcast('host:config-change', config)
|
2021-12-16 01:21:56 +03:00
|
|
|
this.configStore = config
|
2020-04-19 12:47:31 +03:00
|
|
|
})
|
2020-04-20 12:25:20 +03:00
|
|
|
|
2022-05-28 13:35:32 +03:00
|
|
|
ipcMain.on('app:save-config', (_event, data) => {
|
|
|
|
saveConfig(data)
|
|
|
|
})
|
|
|
|
|
2020-04-19 12:47:31 +03:00
|
|
|
ipcMain.on('app:register-global-hotkey', (_event, specs) => {
|
|
|
|
globalShortcut.unregisterAll()
|
2020-12-24 16:03:14 +03:00
|
|
|
for (const spec of specs) {
|
2021-08-07 11:25:49 +03:00
|
|
|
globalShortcut.register(spec, () => this.globalHotkey$.next())
|
2020-04-19 12:47:31 +03:00
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
})
|
2018-10-06 21:50:06 +03:00
|
|
|
|
2021-08-07 11:25:49 +03:00
|
|
|
this.globalHotkey$.pipe(throttleTime(100)).subscribe(() => {
|
|
|
|
this.onGlobalHotkey()
|
|
|
|
})
|
|
|
|
|
2021-06-19 02:36:25 +03:00
|
|
|
;(promiseIpc as any).on('plugin-manager:install', (name, version) => {
|
|
|
|
return pluginManager.install(this.userPluginsPath, name, version)
|
2021-01-01 20:25:54 +03:00
|
|
|
})
|
|
|
|
|
2021-06-19 02:36:25 +03:00
|
|
|
;(promiseIpc as any).on('plugin-manager:uninstall', (name) => {
|
|
|
|
return pluginManager.uninstall(this.userPluginsPath, name)
|
2021-08-20 00:29:44 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
;(promiseIpc as any).on('get-default-mac-shell', async () => {
|
2021-11-13 21:04:30 +03:00
|
|
|
try {
|
|
|
|
return (await exec(`/usr/bin/dscl . -read /Users/${process.env.LOGNAME} UserShell`))[0].toString().split(' ')[1].trim()
|
|
|
|
} catch {
|
|
|
|
return '/bin/bash'
|
|
|
|
}
|
2021-01-01 20:25:54 +03:00
|
|
|
})
|
|
|
|
|
2019-12-29 21:07:32 +03:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
app.commandLine.appendSwitch('no-sandbox')
|
2021-12-16 01:21:56 +03:00
|
|
|
if (((this.configStore.appearance || {}).opacity || 1) !== 1) {
|
2019-12-29 21:07:32 +03:00
|
|
|
app.commandLine.appendSwitch('enable-transparent-visuals')
|
|
|
|
app.disableHardwareAcceleration()
|
|
|
|
}
|
2022-01-14 02:25:40 +03:00
|
|
|
}
|
|
|
|
if (this.configStore.hacks?.disableGPU) {
|
|
|
|
app.commandLine.appendSwitch('disable-gpu')
|
|
|
|
app.disableHardwareAcceleration()
|
2018-10-06 21:50:06 +03:00
|
|
|
}
|
|
|
|
|
2021-06-19 02:36:25 +03:00
|
|
|
this.userPluginsPath = path.join(
|
|
|
|
app.getPath('userData'),
|
|
|
|
'plugins',
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!fs.existsSync(this.userPluginsPath)) {
|
|
|
|
fs.mkdirSync(this.userPluginsPath)
|
|
|
|
}
|
|
|
|
|
2018-10-06 21:50:06 +03:00
|
|
|
app.commandLine.appendSwitch('disable-http-cache')
|
2021-04-24 12:05:37 +03:00
|
|
|
app.commandLine.appendSwitch('max-active-webgl-contexts', '9000')
|
2019-01-28 01:58:55 +03:00
|
|
|
app.commandLine.appendSwitch('lang', 'EN')
|
2019-06-14 18:15:52 +03:00
|
|
|
|
2021-12-16 01:21:56 +03:00
|
|
|
for (const flag of this.configStore.flags || [['force_discrete_gpu', '0']]) {
|
2019-06-14 18:15:52 +03:00
|
|
|
app.commandLine.appendSwitch(flag[0], flag[1])
|
|
|
|
}
|
2021-08-27 19:26:57 +03:00
|
|
|
|
2022-02-13 19:37:56 +03:00
|
|
|
app.on('before-quit', () => {
|
|
|
|
this.quitRequested = true
|
|
|
|
})
|
|
|
|
|
2021-08-27 19:26:57 +03:00
|
|
|
app.on('window-all-closed', () => {
|
2022-02-12 12:03:49 +03:00
|
|
|
if (this.quitRequested || process.platform !== 'darwin') {
|
2021-08-27 19:26:57 +03:00
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-01 18:10:45 +03:00
|
|
|
init (): void {
|
2020-12-24 16:03:14 +03:00
|
|
|
screen.on('display-metrics-changed', () => this.broadcast('host:display-metrics-changed'))
|
|
|
|
screen.on('display-added', () => this.broadcast('host:displays-changed'))
|
|
|
|
screen.on('display-removed', () => this.broadcast('host:displays-changed'))
|
2019-02-10 02:23:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 15:24:27 +03:00
|
|
|
async newWindow (options?: WindowOptions): Promise<Window> {
|
2022-05-28 13:35:32 +03:00
|
|
|
const window = new Window(this, this.configStore, options)
|
2018-08-31 16:41:28 +03:00
|
|
|
this.windows.push(window)
|
2021-12-16 01:21:56 +03:00
|
|
|
if (this.windows.length === 1){
|
|
|
|
window.makeMain()
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
window.visible$.subscribe(visible => {
|
|
|
|
if (visible) {
|
|
|
|
this.disableTray()
|
|
|
|
} else {
|
|
|
|
this.enableTray()
|
|
|
|
}
|
|
|
|
})
|
2020-04-19 12:47:31 +03:00
|
|
|
window.closed$.subscribe(() => {
|
|
|
|
this.windows = this.windows.filter(x => x !== window)
|
2021-12-16 01:21:56 +03:00
|
|
|
if (!this.windows.some(x => x.isMainWindow)) {
|
|
|
|
this.windows[0]?.makeMain()
|
|
|
|
this.windows[0]?.present()
|
|
|
|
}
|
2020-04-19 12:47:31 +03:00
|
|
|
})
|
2018-09-20 14:01:25 +03:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.setupMenu()
|
|
|
|
}
|
2018-08-31 16:41:28 +03:00
|
|
|
await window.ready
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:25:20 +03:00
|
|
|
onGlobalHotkey (): void {
|
2021-12-16 01:21:56 +03:00
|
|
|
let isPresent = this.windows.some(x => x.isFocused() && x.isVisible())
|
|
|
|
const isDockedOnTop = this.windows.some(x => x.isDockedOnTop())
|
|
|
|
if (isDockedOnTop) {
|
|
|
|
// if docked and on top, hide even if not focused right now
|
|
|
|
isPresent = this.windows.some(x => x.isVisible())
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPresent) {
|
2020-12-24 16:03:14 +03:00
|
|
|
for (const window of this.windows) {
|
2020-04-19 12:47:31 +03:00
|
|
|
window.hide()
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-24 16:03:14 +03:00
|
|
|
for (const window of this.windows) {
|
2020-04-19 12:47:31 +03:00
|
|
|
window.present()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:25:20 +03:00
|
|
|
presentAllWindows (): void {
|
2020-12-24 16:03:14 +03:00
|
|
|
for (const window of this.windows) {
|
2020-04-19 12:47:31 +03:00
|
|
|
window.present()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 16:03:14 +03:00
|
|
|
broadcast (event: string, ...args: any[]): void {
|
2020-03-01 18:10:45 +03:00
|
|
|
for (const window of this.windows) {
|
2018-08-31 16:41:28 +03:00
|
|
|
window.send(event, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 16:03:14 +03:00
|
|
|
async send (event: string, ...args: any[]): Promise<void> {
|
2018-08-31 16:41:28 +03:00
|
|
|
if (!this.hasWindows()) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
2019-07-17 06:36:24 +03:00
|
|
|
this.windows.filter(w => !w.isDestroyed())[0].send(event, ...args)
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-01 18:10:45 +03:00
|
|
|
enableTray (): void {
|
2021-04-04 20:59:23 +03:00
|
|
|
if (this.tray || process.platform === 'linux') {
|
2018-08-31 16:41:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.tray = new Tray(`${app.getAppPath()}/assets/tray-darwinTemplate.png`)
|
|
|
|
this.tray.setPressedImage(`${app.getAppPath()}/assets/tray-darwinHighlightTemplate.png`)
|
|
|
|
} else {
|
|
|
|
this.tray = new Tray(`${app.getAppPath()}/assets/tray.png`)
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:16:31 +03:00
|
|
|
this.tray.on('click', () => setTimeout(() => this.focus()))
|
2018-08-31 16:41:28 +03:00
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([{
|
|
|
|
label: 'Show',
|
|
|
|
click: () => this.focus(),
|
|
|
|
}])
|
|
|
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.tray.setContextMenu(contextMenu)
|
|
|
|
}
|
|
|
|
|
2021-06-30 00:57:04 +03:00
|
|
|
this.tray.setToolTip(`Tabby ${app.getVersion()}`)
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-01 18:10:45 +03:00
|
|
|
disableTray (): void {
|
2021-04-04 20:59:23 +03:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
return
|
|
|
|
}
|
2021-01-02 22:10:00 +03:00
|
|
|
this.tray?.destroy()
|
|
|
|
this.tray = null
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-01 19:07:11 +03:00
|
|
|
hasWindows (): boolean {
|
2018-08-31 16:41:28 +03:00
|
|
|
return !!this.windows.length
|
|
|
|
}
|
|
|
|
|
2020-03-01 18:10:45 +03:00
|
|
|
focus (): void {
|
2020-12-24 16:03:14 +03:00
|
|
|
for (const window of this.windows) {
|
2021-12-16 01:21:56 +03:00
|
|
|
window.present()
|
2018-08-31 16:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 22:30:03 +03:00
|
|
|
async handleSecondInstance (argv: string[], cwd: string): Promise<void> {
|
2022-04-15 21:04:30 +03:00
|
|
|
if (!this.windows.length) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
2020-05-02 18:23:35 +03:00
|
|
|
this.presentAllWindows()
|
2021-01-24 21:06:41 +03:00
|
|
|
this.windows[this.windows.length - 1].passCliArguments(argv, cwd, true)
|
2020-05-02 18:23:35 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:48:12 +03:00
|
|
|
private useBuiltinGraphics (): void {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
const keyPath = 'SOFTWARE\\Microsoft\\DirectX\\UserGpuPreferences'
|
|
|
|
const valueName = app.getPath('exe')
|
|
|
|
if (!wnr.getRegistryValue(wnr.HK.CU, keyPath, valueName)) {
|
|
|
|
wnr.setRegistryValue(wnr.HK.CU, keyPath, valueName, wnr.REG.SZ, 'GpuPreference=1;')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
private setupMenu () {
|
2020-12-24 16:03:14 +03:00
|
|
|
const template: MenuItemConstructorOptions[] = [
|
2018-08-31 16:41:28 +03:00
|
|
|
{
|
|
|
|
label: 'Application',
|
|
|
|
submenu: [
|
2021-06-30 00:57:04 +03:00
|
|
|
{ role: 'about', label: 'About Tabby' },
|
2018-08-31 16:41:28 +03:00
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Preferences',
|
|
|
|
accelerator: 'Cmd+,',
|
2018-12-30 19:32:30 +03:00
|
|
|
click: async () => {
|
2018-08-31 16:41:28 +03:00
|
|
|
if (!this.hasWindows()) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
|
|
|
this.windows[0].send('host:preferences-menu')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'services', submenu: [] },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
2019-08-27 12:52:52 +03:00
|
|
|
{ role: 'hideOthers' },
|
2018-08-31 16:41:28 +03:00
|
|
|
{ role: 'unhide' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Quit',
|
|
|
|
accelerator: 'Cmd+Q',
|
2021-08-27 19:26:57 +03:00
|
|
|
click: () => {
|
|
|
|
this.quitRequested = true
|
2018-08-31 16:41:28 +03:00
|
|
|
app.quit()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
2019-08-27 12:52:52 +03:00
|
|
|
{ role: 'pasteAndMatchStyle' },
|
2018-08-31 16:41:28 +03:00
|
|
|
{ role: 'delete' },
|
2019-08-27 12:52:52 +03:00
|
|
|
{ role: 'selectAll' },
|
2018-08-31 16:41:28 +03:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
2019-08-27 12:52:52 +03:00
|
|
|
{ role: 'toggleDevTools' },
|
2018-08-31 16:41:28 +03:00
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'togglefullscreen' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'window',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'front' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Website',
|
|
|
|
click () {
|
2021-07-01 23:10:12 +03:00
|
|
|
shell.openExternal('https://eugeny.github.io/tabby')
|
2018-08-31 16:41:28 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-02-05 15:16:31 +03:00
|
|
|
},
|
2018-08-31 16:41:28 +03:00
|
|
|
]
|
|
|
|
|
2021-10-28 10:26:55 +03:00
|
|
|
if (process.env.TABBY_DEV) {
|
2021-10-28 10:55:20 +03:00
|
|
|
template[2].submenu['unshift']({ role: 'reload' })
|
2021-10-28 10:26:55 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:41:28 +03:00
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
|
|
|
|
}
|
|
|
|
}
|