Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/actions/browser/actionWidgetDropdownActionViewItem.ts
5252 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 { $, append } from '../../../base/browser/dom.js';
7
import { BaseActionViewItem } from '../../../base/browser/ui/actionbar/actionViewItems.js';
8
import { ILabelRenderer } from '../../../base/browser/ui/dropdown/dropdown.js';
9
import { getBaseLayerHoverDelegate } from '../../../base/browser/ui/hover/hoverDelegate2.js';
10
import { getDefaultHoverDelegate } from '../../../base/browser/ui/hover/hoverDelegateFactory.js';
11
import { IAction } from '../../../base/common/actions.js';
12
import { IDisposable } from '../../../base/common/lifecycle.js';
13
import { IActionWidgetService } from '../../actionWidget/browser/actionWidget.js';
14
import { ActionWidgetDropdown, IActionWidgetDropdownOptions } from '../../actionWidget/browser/actionWidgetDropdown.js';
15
import { IContextKeyService } from '../../contextkey/common/contextkey.js';
16
import { IKeybindingService } from '../../keybinding/common/keybinding.js';
17
import { ITelemetryService } from '../../telemetry/common/telemetry.js';
18
19
/**
20
* Action view item for the custom action widget dropdown widget.
21
* Very closely based off of `DropdownMenuActionViewItem`, would be good to have some code re-use in the future
22
*/
23
export class ActionWidgetDropdownActionViewItem extends BaseActionViewItem {
24
private actionWidgetDropdown: ActionWidgetDropdown | undefined;
25
private actionItem: HTMLElement | null = null;
26
constructor(
27
action: IAction,
28
private readonly actionWidgetOptions: Omit<IActionWidgetDropdownOptions, 'label' | 'labelRenderer'>,
29
@IActionWidgetService private readonly _actionWidgetService: IActionWidgetService,
30
@IKeybindingService private readonly _keybindingService: IKeybindingService,
31
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
32
@ITelemetryService private readonly _telemetryService: ITelemetryService,
33
) {
34
super(undefined, action);
35
}
36
37
override render(container: HTMLElement): void {
38
this.actionItem = container;
39
40
const labelRenderer: ILabelRenderer = (el: HTMLElement): IDisposable | null => {
41
this.element = append(el, $('a.action-label'));
42
return this.renderLabel(this.element);
43
};
44
45
this.actionWidgetDropdown = this._register(new ActionWidgetDropdown(container, { ...this.actionWidgetOptions, labelRenderer }, this._actionWidgetService, this._keybindingService, this._telemetryService));
46
this._register(this.actionWidgetDropdown.onDidChangeVisibility(visible => {
47
this.element?.setAttribute('aria-expanded', `${visible}`);
48
}));
49
50
this.updateTooltip();
51
this.updateEnabled();
52
}
53
54
protected renderLabel(element: HTMLElement): IDisposable | null {
55
// todo@aeschli: remove codicon, should come through `this.options.classNames`
56
element.classList.add('codicon');
57
58
if (this._action.label) {
59
this._register(getBaseLayerHoverDelegate().setupManagedHover(this.options.hoverDelegate ?? getDefaultHoverDelegate('mouse'), element, this._action.label));
60
}
61
62
return null;
63
}
64
65
protected override updateAriaLabel(): void {
66
if (this.element) {
67
this.setAriaLabelAttributes(this.element);
68
}
69
}
70
71
protected setAriaLabelAttributes(element: HTMLElement): void {
72
element.setAttribute('role', 'button');
73
element.setAttribute('aria-haspopup', 'true');
74
element.setAttribute('aria-expanded', 'false');
75
element.ariaLabel = (this.getTooltip() + ' - ' + (element.textContent || this._action.label)) || '';
76
}
77
78
protected override getTooltip() {
79
const tooltip = this.action.tooltip ?? this.action.label;
80
return this._keybindingService.appendKeybinding(tooltip, this.action.id, this._contextKeyService);
81
}
82
83
show(): void {
84
this.actionWidgetDropdown?.show();
85
}
86
87
protected override updateEnabled(): void {
88
const disabled = !this.action.enabled;
89
this.actionItem?.classList.toggle('disabled', disabled);
90
this.element?.classList.toggle('disabled', disabled);
91
this.actionWidgetDropdown?.setEnabled(!disabled);
92
}
93
94
}
95
96