tabby/app/lib/app.ts

324 lines
10 KiB
TypeScript
Raw Normal View History

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