/
buttonbar.ts
172 lines (144 loc) · 5.77 KB
/
buttonbar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ButtonBar, IButton } from 'vs/base/browser/ui/button/button';
import { getDefaultHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegate';
import { setupCustomHover } from 'vs/base/browser/ui/iconLabel/iconLabelHover';
import { ActionRunner, IAction, IActionRunner, SubmenuAction, WorkbenchActionExecutedClassification, WorkbenchActionExecutedEvent } from 'vs/base/common/actions';
import { Emitter, Event } from 'vs/base/common/event';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { ThemeIcon } from 'vs/base/common/themables';
import { localize } from 'vs/nls';
import { MenuId, IMenuService, MenuItemAction } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
export type IButtonConfigProvider = (action: IAction) => {
showIcon?: boolean;
showLabel?: boolean;
isSecondary?: boolean;
} | undefined;
export interface IWorkbenchButtonBarOptions {
telemetrySource?: string;
buttonConfigProvider?: IButtonConfigProvider;
}
export class WorkbenchButtonBar extends ButtonBar {
protected readonly _store = new DisposableStore();
protected readonly _updateStore = new DisposableStore();
private readonly _actionRunner: IActionRunner;
private readonly _onDidChange = new Emitter<this>();
readonly onDidChange: Event<this> = this._onDidChange.event;
constructor(
container: HTMLElement,
private readonly _options: IWorkbenchButtonBarOptions | undefined,
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
@IKeybindingService private readonly _keybindingService: IKeybindingService,
@ITelemetryService telemetryService: ITelemetryService,
) {
super(container);
this._actionRunner = this._store.add(new ActionRunner());
if (_options?.telemetrySource) {
this._actionRunner.onDidRun(e => {
telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>(
'workbenchActionExecuted',
{ id: e.action.id, from: _options.telemetrySource! }
);
}, undefined, this._store);
}
}
override dispose() {
this._onDidChange.dispose();
this._updateStore.dispose();
this._store.dispose();
super.dispose();
}
update(actions: IAction[]): void {
const conifgProvider: IButtonConfigProvider = this._options?.buttonConfigProvider ?? (() => ({ showLabel: true }));
this._updateStore.clear();
this.clear();
// Support instamt hover between buttons
const hoverDelegate = this._updateStore.add(getDefaultHoverDelegate('element', true));
for (let i = 0; i < actions.length; i++) {
const secondary = i > 0;
const actionOrSubmenu = actions[i];
let action: IAction;
let btn: IButton;
if (actionOrSubmenu instanceof SubmenuAction && actionOrSubmenu.actions.length > 0) {
const [first, ...rest] = actionOrSubmenu.actions;
action = <MenuItemAction>first;
btn = this.addButtonWithDropdown({
secondary: conifgProvider(action)?.isSecondary ?? secondary,
actionRunner: this._actionRunner,
actions: rest,
contextMenuProvider: this._contextMenuService,
ariaLabel: action.label
});
} else {
action = actionOrSubmenu;
btn = this.addButton({
secondary: conifgProvider(action)?.isSecondary ?? secondary,
ariaLabel: action.label
});
}
btn.enabled = action.enabled;
btn.element.classList.add('default-colors');
if (conifgProvider(action)?.showLabel ?? true) {
btn.label = action.label;
} else {
btn.element.classList.add('monaco-text-button');
}
if (conifgProvider(action)?.showIcon) {
if (action instanceof MenuItemAction && ThemeIcon.isThemeIcon(action.item.icon)) {
btn.icon = action.item.icon;
} else if (action.class) {
btn.element.classList.add(...action.class.split(' '));
}
}
const kb = this._keybindingService.lookupKeybinding(action.id);
let tooltip: string;
if (kb) {
tooltip = localize('labelWithKeybinding', "{0} ({1})", action.label, kb.getLabel());
} else {
tooltip = action.label;
}
this._updateStore.add(setupCustomHover(hoverDelegate, btn.element, tooltip));
this._updateStore.add(btn.onDidClick(async () => {
this._actionRunner.run(action);
}));
}
this._onDidChange.fire(this);
}
}
export class MenuWorkbenchButtonBar extends WorkbenchButtonBar {
constructor(
container: HTMLElement,
menuId: MenuId,
options: IWorkbenchButtonBarOptions | undefined,
@IMenuService menuService: IMenuService,
@IContextKeyService contextKeyService: IContextKeyService,
@IContextMenuService contextMenuService: IContextMenuService,
@IKeybindingService keybindingService: IKeybindingService,
@ITelemetryService telemetryService: ITelemetryService,
) {
super(container, options, contextMenuService, keybindingService, telemetryService);
const menu = menuService.createMenu(menuId, contextKeyService);
this._store.add(menu);
const update = () => {
this.clear();
const actions = menu
.getActions({ renderShortTitle: true })
.flatMap(entry => entry[1]);
super.update(actions);
};
this._store.add(menu.onDidChange(update));
update();
}
override dispose() {
super.dispose();
}
override update(_actions: IAction[]): void {
throw new Error('Use Menu or WorkbenchButtonBar');
}
}