/
commandCenterControl.ts
189 lines (162 loc) · 8.24 KB
/
commandCenterControl.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { reset } from 'vs/base/browser/dom';
import { BaseActionViewItem, IBaseActionViewItemOptions } from 'vs/base/browser/ui/actionbar/actionViewItems';
import { IHoverDelegate } from 'vs/base/browser/ui/iconLabel/iconHoverDelegate';
import { setupCustomHover } from 'vs/base/browser/ui/iconLabel/iconLabelHover';
import { renderIcon } from 'vs/base/browser/ui/iconLabel/iconLabels';
import { IAction } from 'vs/base/common/actions';
import { Codicon } from 'vs/base/common/codicons';
import { Color } from 'vs/base/common/color';
import { Emitter, Event } from 'vs/base/common/event';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { localize } from 'vs/nls';
import { createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { HiddenItemStrategy, MenuWorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
import { MenuId, MenuItemAction } from 'vs/platform/actions/common/actions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import * as colors from 'vs/platform/theme/common/colorRegistry';
import { WindowTitle } from 'vs/workbench/browser/parts/titlebar/windowTitle';
import { MENUBAR_SELECTION_BACKGROUND, MENUBAR_SELECTION_FOREGROUND, TITLE_BAR_ACTIVE_FOREGROUND, TITLE_BAR_INACTIVE_FOREGROUND } from 'vs/workbench/common/theme';
export class CommandCenterControl {
private readonly _disposables = new DisposableStore();
private readonly _onDidChangeVisibility = new Emitter<void>();
readonly onDidChangeVisibility: Event<void> = this._onDidChangeVisibility.event;
readonly element: HTMLElement = document.createElement('div');
constructor(
windowTitle: WindowTitle,
hoverDelegate: IHoverDelegate,
@IInstantiationService instantiationService: IInstantiationService,
@IQuickInputService quickInputService: IQuickInputService,
@IKeybindingService keybindingService: IKeybindingService
) {
this.element.classList.add('command-center');
const titleToolbar = instantiationService.createInstance(MenuWorkbenchToolBar, this.element, MenuId.CommandCenter, {
contextMenu: MenuId.TitleBarContext,
hiddenItemStrategy: HiddenItemStrategy.Ignore,
toolbarOptions: {
primaryGroup: () => true,
},
telemetrySource: 'commandCenter',
actionViewItemProvider: (action) => {
if (action instanceof MenuItemAction && action.id === 'workbench.action.quickOpenWithModes') {
class CommandCenterViewItem extends BaseActionViewItem {
constructor(action: IAction, options: IBaseActionViewItemOptions) {
super(undefined, action, options);
}
override render(container: HTMLElement): void {
super.render(container);
container.classList.add('command-center');
// icon (search)
const searchIcon = renderIcon(Codicon.search);
searchIcon.classList.add('search-icon');
// label: just workspace name and optional decorations
const label = this._getLabel();
const labelElement = document.createElement('span');
labelElement.classList.add('search-label');
labelElement.innerText = label;
reset(container, searchIcon, labelElement);
const hover = this._store.add(setupCustomHover(hoverDelegate, container, this.getTooltip()));
// update label & tooltip when window title changes
this._store.add(windowTitle.onDidChange(() => {
hover.update(this.getTooltip());
labelElement.innerText = this._getLabel();
}));
}
private _getLabel(): string {
const { prefix, suffix } = windowTitle.getTitleDecorations();
let label = windowTitle.isCustomTitleFormat() ? windowTitle.getWindowTitle() : windowTitle.workspaceName;
if (!label) {
label = localize('label.dfl', "Search");
}
if (prefix) {
label = localize('label1', "{0} {1}", prefix, label);
}
if (suffix) {
label = localize('label2', "{0} {1}", label, suffix);
}
return label;
}
override getTooltip() {
// tooltip: full windowTitle
const kb = keybindingService.lookupKeybinding(action.id)?.getLabel();
const title = kb
? localize('title', "Search {0} ({1}) \u2014 {2}", windowTitle.workspaceName, kb, windowTitle.value)
: localize('title2', "Search {0} \u2014 {1}", windowTitle.workspaceName, windowTitle.value);
return title;
}
}
return instantiationService.createInstance(CommandCenterViewItem, action, {});
} else {
return createActionViewItem(instantiationService, action, { hoverDelegate });
}
}
});
this._disposables.add(quickInputService.onShow(this._setVisibility.bind(this, false)));
this._disposables.add(quickInputService.onHide(this._setVisibility.bind(this, true)));
this._disposables.add(titleToolbar);
}
private _setVisibility(show: boolean): void {
this.element.classList.toggle('hide', !show);
this._onDidChangeVisibility.fire();
}
dispose(): void {
this._disposables.dispose();
}
}
// --- theme colors
// foreground (inactive and active)
colors.registerColor(
'commandCenter.foreground',
{ dark: TITLE_BAR_ACTIVE_FOREGROUND, hcDark: TITLE_BAR_ACTIVE_FOREGROUND, light: TITLE_BAR_ACTIVE_FOREGROUND, hcLight: TITLE_BAR_ACTIVE_FOREGROUND },
localize('commandCenter-foreground', "Foreground color of the command center"),
false
);
colors.registerColor(
'commandCenter.activeForeground',
{ dark: MENUBAR_SELECTION_FOREGROUND, hcDark: MENUBAR_SELECTION_FOREGROUND, light: MENUBAR_SELECTION_FOREGROUND, hcLight: MENUBAR_SELECTION_FOREGROUND },
localize('commandCenter-activeForeground', "Active foreground color of the command center"),
false
);
colors.registerColor(
'commandCenter.inactiveForeground',
{ dark: TITLE_BAR_INACTIVE_FOREGROUND, hcDark: TITLE_BAR_INACTIVE_FOREGROUND, light: TITLE_BAR_INACTIVE_FOREGROUND, hcLight: TITLE_BAR_INACTIVE_FOREGROUND },
localize('commandCenter-inactiveForeground', "Foreground color of the command center when the window is inactive"),
false
);
// background (inactive and active)
colors.registerColor(
'commandCenter.background',
{
dark: Color.white.transparent(0.05), hcDark: null, light: Color.black.transparent(0.05), hcLight: null
},
localize('commandCenter-background', "Background color of the command center"),
false
);
colors.registerColor(
'commandCenter.activeBackground',
{ dark: Color.white.transparent(0.08), hcDark: MENUBAR_SELECTION_BACKGROUND, light: Color.black.transparent(0.08), hcLight: MENUBAR_SELECTION_BACKGROUND },
localize('commandCenter-activeBackground', "Active background color of the command center"),
false
);
// border: active and inactive. defaults to active background
colors.registerColor(
'commandCenter.border', { dark: colors.transparent(TITLE_BAR_ACTIVE_FOREGROUND, .20), hcDark: colors.transparent(TITLE_BAR_ACTIVE_FOREGROUND, .60), light: colors.transparent(TITLE_BAR_ACTIVE_FOREGROUND, .20), hcLight: colors.transparent(TITLE_BAR_ACTIVE_FOREGROUND, .60) },
localize('commandCenter-border', "Border color of the command center"),
false
);
colors.registerColor(
'commandCenter.activeBorder', { dark: colors.transparent(TITLE_BAR_ACTIVE_FOREGROUND, .30), hcDark: TITLE_BAR_ACTIVE_FOREGROUND, light: colors.transparent(TITLE_BAR_ACTIVE_FOREGROUND, .30), hcLight: TITLE_BAR_ACTIVE_FOREGROUND },
localize('commandCenter-activeBorder', "Active border color of the command center"),
false
);
// border: defaults to active background
colors.registerColor(
'commandCenter.inactiveBorder', { dark: colors.transparent(TITLE_BAR_INACTIVE_FOREGROUND, .25), hcDark: colors.transparent(TITLE_BAR_INACTIVE_FOREGROUND, .25), light: colors.transparent(TITLE_BAR_INACTIVE_FOREGROUND, .25), hcLight: colors.transparent(TITLE_BAR_INACTIVE_FOREGROUND, .25) },
localize('commandCenter-inactiveBorder', "Border color of the command center when the window is inactive"),
false
);