Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/suggest/browser/suggestWidgetStatus.ts
4797 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 * as dom from '../../../../base/browser/dom.js';
7
import { ActionBar, IActionViewItemProvider } from '../../../../base/browser/ui/actionbar/actionbar.js';
8
import { IAction } from '../../../../base/common/actions.js';
9
import { DisposableStore } from '../../../../base/common/lifecycle.js';
10
import { MenuEntryActionViewItem, TextOnlyMenuEntryActionViewItem } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
11
import { IMenuService, MenuId, MenuItemAction } from '../../../../platform/actions/common/actions.js';
12
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
13
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
14
15
export interface ISuggestWidgetStatusOptions {
16
/**
17
* Whether to show icons instead of text where possible and avoid
18
* keybindings all together.
19
*/
20
readonly showIconsNoKeybindings?: boolean;
21
}
22
23
export class SuggestWidgetStatus {
24
25
readonly element: HTMLElement;
26
27
private readonly _leftActions: ActionBar;
28
private readonly _rightActions: ActionBar;
29
private readonly _menuDisposables = new DisposableStore();
30
31
constructor(
32
container: HTMLElement,
33
private readonly _menuId: MenuId,
34
options: ISuggestWidgetStatusOptions | undefined,
35
@IInstantiationService instantiationService: IInstantiationService,
36
@IMenuService private _menuService: IMenuService,
37
@IContextKeyService private _contextKeyService: IContextKeyService,
38
) {
39
this.element = dom.append(container, dom.$('.suggest-status-bar'));
40
41
const actionViewItemProvider = <IActionViewItemProvider>(action => {
42
if (options?.showIconsNoKeybindings) {
43
return action instanceof MenuItemAction ? instantiationService.createInstance(MenuEntryActionViewItem, action, undefined) : undefined;
44
} else {
45
return action instanceof MenuItemAction ? instantiationService.createInstance(TextOnlyMenuEntryActionViewItem, action, { useComma: false }) : undefined;
46
}
47
});
48
this._leftActions = new ActionBar(this.element, { actionViewItemProvider });
49
this._rightActions = new ActionBar(this.element, { actionViewItemProvider });
50
51
this._leftActions.domNode.classList.add('left');
52
this._rightActions.domNode.classList.add('right');
53
}
54
55
dispose(): void {
56
this._menuDisposables.dispose();
57
this._leftActions.dispose();
58
this._rightActions.dispose();
59
this.element.remove();
60
}
61
62
show(): void {
63
const menu = this._menuService.createMenu(this._menuId, this._contextKeyService);
64
const renderMenu = () => {
65
const left: IAction[] = [];
66
const right: IAction[] = [];
67
for (const [group, actions] of menu.getActions()) {
68
if (group === 'left') {
69
left.push(...actions);
70
} else {
71
right.push(...actions);
72
}
73
}
74
this._leftActions.clear();
75
this._leftActions.push(left);
76
this._rightActions.clear();
77
this._rightActions.push(right);
78
};
79
this._menuDisposables.add(menu.onDidChange(() => renderMenu()));
80
this._menuDisposables.add(menu);
81
}
82
83
hide(): void {
84
this._menuDisposables.clear();
85
}
86
}
87
88