moved touchbar handling into main process

This commit is contained in:
Eugene Pankov
2021-08-07 19:34:37 +02:00
parent 25fdba7104
commit 9fbf9136fc
2 changed files with 33 additions and 47 deletions

View File

@@ -1,56 +1,36 @@
import { SegmentedControlSegment, TouchBarSegmentedControl } from 'electron'
import { ipcRenderer, NativeImage } from 'electron'
import { Injectable, NgZone } from '@angular/core'
import { AppService, HostAppService, Platform } from 'tabby-core'
import { ElectronService } from '../services/electron.service'
import { ElectronHostWindow } from './hostWindow.service'
/** @hidden */
@Injectable({ providedIn: 'root' })
export class TouchbarService {
private tabsSegmentedControl: TouchBarSegmentedControl
private tabSegments: SegmentedControlSegment[] = []
private activityIcon: NativeImage
private constructor (
private app: AppService,
private hostApp: HostAppService,
private hostWindow: ElectronHostWindow,
private electron: ElectronService,
private zone: NgZone,
) {
if (this.hostApp.platform !== Platform.macOS) {
return
}
app.tabsChanged$.subscribe(() => this.updateTabs())
app.activeTabChange$.subscribe(() => this.updateTabs())
app.tabsChanged$.subscribe(() => this.update())
app.activeTabChange$.subscribe(() => this.update())
const activityIconPath = `${electron.app.getAppPath()}/assets/activity.png`
const activityIcon = this.electron.nativeImage.createFromPath(activityIconPath)
app.tabOpened$.subscribe(tab => {
tab.titleChange$.subscribe(title => {
const segment = this.tabSegments[app.tabs.indexOf(tab)]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (segment) {
segment.label = this.shortenTitle(title)
this.tabsSegmentedControl.segments = this.tabSegments
}
})
tab.activity$.subscribe(hasActivity => {
const showIcon = this.app.activeTab !== tab && hasActivity
const segment = this.tabSegments[app.tabs.indexOf(tab)]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (segment) {
segment.icon = showIcon ? activityIcon : undefined
}
})
})
}
this.activityIcon = this.electron.nativeImage.createFromPath(activityIconPath)
updateTabs (): void {
this.tabSegments = this.app.tabs.map(tab => ({
label: this.shortenTitle(tab.title),
app.tabOpened$.subscribe(tab => {
tab.titleChange$.subscribe(() => this.update())
tab.activity$.subscribe(() => this.update())
})
ipcRenderer.on('touchbar-selection', (_event, index) => this.zone.run(() => {
this.app.selectTab(this.app.tabs[index])
}))
this.tabsSegmentedControl.segments = this.tabSegments
this.tabsSegmentedControl.selectedIndex = this.app.activeTab ? this.app.tabs.indexOf(this.app.activeTab) : 0
}
update (): void {
@@ -58,20 +38,12 @@ export class TouchbarService {
return
}
this.tabsSegmentedControl = new this.electron.TouchBar.TouchBarSegmentedControl({
segments: this.tabSegments,
selectedIndex: this.app.activeTab ? this.app.tabs.indexOf(this.app.activeTab) : undefined,
change: (selectedIndex) => this.zone.run(() => {
this.app.selectTab(this.app.tabs[selectedIndex])
}),
})
const tabSegments = this.app.tabs.map(tab => ({
label: this.shortenTitle(tab.title),
icon: this.app.activeTab !== tab && tab.hasActivity ? this.activityIcon : undefined,
}))
const touchBar = new this.electron.TouchBar({
items: [
this.tabsSegmentedControl,
],
})
this.hostWindow.setTouchBar(touchBar)
ipcRenderer.send('window-set-touch-bar', tabSegments, this.app.activeTab ? this.app.tabs.indexOf(this.app.activeTab) : undefined)
}
private shortenTitle (title: string): string {