Path: blob/main/src/vs/platform/actions/browser/actionWidgetDropdownActionViewItem.ts
3294 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { $, append } from '../../../base/browser/dom.js';6import { BaseActionViewItem } from '../../../base/browser/ui/actionbar/actionViewItems.js';7import { ILabelRenderer } from '../../../base/browser/ui/dropdown/dropdown.js';8import { getBaseLayerHoverDelegate } from '../../../base/browser/ui/hover/hoverDelegate2.js';9import { getDefaultHoverDelegate } from '../../../base/browser/ui/hover/hoverDelegateFactory.js';10import { IAction } from '../../../base/common/actions.js';11import { IDisposable } from '../../../base/common/lifecycle.js';12import { IActionWidgetService } from '../../actionWidget/browser/actionWidget.js';13import { ActionWidgetDropdown, IActionWidgetDropdownOptions } from '../../actionWidget/browser/actionWidgetDropdown.js';14import { IContextKeyService } from '../../contextkey/common/contextkey.js';15import { IKeybindingService } from '../../keybinding/common/keybinding.js';1617/**18* Action view item for the custom action widget dropdown widget.19* Very closely based off of `DropdownMenuActionViewItem`, would be good to have some code re-use in the future20*/21export class ActionWidgetDropdownActionViewItem extends BaseActionViewItem {22private actionWidgetDropdown: ActionWidgetDropdown | undefined;23private actionItem: HTMLElement | null = null;24constructor(25action: IAction,26private readonly actionWidgetOptions: Omit<IActionWidgetDropdownOptions, 'label' | 'labelRenderer'>,27@IActionWidgetService private readonly _actionWidgetService: IActionWidgetService,28@IKeybindingService private readonly _keybindingService: IKeybindingService,29@IContextKeyService private readonly _contextKeyService: IContextKeyService,30) {31super(undefined, action);32}3334override render(container: HTMLElement): void {35this.actionItem = container;3637const labelRenderer: ILabelRenderer = (el: HTMLElement): IDisposable | null => {38this.element = append(el, $('a.action-label'));39return this.renderLabel(this.element);40};4142this.actionWidgetDropdown = this._register(new ActionWidgetDropdown(container, { ...this.actionWidgetOptions, labelRenderer }, this._actionWidgetService, this._keybindingService));43this._register(this.actionWidgetDropdown.onDidChangeVisibility(visible => {44this.element?.setAttribute('aria-expanded', `${visible}`);45}));4647this.updateTooltip();48this.updateEnabled();49}5051protected renderLabel(element: HTMLElement): IDisposable | null {52// todo@aeschli: remove codicon, should come through `this.options.classNames`53element.classList.add('codicon');5455if (this._action.label) {56this._register(getBaseLayerHoverDelegate().setupManagedHover(this.options.hoverDelegate ?? getDefaultHoverDelegate('mouse'), element, this._action.label));57}5859return null;60}6162protected override updateAriaLabel(): void {63if (this.element) {64this.setAriaLabelAttributes(this.element);65}66}6768protected setAriaLabelAttributes(element: HTMLElement): void {69element.setAttribute('role', 'button');70element.setAttribute('aria-haspopup', 'true');71element.setAttribute('aria-expanded', 'false');72element.ariaLabel = (this.getTooltip() + ' - ' + (element.textContent || this._action.label)) || '';73}7475protected override getTooltip() {76const keybinding = this._keybindingService.lookupKeybinding(this.action.id, this._contextKeyService);77const keybindingLabel = keybinding && keybinding.getLabel();7879const tooltip = this.action.tooltip ?? this.action.label;80return keybindingLabel81? `${tooltip} (${keybindingLabel})`82: tooltip;83}8485show(): void {86this.actionWidgetDropdown?.show();87}8889protected override updateEnabled(): void {90const disabled = !this.action.enabled;91this.actionItem?.classList.toggle('disabled', disabled);92this.element?.classList.toggle('disabled', disabled);93}9495}969798