1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-11-27 10:42:35 +03:00
tabby/app/lib/app.ts

231 lines
7.1 KiB
TypeScript
Raw Normal View History

2020-12-24 16:03:14 +03:00
import { app, ipcMain, Menu, Tray, shell, screen, globalShortcut, MenuItemConstructorOptions } from 'electron'
2018-10-06 21:50:06 +03: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', (_event, config) => {
this.broadcast('host:config-change', config)
})
2020-04-20 12:25:20 +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) {
globalShortcut.register(spec, () => {
this.onGlobalHotkey()
})
}
})
2018-10-06 21:50:06 +03: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 21:50:06 +03:00
}
app.commandLine.appendSwitch('disable-http-cache')
2019-01-28 01:58:55 +03:00
app.commandLine.appendSwitch('lang', 'EN')
2020-02-05 14:54:10 +03:00
app.allowRendererProcessReuse = false
for (const flag of configData.flags || [['force_discrete_gpu', '0']]) {
app.commandLine.appendSwitch(flag[0], flag[1])
}
}
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'))
}
async newWindow (options?: WindowOptions): Promise<Window> {
2020-12-24 16:03:14 +03:00
const window = new Window(options)
this.windows.push(window)
window.visible$.subscribe(visible => {
if (visible) {
this.disableTray()
} else {
this.enableTray()
}
})
window.closed$.subscribe(() => {
this.windows = this.windows.filter(x => x !== window)
})
2018-09-20 14:01:25 +03:00
if (process.platform === 'darwin') {
this.setupMenu()
}
await window.ready
return window
}
2020-04-20 12:25:20 +03:00
onGlobalHotkey (): void {
if (this.windows.some(x => x.isFocused())) {
2020-12-24 16:03:14 +03:00
for (const window of this.windows) {
window.hide()
}
} else {
2020-12-24 16:03:14 +03:00
for (const window of this.windows) {
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) {
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) {
window.send(event, ...args)
}
}
2020-12-24 16:03:14 +03: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 18:10:45 +03: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 15:16:31 +03: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 18:10:45 +03:00
disableTray (): void {
if (this.tray) {
this.tray.destroy()
this.tray = null
}
}
2020-03-01 19:07:11 +03:00
hasWindows (): boolean {
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) {
window.show()
}
}
handleSecondInstance (argv: string[], cwd: string): void {
this.presentAllWindows()
this.windows[this.windows.length - 1].handleSecondInstance(argv, cwd)
}
private setupMenu () {
2020-12-24 16:03:14 +03:00
const template: MenuItemConstructorOptions[] = [
{
label: 'Application',
submenu: [
{ role: 'about', label: 'About Terminus' },
{ type: 'separator' },
{
label: 'Preferences',
accelerator: 'Cmd+,',
2018-12-30 19:32:30 +03: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 12:52:52 +03: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 12:52:52 +03:00
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
2019-08-27 12:52:52 +03:00
{ role: 'selectAll' },
],
},
{
label: 'View',
submenu: [
{ role: 'reload' },
2019-08-27 12:52:52 +03:00
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
2019-08-27 12:52:52 +03: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 15:16:31 +03:00
},
]
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
}
}