Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/fixtures/edit-refactor-loop/index.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 { reset } from 'vs/base/browser/dom';
7
import { BaseActionViewItem, IBaseActionViewItemOptions } from 'vs/base/browser/ui/actionbar/actionViewItems';
8
import { IHoverDelegate } from 'vs/base/browser/ui/iconLabel/iconHoverDelegate';
9
import { setupCustomHover } from 'vs/base/browser/ui/iconLabel/iconLabelHover';
10
import { IAction, SubmenuAction } from 'vs/base/common/actions';
11
import { Codicon } from 'vs/base/common/codicons';
12
import { Emitter, Event } from 'vs/base/common/event';
13
import { DisposableStore } from 'vs/base/common/lifecycle';
14
import { localize } from 'vs/nls';
15
import { createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
16
import { HiddenItemStrategy, MenuWorkbenchToolBar, WorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
17
import { MenuId, MenuRegistry, SubmenuItemAction } from 'vs/platform/actions/common/actions';
18
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
19
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
20
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
21
import { WindowTitle } from 'vs/workbench/browser/parts/titlebar/windowTitle';
22
23
export class CommandCenterControl {
24
25
private readonly _disposables = new DisposableStore();
26
27
private readonly _onDidChangeVisibility = new Emitter<void>();
28
readonly onDidChangeVisibility: Event<void> = this._onDidChangeVisibility.event;
29
30
readonly element: HTMLElement = document.createElement('div');
31
32
constructor(
33
windowTitle: WindowTitle,
34
hoverDelegate: IHoverDelegate,
35
@IInstantiationService instantiationService: IInstantiationService,
36
@IQuickInputService quickInputService: IQuickInputService,
37
@IKeybindingService keybindingService: IKeybindingService
38
) {
39
this.element.classList.add('command-center');
40
41
const titleToolbar = instantiationService.createInstance(MenuWorkbenchToolBar, this.element, MenuId.CommandCenter, {
42
contextMenu: MenuId.TitleBarContext,
43
hiddenItemStrategy: HiddenItemStrategy.Ignore,
44
toolbarOptions: {
45
primaryGroup: () => true,
46
},
47
telemetrySource: 'commandCenter',
48
actionViewItemProvider: (action) => {
49
if (action instanceof SubmenuItemAction && action.item.submenu === MenuId.CommandCenterCenter) {
50
return instantiationService.createInstance(CommandCenterCenterViewItem, action, windowTitle, hoverDelegate, {});
51
} else {
52
return createActionViewItem(instantiationService, action, { hoverDelegate });
53
}
54
}
55
});
56
57
this._disposables.add(quickInputService.onShow(this._setVisibility.bind(this, false)));
58
this._disposables.add(quickInputService.onHide(this._setVisibility.bind(this, true)));
59
this._disposables.add(titleToolbar);
60
}
61
62
private _setVisibility(show: boolean): void {
63
this.element.classList.toggle('hide', !show);
64
this._onDidChangeVisibility.fire();
65
}
66
67
dispose(): void {
68
this._disposables.dispose();
69
}
70
}
71
72
73
class CommandCenterCenterViewItem extends BaseActionViewItem {
74
75
private static readonly _quickOpenCommandId = 'workbench.action.quickOpenWithModes';
76
77
constructor(
78
private readonly _submenu: SubmenuItemAction,
79
private readonly _windowTitle: WindowTitle,
80
private readonly _hoverDelegate: IHoverDelegate,
81
options: IBaseActionViewItemOptions,
82
@IKeybindingService private _keybindingService: IKeybindingService,
83
@IInstantiationService private _instaService: IInstantiationService,
84
) {
85
super(undefined, _submenu.actions[0], options);
86
}
87
88
override render(container: HTMLElement): void {
89
super.render(container);
90
container.classList.add('command-center');
91
if (this._submenu.actions.length === 1 && this._submenu.actions[0].id === CommandCenterCenterViewItem._quickOpenCommandId) {
92
this._renderSingleItem(container);
93
} else {
94
this._renderMultipleItems(container);
95
container.classList.add('multiple');
96
}
97
}
98
99
private _renderMultipleItems(container: HTMLElement) {
100
101
const groups: (readonly IAction[])[] = [];
102
for (const action of this._submenu.actions) {
103
if (action instanceof SubmenuAction) {
104
groups.push(action.actions);
105
} else {
106
groups.push([action]);
107
}
108
}
109
110
for (const group of groups) {
111
112
// nested toolbar
113
const toolbar = this._instaService.createInstance(WorkbenchToolBar, container, {
114
hiddenItemStrategy: HiddenItemStrategy.NoHide,
115
telemetrySource: 'commandCenterCenter',
116
actionViewItemProvider: (action, options) => {
117
return createActionViewItem(this._instaService, action, {
118
...options,
119
hoverDelegate: this._hoverDelegate,
120
});
121
}
122
});
123
toolbar.setActions(group);
124
this._store.add(toolbar);
125
126
}
127
}
128
129
private _renderSingleItem(container: HTMLElement) {
130
131
const action = this._submenu.actions[0];
132
133
// icon (search)
134
const searchIcon = document.createElement('span');
135
searchIcon.className = action.class ?? '';
136
searchIcon.classList.add('search-icon');
137
138
// label: just workspace name and optional decorations
139
const label = this._getLabel();
140
const labelElement = document.createElement('span');
141
labelElement.classList.add('search-label');
142
labelElement.innerText = label;
143
reset(container, searchIcon, labelElement);
144
145
const hover = this._store.add(setupCustomHover(this._hoverDelegate, container, this.getTooltip()));
146
147
// update label & tooltip when window title changes
148
this._store.add(this._windowTitle.onDidChange(() => {
149
hover.update(this.getTooltip());
150
labelElement.innerText = this._getLabel();
151
}));
152
}
153
154
private _getLabel(): string {
155
const { prefix, suffix } = this._windowTitle.getTitleDecorations();
156
let label = this._windowTitle.isCustomTitleFormat() ? this._windowTitle.getWindowTitle() : this._windowTitle.workspaceName;
157
if (!label) {
158
label = localize('label.dfl', "Search");
159
}
160
if (prefix) {
161
label = localize('label1', "{0} {1}", prefix, label);
162
}
163
if (suffix) {
164
label = localize('label2', "{0} {1}", label, suffix);
165
}
166
return label;
167
}
168
169
protected override getTooltip() {
170
171
// tooltip: full windowTitle
172
const kb = this._keybindingService.lookupKeybinding(this.action.id)?.getLabel();
173
const title = kb
174
? localize('title', "Search {0} ({1}) \u2014 {2}", this._windowTitle.workspaceName, kb, this._windowTitle.value)
175
: localize('title2', "Search {0} \u2014 {1}", this._windowTitle.workspaceName, this._windowTitle.value);
176
177
return title;
178
}
179
}
180
181
182
MenuRegistry.appendMenuItem(MenuId.CommandCenter, {
183
submenu: MenuId.CommandCenterCenter,
184
title: localize('title3', "Command Center"),
185
icon: Codicon.shield,
186
order: 101,
187
});
188
189