tabby/app/lib/app.ts

196 lines
6 KiB
TypeScript
Raw Normal View History

import { app, ipcMain, Menu, Tray, shell } from 'electron'
2020-02-05 12:16:31 +00:00
// eslint-disable-next-line no-duplicate-imports
import * as electron from 'electron'
2018-10-06 18:50:06 +00:00
import { loadConfig } from './config'
import { Window, WindowOptions } from './window'
export class Application {
private tray: Tray
private windows: Window[] = []
constructor () {
ipcMain.on('app:config-change', () => {
this.broadcast('host:config-change')
})
2018-10-06 18:50:06 +00:00
const configData = loadConfig()
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')
2019-01-27 22:58:55 +00:00
app.commandLine.appendSwitch('lang', 'EN')
2020-02-05 11:54:10 +00:00
app.allowRendererProcessReuse = false
for (const flag of configData.flags || [['force_discrete_gpu', '0']]) {
app.commandLine.appendSwitch(flag[0], flag[1])
}
}
2020-03-01 15:10:45 +00:00
init (): void {
electron.screen.on('display-metrics-changed', () => this.broadcast('host:display-metrics-changed'))
}
async newWindow (options?: WindowOptions): Promise<Window> {
let window = new Window(options)
this.windows.push(window)
window.visible$.subscribe(visible => {
if (visible) {
this.disableTray()
} else {
this.enableTray()
}
})
2018-09-20 11:01:25 +00:00
if (process.platform === 'darwin') {
this.setupMenu()
}
await window.ready
return window
}
2020-03-01 15:10:45 +00:00
broadcast (event: string, ...args): void {
for (const window of this.windows) {
window.send(event, ...args)
}
}
2020-03-01 16:07:11 +00:00
async send (event: string, ...args): 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) {
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)
}
this.tray.setToolTip(`Terminus ${app.getVersion()}`)
}
2020-03-01 15:10:45 +00:00
disableTray (): void {
if (this.tray) {
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 {
for (let window of this.windows) {
window.show()
}
}
private setupMenu () {
let template: Electron.MenuItemConstructorOptions[] = [
{
label: 'Application',
submenu: [
{ role: 'about', label: 'About Terminus' },
{ 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 () {
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: [
{ role: 'reload' },
2019-08-27 09:52:52 +00:00
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
2019-08-27 09:52:52 +00:00
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ 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
},
]
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
}
}