nicer touch bar buttons

This commit is contained in:
Eugene Pankov 2018-08-25 10:11:32 +02:00
parent 06d14f9bb2
commit 7f45bb57fc
10 changed files with 29 additions and 21 deletions

View File

@ -2,6 +2,7 @@ import { SafeHtml } from '@angular/platform-browser'
export interface IToolbarButton { export interface IToolbarButton {
icon: SafeHtml icon: SafeHtml
touchBarNSImage?: string
title: string title: string
touchBarTitle?: string touchBarTitle?: string
weight?: number weight?: number

View File

@ -24,7 +24,7 @@ title-bar(
[index]='idx', [index]='idx',
[tab]='tab', [tab]='tab',
[active]='tab == app.activeTab', [active]='tab == app.activeTab',
[hasActivity]='tab.hasActivity', [hasActivity]='tab.activity$|async',
@animateTab, @animateTab,
(click)='app.selectTab(tab)', (click)='app.selectTab(tab)',
[class.fully-draggable]='hostApp.platform != Platform.macOS', [class.fully-draggable]='hostApp.platform != Platform.macOS',

View File

@ -144,7 +144,7 @@ export class AppRootComponent {
if (progress !== null) { if (progress !== null) {
this.hostApp.getWindow().setProgressBar(progress / 100.0, 'normal') this.hostApp.getWindow().setProgressBar(progress / 100.0, 'normal')
} else { } else {
this.hostApp.getWindow().setProgressBar(0, 'none') this.hostApp.getWindow().setProgressBar(-1, 'none')
} }
}) })
}) })

View File

