2020-12-24 13:03:14 +00:00
|
|
|
import { app, ipcMain, Menu, Tray, shell, screen, globalShortcut, MenuItemConstructorOptions } from 'electron'
|
2021-01-01 17:25:54 +00:00
|
|
|
import * as promiseIpc from 'electron-promise-ipc'
|
2021-03-20 16:12:39 +00:00
|
|
|
import * as remote from '@electron/remote/main'
|
|
|
|
|
2018-10-06 18:50:06 +00:00
|
|
|
import { loadConfig } from './config'
|
2018-11-11 12:24:27 +00:00
|
|
|
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'
|
2018-08-31 13:41:28 +00:00
|
|
|
|
|
|
|
export class Application {
|
2021-01-02 19:10:00 +00:00
|
|
|
private tray?: Tray
|
2021-04-04 18:07:57 +00:00
|
|
|
private ptyManager = new PTYManager()
|
2018-08-31 13:41:28 +00:00
|
|
|
private windows: Window[] = []
|
|
|
|
|
|
|
|
constructor () {
|
2021-03-20 16:12:39 +00:00
|
|
|
remote.initialize()
|
2021-04-04 18:07:57 +00:00
|
|
|
this.ptyManager.init(this)
|
2021-03-20 16:12:39 +00:00
|
|
|
|
2020-04-19 09:47:31 +00:00
|
|
|
ipcMain.on('app:config-change', (_event, config) => {
|
|
|
|
this.broadcast('host:config-change', config)
|
|
|
|
})
|
2020-04-20 09:25:20 +00:00
|
|
|
|
2020-04-19 09:47:31 +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) {
|
2020-04-19 09:47:31 +00:00
|
|
|
globalShortcut.register(spec, () => {
|
|
|
|
this.onGlobalHotkey()
|
|
|
|
})
|
|
|
|
}
|
2018-08-31 13:41:28 +00:00
|
|
|
})
|
2018-10-06 18:50:06 +00:00
|
|
|
|
2021-01-01 17:25:54 +00:00
|
|
|
;(promiseIpc as any).on('plugin-manager:install', (path, name, version) => {
|
|
|
|
return pluginManager.install(path, name, version)
|
|
|
|
})
|
|
|
|
|
|
|
|
;(promiseIpc as any).on('plugin-manager:uninstall', (path, name) => {
|
|
|
|
return pluginManager.uninstall(path, name)
|
|
|
|
})
|
|
|
|
|
2018-10-06 18:50:06 +00:00
|
|
|
const configData = loadConfig()
|
2019-12-29 18:07:32 +00:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
app.commandLine.appendSwitch('no-sandbox')
|
|
|
|
if (((configData.appearance || {}).opacity || 1) !== 1) {
|
|
|
|
app.commandLine.appendSwitch('enable-transparent-visuals')
|
|
|
|
app.disableHardwareAcceleration()
|
|
|
|
}
|
2018-10-06 18:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.commandLine.appendSwitch('disable-http-cache')
|
2021-04-24 09:05:37 +00:00
|
|
|
app.commandLine.appendSwitch('max-active-webgl-contexts', '9000')
|
2019-01-27 22:58:55 +00:00
|
|
|
app.commandLine.appendSwitch('lang', 'EN')
|
2020-02-05 11:54:10 +00:00
|
|
|
app.allowRendererProcessReuse = false
|
2019-06-14 15:15:52 +00:00
|
|
|
|
|
|
|
for (const flag of configData.flags || [['force_discrete_gpu', '0']]) {
|
|
|
|
app.commandLine.appendSwitch(flag[0], flag[1])
|
|
|
|
}
|
2018-08-31 13:41:28 +00:00
|
|
|
}
|
|
|
|
|
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'))
|
2019-02-09 23:23:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 12:24:27 +00:00
|
|
|
async newWindow (options?: WindowOptions): Promise<Window> {
|
2020-12-24 13:03:14 +00:00
|
|
|
const window = new Window(options)
|
2018-08-31 13:41:28 +00:00
|
|
|
this.windows.push(window)
|
|
|
|
window.visible$.subscribe(visible => {
|
|
|
|
if (visible) {
|
|
|
|
this.disableTray()
|
|
|
|
} else {
|
|
|
|
this.enableTray()
|
|
|
|
}
|
|
|
|
})
|
2020-04-19 09:47:31 +00:00
|
|
|
window.closed$.subscribe(() => {
|
|
|
|
this.windows = this.windows.filter(x => x !== window)
|
|
|
|
})
|
2018-09-20 11:01:25 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.setupMenu()
|
|
|
|
}
|
2018-08-31 13:41:28 +00:00
|
|
|
await window.ready
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
2020-04-20 09:25:20 +00:00
|
|
|
onGlobalHotkey (): void {
|
2021-01-24 18:27:36 +00:00
|
|
|
if (this.windows.some(x => x.isFocused() && x.isVisible())) {
|
2020-12-24 13:03:14 +00:00
|
|
|
for (const window of this.windows) {
|
2020-04-19 09:47:31 +00:00
|
|
|
window.hide()
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-24 13:03:14 +00:00
|
|
|
for (const window of this.windows) {
|
2020-04-19 09:47:31 +00:00
|
|
|
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) {
|
2020-04-19 09:47:31 +00:00
|
|
|
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) {
|
2018-08-31 13:41:28 +00:00
|
|
|
window.send(event, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 13:03:14 +00:00
|
|
|
async send (event: string, ...args: any[]): Promise<void> {
|
2018-08-31 13:41:28 +00:00
|
|
|
if (!this.hasWindows()) {
|
|
|
|
await this.newWindow()
|
|
|
|
}
|
2019-07-17 03:36:24 +00:00
|
|
|
this.windows.filter(w => !w.isDestroyed())[0].send(event, ...args)
|
2018-08-31 13:41:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:10:45 +00:00
|
|
|
enableTray (): void {
|
2021-04-04 17:59:23 +00:00
|
|
|
if (this.tray || process.platform === 'linux') {
|
2018-08-31 13:41:28 +00: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 12:16:31 +00:00
|
|
|
this.tray.on('click', () => setTimeout(() => this.focus()))
|
2018-08-31 13:41:28 +00:00
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([{
|
|
|
|
label: 'Show',
|
|
|
|
click: () => this.focus(),
|
|
|
|
}])
|
|
|
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.tray.setContextMenu(contextMenu)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tray.setToolTip(`Terminus ${app.getVersion()}`)
|
|
|
|
}
|
|
|
|
|
2020-03-01 15:10:45 +00:00
|
|
|
disableTray (): void {
|
2021-04-04 17:59:23 +00:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
return
|
|
|
|
}
|
2021-01-02 19:10:00 +00:00
|
|
|
this.tray?.destroy()
|
|
|
|
this.tray = null
|
2018-08-31 13:41:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 16:07:11 +00:00
|
|
|
hasWindows (): boolean {
|
2018-08-31 13:41:28 +00:00
|
|
|
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) {
|
2018-08-31 13:41:28 +00:00
|
|
|
window.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:23:35 +00:00
|
|
|
handleSecondInstance (argv: string[], cwd: string): void {
|
|
|
|
this.presentAllWindows()
|
2021-01-24 18:06:41 +00:00
|
|
|
this.windows[this.windows.length - 1].passCliArguments(argv, cwd, true)
|
2020-05-02 15:23:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 13:41:28 +00:00
|
|
|
private setupMenu () {
|
2020-12-24 13:03:14 +00:00
|
|
|
const template: MenuItemConstructorOptions[] = [
|
2018-08-31 13:41:28 +00:00
|
|
|
{
|
|
|
|
label: 'Application',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'about', label: 'About Terminus' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Preferences',
|
|
|
|
accelerator: 'Cmd+,',
|
2018-12-30 16:32:30 +00:00
|
|
|
click: async () => {
|
2018-08-31 13:41:28 +00: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 09:52:52 +00:00
|
|
|
{ role: 'hideOthers' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ role: 'unhide' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Quit',
|
|
|
|
accelerator: 'Cmd+Q',
|
|
|
|
click () {
|
|
|
|
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' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ role: 'delete' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'selectAll' },
|
2018-08-31 13:41:28 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'reload' },
|
2019-08-27 09:52:52 +00:00
|
|
|
{ role: 'toggleDevTools' },
|
2018-08-31 13:41:28 +00:00
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'togglefullscreen' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'window',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'front' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Website',
|
|
|
|
click () {
|
|
|
|
shell.openExternal('https://eugeny.github.io/terminus')
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-02-05 12:16:31 +00:00
|
|
|
},
|
2018-08-31 13:41:28 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
|
|
|
|
}
|
|
|
|
}
|