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
No known key found for this signature in database
GPG Key ID: 5896FCBBDD1CF4F4
2 changed files with 33 additions and 47 deletions

View File

@ -1,7 +1,7 @@
import * as glasstron from 'glasstron'
import { Subject, Observable, debounceTime } from 'rxjs'
import { BrowserWindow, app, ipcMain, Rectangle, Menu, screen, BrowserWindowConstructorOptions } from 'electron'
import { BrowserWindow, app, ipcMain, Rectangle, Menu, screen, BrowserWindowConstructorOptions, TouchBar } from 'electron'
import ElectronConfig = require('electron-config')
import * as os from 'os'
import * as path from 'path'
@ -39,6 +39,7 @@ export class Window {
private lastVibrancy: { enabled: boolean, type?: string } | null = null
private disableVibrancyWhileDragging = false
private configStore: any
private touchBarControl: any
get visible$ (): Observable<boolean> { return this.visible }
get closed$ (): Observable<void> { return this.closed }
@ -127,7 +128,15 @@ export class Window {
this.window.webContents.setVisualZoomLevelLimits(1, 1)
this.window.webContents.setZoomFactor(1)
if (process.platform !== 'darwin') {
if (process.platform === 'darwin') {
this.touchBarControl = new TouchBar.TouchBarSegmentedControl({
segments: [],
change: index => this.send('touchbar-selection', index),
})
this.window.setTouchBar(new TouchBar({
items: [this.touchBarControl],
}))
} else {
this.window.setMenu(null)
}
@ -357,6 +366,11 @@ export class Window {
this.window.close()
})
ipcMain.on('window-set-touch-bar', (_event, segments, selectedIndex) => {
this.touchBarControl.segments = segments
this.touchBarControl.selectedIndex = selectedIndex
})
this.window.webContents.on('new-window', event => event.preventDefault())
ipcMain.on('window-set-disable-vibrancy-while-dragging', (_event, value) => {

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 {