提交 10237b64 编写于 作者: B Benjamin Pasero

touch bar: reuse entries to avoid flickering

上级 b86108e5
...@@ -94,6 +94,8 @@ export class CodeWindow implements ICodeWindow { ...@@ -94,6 +94,8 @@ export class CodeWindow implements ICodeWindow {
private currentConfig: IWindowConfiguration; private currentConfig: IWindowConfiguration;
private pendingLoadConfig: IWindowConfiguration; private pendingLoadConfig: IWindowConfiguration;
private touchBarGroups: Map<string, Electron.TouchBarSegmentedControl>;
constructor( constructor(
config: IWindowCreationOptions, config: IWindowCreationOptions,
@ILogService private logService: ILogService, @ILogService private logService: ILogService,
...@@ -103,6 +105,7 @@ export class CodeWindow implements ICodeWindow { ...@@ -103,6 +105,7 @@ export class CodeWindow implements ICodeWindow {
@IWorkspacesMainService private workspaceService: IWorkspacesMainService, @IWorkspacesMainService private workspaceService: IWorkspacesMainService,
@IBackupMainService private backupService: IBackupMainService @IBackupMainService private backupService: IBackupMainService
) { ) {
this.touchBarGroups = new Map<string, Electron.TouchBarSegmentedControl>();
this._lastFocusTime = -1; this._lastFocusTime = -1;
this._readyState = ReadyState.NONE; this._readyState = ReadyState.NONE;
this.whenReadyCallbacks = []; this.whenReadyCallbacks = [];
...@@ -858,49 +861,80 @@ export class CodeWindow implements ICodeWindow { ...@@ -858,49 +861,80 @@ export class CodeWindow implements ICodeWindow {
this._win.webContents.send(channel, ...args); this._win.webContents.send(channel, ...args);
} }
public updateTouchBar(items: ICommandAction[][]): void { public updateTouchBar(groups: ICommandAction[][]): void {
if (!isMacintosh) { if (!isMacintosh) {
return; // only supported on macOS return; // only supported on macOS
} }
const groups: (Electron.TouchBarGroup | Electron.TouchBarSpacer)[] = []; // Clean up previous groups no longer in use
const groupHashes: string[] = [];
groups.forEach(group => groupHashes.push(this.getTouchBarGroupHash(group)));
this.touchBarGroups.forEach((value, key) => {
if (groupHashes.indexOf(key) === -1) {
this.touchBarGroups.delete(key);
}
});
items.forEach(itemGroup => { // Build touchbar from groups
if (itemGroup.length) { const touchBarGroups: (Electron.TouchBarGroup | Electron.TouchBarSpacer)[] = [];
groups.forEach(group => {
if (group.length) {
const groupHash = this.getTouchBarGroupHash(group);
// To avoid flickering, we try to reuse the touch bar group
// as much as possible by checking for a previously created
// group that has the same number of items with same style.
let groupTouchBar: Electron.TouchBarSegmentedControl;
if (this.touchBarGroups.has(groupHash)) {
groupTouchBar = this.touchBarGroups.get(groupHash);
} else {
groupTouchBar = this.createTouchBarGroup(group);
this.touchBarGroups.set(groupHash, groupTouchBar);
}
// Group Segments // Push and add small space between groups
const groupSegments = itemGroup.map(item => { touchBarGroups.push(groupTouchBar);
let icon: Electron.NativeImage; touchBarGroups.push(new TouchBar.TouchBarSpacer({ size: 'small' }));
if (item.iconPath) { }
icon = nativeImage.createFromPath(item.iconPath); });
if (icon.isEmpty()) {
icon = void 0;
}
}
return { this._win.setTouchBar(new TouchBar({ items: touchBarGroups }));
label: !icon ? item.title as string : void 0, }
icon
};
});
// Group Touch Bar private getTouchBarGroupHash(items: ICommandAction[]): string {
const groupTouchBar = new TouchBar.TouchBarSegmentedControl({ let id = '';
segments: groupSegments, items.forEach(item => id += (item.id + item.title + item.iconPath));
mode: 'buttons',
segmentStyle: 'automatic',
change: (selectedIndex) => {
this.sendWhenReady('vscode:runAction', itemGroup[selectedIndex].id);
}
});
// Push and add small space between groups return id;
groups.push(groupTouchBar); }
groups.push(new TouchBar.TouchBarSpacer({ size: 'small' }));
private createTouchBarGroup(items: ICommandAction[]): Electron.TouchBarSegmentedControl {
// Group Segments
const segments = items.map(item => {
let icon: Electron.NativeImage;
if (item.iconPath) {
icon = nativeImage.createFromPath(item.iconPath);
if (icon.isEmpty()) {
icon = void 0;
}
} }
return {
label: !icon ? item.title as string : void 0,
icon
};
}); });
this._win.setTouchBar(new TouchBar({ items: groups })); // Group Touch Bar
return new TouchBar.TouchBarSegmentedControl({
segments,
mode: 'buttons',
segmentStyle: 'automatic',
change: (selectedIndex) => {
this.sendWhenReady('vscode:runAction', items[selectedIndex].id);
}
});
} }
public dispose(): void { public dispose(): void {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册