Path: blob/main/src/vs/platform/actions/browser/actionWidgetDropdownActionViewItem.ts
5252 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';16import { ITelemetryService } from '../../telemetry/common/telemetry.js';1718/**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 future21*/22export class ActionWidgetDropdownActionViewItem extends BaseActionViewItem {23private actionWidgetDropdown: ActionWidgetDropdown | undefined;24private actionItem: HTMLElement | null = null;25constructor(26action: IAction,27private 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@ITelemetryService private readonly _telemetryService: ITelemetryService,32) {33super(undefined, action);34}3536override render(container: HTMLElement): void {37this.actionItem = container;3839const labelRenderer: ILabelRenderer = (el: HTMLElement): IDisposable | null => {40this.element = append(el, $('a.action-label'));41return this.renderLabel(this.element);42};4344this.actionWidgetDropdown = this._register(new ActionWidgetDropdown(container, { ...this.actionWidgetOptions, labelRenderer }, this._actionWidgetService, this._keybindingService, this._telemetryService));45this._register(this.actionWidgetDropdown.onDidChangeVisibility(visible => {46this.element?.setAttribute('aria-expanded', `${visible}`);47}));4849this.updateTooltip();50this.updateEnabled();51}5253protected renderLabel(element: HTMLElement): IDisposable | null {54// todo@aeschli: remove codicon, should come through `this.options.classNames`55element.classList.add('codicon');5657if (this._action.label) {58this._register(getBaseLayerHoverDelegate().setupManagedHover(this.options.hoverDelegate ?? getDefaultHoverDelegate('mouse'), element, this._action.label));59}6061return null;62}6364protected override updateAriaLabel(): void {65if (this.element) {66this.setAriaLabelAttributes(this.element);67}68}6970protected setAriaLabelAttributes(element: HTMLElement): void {71element.setAttribute('role', 'button');72element.setAttribute('aria-haspopup', 'true');73element.setAttribute('aria-expanded', 'false');74element.ariaLabel = (this.getTooltip() + ' - ' + (element.textContent || this._action.label)) || '';75}7677protected override getTooltip() {78const tooltip = this.action.tooltip ?? this.action.label;79return this._keybindingService.appendKeybinding(tooltip, this.action.id, this._contextKeyService);80}8182show(): void {83this.actionWidgetDropdown?.show();84}8586protected override updateEnabled(): void {87const disabled = !this.action.enabled;88this.actionItem?.classList.toggle('disabled', disabled);89this.element?.classList.toggle('disabled', disabled);90this.actionWidgetDropdown?.setEnabled(!disabled);91}9293}949596