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