Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/fixtures/gen/commandCenterControl.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { isActiveDocument, reset } from 'vs/base/browser/dom';
7
import { BaseActionViewItem, IBaseActionViewItemOptions } from 'vs/base/browser/ui/actionbar/actionViewItems';
8
import { getDefaultHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegateFactory';
9
import { IHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegate';
10
import { renderIcon } from 'vs/base/browser/ui/iconLabel/iconLabels';
11
import { IAction, SubmenuAction } from 'vs/base/common/actions';
12
import { Codicon } from 'vs/base/common/codicons';
13
import { Emitter, Event } from 'vs/base/common/event';
14
import { DisposableStore } from 'vs/base/common/lifecycle';
15
import { localize } from 'vs/nls';
16
import { createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
17
import { HiddenItemStrategy, MenuWorkbenchToolBar, WorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
18
import { MenuId, MenuRegistry, SubmenuItemAction } from 'vs/platform/actions/common/actions';
19
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
20
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
21
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
22
import { WindowTitle } from 'vs/workbench/browser/parts/titlebar/windowTitle';
23
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
24
import { IHoverService } from 'vs/platform/hover/browser/hover';
25
26
export class CommandCenterControl {
27
28
private readonly _disposables = new DisposableStore();
29
30
private readonly _onDidChangeVisibility = new Emitter<void>();
31
readonly onDidChangeVisibility: Event<void> = this._onDidChangeVisibility.event;
32
33
readonly element: HTMLElement = document.createElement('div');
34
35
constructor(
36
windowTitle: WindowTitle,
37
hoverDelegate: IHoverDelegate,
38
@IInstantiationService instantiationService: IInstantiationService,
39
@IQuickInputService quickInputService: IQuickInputService,
40
) {
41
this.element.classList.add('command-center');
42
43
const titleToolbar = instantiationService.createInstance(MenuWorkbenchToolBar, this.element, MenuId.CommandCenter, {
44
contextMenu: MenuId.TitleBarContext,
45
hiddenItemStrategy: HiddenItemStrategy.NoHide,
46
toolbarOptions: {
47
primaryGroup: () => true,
48
},
49
telemetrySource: 'commandCenter',
50
actionViewItemProvider: (action, options) => {
51
if (action instanceof SubmenuItemAction && action.item.submenu === MenuId.CommandCenterCenter) {
52
return instantiationService.createInstance(CommandCenterCenterViewItem, action, windowTitle, { ...options, hoverDelegate });
53
} else {
54
return createActionViewItem(instantiationService, action, { ...options, hoverDelegate });
55
}
56
}
57
});
58
59
this._disposables.add(Event.filter(quickInputService.onShow, () => isActiveDocument(this.element), this._disposables)(this._setVisibility.bind(this, false)));
60
this._disposables.add(Event.filter(quickInputService.onHide, () => isActiveDocument(this.element), this._disposables)(this._setVisibility.bind(this, true)));
61
this._disposables.add(titleToolbar);
62
}
63
64
private _setVisibility(show: boolean): void {
65
this.element.classList.toggle('hide', !show);
66
this._onDidChangeVisibility.fire();
67
}
68
69
dispose(): void {
70
this._disposables.dispose();
71
}
72
}
73
74
75
class CommandCenterCenterViewItem extends BaseActionViewItem {
76
77
private static readonly _quickOpenCommandId = 'workbench.action.quickOpenWithModes';
78
79
private readonly _hoverDelegate: IHoverDelegate;
80
81
constructor(
82
private readonly _submenu: SubmenuItemAction,
83
private readonly _windowTitle: WindowTitle,
84
options: IBaseActionViewItemOptions,
85
@IHoverService private readonly _hoverService: IHoverService,
86
@IKeybindingService private _keybindingService: IKeybindingService,
87
@IInstantiationService private _instaService: IInstantiationService,
88
@IEditorGroupsService private _editorGroupService: IEditorGroupsService,
89
) {
90
super(undefined, _submenu.actions.find(action => action.id === 'workbench.action.quickOpenWithModes') ?? _submenu.actions[0], options);
91
this._hoverDelegate = options.hoverDelegate ?? getDefaultHoverDelegate('mouse');
92
}
93
94
override render(container: HTMLElement): void {
95
super.render(container);
96
container.classList.add('command-center-center');
97
container.classList.toggle('multiple', (this._submenu.actions.length > 1));
98
99
const hover = this._store.add(this._hoverService.setupUpdatableHover(this._hoverDelegate, container, this.getTooltip()));
100
101
// update label & tooltip when window title changes
102
this._store.add(this._windowTitle.onDidChange(() => {
103
hover.update(this.getTooltip());
104
}));
105
106
const groups: (readonly IAction[])[] = [];
107
for (const action of this._submenu.actions) {
108
if (action instanceof SubmenuAction) {
109
groups.push(action.actions);
110
} else {
111
groups.push([action]);
112
}
113
}
114
115
116
for (let i = 0; i < groups.length; i++) {
117
const group = groups[i];
118
119
// nested toolbar
120
const toolbar = this._instaService.createInstance(WorkbenchToolBar, container, {
121
hiddenItemStrategy: HiddenItemStrategy.NoHide,
122
telemetrySource: 'commandCenterCenter',
123
actionViewItemProvider: (action, options) => {
124
options = {
125
...options,
126
hoverDelegate: this._hoverDelegate,
127
};
128
129
if (action.id !== CommandCenterCenterViewItem._quickOpenCommandId) {
130
return createActionViewItem(this._instaService, action, options);
131
}
132
133
const that = this;
134
135
return this._instaService.createInstance(class CommandCenterQuickPickItem extends BaseActionViewItem {
136
137
constructor() {
138
super(undefined, action, options);
139
}
140
141
override render(container: HTMLElement): void {
142
super.render(container);
143
container.classList.toggle('command-center-quick-pick');
144
145
const action = this.action;
146
147
// icon (search)
148
const searchIcon = document.createElement('span');
149
searchIcon.ariaHidden = 'true';
150
searchIcon.className = action.class ?? '';
151
searchIcon.classList.add('search-icon');
152
153
// label: just workspace name and optional decorations
154
const label = this._getLabel();
155
const labelElement = document.createElement('span');
156
labelElement.classList.add('search-label');
157
labelElement.innerText = label;
158
reset(container, searchIcon, labelElement);
159
160
const hover = this._store.add(that._hoverService.setupUpdatableHover(that._hoverDelegate, container, this.getTooltip()));
161
162
// update label & tooltip when window title changes
163
this._store.add(that._windowTitle.onDidChange(() => {
164
hover.update(this.getTooltip());
165
labelElement.innerText = this._getLabel();
166
}));
167
168
// update label & tooltip when tabs visibility changes
169
this._store.add(that._editorGroupService.onDidChangeEditorPartOptions(({ newPartOptions, oldPartOptions }) => {
170
if (newPartOptions.showTabs !== oldPartOptions.showTabs) {
171
hover.update(this.getTooltip());
172
labelElement.innerText = this._getLabel();
173
}
174
}));
175
}
176
177
protected override getTooltip() {
178
return that.getTooltip();
179
}
180
181
private _getLabel(): string {
182
const { prefix, suffix } = that._windowTitle.getTitleDecorations();
183
let label = that._windowTitle.workspaceName;
184
if (that._windowTitle.isCustomTitleFormat()) {
185
label = that._windowTitle.getWindowTitle();
186
} else if (that._editorGroupService.partOptions.showTabs === 'none') {
187
label = that._windowTitle.fileName ?? label;
188
}
189
190
if (!label) {
191
label = localize('label.dfl', "Search");
192
}
193
if (prefix) {
194
label = localize('label1', "{0} {1}", prefix, label);
195
}
196
if (suffix) {
197
label = localize('label2', "{0} {1}", label, suffix);
198
}
199
200
return label;
201
}
202
});
203
}
204
});
205
toolbar.setActions(group);
206
this._store.add(toolbar);
207
208
209
// spacer
210
if (i < groups.length - 1) {
211
const icon = renderIcon(Codicon.circleSmallFilled);
212
icon.style.padding = '0 12px';
213
icon.style.height = '100%';
214
icon.style.opacity = '0.5';
215
container.appendChild(icon);
216
}
217
}
218
}
219
220
protected override getTooltip() {
221
222
// tooltip: full windowTitle
223
const kb = this._keybindingService.lookupKeybinding(this.action.id)?.getLabel();
224
const title = kb
225
? localize('title', "Search {0} ({1}) \u2014 {2}", this._windowTitle.workspaceName, kb, this._windowTitle.value)
226
: localize('title2', "Search {0} \u2014 {1}", this._windowTitle.workspaceName, this._windowTitle.value);
227
228
return title;
229
}
230
}
231
232
MenuRegistry.appendMenuItem(MenuId.CommandCenter, {
233
submenu: MenuId.CommandCenterCenter,
234
title: localize('title3', "Command Center"),
235
icon: Codicon.shield,
236
order: 101,
237
});
238
239