@ -6,18 +6,20 @@ export abstract class BaseTabComponent {
id: number id: number
title: string title: string
customTitle: string customTitle: string
hasActivity = false
hasFocus = false hasFocus = false
hasActivity = false
hostView: ViewRef hostView: ViewRef
protected titleChange = new Subject<string>() protected titleChange = new Subject<string>()
protected focused = new Subject<void>() protected focused = new Subject<void>()
protected blurred = new Subject<void>() protected blurred = new Subject<void>()
protected progress = new Subject<number>() protected progress = new Subject<number>()
protected activity = new Subject<boolean>()
get focused$ (): Observable<void> { return this.focused } get focused$ (): Observable<void> { return this.focused }
get blurred$ (): Observable<void> { return this.blurred } get blurred$ (): Observable<void> { return this.blurred }
get titleChange$ (): Observable<string> { return this.titleChange } get titleChange$ (): Observable<string> { return this.titleChange }
get progress$ (): Observable<number> { return this.progress } get progress$ (): Observable<number> { return this.progress }
get activity$ (): Observable<boolean> { return this.activity }
constructor () { constructor () {
this.id = BaseTabComponent.lastTabID++ this.id = BaseTabComponent.lastTabID++
@ -42,6 +44,12 @@ export abstract class BaseTabComponent {
displayActivity (): void { displayActivity (): void {
this.hasActivity = true this.hasActivity = true
this.activity.next(true)
}
clearActivity (): void {
this.hasActivity = false
this.activity.next(false)
} }
getRecoveryToken (): any { getRecoveryToken (): any {

View File

@ -66,7 +66,7 @@ export class AppService {
this.lastTabIndex = null this.lastTabIndex = null
} }
if (this.activeTab) { if (this.activeTab) {
this.activeTab.hasActivity = false this.activeTab.clearActivity()
this.activeTab.emitBlurred() this.activeTab.emitBlurred()
} }
this.activeTab = tab this.activeTab = tab

View File

@ -9,6 +9,7 @@ export class ElectronService {
dialog: any dialog: any
clipboard: any clipboard: any
globalShortcut: any globalShortcut: any
nativeImage: any
screen: any screen: any
remote: any remote: any
TouchBar: typeof TouchBar TouchBar: typeof TouchBar
@ -24,6 +25,7 @@ export class ElectronService {
this.clipboard = this.electron.clipboard this.clipboard = this.electron.clipboard
this.ipcRenderer = this.electron.ipcRenderer this.ipcRenderer = this.electron.ipcRenderer
this.globalShortcut = this.remote.globalShortcut this.globalShortcut = this.remote.globalShortcut
this.nativeImage = this.remote.nativeImage
this.TouchBar = this.remote.TouchBar this.TouchBar = this.remote.TouchBar
} }

View File

@ -1,15 +1,12 @@
import { Injectable, Inject, NgZone } from '@angular/core' import { Injectable, Inject, NgZone } from '@angular/core'
import { TouchBarSegmentedControl, SegmentedControlSegment } from 'electron' import { TouchBarSegmentedControl, SegmentedControlSegment } from 'electron'
import { Subscription } from 'rxjs'
import { AppService } from './app.service' import { AppService } from './app.service'
import { ConfigService } from './config.service' import { ConfigService } from './config.service'
import { ElectronService } from './electron.service' import { ElectronService } from './electron.service'
import { BaseTabComponent } from '../components/baseTab.component'
import { IToolbarButton, ToolbarButtonProvider } from '../api' import { IToolbarButton, ToolbarButtonProvider } from '../api'
@Injectable() @Injectable()
export class TouchbarService { export class TouchbarService {
private titleSubscriptions = new Map<BaseTabComponent, Subscription>()
private tabsSegmentedControl: TouchBarSegmentedControl private tabsSegmentedControl: TouchBarSegmentedControl
private tabSegments: SegmentedControlSegment[] = [] private tabSegments: SegmentedControlSegment[] = []
@ -23,15 +20,10 @@ export class TouchbarService {
app.tabsChanged$.subscribe(() => this.update()) app.tabsChanged$.subscribe(() => this.update())
app.activeTabChange$.subscribe(() => this.update()) app.activeTabChange$.subscribe(() => this.update())
app.tabOpened$.subscribe(tab => { app.tabOpened$.subscribe(tab => {
let sub = tab.titleChange$.subscribe(title => { tab.titleChange$.subscribe(title => {
this.tabSegments[app.tabs.indexOf(tab)].label = this.shortenTitle(title) this.tabSegments[app.tabs.indexOf(tab)].label = this.shortenTitle(title)
this.tabsSegmentedControl.segments = this.tabSegments this.tabsSegmentedControl.segments = this.tabSegments
}) })
this.titleSubscriptions.set(tab, sub)
})
app.tabClosed$.subscribe(tab => {
this.titleSubscriptions.get(tab).unsubscribe()
this.titleSubscriptions.delete(tab)
}) })
} }
@ -56,16 +48,21 @@ export class TouchbarService {
this.tabsSegmentedControl, this.tabsSegmentedControl,
new this.electron.TouchBar.TouchBarSpacer({size: 'flexible'}), new this.electron.TouchBar.TouchBarSpacer({size: 'flexible'}),
new this.electron.TouchBar.TouchBarSpacer({size: 'small'}), new this.electron.TouchBar.TouchBarSpacer({size: 'small'}),
...buttons.map(button => new this.electron.TouchBar.TouchBarButton({ ...buttons.map(button => this.getButton(button))
label: this.shortenTitle(button.touchBarTitle || button.title),
// backgroundColor: '#0022cc',
click: () => this.zone.run(() => button.click()),
}))
] ]
}) })
this.electron.app.window.setTouchBar(touchBar) this.electron.app.window.setTouchBar(touchBar)
} }
private getButton (button: IToolbarButton): Electron.TouchBarButton {
return new this.electron.TouchBar.TouchBarButton({
label: button.touchBarNSImage ? null : this.shortenTitle(button.touchBarTitle || button.title),
icon: button.touchBarNSImage ?
this.electron.nativeImage.createFromNamedImage(button.touchBarNSImage, [0, 0, 1]) : null,
click: () => this.zone.run(() => button.click()),
})
}
private shortenTitle (title: string): string { private shortenTitle (title: string): string {
if (title.length > 15) { if (title.length > 15) {
title = title.substring(0, 15) + '...' title = title.substring(0, 15) + '...'

View File

@ -19,7 +19,7 @@ export class ButtonProvider extends ToolbarButtonProvider {
return [{ return [{
icon: this.domSanitizer.bypassSecurityTrustHtml(require('./icons/cog.svg')), icon: this.domSanitizer.bypassSecurityTrustHtml(require('./icons/cog.svg')),
title: 'Settings', title: 'Settings',
touchBarTitle: '⚙️', touchBarNSImage: 'NSTouchBarComposeTemplate',
weight: 10, weight: 10,
click: () => this.open(), click: () => this.open(),
}] }]

View File

@ -28,7 +28,7 @@ export class ButtonProvider extends ToolbarButtonProvider {
icon: this.domSanitizer.bypassSecurityTrustHtml(require('./icons/globe.svg')), icon: this.domSanitizer.bypassSecurityTrustHtml(require('./icons/globe.svg')),
weight: 5, weight: 5,
title: 'SSH connections', title: 'SSH connections',
touchBarTitle: 'SSH', touchBarNSImage: 'NSTouchBarOpenInBrowserTemplate',
click: async () => { click: async () => {
this.activate() this.activate()
} }

View File

@ -55,7 +55,7 @@ export class ButtonProvider extends ToolbarButtonProvider {
return [{ return [{
icon: this.domSanitizer.bypassSecurityTrustHtml(require('./icons/plus.svg')), icon: this.domSanitizer.bypassSecurityTrustHtml(require('./icons/plus.svg')),
title: 'New terminal', title: 'New terminal',
touchBarTitle: 'New', touchBarNSImage: 'NSTouchBarAddDetailTemplate',
click: async () => { click: async () => {
this.openNewTab() this.openNewTab()
} }