From 9fbf9136fc0694b7c53b590044c3f1a6a819769f Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Sat, 7 Aug 2021 19:34:37 +0200 Subject: [PATCH] moved touchbar handling into main process --- app/lib/window.ts | 18 +++++- .../src/services/touchbar.service.ts | 62 +++++-------------- 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/app/lib/window.ts b/app/lib/window.ts index f8c234b9..ca3ebeba 100644 --- a/app/lib/window.ts +++ b/app/lib/window.ts @@ -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 { return this.visible } get closed$ (): Observable { 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) => { diff --git a/tabby-electron/src/services/touchbar.service.ts b/tabby-electron/src/services/touchbar.service.ts index fc75a370..3f81a20a 100644 --- a/tabby-electron/src/services/touchbar.service.ts +++ b/tabby-electron/src/services/touchbar.service.ts @@ -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 {