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 * as glasstron from 'glasstron'
import { Subject, Observable, debounceTime } from 'rxjs' 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 ElectronConfig = require('electron-config')
import * as os from 'os' import * as os from 'os'
import * as path from 'path' import * as path from 'path'
@ -39,6 +39,7 @@ export class Window {
private lastVibrancy: { enabled: boolean, type?: string } | null = null private lastVibrancy: { enabled: boolean, type?: string } | null = null
private disableVibrancyWhileDragging = false private disableVibrancyWhileDragging = false
private configStore: any private configStore: any
private touchBarControl: any
get visible$ (): Observable<boolean> { return this.visible } get visible$ (): Observable<boolean> { return this.visible }
get closed$ (): Observable<void> { return this.closed } get closed$ (): Observable<void> { return this.closed }
@ -127,7 +128,15 @@ export class Window {
this.window.webContents.setVisualZoomLevelLimits(1, 1) this.window.webContents.setVisualZoomLevelLimits(1, 1)
this.window.webContents.setZoomFactor(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) this.window.setMenu(null)
} }
@ -357,6 +366,11 @@ export class Window {
this.window.close() 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()) this.window.webContents.on('new-window', event => event.preventDefault())
ipcMain.on('window-set-disable-vibrancy-while-dragging', (_event, value) => { 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 { Injectable, NgZone } from '@angular/core'
import { AppService, HostAppService, Platform } from 'tabby-core' import { AppService, HostAppService, Platform } from 'tabby-core'
import { ElectronService } from '../services/electron.service' import { ElectronService } from '../services/electron.service'
import { ElectronHostWindow } from './hostWindow.service'
/** @hidden */ /** @hidden */
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class TouchbarService { export class TouchbarService {
private tabsSegmentedControl: TouchBarSegmentedControl private activityIcon: NativeImage
private tabSegments: SegmentedControlSegment[] = []
private constructor ( private constructor (
private app: AppService, private app: AppService,
private hostApp: HostAppService, private hostApp: HostAppService,
private hostWindow: ElectronHostWindow,
private electron: ElectronService, private electron: ElectronService,
private zone: NgZone, private zone: NgZone,
) { ) {
if (this.hostApp.platform !== Platform.macOS) { if (this.hostApp.platform !== Platform.macOS) {
return return
} }
app.tabsChanged$.subscribe(() => this.updateTabs()) app.tabsChanged$.subscribe(() => this.update())
app.activeTabChange$.subscribe(() => this.updateTabs()) app.activeTabChange$.subscribe(() => this.update())
const activityIconPath = `${electron.app.getAppPath()}/assets/activity.png` const activityIconPath = `${electron.app.getAppPath()}/assets/activity.png`
const activityIcon = this.electron.nativeImage.createFromPath(activityIconPath) this.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
}
})
})
}
updateTabs (): void { app.tabOpened$.subscribe(tab => {
this.tabSegments = this.app.tabs.map(tab => ({ tab.titleChange$.subscribe(() => this.update())
label: this.shortenTitle(tab.title), 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 { update (): void {
@ -58,20 +38,12 @@ export class TouchbarService {
return return
} }
this.tabsSegmentedControl = new this.electron.TouchBar.TouchBarSegmentedControl({ const tabSegments = this.app.tabs.map(tab => ({
segments: this.tabSegments, label: this.shortenTitle(tab.title),
selectedIndex: this.app.activeTab ? this.app.tabs.indexOf(this.app.activeTab) : undefined, icon: this.app.activeTab !== tab && tab.hasActivity ? this.activityIcon : undefined,
change: (selectedIndex) => this.zone.run(() => { }))
this.app.selectTab(this.app.tabs[selectedIndex])
}),
})
const touchBar = new this.electron.TouchBar({ ipcRenderer.send('window-set-touch-bar', tabSegments, this.app.activeTab ? this.app.tabs.indexOf(this.app.activeTab) : undefined)
items: [
this.tabsSegmentedControl,
],
})
this.hostWindow.setTouchBar(touchBar)
} }
private shortenTitle (title: string): string { private shortenTitle (title: string): string {