Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/ui/dropdown/dropdown.ts
5243 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 { IContextMenuProvider } from '../../contextmenu.js';
7
import { $, addDisposableListener, append, EventHelper, EventType, isMouseEvent } from '../../dom.js';
8
import { StandardKeyboardEvent } from '../../keyboardEvent.js';
9
import { EventType as GestureEventType, Gesture } from '../../touch.js';
10
import { AnchorAlignment } from '../contextview/contextview.js';
11
import type { IManagedHover } from '../hover/hover.js';
12
import { getBaseLayerHoverDelegate } from '../hover/hoverDelegate2.js';
13
import { getDefaultHoverDelegate } from '../hover/hoverDelegateFactory.js';
14
import { IMenuOptions } from '../menu/menu.js';
15
import { ActionRunner, IAction } from '../../../common/actions.js';
16
import { Emitter } from '../../../common/event.js';
17
import { KeyCode } from '../../../common/keyCodes.js';
18
import { IDisposable } from '../../../common/lifecycle.js';
19
import './dropdown.css';
20
21
export interface ILabelRenderer {
22
(container: HTMLElement): IDisposable | null;
23
}
24
25
export interface IBaseDropdownOptions {
26
label?: string;
27
labelRenderer?: ILabelRenderer;
28
}
29
30
export class BaseDropdown extends ActionRunner {
31
private _element: HTMLElement;
32
private boxContainer?: HTMLElement;
33
private _label?: HTMLElement;
34
private contents?: HTMLElement;
35
36
private visible: boolean | undefined;
37
private _onDidChangeVisibility = this._register(new Emitter<boolean>());
38
readonly onDidChangeVisibility = this._onDidChangeVisibility.event;
39
40
private hover: IManagedHover | undefined;
41
42
constructor(container: HTMLElement, options: IBaseDropdownOptions) {
43
super();
44
45
this._element = append(container, $('.monaco-dropdown'));
46
47
this._label = append(this._element, $('.dropdown-label'));
48
49
let labelRenderer = options.labelRenderer;
50
if (!labelRenderer) {
51
labelRenderer = (container: HTMLElement): IDisposable | null => {
52
container.textContent = options.label || '';
53
54
return null;
55
};
56
}
57
58
for (const event of [EventType.CLICK, EventType.MOUSE_DOWN, GestureEventType.Tap]) {
59
this._register(addDisposableListener(this.element, event, e => EventHelper.stop(e, true))); // prevent default click behaviour to trigger
60
}
61
62
for (const event of [EventType.MOUSE_DOWN, GestureEventType.Tap]) {
63
this._register(addDisposableListener(this._label, event, e => {
64
if (isMouseEvent(e) && e.button !== 0) {
65
// prevent right click trigger to allow separate context menu (https://github.com/microsoft/vscode/issues/151064)
66
return;
67
}
68
69
if (this.visible) {
70
this.hide();
71
} else {
72
this.show();
73
}
74
}));
75
}
76
77
this._register(addDisposableListener(this._label, EventType.KEY_DOWN, e => {
78
const event = new StandardKeyboardEvent(e);
79
if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
80
EventHelper.stop(e, true); // https://github.com/microsoft/vscode/issues/57997
81
82
if (this.visible) {
83
this.hide();
84
} else {
85
this.show();
86
}
87
}
88
}));
89
90
const cleanupFn = labelRenderer(this._label);
91
if (cleanupFn) {
92
this._register(cleanupFn);
93
}
94
95
this._register(Gesture.addTarget(this._label));
96
}
97
98
get element(): HTMLElement {
99
return this._element;
100
}
101
102
get label() {
103
return this._label;
104
}
105
106
set tooltip(tooltip: string) {
107
if (this._label) {
108
if (!this.hover && tooltip !== '') {
109
this.hover = this._register(getBaseLayerHoverDelegate().setupManagedHover(getDefaultHoverDelegate('mouse'), this._label, tooltip));
110
} else if (this.hover) {
111
this.hover.update(tooltip);
112
}
113
}
114
}
115
116
show(): void {
117
if (!this.visible) {
118
this.visible = true;
119
this._onDidChangeVisibility.fire(true);
120
}
121
}
122
123
hide(): void {
124
if (this.visible) {
125
this.visible = false;
126
this._onDidChangeVisibility.fire(false);
127
}
128
}
129
130
isVisible(): boolean {
131
return !!this.visible;
132
}
133
134
protected onEvent(_e: Event, activeElement: HTMLElement): void {
135
this.hide();
136
}
137
138
override dispose(): void {
139
super.dispose();
140
this.hide();
141
142
if (this.boxContainer) {
143
this.boxContainer.remove();
144
this.boxContainer = undefined;
145
}
146
147
if (this.contents) {
148
this.contents.remove();
149
this.contents = undefined;
150
}
151
152
if (this._label) {
153
this._label.remove();
154
this._label = undefined;
155
}
156
}
157
}
158
159
export interface IActionProvider {
160
getActions(): readonly IAction[];
161
}
162
163
export function isActionProvider(obj: unknown): obj is IActionProvider {
164
const candidate = obj as IActionProvider | undefined;
165
166
return typeof candidate?.getActions === 'function';
167
}
168
169
export interface IDropdownMenuOptions extends IBaseDropdownOptions {
170
contextMenuProvider: IContextMenuProvider;
171
readonly actions?: IAction[];
172
readonly actionProvider?: IActionProvider;
173
menuClassName?: string;
174
menuAsChild?: boolean; // scope down for #99448
175
readonly skipTelemetry?: boolean;
176
}
177
178
export class DropdownMenu extends BaseDropdown {
179
private _menuOptions: IMenuOptions | undefined;
180
private _actions: readonly IAction[] = [];
181
182
constructor(container: HTMLElement, private readonly _options: IDropdownMenuOptions) {
183
super(container, _options);
184
185
this.actions = _options.actions || [];
186
}
187
188
set menuOptions(options: IMenuOptions | undefined) {
189
this._menuOptions = options;
190
}
191
192
get menuOptions(): IMenuOptions | undefined {
193
return this._menuOptions;
194
}
195
196
private get actions(): readonly IAction[] {
197
if (this._options.actionProvider) {
198
return this._options.actionProvider.getActions();
199
}
200
201
return this._actions;
202
}
203
204
private set actions(actions: readonly IAction[]) {
205
this._actions = actions;
206
}
207
208
override show(): void {
209
super.show();
210
211
this.element.classList.add('active');
212
213
this._options.contextMenuProvider.showContextMenu({
214
getAnchor: () => this.element,
215
getActions: () => this.actions,
216
getActionsContext: () => this.menuOptions ? this.menuOptions.context : null,
217
getActionViewItem: (action, options) => this.menuOptions && this.menuOptions.actionViewItemProvider ? this.menuOptions.actionViewItemProvider(action, options) : undefined,
218
getKeyBinding: action => this.menuOptions && this.menuOptions.getKeyBinding ? this.menuOptions.getKeyBinding(action) : undefined,
219
getMenuClassName: () => this._options.menuClassName || '',
220
onHide: () => this.onHide(),
221
actionRunner: this.menuOptions ? this.menuOptions.actionRunner : undefined,
222
anchorAlignment: this.menuOptions ? this.menuOptions.anchorAlignment : AnchorAlignment.LEFT,
223
domForShadowRoot: this._options.menuAsChild ? this.element : undefined,
224
skipTelemetry: this._options.skipTelemetry
225
});
226
}
227
228
override hide(): void {
229
super.hide();
230
}
231
232
private onHide(): void {
233
this.hide();
234
this.element.classList.remove('active');
235
}
236
}
237
238